19 #include "../opencc.h"
31 #define BUFFER_SIZE 65536
33 void convert(
const char* input_file,
34 const char* output_file,
35 const char* config_file) {
37 if (od == (opencc_t)-1) {
44 fp = fopen(input_file,
"r");
46 fprintf(stderr, _(
"Can not read file: %s\n"), input_file);
52 fpo = fopen(output_file,
"w");
54 fprintf(stderr, _(
"Can not write file: %s\n"), output_file);
58 size_t size = BUFFER_SIZE;
59 char* buffer_in = NULL, * buffer_out = NULL;
60 buffer_in = (
char*)malloc(size *
sizeof(
char));
61 char* lookahead = (
char*)malloc(size *
sizeof(
char));
62 size_t lookahead_size = 0;
65 if (lookahead_size > 0) {
66 memcpy(buffer_in, lookahead, lookahead_size);
68 fread(buffer_in + lookahead_size, 1, size - lookahead_size,
72 read = fread(buffer_in, 1, size, fp);
80 for (i = read - 1; i >= 0; i--) {
81 char c = buffer_in[i];
82 if (!(c & 0x80) || ((c & 0xC0) == 0xC0)) {
87 memcpy(lookahead, buffer_in + i, read - i);
88 lookahead_size = read - i;
91 buffer_in[read] =
'\0';
94 if (buffer_out != (
char*)-1) {
95 fprintf(fpo,
"%s", buffer_out);
103 if (lookahead_size > 0) {
104 assert(lookahead_size < size);
105 lookahead[lookahead_size] =
'\0';
107 if (buffer_out != (
char*)-1) {
108 fprintf(fpo,
"%s", buffer_out);
121 void show_version() {
123 printf(_(
"Open Chinese Convert (OpenCC) Command Line Tool\n"));
124 printf(_(
"Version %s\n"), VERSION);
126 printf(_(
"Author: %s\n"),
"BYVoid <byvoid@byvoid.com>");
127 printf(_(
"Bug Report: %s\n"),
"http://github.com/BYVoid/OpenCC/issues");
133 printf(_(
"Usage:\n"));
134 printf(_(
" opencc [Options]\n"));
136 printf(_(
"Options:\n"));
137 printf(_(
" -i [file], --input=[file] Read original text from [file].\n"));
138 printf(_(
" -o [file], --output=[file] Write converted text to [file].\n"));
140 " -c [file], --config=[file] Load configuration of conversion from [file].\n"));
141 printf(_(
" -v, --version Print version and build information.\n"));
142 printf(_(
" -h, --help Print this help.\n"));
145 "With no input file, reads standard input and writes converted stream to standard output.\n"));
147 "Default configuration(%s) will be loaded if not set.\n"),
152 int main(
int argc,
char** argv) {
153 #ifdef ENABLE_GETTEXT
154 setlocale(LC_ALL,
"");
155 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
157 static struct option longopts[] =
159 {
"version", no_argument, NULL,
'v' },
160 {
"help", no_argument, NULL,
'h' },
161 {
"input", required_argument, NULL,
'i' },
162 {
"output", required_argument, NULL,
'o' },
163 {
"config", required_argument, NULL,
'c' },
167 static char* input_file, * output_file, * config_file;
168 while ((oc = getopt_long(argc, argv,
"vh?i:o:c:", longopts, NULL)) != -1) {
178 input_file = mstrcpy(optarg);
181 output_file = mstrcpy(optarg);
184 config_file = mstrcpy(optarg);
188 if (config_file == NULL) {
191 convert(input_file, output_file, config_file);