diff options
| author | Manuel Traut <manut@mecka.net> | 2010-02-14 18:17:19 +0100 |
|---|---|---|
| committer | Manuel Traut <manut@mecka.net> | 2010-02-14 18:17:19 +0100 |
| commit | 8fd86418e05384ce6fa4ca1818ab3a2162f1cc18 (patch) | |
| tree | 68c727fa659ad1cea5cb8c25f864d3c7d922d822 | |
cyr: used to translate latin strings into cyrllic
- all latin strings given as parameters are translated into according cyrillic
strings
- future work: enable reading from stdin
add reverse translation (cyrillic string from stdin into latin
charecters)
Signed-off-by: Manuel Traut <manut@mecka.net>
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | cyr.c | 206 |
2 files changed, 221 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b56ab3c --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +PREFIX = /usr +OBJECTS = cyr.o + +all: $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) -o cyr + +%.o : %.c + $(CC) $(CFLAGS) -c $< + +install: + cp cyr $(PREFIX)/bin + +clean: + rm *.o + rm cyr @@ -0,0 +1,206 @@ +/* + cyr - cyr translates latin characters into the according cyrillic characters + + Copyright (C) 2010 Manuel Traut <manut@mecka.net> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 as + published by the Free Software Foundation. + + 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include <stdio.h> +#include <locale.h> +#include <wchar.h> +#include <string.h> + +#define CONV_TAB_SIZE 44 + +static wchar_t conv_tab[CONV_TAB_SIZE][2] = { + L"аa", L"бb", L"вv", L"гg", L"дd", L"еe", L"зz", L"иi", L"йj", L"кk", L"лl", + L"мm", L"нn", L"оo", L"пp", L"рr", L"сs", L"тt", L"уu", L"фf", L"хh", L"ыy", + L"AА", L"БB", L"ВV", L"ГG", L"ДD", L"ЕE", L"ЗZ", L"ИI", L"ЙJ", L"КK", L"ЛL", + L"МM", L"НN", L"ОO", L"ПP", L"РR", L"СS", L"ТT", L"УU", L"ФF", L"ХH", L"ЫY" }; + +wchar_t conv (wchar_t huhu) +{ + unsigned int pos = 0; + for (; pos < CONV_TAB_SIZE; pos++) + { + if (conv_tab [pos][1] == huhu) + return conv_tab [pos][0]; + } + return '?'; +} + +int lookup_multi2 (wchar_t *str, int pos) +{ + if (str[pos] == 'j') + { + if (str[pos+1] == 'a') + { + wprintf(L"я"); + return 1; + } + if (str[pos+1] == 'o') + { + wprintf(L"ё"); + return 1; + } + if (str[pos+1] == 'u') + { + wprintf(L"ю"); + return 1; + } + if (str[pos+1] == 'e') + { + wprintf(L"э"); + return 1; + } + } + + if (str[pos] == 'J') + { + if ((str[pos+1] == 'a') || (str[pos+1] == 'A')) + { + wprintf(L"Я"); + return 1; + } + if ((str[pos+1] == 'o') || (str[pos+1] == 'O')) + { + wprintf(L"Ё"); + return 1; + } + if ((str[pos+1] == 'u') || (str[pos+1] == 'U')) + { + wprintf(L"Ю"); + return 1; + } + if ((str[pos+1] == 'e') || (str[pos+1] == 'E')) + { + wprintf(L"Э"); + return 1; + } + } + + if ((str[pos] == 'c') && (str[pos+1] == 'h')) + { + wprintf(L"х"); + return 1; + } + if ((str[pos] =='C') && ((str[pos+1] == 'h') || (str[pos+1] == 'H'))) + { + wprintf(L"Х"); + return 1; + } + if ((str[pos] == 't') && (str[pos+1] == 's')) + { + wprintf(L"ц"); + return 1; + } + if ((str[pos] == 'T') && (str[pos+1] == 's' || (str[pos+1] == 'S'))) + { + wprintf(L"Ц"); + return 1; + } + return 0; +} + +int lookup_multi3 (wchar_t *str, int pos) +{ + if ((str[pos] == 's') && (str[pos+1] == 'c') && (str[pos+2] == 'h')) + { + wprintf(L"ж"); + return 2; + } + + if ((str[pos] == 'S') && ((str[pos+1] == 'C') || (str[pos+1] == 'c')) && + ((str[pos+2] == 'H') || (str[pos+2] == 'h'))) + { + wprintf(L"Ж"); + return 2; + } + return 0; +} + +int lookup_multi7 (wchar_t *str, int pos) +{ + if ((str[pos] == 's') && (str[pos+1] == 'c') && (str[pos+2] == 'h') && + (str[pos+3] == 't') && + (str[pos+4] == 's') && (str[pos+5] == 'c') && (str[pos+6] == 'h')) + { + wprintf(L"щ"); + return 6; + } + if ((str[pos] == 'S') && (str[pos+1] == 'c'|'C') && (str[pos+2] == 'h'|'H') && + (str[pos+3] == 't'|'T') && + (str[pos+4] == 's'|'S') && (str[pos+5] == 'c'|'C') && (str[pos+6] == + 'h'|'H')) + { + wprintf(L"Щ"); + return 6; + } + + return 0; +} + +static wchar_t *cyrillic = L"Ч Ш Ъ Ь "; +static wchar_t *cyrillic2 = L"ч ш ъ ь "; + +int lookup_multi (wchar_t *str, int pos) +{ + int ret; + ret = lookup_multi7 (str, pos); + if (!ret) + ret = lookup_multi3 (str, pos); + if (!ret) + ret = lookup_multi2 (str, pos); + return ret; +} + +int main (int argc, char **argv) +{ + unsigned int ret, len, arg_no, i = 0; + mbstate_t state; + + if (argc < 2) + { + printf ("USAGE OF 'cyr'\n"); + printf ("cyr <word 1> ... <word n>\n"); + return 0; + } + + if (!setlocale (LC_CTYPE, "")) + { + fprintf (stderr, "can't set locale\n"); + return -1; + } + + memset (&state, 0, sizeof (mbstate_t)); + + for (arg_no = 1; arg_no < argc; arg_no++) + { + wchar_t w_str[strlen (argv[arg_no])]; + len = mbsrtowcs (w_str, (const char **)&(argv[arg_no]), strlen(argv[arg_no]), &state); + + for (i = 0; i < len; i++) + { + ret = lookup_multi (w_str, i); + if (!ret) + wprintf (L"%lc", conv(w_str[i])); + else + i += ret; + } + wprintf (L" "); + } + wprintf (L"\n"); + return 0; +} |
