diff --git a/src/connector/odbc/tests/CMakeLists.txt b/src/connector/odbc/tests/CMakeLists.txt index cb159a79abda1798935d06b92a8f69c0983ab916..1cc6acaf4bf34aa2158cc1f4fa0836d6e51f3a41 100644 --- a/src/connector/odbc/tests/CMakeLists.txt +++ b/src/connector/odbc/tests/CMakeLists.txt @@ -8,9 +8,11 @@ IF (TD_LINUX) ENDIF () 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) ADD_EXECUTABLE(tcodbc main.c) TARGET_LINK_LIBRARIES(tcodbc odbc32 odbccp32 user32 legacy_stdio_definitions os) ADD_EXECUTABLE(tconv tconv.c) - TARGET_LINK_LIBRARIES(tconv os) + TARGET_LINK_LIBRARIES(tconv tutil) ENDIF () diff --git a/src/connector/odbc/tests/tconv.c b/src/connector/odbc/tests/tconv.c index f0a528cf9a94a3359fdb9b2aaf3983614a30e734..acae6421bbe52d8522f37a756d5097a3b32bb5c7 100644 --- a/src/connector/odbc/tests/tconv.c +++ b/src/connector/odbc/tests/tconv.c @@ -3,21 +3,21 @@ #ifdef _MSC_VER #include #include -#include "msvcIconv.h" -#else -#include #endif +#include + #include #include 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[]) { const char *from_enc = "UTF-8"; const char *to_enc = "UTF-8"; + const char *dst_file = NULL; const char *src = NULL; #ifdef _MSC_VER from_enc = "CP936"; @@ -44,6 +44,14 @@ int main(int argc, char *argv[]) { } to_enc = argv[i]; continue; + } else if (strcmp(arg, "-o") == 0 ) { + i += 1; + if (i>=argc) { + fprintf(stderr, "expecing , but got nothing\n"); + return 1; + } + dst_file = argv[i]; + continue; } else if (arg[0]=='-') { fprintf(stderr, "unknown argument: [%s]\n", arg); return 1; @@ -56,33 +64,49 @@ int main(int argc, char *argv[]) { continue; } } + int r = -1; FILE *fin = src ? fopen(src, "rb") : stdin; - if (!fin) { - fprintf(stderr, "failed to open file [%s]\n", src); - return 1; - } - int r = 0; + FILE *fout = dst_file ? fopen(dst_file, "wb") : stdout; + iconv_t cnv = iconv_open(to_enc, from_enc); do { - iconv_t cnv = iconv_open(to_enc, from_enc); + if (!fin) { + fprintf(stderr, "failed to open file [%s]\n", src); + break; + } + if (!fout) { + fprintf(stderr, "failed to open file [%s]\n", dst_file); + 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) { 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); + cnv = (iconv_t)-1; } while (0); - fclose(fin); + if (fin && fin != stdin) fclose(fin); + if (fout && fout != stdout) fclose(fout); return r ? 1 : 0; } static void usage(const char *arg0) { - fprintf(stderr, "%s -h | [-f ] [-t ] [file]\n", arg0); + fprintf(stderr, "%s -h | [-f ] [-t ] [-o ] [file]\n", arg0); return; } -#define IN_SIZE (256*1024) +#define IN_SIZE (64*1024) #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; char src[IN_SIZE]; size_t slen = sizeof(src); @@ -117,7 +141,7 @@ static int do_conv(iconv_t cnv, FILE *fin) { break; } } - n = fwrite(dst, 1, (size_t)(dd-dst), stdout); + n = fwrite(dst, 1, (size_t)(dd-dst), fout); if (n