implement syllable modifiers

This commit is contained in:
Vlasta Vesely 2018-04-25 18:54:47 +02:00
parent 63eb389ae2
commit 2f6c487976
3 changed files with 54 additions and 4 deletions

13
iast.c
View file

@ -1,6 +1,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "iast.h" #include "iast.h"
void diacritics_modifier_test(struct syllable *syllable)
{
free(syllable->data);
syllable->data = strdup("X");
}
static const struct transliteration_letter table_letters[] = { static const struct transliteration_letter table_letters[] = {
/* Vowels */ /* Vowels */
@ -62,12 +69,18 @@ static const struct transliteration_letter table_letters[] = {
{0, NULL} {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 *transliteration_context_iast_alloc()
{ {
struct transliteration_context *context; struct transliteration_context *context;
context = malloc(sizeof(*context)); context = malloc(sizeof(*context));
context->table_letters = table_letters; context->table_letters = table_letters;
context->table_modifiers = table_modifiers;
return context; return context;
} }

View file

@ -5,10 +5,30 @@
#include "syllable.h" #include "syllable.h"
#include "utf8.h" #include "utf8.h"
struct transliteration_letter *find_letter_by_code(unsigned long c, const struct transliteration_letter *find_letter_by_code(unsigned long c,
struct transliteration_letter *table) 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) { while (walk->code != 0) {
if (c == walk->code) if (c == walk->code)
@ -28,7 +48,8 @@ char *transliterate_devanagari_to_latin(const char *text,
char *tmp; char *tmp;
unsigned long c; unsigned long c;
struct syllable *head, *tail; struct syllable *head, *tail;
struct transliteration_letter *letter; const struct transliteration_letter *letter;
const struct transliteration_modifier *modifier;
head = syllable_alloc(""); head = syllable_alloc("");
tail = head; tail = head;
@ -43,6 +64,12 @@ char *transliterate_devanagari_to_latin(const char *text,
continue; continue;
} }
modifier = find_modifier_by_code(c, context->table_modifiers);
if (modifier != NULL) {
modifier->modifier(tail);
continue;
}
tmp = utf8_code_to_string(c); tmp = utf8_code_to_string(c);
tail = syllable_append(tail, tmp); tail = syllable_append(tail, tmp);
free(tmp); free(tmp);

View file

@ -1,13 +1,23 @@
#ifndef __TRANSLITERATION_H #ifndef __TRANSLITERATION_H
#define __TRANSLITERATION_H #define __TRANSLITERATION_H
#include "syllable.h"
struct transliteration_letter { struct transliteration_letter {
unsigned long code; unsigned long code;
const char *data; const char *data;
}; };
typedef void (*syllable_modification_t)(struct syllable *syllable);
struct transliteration_modifier {
unsigned long code;
syllable_modification_t modifier;
};
struct transliteration_context { struct transliteration_context {
const struct transliteration_letter *table_letters; const struct transliteration_letter *table_letters;
const struct transliteration_modifier *table_modifiers;
}; };
char *transliterate_devanagari_to_latin(const char *text, char *transliterate_devanagari_to_latin(const char *text,