tokiponize/tokiponize.c

247 lines
4.6 KiB
C

/*
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <uthash.h>
#include <yac.h>
const char *TOKI_PONA_KEY[] = {
"a", "󱤀",
"akesi", "󱤁",
"ala", "󱤂",
"alasa", "󱤃",
"ale", "󱤄",
"anpa", "󱤅",
"ante", "󱤆",
"anu", "󱤇",
"awen", "󱤈",
"e", "󱤉",
"en", "󱤊",
"esun", "󱤋",
"ijo", "󱤌",
"ike", "󱤍",
"ilo", "󱤎",
"insa", "󱤏",
"jaki", "󱤐",
"jan", "󱤑",
"jelo", "󱤒",
"jo", "󱤓",
"kala", "󱤔",
"kalama", "󱤕",
"kama", "󱤖",
"kasi", "󱤗",
"ken", "󱤘",
"kepeken", "󱤙",
"kili", "󱤚",
"kiwen", "󱤛",
"ko", "󱤜",
"kon", "󱤝",
"kule", "󱤞",
"kulupu", "󱤟",
"kute", "󱤠",
"la", "󱤡",
"lape", "󱤢",
"laso", "󱤣",
"lawa", "󱤤",
"len", "󱤥",
"lete", "󱤦",
"li", "󱤧",
"lili", "󱤨",
"linja", "󱤩",
"lipu", "󱤪",
"loje", "󱤫",
"lon", "󱤬",
"luka", "󱤭",
"lukin", "󱤮",
"lupa", "󱤯",
"ma", "󱤰",
"mama", "󱤱",
"mani", "󱤲",
"meli", "󱤳",
"mi", "󱤴",
"mije", "󱤵",
"moku", "󱤶",
"moli", "󱤷",
"monsi", "󱤸",
"mu", "󱤹",
"mun", "󱤺",
"musi", "󱤻",
"mute", "󱤼",
"nanpa", "󱤽",
"nasa", "󱤾",
"nasin", "󱤿",
"nena", "󱥀",
"ni", "󱥁",
"nimi", "󱥂",
"noka", "󱥃",
"o", "󱥄",
"olin", "󱥅",
"ona", "󱥆",
"open", "󱥇",
"pakala", "󱥈",
"pali", "󱥉",
"palisa", "󱥊",
"pan", "󱥋",
"pana", "󱥌",
"pi", "󱥍",
"pilin", "󱥎",
"pimeja", "󱥏",
"pini", "󱥐",
"pipi", "󱥑",
"poka", "󱥒",
"poki", "󱥓",
"pona", "󱥔",
"pu", "󱥕",
"sama", "󱥖",
"seli", "󱥗",
"selo", "󱥘",
"seme", "󱥙",
"sewi", "󱥚",
"sijelo", "󱥛",
"sike", "󱥜",
"sin", "󱥝",
"sina", "󱥞",
"sinpin", "󱥟",
"sitelen", "󱥠",
"sona", "󱥡",
"soweli", "󱥢",
"suli", "󱥣",
"suno", "󱥤",
"supa", "󱥥",
"suwi", "󱥦",
"tan", "󱥧",
"taso", "󱥨",
"tawa", "󱥩",
"telo", "󱥪",
"tenpo", "󱥫",
"toki", "󱥬",
"tomo", "󱥭",
"tu", "󱥮",
"unpa", "󱥯",
"uta", "󱥰",
"utala", "󱥱",
"walo", "󱥲",
"wan", "󱥳",
"waso", "󱥴",
"wawa", "󱥵",
"weka", "󱥶",
"wile", "󱥷",
"namako", "󱥸",
"kin", "󱥹",
"oko", "󱥺",
"kipisi", "󱥻",
"leko", "󱥼",
"monsuta", "󱥽",
"tonsi", "󱥾",
"jasima", "󱥿",
"kijetesantakalu", "󱦀",
"soko", "󱦁",
"meso", "󱦂",
"epiku", "󱦃",
"kokosila", "󱦄",
"lanpan", "󱦅",
"n", "󱦆",
"misikeke", "󱦇",
"ku", "󱦈",
"pake", "󱦠",
"apeja", "󱦡",
"majuna", "󱦢",
"powe", "󱦣"
};
struct nimi {
const char *word;
const char *glyph;
UT_hash_handle hh;
};
struct nimi *nimi_mute = NULL;
const char *ante(char *search_word, struct nimi *n) {
const char *pana;
struct nimi *result = NULL;
HASH_FIND_STR(n, search_word, result);
if (result) {
pana = result->glyph;
} else { pana = search_word; }
return pana;
}
void lukin(FILE *file, struct nimi *n) {
int byte;
char lasina[16];
for (int i = 0; (byte = fgetc(file)) != EOF; i++) {
switch (byte) {
case ' ':
lasina[i] = '\0';
printf("%s ", ante(lasina, n));
i = -1;
break;
case '\n':
lasina[i] = '\0';
printf("%s\n", ante(lasina, n));
i = -1;
break;
default:
lasina[i] = byte;
break;
}
}
}
int main(int argc, char *argv[]) {
for (int i = 0; i < sizeof(TOKI_PONA_KEY) / sizeof(char *); i += 2) {
const char *word = TOKI_PONA_KEY[i];
const char *glyph = TOKI_PONA_KEY[i + 1];
struct nimi *n = malloc(sizeof(struct nimi));
if (n == NULL) { exit(1); }
n->word = word;
n->glyph = glyph;
HASH_ADD_KEYPTR(hh, nimi_mute, n->word, strlen(n->word), n);
}
if (argc = 1) {
lukin(stdin, nimi_mute);
}
FILE *file;
for (int i = 1; i < argc; i++) {
file = rpath(argv[0], argv[i]);
if (file != NULL) {
lukin(file, nimi_mute);
if (file != stdin) { fclose(file); }
} else { continue; }
}
}