main.c - rewrite actions dispatching
This commit is contained in:
parent
6f8417f964
commit
7960aef783
2 changed files with 57 additions and 32 deletions
85
main.c
85
main.c
|
@ -78,55 +78,59 @@ static void error(const char *msg, ...)
|
||||||
va_end(params);
|
va_end(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int process_input(const char *input, char **out, unsigned int flags)
|
static int velthuis_encode(const char *in, char **out, unsigned int flags)
|
||||||
{
|
{
|
||||||
char *tmp = NULL;
|
char *tmp = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (flags & FLAG_REVERSE) {
|
if (flags & FLAG_REVERSE)
|
||||||
if (flags & FLAG_VELTHUIS) {
|
return encode_iast_to_velthuis(in, out);
|
||||||
return encode_iast_to_velthuis(input, out);
|
|
||||||
} else {
|
|
||||||
return transliterate_latin_to_devanagari(input, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & FLAG_VELTHUIS) {
|
ret = encode_velthuis_to_iast(in, out);
|
||||||
if (flags & FLAG_DEVANAGARI) {
|
if (flags & FLAG_DEVANAGARI) {
|
||||||
ret = encode_velthuis_to_iast(input, &tmp);
|
ret = transliterate_latin_to_devanagari(*out, &tmp);
|
||||||
if (ret != 0)
|
free(*out);
|
||||||
return ret;
|
*out = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
ret = transliterate_latin_to_devanagari(tmp, out);
|
|
||||||
free(tmp);
|
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
}
|
||||||
return encode_velthuis_to_iast(input, out);
|
|
||||||
}
|
static int iast_transliterate(const char *in, char **out, unsigned int flags)
|
||||||
|
{
|
||||||
|
char *tmp = NULL;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (flags & FLAG_REVERSE)
|
||||||
|
return transliterate_latin_to_devanagari(in, out);
|
||||||
|
|
||||||
|
ret = transliterate_devanagari_to_latin(in, out);
|
||||||
|
if (flags & FLAG_ASCII) {
|
||||||
|
ret = encode_iast_to_velthuis(*out, &tmp);
|
||||||
|
free(*out);
|
||||||
|
*out = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int process_input(const char *input, char **out, unsigned int flags)
|
||||||
|
{
|
||||||
|
if (flags & FLAG_HINDI)
|
||||||
|
return transcript_devanagari_to_hindi(input, out);
|
||||||
|
|
||||||
if (flags & FLAG_CZECH)
|
if (flags & FLAG_CZECH)
|
||||||
return transcript_devanagari_to_czech(input, out);
|
return transcript_devanagari_to_czech(input, out);
|
||||||
|
|
||||||
if (flags & FLAG_HINDI)
|
if (flags & FLAG_VELTHUIS)
|
||||||
return transcript_devanagari_to_hindi(input, out);
|
return velthuis_encode(input, out, flags);
|
||||||
|
|
||||||
if (flags & FLAG_ASCII) {
|
return iast_transliterate(input, out, flags);
|
||||||
ret = transliterate_devanagari_to_latin(input, &tmp);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = encode_iast_to_velthuis(tmp, out);
|
|
||||||
free(tmp);
|
|
||||||
return ret;
|
|
||||||
} else {
|
|
||||||
return transliterate_devanagari_to_latin(input, out);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int process_string(const char *input, unsigned int flags)
|
static int process_string(const char *input, unsigned int flags)
|
||||||
{
|
{
|
||||||
char *output;
|
char *output = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = process_input(input, &output, flags);
|
ret = process_input(input, &output, flags);
|
||||||
|
@ -145,6 +149,21 @@ static int process_string(const char *input, unsigned int flags)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int validate_flags(unsigned int flags)
|
||||||
|
{
|
||||||
|
if (flags & FLAG_HINDI && flags & FLAG_REVERSE) {
|
||||||
|
error("invalid combination of '-H' and '-r'.");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & FLAG_CZECH && flags & FLAG_REVERSE) {
|
||||||
|
error("invalid combination of '-c' and '-r'.");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#define CHUNKSIZE 1024
|
#define CHUNKSIZE 1024
|
||||||
static int read_fd(char **out, int fd)
|
static int read_fd(char **out, int fd)
|
||||||
{
|
{
|
||||||
|
@ -247,6 +266,10 @@ int main(int argc, const char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retval = validate_flags(flags);
|
||||||
|
if (retval != 0)
|
||||||
|
return retval;
|
||||||
|
|
||||||
while (optind < argc) {
|
while (optind < argc) {
|
||||||
const char *arg = argv[optind++];
|
const char *arg = argv[optind++];
|
||||||
|
|
||||||
|
|
|
@ -148,6 +148,8 @@ START_TEST(test_errors)
|
||||||
{
|
{
|
||||||
test_output("./iast -x 2>&1", "[iast] error: unrecognised option '-x'.\n");
|
test_output("./iast -x 2>&1", "[iast] error: unrecognised option '-x'.\n");
|
||||||
test_output("./iast -f xxx 2>&1", "[iast] error: failed to read file 'xxx'.\n");
|
test_output("./iast -f xxx 2>&1", "[iast] error: failed to read file 'xxx'.\n");
|
||||||
|
test_output("./iast -Hr 2>&1", "[iast] error: invalid combination of '-H' and '-r'.\n");
|
||||||
|
test_output("./iast -cr 2>&1", "[iast] error: invalid combination of '-c' and '-r'.\n");
|
||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue