#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include "config.h"
#include <librcc.h>
|
enum | Modes { MODE_STDIN = 0x1000,
MODE_DIRECTORY,
MODE_FILE,
MODE_FILELIST
} |
|
enum | Options {
OPT_CONFIG = 'c',
OPT_ENCODING_IN = 'e',
OPT_FROM = 'f',
OPT_HELP = 'h',
OPT_LANGUAGE_IN = 'l',
OPT_TO = 't',
OPT_YES = 'y',
OPT_ENCODING_OUT,
OPT_LANGUAGE_OUT,
OPT_TRANSLATION,
OPT_CACHING,
OPT_CACHE,
OPT_AUTODETECT,
OPT_OFFLINE,
OPT_TIMEOUT,
OPT_SUBDIRS
} |
|
◆ RCC_OPTION_TRANSLATE_SKIP_PARENT
◆ Modes
Enumerator |
---|
MODE_STDIN | |
MODE_DIRECTORY | |
MODE_FILE | |
MODE_FILELIST | |
◆ Options
Enumerator |
---|
OPT_CONFIG | |
OPT_ENCODING_IN | |
OPT_FROM | |
OPT_HELP | |
OPT_LANGUAGE_IN | |
OPT_TO | |
OPT_YES | |
OPT_ENCODING_OUT | |
OPT_LANGUAGE_OUT | |
OPT_TRANSLATION | |
OPT_CACHING | |
OPT_CACHE | |
OPT_AUTODETECT | |
OPT_OFFLINE | |
OPT_TIMEOUT | |
OPT_SUBDIRS | |
◆ Directory()
int Directory |
( |
const char * |
arg | ) |
|
561 struct dirent *entry;
570 printf(
"Processing directory: %s\n", arg);
574 fprintf(stderr,
"*** Failed to process directory: %s\n", arg);
578 entry = readdir(dir);
580 if (entry->d_name[0] ==
'.') {
581 entry = readdir(dir);
587 if (strcmp(res, entry->d_name)) {
589 printf(
"Rename \"%s\" to \"%s\" (y/[n]) ", entry->d_name, res);
590 scanf(
"%c", &answer);
591 if (answer !=
'\n') fgets(stmp, 255, stdin);
592 answer = ((answer==
'y')||(answer==
'Y'))?1:0;
601 if (!lstat(nfn, &st)) {
603 printf(
"Trying rename \"%s\" to \"%s\"\n", entry->d_name, res);
606 if (S_ISDIR(st.st_mode)) {
607 printf(
"*** Directory with that name exists, skipping\n");
610 printf(
"*** File exists, overwrite (y/[n]) ");
611 scanf(
"%c", &answer);
612 if (answer !=
'\n') fgets(stmp, 255, stdin);
613 answer = ((answer==
'y')||(answer==
'Y'))?1:0;
617 err = rename(fn, nfn);
625 printf(
"*** Renaming \"%s\" to \"%s\" is failed (errno: %u)\n", entry->d_name, res, errno);
627 printf(
"Rename completed: \"%s\" to \"%s\"\n", entry->d_name, res);
633 entry = readdir(dir);
637 if (process_subdirs) {
641 entry = readdir(dir);
643 if (entry->d_name[0] ==
'.') {
644 entry = readdir(dir);
650 if ((!lstat(fn, &st))&&((S_ISDIR(st.st_mode)))) {
655 entry = readdir(dir);
References Translate().
◆ Fullname()
char* Fullname |
( |
const char * |
path, |
|
|
const char * |
name |
|
) |
| |
544 res = (
char*)malloc(strlen(path) + strlen(name) + 2);
546 if (path[strlen(path)-1] ==
'/')
547 sprintf(res,
"%s%s",path,name);
549 sprintf(res,
"%s/%s",path,name);
◆ GetClass()
198 for (i = 1; classes[i].
name; i++) {
199 if ((!strcasecmp(name, classes[i].name))||(!strcasecmp(name, classes[i].fullname)))
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
225 char *config_name = NULL;
226 char *cache_name = NULL;
231 unsigned char from_forced = 0;
232 unsigned char to_forced = 0;
241 int ldetect_force = 0;
243 unsigned long timeout = 0;
246 int option_index = 0;
247 while ((c = getopt_long(argc, argv,
"yhe:f:l:t:", long_options, &option_index)) != (
unsigned char)-1) {
256 config_name = optarg;
280 if (!ldetect_force) {
292 else if (!strcasecmp(optarg,
"full"))
294 else if (!strcasecmp(optarg,
"skip_parent"))
296 else if (!strcasecmp(optarg,
"skip_related"))
298 else if (!strcasecmp(optarg,
"english"))
300 else if (!strcasecmp(optarg,
"transliterate"))
302 else if (!strcasecmp(optarg,
"off"))
305 fprintf(stderr,
"*** Unknown translation mode: %s\n\n", optarg);
310 if (!ldetect_force) {
311 if (!strcasecmp(optarg,
"off"))
320 else if (!strcasecmp(optarg,
"off"))
322 else if (!strcasecmp(optarg,
"use"))
324 else if (!strcasecmp(optarg,
"add"))
326 else if (!strcasecmp(optarg,
"replace"))
329 fprintf(stderr,
"*** Unknown caching mode: %s\n\n", optarg);
337 if (!optarg) ldetect = 1;
338 else if (!strcasecmp(optarg,
"off")) {
341 }
else if (!strcasecmp(optarg,
"on")) {
345 }
else if (!strcasecmp(optarg,
"all")) {
352 timeout = atoi(optarg);
370 if ((optind + 1) < argc) {
371 fprintf(stderr,
"*** Invalid non-option arguments:\n");
372 for (;optind < argc;optind++) {
375 fprintf(stderr,
"\n\n");
384 if (!from_forced) from =
"fs";
385 if (!to_forced) to =
"fs";
391 setlocale(LC_ALL,
"");
401 if (config_name)
rccLoad(NULL, config_name);
417 if ((lfrom)||(lto)) {
422 #ifdef RCC_OPTION_OFFLINE
431 fprintf(stderr,
"*** Invalid source class (%s) specified\n", from);
439 fprintf(stderr,
"*** Invalid target class (%s) specified\n", to);
451 fprintf(stderr,
"*** Invalid source language (%s) specified\n", lfrom);
454 }
else source_language_id = current_language_id;
460 fprintf(stderr,
"*** Invalid target language (%s) specified\n", lto);
463 }
else target_language_id = current_language_id;
465 if (source_language_id == target_language_id) {
466 language_id = source_language_id;
468 if (language_id != current_language_id) {
471 fprintf(stderr,
"*** Unable to set the specified language (%s)\n",
rccGetLanguageName(NULL, language_id));
477 if (current_language_id != english_language_id) {
478 language_id = english_language_id;
484 fprintf(stderr,
"*** Default language (%s) is not configured\n",
rccGetLanguageName(NULL, current_language_id));
496 fprintf(stderr,
"*** Unable to set source language (%s)\n",
rccGetLanguageName(NULL, source_language_id));
501 fprintf(stderr,
"*** Unable to set target language (%s)\n",
rccGetLanguageName(NULL, target_language_id));
514 fprintf(stderr,
"*** Mode (FILE) is not supported in current version\n");
517 fprintf(stderr,
"*** Mode (FILELIST) is not supported in current version\n");
References RCC_OPTION_LEARNING_FLAG_USE.
◆ Stdin()
int Stdin |
( |
const char * |
arg | ) |
|
532 while (fgets(buf,16384,stdin)) {
534 fprintf(stdout, res?res:buf);
References Translate().
◆ Translate()
char * Translate |
( |
const char * |
source | ) |
|
666 char *recoded, *stmp;
668 if (strlen(source)<2)
return NULL;
670 if (source_language_id != target_language_id) {
675 else rccstring =
rccFrom(NULL, source_class_id, source);
677 if (!rccstring)
return NULL;
679 if (source_language_id != target_language_id)
684 stmp =
rccTo(NULL, target_class_id, rccstring);
687 if (recoded) free(stmp);
689 }
else recoded = NULL;
694 }
else recoded =
rccTo(NULL, target_class_id, rccstring);
Referenced by Directory(), and Stdin().
◆ Usage()
void Usage |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
110 " %s [options] [mode] [file|directory]\n"
112 " --stdin - Convert stdin to stdout\n"
113 " --directory - Convert file names in specified directory\n"
114 " --file - Convert specified file\n"
115 " --filelist - Convert all files writed on stdin\n"
116 " --help - Help message\n"
119 " -c <config> - Specify configuration name\n"
120 " -f <class> - Source class ('in' is default)\n"
121 " -t <class> - Output class ('out' is default)\n"
122 " -e <enc> - Force specified source encoding (autodetection)\n"
123 " -l <lang> - Force specified source language (from LC_CTYPE)\n"
124 " --force-target-encoding=<enc>\n"
125 " - Convert to the specified encoding\n"
126 " --force-target-language=<enc>\n"
127 " - Translate to the specified language\n"
128 " --caching=[mode]\n"
129 " - Use recodings cache. Following modes are supported\n"
131 " use - Use cached values (default)\n"
132 " add - Add new recodings to cache\n"
133 " replace - Replace encodings in cache\n"
135 " - Use specified cache database instead of default one\n"
136 " --translation=[mode]\n"
137 " - Enable translation. Following modes are supported:\n"
139 " skip_parent - Skip translation to parent lang\n"
140 " skip_related - Skip translation between related langs\n"
141 " english - Translate to english (default)\n"
142 " transliterate - Transliterate\n"
143 " --language-detection=[mode]\n"
144 " - Lanuage autodetection. Following modes are supported:\n"
145 " off - Current language is considered\n"
146 " on - Use only configured langs (default)\n"
147 " all - Try everything (slow)\n"
149 " - Specify recoding timeout in microseconds (1s default)\n"
151 " -y - Do not ask any question\n"
152 " --disable-subdirs\n"
153 " - Do not descend into the sub directories\n"
155 " Language Relations:\n"
156 " To prevent unneccesary translations the concept of related/parent languages is\n"
157 " introduced. For each language you can specify a parent language.\n"
158 " skip_parent translation option will turn off translation to parent language\n"
159 " skip_related translation option will additionaly turn off translation from\n"
160 " parent language.\n"
162 " For example, in the default configuration Russian is parent of Ukrainian, and\n"
163 " English is parent of all other languages. With \"skip_parrent\" option the\n"
164 " translation from Russian to Ukrainian would be turned off, but translation\n"
165 " from Ukrainian to Russian would operate. With \"skip_related\" option the\n"
166 " translation in both directions would be disabled\n"
168 " Language Detection:\n"
169 " Current version uses aspell dictionaries to autodetect language. Therefore,\n"
170 " only languages with aspell available in the system aspell dictionaries are\n"
171 " autodected. Beware, if your system contains a lot of installed languages,\n"
172 " the autodection may take considerable amount of time.\n"
◆ mode
@ OPT_TRANSLATION
Definition: rcc-recode.c:72
int rccInitDefaultContext(const char *locale_variable, unsigned int max_languages, unsigned int max_classes, rcc_class_ptr defclasses, rcc_init_flags flags)
char * Translate(const char *source)
Definition: rcc-recode.c:664
@ RCC_OPTION_TRANSLATE_TRANSLITERATE
Definition: librcc.h:449
#define rccTo(ctx, class_id, buf)
Definition: librcc.h:1454
void Usage(int argc, char *argv[])
Definition: rcc-recode.c:107
@ RCC_OPTION_TRANSLATE_SKIP_RELATED
Definition: librcc.h:451
@ OPT_AUTODETECT
Definition: rcc-recode.c:75
@ RCC_OPTION_TRANSLATE_FULL
Definition: librcc.h:453
char * Fullname(const char *path, const char *name)
Definition: rcc-recode.c:541
const char * rccGetLanguageName(rcc_context ctx, rcc_language_id language_id)
@ OPT_FROM
Definition: rcc-recode.c:65
rcc_language_id rccGetCurrentLanguage(rcc_context ctx)
#define RCC_OPTION_TRANSLATE_SKIP_PARENT
Definition: rcc-recode.c:50
#define rccFrom(ctx, class_id, buf)
Definition: librcc.h:1453
@ RCC_OPTION_CONFIGURED_LANGUAGES_ONLY
Definition: librcc.h:463
@ OPT_TO
Definition: rcc-recode.c:68
@ RCC_OPTION_TRANSLATE
Definition: librcc.h:466
#define rccToCharset(ctx, charset, buf)
Definition: librcc.h:1458
@ RCC_OPTION_AUTODETECT_LANGUAGE
Definition: librcc.h:465
@ MODE_FILE
Definition: rcc-recode.c:56
@ OPT_HELP
Definition: rcc-recode.c:66
int rcc_class_id
Definition: librcc.h:75
@ OPT_CACHING
Definition: rcc-recode.c:73
int rccSetLanguage(rcc_context ctx, rcc_language_id language_id)
int rccLoad(rcc_context ctx, const char *name)
rcc_language_id rccGetLanguageByName(rcc_context ctx, const char *name)
@ OPT_CONFIG
Definition: rcc-recode.c:63
@ RCC_OPTION_TRANSLATE_TO_ENGLISH
Definition: librcc.h:450
Options
Definition: rcc-recode.c:62
@ OPT_LANGUAGE_IN
Definition: rcc-recode.c:67
unsigned char rcc_language_id
Definition: librcc.h:43
@ MODE_DIRECTORY
Definition: rcc-recode.c:55
@ OPT_TIMEOUT
Definition: rcc-recode.c:77
@ OPT_CACHE
Definition: rcc-recode.c:74
#define RCC_OPTION_LEARNING_FLAG_USE
Definition: librcc.h:441
int Stdin(const char *arg)
Definition: rcc-recode.c:528
int mode
Definition: rcc-recode.c:60
@ OPT_ENCODING_IN
Definition: rcc-recode.c:64
#define rccRecodeCharsets(ctx, from, to, buf)
Definition: librcc.h:1461
@ RCC_OPTION_TRANSLATE_OFF
Definition: librcc.h:448
#define RCC_OPTION_LEARNING_FLAG_LEARN
Definition: librcc.h:445
int Directory(const char *arg)
Definition: rcc-recode.c:556
char * rcc_string
Definition: librcc.h:1032
@ RCC_OPTION_OFFLINE
Definition: librcc.h:468
int rccSetOption(rcc_context ctx, rcc_option option, rcc_option_value value)
rcc_class_id GetClass(const char *name)
Definition: rcc-recode.c:195
@ MODE_FILELIST
Definition: rcc-recode.c:57
@ OPT_ENCODING_OUT
Definition: rcc-recode.c:70
@ OPT_OFFLINE
Definition: rcc-recode.c:76
@ OPT_YES
Definition: rcc-recode.c:69
@ RCC_OPTION_TIMEOUT
Definition: librcc.h:467
int rccInitDb4(rcc_context ctx, const char *name, rcc_db4_flags flags)
const char * name
Definition: librcc.h:369
@ RCC_OPTION_LEARNING_MODE
Definition: librcc.h:460
#define rccFromCharset(ctx, charset, buf)
Definition: librcc.h:1457
@ MODE_STDIN
Definition: rcc-recode.c:54
@ OPT_LANGUAGE_OUT
Definition: rcc-recode.c:71
const char * rccGetCurrentLanguageName(rcc_context ctx)
@ OPT_SUBDIRS
Definition: rcc-recode.c:78
Modes
Definition: rcc-recode.c:53