sanskrit-iast/main.c

185 lines
3.7 KiB
C
Raw Normal View History

2020-01-02 16:21:58 +01:00
#include "compat.h"
#include "transliteration.h"
2020-01-02 16:21:58 +01:00
#include "transcription.h"
2018-05-18 10:55:23 +02:00
#include "encoder.h"
2018-04-28 14:03:30 +02:00
#define PROGNAME "iast"
2020-01-02 16:21:58 +01:00
#define VERSION "1.0"
#define FLAG_REVERSE 1 << 0
#define FLAG_ENCODE 1 << 1
#define FLAG_CZECH 1 << 2
2018-06-02 14:22:19 +02:00
static const char *usage_str =
PROGNAME ", a helper for Sanskrit transliteration.\n"
2018-05-18 15:48:39 +02:00
"\n"
"usage:\n"
" " PROGNAME " [flags and text arguments in any order]\n"
2018-05-18 15:48:39 +02:00
"\n"
"options:\n"
" -h shows this help and exits\n"
" -v shows version number and exits\n"
2020-10-11 09:48:55 +02:00
" -f specifies file for conversion (- means standard input)\n"
2020-01-02 16:21:58 +01:00
" -r reverse transliteration (from Latin to Devanagari)\n"
2018-05-18 15:48:39 +02:00
" -e convert symbolic ASCII text to IAST representation\n"
2020-01-02 16:21:58 +01:00
" -c transcript Devanagari to Czech language\n"
2018-05-18 15:48:39 +02:00
"\n"
2020-09-25 15:35:36 +02:00
" By default, " PROGNAME " takes all input arguments written in Devanagari\n"
" and transliterates them to the IAST version.\n"
2018-05-18 15:48:39 +02:00
"\n"
2020-09-25 15:35:36 +02:00
" When the flag -e is set up, the program converts purely ASCII-encoded\n"
" strings into the special characters of the IAST alphabet. For example, it\n"
" converts sam.skr.tam to saṃskṛtam or s,a-stram to śāstram.\n";
2018-05-18 15:48:39 +02:00
static void print_usage()
2018-05-18 15:48:39 +02:00
{
fprintf(stdout, "%s\n", usage_str);
}
static void print_version()
{
fprintf(stdout, PROGNAME " v" VERSION "\n");
}
static void error(const char *msg, ...)
{
va_list params;
char buf[256];
va_start(params, msg);
vsnprintf(buf, sizeof(buf), msg, params);
fprintf(stderr, "[" PROGNAME "] error: %s\n", buf);
va_end(params);
}
2020-01-02 16:21:58 +01:00
static char *process_input(const char *input, unsigned int flags)
{
if (flags & FLAG_ENCODE) {
return encode_iast_punctation(input);
} else if (flags & FLAG_REVERSE) {
return transliterate_latin_to_devanagari(input);
} else if (flags & FLAG_CZECH) {
return transcript_devanagari_to_czech(input);
} else {
return transliterate_devanagari_to_latin(input);
}
}
#define CHUNKSIZE 1024
static int read_fd(char **out, int fd)
{
int n, alloc = 0, space, len = 0;
char *buf = NULL;
while (1) {
space = alloc - len;
if (space == 0) {
space = CHUNKSIZE;
alloc += space;
buf = realloc(buf, alloc + 1); /* + 1 is '\0' */
}
n = read(fd, buf + len, space);
if (n > 0) {
len += n;
continue;
} else if (n == 0) {
break;
}
free(buf);
return -errno;
}
if (buf)
buf[len] = '\0';
*out = buf;
return 0;
}
static int read_file(char **out, const char *path)
{
int fd, retval;
fd = open(path, O_RDONLY);
if (fd == -1)
return -errno;
retval = read_fd(out, fd);
close(fd);
return retval;
}
2018-04-25 14:29:11 +02:00
int main(int argc, const char **argv)
{
int i, retval;
2018-04-28 14:03:30 +02:00
const char *arg;
const char *files[argc], *texts[argc];
unsigned int nfiles = 0, ntexts = 0;
unsigned int flags = 0;
char *input, *output;
2018-04-28 14:03:30 +02:00
2020-01-02 16:21:58 +01:00
if (argc == 1) {
print_usage();
return -1;
}
for (i = 1; i < argc; i++) {
2018-04-28 14:03:30 +02:00
arg = argv[i];
if (*arg == '-') {
switch (arg[1]) {
2020-01-02 16:21:58 +01:00
case 'r':
flags |= FLAG_REVERSE;
2018-04-28 14:03:30 +02:00
continue;
2018-05-18 10:55:23 +02:00
case 'e':
flags |= FLAG_ENCODE;
continue;
2020-01-02 16:21:58 +01:00
case 'c':
flags |= FLAG_CZECH;
continue;
case 'f':
files[nfiles++] = argv[++i];
continue;
2018-05-18 15:48:39 +02:00
case 'h':
print_usage();
return 0;
case 'v':
print_version();
return 0;
2018-04-28 14:03:30 +02:00
}
error("unknown option '%s'.", arg);
return -1;
} else {
texts[ntexts++] = arg;
2018-04-28 14:03:30 +02:00
}
}
for (i = 0; i < ntexts; i++) {
output = process_input(texts[i], flags);
fprintf(stdout, "%s\n", output);
free(output);
}
for (i = 0; i < nfiles; i++) {
if (strcmp(files[i], "-") == 0)
retval = read_fd(&input, STDIN_FILENO);
else
retval = read_file(&input, files[i]);
if (retval != 0) {
error("failed to read file '%s'.", files[i]);
return retval;
}
2020-01-02 16:21:58 +01:00
output = process_input(input, flags);
fprintf(stdout, "%s", output);
free(output);
free(input);
2020-09-25 15:35:36 +02:00
}
return 0;
2018-04-25 14:29:11 +02:00
}