From fe6b2fe2f6cfb71d33cc06ba26be961569e585cc Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Wed, 25 Apr 2018 18:11:56 +0200 Subject: [PATCH] define transliteration context --- Makefile | 2 +- iast.c | 21 +++++++++++++++++++++ iast.h | 8 ++++++++ transliteration.c | 24 +++++++++++++++++++++++- transliteration.h | 12 +++++++++++- 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 iast.c create mode 100644 iast.h diff --git a/Makefile b/Makefile index 9796375..57c4fad 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: all clean all: - $(CC) main.c syllable.c utf8.c transliteration.c -o main + $(CC) main.c syllable.c utf8.c transliteration.c iast.c -o main ./main clean: diff --git a/iast.c b/iast.c new file mode 100644 index 0000000..b7dc704 --- /dev/null +++ b/iast.c @@ -0,0 +1,21 @@ +#include +#include "iast.h" + +static const struct transliteration_letter table_letters[] = { + + /* Vowels */ + {0x0905, "a"}, /* अ */ + {0x0906, "ā"}, /* आ */ + + {0, NULL} +}; + +struct transliteration_context *transliteration_context_iast_alloc() +{ + struct transliteration_context *context; + + context = malloc(sizeof(*context)); + context->table_letters = table_letters; + + return context; +} diff --git a/iast.h b/iast.h new file mode 100644 index 0000000..607e370 --- /dev/null +++ b/iast.h @@ -0,0 +1,8 @@ +#ifndef __IAST_H +#define __IAST_H + +#include "transliteration.h" + +struct transliteration_context *transliteration_context_iast_alloc(); + +#endif /* __IAST_H */ diff --git a/transliteration.c b/transliteration.c index b838747..646894e 100644 --- a/transliteration.c +++ b/transliteration.c @@ -5,7 +5,22 @@ #include "syllable.h" #include "utf8.h" -char *transliterate_devanagari_to_latin(const char *text) +struct transliteration_letter *find_letter_by_code(unsigned long c, + struct transliteration_letter *table) +{ + struct transliteration_letter *walk = table; + + while (walk->code != 0) { + if (c == walk->code) + return walk; + walk++; + } + + return NULL; +} + +char *transliterate_devanagari_to_latin(const char *text, + struct transliteration_context *context) { unsigned int length = strlen(text); const char *ptr = text; @@ -13,6 +28,7 @@ char *transliterate_devanagari_to_latin(const char *text) char *tmp; unsigned long c; struct syllable *head, *tail; + struct transliteration_letter *letter; head = syllable_alloc(""); tail = head; @@ -21,6 +37,12 @@ char *transliterate_devanagari_to_latin(const char *text) c = utf8_unpack_char(ptr); ptr += utf8_char_length(c); + letter = find_letter_by_code(c, context->table_letters); + if (letter != NULL) { + tail = syllable_append(tail, letter->data); + continue; + } + tmp = utf8_code_to_string(c); tail = syllable_append(tail, tmp); free(tmp); diff --git a/transliteration.h b/transliteration.h index d1da50e..2b95123 100644 --- a/transliteration.h +++ b/transliteration.h @@ -1,6 +1,16 @@ #ifndef __TRANSLITERATION_H #define __TRANSLITERATION_H -char *transliterate_devanagari_to_latin(const char *text); +struct transliteration_letter { + unsigned long code; + const char *data; +}; + +struct transliteration_context { + const struct transliteration_letter *table_letters; +}; + +char *transliterate_devanagari_to_latin(const char *text, + struct transliteration_context *context); #endif /* __TRANSLITERATION_H */