提交 9491a4ac 编写于 作者: F freemine

port tconv to windows

上级 d117a0ae
...@@ -8,9 +8,11 @@ IF (TD_LINUX) ...@@ -8,9 +8,11 @@ IF (TD_LINUX)
ENDIF () ENDIF ()
IF (TD_WINDOWS_64) IF (TD_WINDOWS_64)
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /GL")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /GL")
# AUX_SOURCE_DIRECTORY(. SRC) # AUX_SOURCE_DIRECTORY(. SRC)
ADD_EXECUTABLE(tcodbc main.c) ADD_EXECUTABLE(tcodbc main.c)
TARGET_LINK_LIBRARIES(tcodbc odbc32 odbccp32 user32 legacy_stdio_definitions os) TARGET_LINK_LIBRARIES(tcodbc odbc32 odbccp32 user32 legacy_stdio_definitions os)
ADD_EXECUTABLE(tconv tconv.c) ADD_EXECUTABLE(tconv tconv.c)
TARGET_LINK_LIBRARIES(tconv os) TARGET_LINK_LIBRARIES(tconv tutil)
ENDIF () ENDIF ()
...@@ -3,21 +3,21 @@ ...@@ -3,21 +3,21 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
#include "msvcIconv.h"
#else
#include <iconv.h>
#endif #endif
#include <iconv.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
static void usage(const char *arg0); static void usage(const char *arg0);
static int do_conv(iconv_t cnv, FILE *fin); static int do_conv(iconv_t cnv, FILE *fin, FILE *fout);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
const char *from_enc = "UTF-8"; const char *from_enc = "UTF-8";
const char *to_enc = "UTF-8"; const char *to_enc = "UTF-8";
const char *dst_file = NULL;
const char *src = NULL; const char *src = NULL;
#ifdef _MSC_VER #ifdef _MSC_VER
from_enc = "CP936"; from_enc = "CP936";
...@@ -44,6 +44,14 @@ int main(int argc, char *argv[]) { ...@@ -44,6 +44,14 @@ int main(int argc, char *argv[]) {
} }
to_enc = argv[i]; to_enc = argv[i];
continue; continue;
} else if (strcmp(arg, "-o") == 0 ) {
i += 1;
if (i>=argc) {
fprintf(stderr, "expecing <dst_file>, but got nothing\n");
return 1;
}
dst_file = argv[i];
continue;
} else if (arg[0]=='-') { } else if (arg[0]=='-') {
fprintf(stderr, "unknown argument: [%s]\n", arg); fprintf(stderr, "unknown argument: [%s]\n", arg);
return 1; return 1;
...@@ -56,33 +64,49 @@ int main(int argc, char *argv[]) { ...@@ -56,33 +64,49 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
} }
int r = -1;
FILE *fin = src ? fopen(src, "rb") : stdin; FILE *fin = src ? fopen(src, "rb") : stdin;
FILE *fout = dst_file ? fopen(dst_file, "wb") : stdout;
iconv_t cnv = iconv_open(to_enc, from_enc);
do {
if (!fin) { if (!fin) {
fprintf(stderr, "failed to open file [%s]\n", src); fprintf(stderr, "failed to open file [%s]\n", src);
return 1; break;
} }
int r = 0; if (!fout) {
do { fprintf(stderr, "failed to open file [%s]\n", dst_file);
iconv_t cnv = iconv_open(to_enc, from_enc); break;
}
#ifdef _MSC_VER
if (fout == stdout) {
r = _setmode(_fileno(fout), _O_BINARY);
if (r == -1) {
fprintf(stderr, "Cannot set binary mode for output stream: %d[%s]\n", errno, strerror(errno));
}
}
#endif
if (cnv == (iconv_t)-1) { if (cnv == (iconv_t)-1) {
fprintf(stderr, "failed to open conv from [%s] to [%s]: [%s]\n", from_enc, to_enc, strerror(errno)); fprintf(stderr, "failed to open conv from [%s] to [%s]: [%s]\n", from_enc, to_enc, strerror(errno));
return -1; break;
} }
r = do_conv(cnv, fin); r = do_conv(cnv, fin, fout);
iconv_close(cnv); iconv_close(cnv);
cnv = (iconv_t)-1;
} while (0); } while (0);
fclose(fin); if (fin && fin != stdin) fclose(fin);
if (fout && fout != stdout) fclose(fout);
return r ? 1 : 0; return r ? 1 : 0;
} }
static void usage(const char *arg0) { static void usage(const char *arg0) {
fprintf(stderr, "%s -h | [-f <from_enc>] [-t <to_enc>] [file]\n", arg0); fprintf(stderr, "%s -h | [-f <from_enc>] [-t <to_enc>] [-o <dst file>] [file]\n", arg0);
return; return;
} }
#define IN_SIZE (256*1024) #define IN_SIZE (64*1024)
#define OUT_SIZE (8*IN_SIZE) #define OUT_SIZE (8*IN_SIZE)
static int do_conv(iconv_t cnv, FILE *fin) { static int do_conv(iconv_t cnv, FILE *fin, FILE *fout) {
int r = 0; int r = 0;
char src[IN_SIZE]; char src[IN_SIZE];
size_t slen = sizeof(src); size_t slen = sizeof(src);
...@@ -117,7 +141,7 @@ static int do_conv(iconv_t cnv, FILE *fin) { ...@@ -117,7 +141,7 @@ static int do_conv(iconv_t cnv, FILE *fin) {
break; break;
} }
} }
n = fwrite(dst, 1, (size_t)(dd-dst), stdout); n = fwrite(dst, 1, (size_t)(dd-dst), fout);
if (n<dd-dst) { if (n<dd-dst) {
fprintf(stderr, "failed to write: [%s]\n", strerror(errno)); fprintf(stderr, "failed to write: [%s]\n", strerror(errno));
r = -1; r = -1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册