diff --git a/Makefile b/Makefile index dc80627..b60b0dc 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ TEST_LIBS = $(LIBS) $(shell pkg-config --libs check) all: iast doc/iast.1.gz -iast: main.o $(OBJFILES) +iast: main.o $(OBJECTS) $(CC) $^ -o $@ $(CFLAGS) test: tests/test diff --git a/main.c b/main.c index dc9adbf..8c06c49 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,6 @@ #include #include "transliteration.h" -#include "iast.h" #include "encoder.h" #define PROGNAME "iast" diff --git a/tests/main.c b/tests/main.c index a7dad47..27ab370 100644 --- a/tests/main.c +++ b/tests/main.c @@ -24,8 +24,14 @@ int main(void) suite = create_test_suite(); runner = srunner_create(suite); + puts("-----------------------------------------"); srunner_run_all(runner, CK_NORMAL); retval = srunner_ntests_failed(runner); + puts("-----------------------------------------"); + + puts(retval == 0 ? "\033[32mpassed\033[0m\n" + : "\033[31mfailed\033[0m\n"); + srunner_free(runner); return retval; diff --git a/tests/test.h b/tests/test.h index 099d2b7..3bbe929 100644 --- a/tests/test.h +++ b/tests/test.h @@ -1,6 +1,7 @@ #ifndef __TEST_TEST_H #define __TEST_TEST_H +#include #include #include diff --git a/transliteration.c b/transliteration.c index c9d5dd2..2dc0066 100644 --- a/transliteration.c +++ b/transliteration.c @@ -5,14 +5,68 @@ #include "iast.h" #include "utf8.h" +#define SCHWA_CHARACTER 'a' +#define CHUNKSIZE 1024 + +static struct translit_letter *letter_by_code(struct translit_letter *table, + unsigned int c) +{ + while (table->code != 0) { + if (table->code == c) + return table; + table++; + } + + return NULL; +} + char *transliterate_devanagari_to_latin(const char *devanagari) { struct translit_letter *table; - char *retval; + struct translit_letter *letter; + unsigned int c, alloc = 0, done = 0, len; + const char *src = devanagari; + char *latin = NULL; table = get_iast_transliteration_table(); - retval = strdup(""); /* FIXME */ + while (1) { + if (alloc < done + UNICODE_MAX_LENGTH) { + latin = realloc(latin, alloc + CHUNKSIZE); + alloc += CHUNKSIZE; + } - return retval; + c = utf8_unpack_char(src); + len = utf8_char_length(c); + src += len; + + letter = letter_by_code(table, c); + if (letter) { + switch (letter->type) { + case CONSONANT: + strcpy(latin + done, letter->data); + done += strlen(letter->data); + *(latin + done++) = SCHWA_CHARACTER; + break; + case VOWEL_SIGN: + if (done) + done--; + strcpy(latin + done, letter->data); + done += strlen(letter->data); + break; + default: + strcpy(latin + done, letter->data); + done += strlen(letter->data); + break; + } + } else { + utf8_pack_char(latin + done, c); + done += len; + } + + if (c == 0) + break; + } + + return latin; } diff --git a/utf8.h b/utf8.h index 83864f2..ec40e01 100644 --- a/utf8.h +++ b/utf8.h @@ -3,6 +3,8 @@ #ifndef __UTF8_H #define __UTF8_H +#define UNICODE_MAX_LENGTH 4 + unsigned int utf8_unpack_char(const char *src); void utf8_pack_char(char *dest, unsigned int c);