translit: now from koi8-u
[k8muffin.git] / src / translit.c
blobd2e13a9a3ac4f697696a17abbee81d307e8ac694
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 #include "translit.h"
12 #include <ctype.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
18 static const struct {
19 uint8_t code;
20 const char *ch;
21 } atrans[74] = {
22 {0xE1, "A"}, //á
23 {0xE0, "YU"}, //à
24 {0xE3, "C"}, //ã
25 {0xE2, "B"}, //â
26 {0xE5, "E"}, //å
27 {0xE4, "D"}, //ä
28 {0xE7, "G"}, //ç
29 {0xE6, "F"}, //æ
30 {0xE9, "I"}, //é
31 {0xE8, "H"}, //è
32 {0xEB, "K"}, //ë
33 {0xEA, "IJ"}, //ê
34 {0xED, "M"}, //í
35 {0xEC, "L"}, //ì
36 {0xEF, "O"}, //ï
37 {0xEE, "N"}, //î
38 {0xF1, "YA"}, //ñ
39 {0xF0, "P"}, //ð
40 {0xF3, "S"}, //ó
41 {0xF2, "R"}, //ò
42 {0xF5, "U"}, //õ
43 {0xF4, "T"}, //ô
44 {0xF7, "V"}, //÷
45 {0xF6, "J"}, //ö
46 {0xF9, "Y"}, //ù
47 {0xF8, ""}, //ø
48 {0xFB, "SH"}, //û
49 {0xFA, "Z"}, //ú
50 {0xFD, "SCH"}, //ý
51 {0xFC, "E"}, //ü
52 {0xFF, ""}, //ÿ
53 {0xFE, "CH"}, //þ
54 {0xA3, "yo"}, //£
55 {0xA4, "ye"}, //¤
56 {0xA7, "yj"}, //§
57 {0xA6, "i"}, //¦
58 {0xAD, "g"}, //­
59 {0xB3, "YO"}, //³
60 {0xB4, "YE"}, //´
61 {0xB7, "YJ"}, //·
62 {0xB6, "I"}, //¶
63 {0xBF, "(c)"}, //¿
64 {0xC1, "a"}, //Á
65 {0xC0, "yu"}, //À
66 {0xC3, "c"}, //Ã
67 {0xC2, "b"}, //Â
68 {0xC5, "e"}, //Å
69 {0xC4, "d"}, //Ä
70 {0xC7, "g"}, //Ç
71 {0xC6, "f"}, //Æ
72 {0xC9, "i"}, //É
73 {0xC8, "h"}, //È
74 {0xCB, "k"}, //Ë
75 {0xCA, "ij"}, //Ê
76 {0xCD, "m"}, //Í
77 {0xCC, "l"}, //Ì
78 {0xCF, "o"}, //Ï
79 {0xCE, "n"}, //Î
80 {0xD1, "ya"}, //Ñ
81 {0xD0, "p"}, //Ð
82 {0xD3, "s"}, //Ó
83 {0xD2, "r"}, //Ò
84 {0xD5, "u"}, //Õ
85 {0xD4, "t"}, //Ô
86 {0xD7, "v"}, //×
87 {0xD6, "j"}, //Ö
88 {0xD9, "y"}, //Ù
89 {0xD8, ""}, //Ø
90 {0xDB, "sh"}, //Û
91 {0xDA, "z"}, //Ú
92 {0xDD, "sch"}, //Ý
93 {0xDC, "e"}, //Ü
94 {0xDF, ""}, //ß
95 {0xDE, "ch"}, //Þ
99 static inline const char *findTRC (char c) {
100 uint8_t cc = c&0xff;
101 for (size_t f = 0; f < sizeof(atrans)/sizeof(atrans[0]); ++f) {
102 if (atrans[f].code == cc) return atrans[f].ch;
104 return NULL;
108 char *translitstr (const char *src) {
109 char *res, *dest;
110 if (src == NULL) src = "";
111 res = dest = calloc(strlen(src)*3+2, sizeof(char));
112 for (int f = 0; f < strlen(src); ++f) {
113 const char *ch = findTRC(src[f]);
114 if (ch != NULL) {
115 for (; *ch; ++ch) *dest++ = le2lower(*ch);
116 } else {
117 *dest++ = le2lower(src[f]);
120 *dest = '\0';
121 return res;