From 2f6c4879761c5e7034b9a3b719be9b6a0aadbb6b Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Wed, 25 Apr 2018 18:54:47 +0200 Subject: [PATCH] implement syllable modifiers --- iast.c | 13 +++++++++++++ transliteration.c | 35 +++++++++++++++++++++++++++++++---- transliteration.h | 10 ++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/iast.c b/iast.c index c945b1c..b6e078d 100644 --- a/iast.c +++ b/iast.c @@ -1,6 +1,13 @@ #include +#include #include "iast.h" +void diacritics_modifier_test(struct syllable *syllable) +{ + free(syllable->data); + syllable->data = strdup("X"); +} + static const struct transliteration_letter table_letters[] = { /* Vowels */ @@ -62,12 +69,18 @@ static const struct transliteration_letter table_letters[] = { {0, NULL} }; +static const struct transliteration_modifier table_modifiers[] = { + {0x0940, diacritics_modifier_test}, + {0, NULL} +}; + struct transliteration_context *transliteration_context_iast_alloc() { struct transliteration_context *context; context = malloc(sizeof(*context)); context->table_letters = table_letters; + context->table_modifiers = table_modifiers; return context; } diff --git a/transliteration.c b/transliteration.c index 646894e..2d4cf41 100644 --- a/transliteration.c +++ b/transliteration.c @@ -5,10 +5,30 @@ #include "syllable.h" #include "utf8.h" -struct transliteration_letter *find_letter_by_code(unsigned long c, - struct transliteration_letter *table) +const struct transliteration_letter *find_letter_by_code(unsigned long c, + const struct transliteration_letter *table) { - struct transliteration_letter *walk = table; + const struct transliteration_letter *walk = table; + + if (c == 0) + return NULL; + + while (walk->code != 0) { + if (c == walk->code) + return walk; + walk++; + } + + return NULL; +} + +const struct transliteration_modifier *find_modifier_by_code(unsigned long c, + const struct transliteration_modifier *table) +{ + const struct transliteration_modifier *walk = table; + + if (c == 0) + return NULL; while (walk->code != 0) { if (c == walk->code) @@ -28,7 +48,8 @@ char *transliterate_devanagari_to_latin(const char *text, char *tmp; unsigned long c; struct syllable *head, *tail; - struct transliteration_letter *letter; + const struct transliteration_letter *letter; + const struct transliteration_modifier *modifier; head = syllable_alloc(""); tail = head; @@ -43,6 +64,12 @@ char *transliterate_devanagari_to_latin(const char *text, continue; } + modifier = find_modifier_by_code(c, context->table_modifiers); + if (modifier != NULL) { + modifier->modifier(tail); + continue; + } + tmp = utf8_code_to_string(c); tail = syllable_append(tail, tmp); free(tmp); diff --git a/transliteration.h b/transliteration.h index 2b95123..8af9be7 100644 --- a/transliteration.h +++ b/transliteration.h @@ -1,13 +1,23 @@ #ifndef __TRANSLITERATION_H #define __TRANSLITERATION_H +#include "syllable.h" + struct transliteration_letter { unsigned long code; const char *data; }; +typedef void (*syllable_modification_t)(struct syllable *syllable); + +struct transliteration_modifier { + unsigned long code; + syllable_modification_t modifier; +}; + struct transliteration_context { const struct transliteration_letter *table_letters; + const struct transliteration_modifier *table_modifiers; }; char *transliterate_devanagari_to_latin(const char *text,