define transliteration context

This commit is contained in:
Vlasta Vesely 2018-04-25 18:11:56 +02:00
parent 2083889d4e
commit fe6b2fe2f6
5 changed files with 64 additions and 3 deletions

View file

@ -1,7 +1,7 @@
.PHONY: all clean .PHONY: all clean
all: 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 ./main
clean: clean:

21
iast.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdlib.h>
#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;
}

8
iast.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef __IAST_H
#define __IAST_H
#include "transliteration.h"
struct transliteration_context *transliteration_context_iast_alloc();
#endif /* __IAST_H */

View file

@ -5,7 +5,22 @@
#include "syllable.h" #include "syllable.h"
#include "utf8.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); unsigned int length = strlen(text);
const char *ptr = text; const char *ptr = text;
@ -13,6 +28,7 @@ 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;
head = syllable_alloc(""); head = syllable_alloc("");
tail = head; tail = head;
@ -21,6 +37,12 @@ char *transliterate_devanagari_to_latin(const char *text)
c = utf8_unpack_char(ptr); c = utf8_unpack_char(ptr);
ptr += utf8_char_length(c); 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); tmp = utf8_code_to_string(c);
tail = syllable_append(tail, tmp); tail = syllable_append(tail, tmp);
free(tmp); free(tmp);

View file

@ -1,6 +1,16 @@
#ifndef __TRANSLITERATION_H #ifndef __TRANSLITERATION_H
#define __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 */ #endif /* __TRANSLITERATION_H */