sanskrit-iast/encoder.c

75 lines
1.4 KiB
C
Raw Normal View History

2018-05-18 10:52:11 +02:00
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "encoder.h"
#include "utf8.h"
struct encoder_tuple {
const char *from;
const char *to;
};
static const struct encoder_tuple table[] = {
2018-05-20 08:42:09 +02:00
{"a-", "ā"}, {"A-", "Ā"},
{"i-", "ī"}, {"I-", "Ī"},
{"u-", "ū"}, {"U-", "Ū"},
{"r.", ""}, {"R.", ""},
{"r.-", ""}, {"R.-", ""},
{"l.", ""}, {"L.", ""},
{"l.-", ""}, {"L.-", ""},
{"n^.", ""}, {"N^.", ""},
{"n~", "ñ"}, {"N~", "Ñ"},
{"s,", "ś"}, {"S,", "Ś"},
{"t.", ""}, {"T.", ""},
{"d.", ""}, {"D.", ""},
{"n.", ""}, {"N.", ""},
{"s.", ""}, {"S.", ""},
{"m.", ""}, {"M.", ""},
{"h.", ""}, {"H.", ""},
2018-05-18 10:52:11 +02:00
{NULL, NULL}
};
2018-06-02 14:22:19 +02:00
static const struct encoder_tuple *find_tuple(const char *text)
2018-05-18 10:52:11 +02:00
{
const struct encoder_tuple *walk = table;
while (walk->from != NULL) {
if (strncmp(text, walk->from, strlen(walk->from)) == 0) {
return walk;
}
walk++;
}
return NULL;
}
char *encode_iast_punctation(const char *text)
{
const char *str = text, *end = str + strlen(str);
const struct encoder_tuple *tuple;
char *buf, *dest;
buf = malloc(strlen(text) << 1);
buf[0] = 0;
dest = buf;
while (str < end) {
tuple = find_tuple(str);
if (tuple) {
sprintf(dest, "%s", tuple->to);
str += strlen(tuple->from);
dest += strlen(tuple->to);
} else {
sprintf(dest, "%c", *str);
str++;
dest++;
}
}
return buf;
}