main: transliterates arguments or contents of stdin

This commit is contained in:
Vlasta Vesely 2018-05-13 17:15:26 +02:00
parent ce3a02ad6e
commit ed32c8fa1d
3 changed files with 60 additions and 34 deletions

View file

@ -3,14 +3,14 @@
OBJS = syllable.o utf8.o transliteration.o iast.o iast-czech.o OBJS = syllable.o utf8.o transliteration.o iast.o iast-czech.o
main: main.o $(OBJS) iast: main.o $(OBJS)
$(CC) $^ -o $@ $(CC) $^ -o $@
test: main test: iast
sh tests/test.sh sh tests/test.sh
%.o: %.c %.o: %.c
$(CC) -MMD -MP -c $< -o $@ $(CC) -MMD -MP -c $< -o $@
clean: clean:
$(RM) main test *.o *.d $(RM) iast *.o *.d

82
main.c
View file

@ -8,25 +8,48 @@
#include "iast.h" #include "iast.h"
#include "iast-czech.h" #include "iast-czech.h"
#define FLAG_CZECH 1 << 0 #define FLAG_STDIN 1 << 0
#define FLAG_CZECH 1 << 1
static char *stdin_read()
{
char buffer[1024];
unsigned int n, length = 0;;
char *text = NULL;
while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
if (text == NULL)
text = malloc(n + 1);
else
text = realloc(text, length + n + 1);
strncpy(text + length, buffer, n);
length += n;
}
if (n == -1)
return NULL;
return text;
}
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
struct transliteration_context *context;
unsigned int n, length = 0;
int i, retval = 0; int i, retval = 0;
char buffer[6]; unsigned int flags = 0, n = 0;
char *text = malloc(0);
char *out;
const char *arg; const char *arg;
unsigned int flags = 0; const char *queue[argc];
char *input, *output;
struct transliteration_context *context;
for (i = 1; i < argc; i++) {
for (i = 0; i < argc; i++) {
arg = argv[i]; arg = argv[i];
if (*arg == '-') { if (*arg == '-') {
switch (arg[1]) { switch (arg[1]) {
case '-':
flags |= FLAG_STDIN;
continue;
case 'c': case 'c':
flags |= FLAG_CZECH; flags |= FLAG_CZECH;
continue; continue;
@ -34,35 +57,38 @@ int main(int argc, const char **argv)
fprintf(stderr, "error: unknown option '%s'\n", arg); fprintf(stderr, "error: unknown option '%s'\n", arg);
exit(1); exit(1);
} else {
queue[n++] = arg;
} }
} }
if (flags & FLAG_CZECH)
context = transliteration_context_iast_czech_alloc();
else
context = transliteration_context_iast_alloc();
while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { if (flags & FLAG_STDIN) {
text = realloc(text, length + n + 1); input = stdin_read();
strncpy(text + length, buffer, n); if (input == NULL) {
length += n; fprintf(stderr, "[iast] failed to read from STDIN.\n");
} retval = -1;
if (n == -1) {
retval = -errno;
goto out; goto out;
} }
text[length] = '\0'; output = transliterate_devanagari_to_latin(input, context);
fprintf(stdout, "%s\n", output);
if (flags & FLAG_CZECH) { free(output);
context = transliteration_context_iast_czech_alloc(); free(input);
} else {
context = transliteration_context_iast_alloc();
} }
out = transliterate_devanagari_to_latin(text, context); for (i = 0; i < n; i++) {
printf("%s\n", out); output = transliterate_devanagari_to_latin(queue[i], context);
fprintf(stdout, "%s\n", output);
transliteration_context_drop(context); free(output);
free(text); }
free(out);
out: out:
transliteration_context_drop(context);
return retval; return retval;
} }

View file

@ -3,7 +3,7 @@ set -e
test_word() test_word()
{ {
transliterated=$(echo -n "$1" | ./main $3) transliterated=$(echo -n "$1" | ./iast $3 --)
expected="$2" expected="$2"
if test "x$transliterated" = "x$expected" if test "x$transliterated" = "x$expected"