From 1a9a55ee416a89507f877ed58cf8700d4ca222ae Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Thu, 2 Jan 2020 16:21:58 +0100 Subject: [PATCH] main: update handling -e and -c --- compat.h | 4 ++++ main.c | 54 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/compat.h b/compat.h index d41a770..25fbad8 100644 --- a/compat.h +++ b/compat.h @@ -1,7 +1,11 @@ #ifndef __COMPAT_H #define __COMPAT_H +#include #include #include +#include +#include +#include #endif /* __COMPAT_H */ diff --git a/main.c b/main.c index 8c06c49..9458fee 100644 --- a/main.c +++ b/main.c @@ -1,19 +1,15 @@ -#include -#include -#include -#include -#include -#include - +#include "compat.h" #include "transliteration.h" +#include "transcription.h" #include "encoder.h" #define PROGNAME "iast" -#define VERSION "0.1" +#define VERSION "1.0" #define FLAG_STDIN 1 << 0 -#define FLAG_CZECH 1 << 1 +#define FLAG_REVERSE 1 << 1 #define FLAG_ENCODE 1 << 2 +#define FLAG_CZECH 1 << 3 static const char *usage_str = @@ -25,8 +21,9 @@ static const char *usage_str = "options:\n" " -h shows this help and exits\n" " -v shows version number and exits\n" - " -c transliterate to Czech language\n" + " -r reverse transliteration (from Latin to Devanagari)\n" " -e convert symbolic ASCII text to IAST representation\n" + " -c transcript Devanagari to Czech language\n" " -- read data from the standard input\n" "\n" " By default, `" PROGNAME "` takes all input arguments written in Devanagari\n" @@ -79,6 +76,19 @@ static char *stdin_read() return text; } +static char *process_input(const char *input, unsigned int flags) +{ + if (flags & FLAG_ENCODE) { + return encode_iast_punctation(input); + } else if (flags & FLAG_REVERSE) { + return transliterate_latin_to_devanagari(input); + } else if (flags & FLAG_CZECH) { + return transcript_devanagari_to_czech(input); + } else { + return transliterate_devanagari_to_latin(input); + } +} + int main(int argc, const char **argv) { int i; @@ -87,6 +97,11 @@ int main(int argc, const char **argv) const char *queue[argc]; char *input, *output; + if (argc == 1) { + print_usage(); + return -1; + } + for (i = 1; i < argc; i++) { arg = argv[i]; @@ -95,12 +110,15 @@ int main(int argc, const char **argv) case '-': flags |= FLAG_STDIN; continue; - case 'c': - flags |= FLAG_CZECH; + case 'r': + flags |= FLAG_REVERSE; continue; case 'e': flags |= FLAG_ENCODE; continue; + case 'c': + flags |= FLAG_CZECH; + continue; case 'h': print_usage(); return 0; @@ -123,11 +141,7 @@ int main(int argc, const char **argv) return -1; } - if (flags & FLAG_ENCODE) { - output = encode_iast_punctation(input); - } else { - output = transliterate_devanagari_to_latin(input); - } + output = process_input(input, flags); fprintf(stdout, "%s\n", output); free(output); @@ -135,11 +149,7 @@ int main(int argc, const char **argv) } for (i = 0; i < n; i++) { - if (flags & FLAG_ENCODE) { - output = encode_iast_punctation(queue[i]); - } else { - output = transliterate_devanagari_to_latin(queue[i]); - } + output = process_input(queue[i], flags); fprintf(stdout, "%s\n", output); free(output);