2018-04-25 19:30:49 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
|
2018-04-25 16:13:34 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "transliteration.h"
|
|
|
|
#include "syllable.h"
|
|
|
|
#include "utf8.h"
|
|
|
|
|
2018-04-29 08:21:46 +02:00
|
|
|
static const struct transliteration_letter *find_letter_by_code(unsigned int c,
|
2018-04-25 18:54:47 +02:00
|
|
|
const struct transliteration_letter *table)
|
2018-04-25 18:11:56 +02:00
|
|
|
{
|
2018-04-25 18:54:47 +02:00
|
|
|
const struct transliteration_letter *walk = table;
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (walk->code != 0) {
|
|
|
|
if (c == walk->code)
|
|
|
|
return walk;
|
|
|
|
walk++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:58:07 +02:00
|
|
|
static void syllable_modify(struct syllable *syllable, const char *data)
|
2018-04-25 18:54:47 +02:00
|
|
|
{
|
2018-04-27 18:58:07 +02:00
|
|
|
char buffer[10];
|
2018-04-25 18:54:47 +02:00
|
|
|
|
2018-04-27 18:58:07 +02:00
|
|
|
strcpy(buffer, syllable->data);
|
|
|
|
buffer[strlen(buffer) - 1] = 0;
|
|
|
|
strcat(buffer, data);
|
2018-04-25 18:11:56 +02:00
|
|
|
|
2018-04-27 18:58:07 +02:00
|
|
|
free(syllable->data);
|
|
|
|
syllable->data = strdup(buffer);
|
2018-04-25 18:11:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *transliterate_devanagari_to_latin(const char *text,
|
|
|
|
struct transliteration_context *context)
|
2018-04-25 16:13:34 +02:00
|
|
|
{
|
|
|
|
unsigned int length = strlen(text);
|
|
|
|
const char *ptr = text;
|
|
|
|
const char *end = ptr + length;
|
|
|
|
char *tmp;
|
2018-04-29 08:21:46 +02:00
|
|
|
unsigned int c;
|
2018-04-25 16:13:34 +02:00
|
|
|
struct syllable *head, *tail;
|
2018-04-25 18:54:47 +02:00
|
|
|
const struct transliteration_letter *letter;
|
2018-04-25 16:13:34 +02:00
|
|
|
|
|
|
|
head = syllable_alloc("");
|
|
|
|
tail = head;
|
|
|
|
|
|
|
|
while (ptr < end) {
|
|
|
|
c = utf8_unpack_char(ptr);
|
|
|
|
ptr += utf8_char_length(c);
|
|
|
|
|
2018-04-27 18:58:07 +02:00
|
|
|
letter = find_letter_by_code(c, context->table);
|
2018-04-25 18:11:56 +02:00
|
|
|
if (letter != NULL) {
|
|
|
|
|
2018-04-27 18:58:07 +02:00
|
|
|
if (letter->flags & FLAG_REGULAR)
|
|
|
|
tail = syllable_append(tail, letter->data);
|
|
|
|
else if (letter->flags & FLAG_MODIFIER)
|
|
|
|
syllable_modify(tail, letter->data);
|
|
|
|
|
2018-04-25 18:54:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:13:34 +02:00
|
|
|
tmp = utf8_code_to_string(c);
|
|
|
|
tail = syllable_append(tail, tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return syllable_chain_to_string(head);
|
|
|
|
}
|
2018-04-27 11:59:32 +02:00
|
|
|
|
|
|
|
void transliteration_context_drop(struct transliteration_context *context)
|
|
|
|
{
|
|
|
|
free(context);
|
|
|
|
}
|