tconv.c 3.9 KB
Newer Older
F
freemine 已提交
1 2 3 4 5 6 7
#include "../src/todbc_log.h"

#ifdef _MSC_VER
#include <winsock2.h>
#include <windows.h>
#endif

F
freemine 已提交
8 9
#include <iconv.h>

F
freemine 已提交
10 11 12 13 14

#include <stdio.h>
#include <string.h>

static void usage(const char *arg0);
F
freemine 已提交
15
static int  do_conv(iconv_t cnv, FILE *fin, FILE *fout);
F
freemine 已提交
16 17 18 19

int main(int argc, char *argv[]) {
  const char *from_enc = "UTF-8";
  const char *to_enc   = "UTF-8";
F
freemine 已提交
20
  const char *dst_file = NULL;
F
freemine 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  const char *src      = NULL;
#ifdef _MSC_VER
  from_enc = "CP936";
  to_enc   = "CP936";
#endif
  for (int i = 1; i < argc; i++) {
    const char *arg = argv[i];
    if (strcmp(arg, "-h") == 0) {
      usage(argv[0]);
      return 0;
    } else if (strcmp(arg, "-f") == 0 ) {
      i += 1;
      if (i>=argc) {
        fprintf(stderr, "expecing <from_enc>, but got nothing\n");
        return 1;
      }
      from_enc = argv[i];
      continue;
    } else if (strcmp(arg, "-t") == 0 ) {
      i += 1;
      if (i>=argc) {
        fprintf(stderr, "expecing <to_enc>, but got nothing\n");
        return 1;
      }
      to_enc = argv[i];
      continue;
F
freemine 已提交
47 48 49 50 51 52 53 54
    } 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;
F
freemine 已提交
55 56 57 58 59 60 61 62 63 64 65 66
    } else if (arg[0]=='-') {
      fprintf(stderr, "unknown argument: [%s]\n", arg);
      return 1;
    } else {
      if (src) {
        fprintf(stderr, "does not allow multiple files\n");
        return 1;
      }
      src = arg;
      continue;
    }
  }
F
freemine 已提交
67
  int r = -1;
F
freemine 已提交
68
  FILE *fin = src ? fopen(src, "rb") : stdin;
F
freemine 已提交
69 70
  FILE *fout = dst_file ? fopen(dst_file, "wb") : stdout;
  iconv_t cnv = iconv_open(to_enc, from_enc);
F
freemine 已提交
71
  do {
F
freemine 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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

F
freemine 已提交
89 90
    if (cnv == (iconv_t)-1) {
      fprintf(stderr, "failed to open conv from [%s] to [%s]: [%s]\n", from_enc, to_enc, strerror(errno));
F
freemine 已提交
91
      break;
F
freemine 已提交
92
    }
F
freemine 已提交
93
    r = do_conv(cnv, fin, fout);
F
freemine 已提交
94
    iconv_close(cnv);
F
freemine 已提交
95
    cnv = (iconv_t)-1;
F
freemine 已提交
96
  } while (0);
F
freemine 已提交
97 98
  if (fin && fin != stdin) fclose(fin);
  if (fout && fout != stdout) fclose(fout);
F
freemine 已提交
99 100 101 102
  return r ? 1 : 0;
}

static void usage(const char *arg0) {
F
freemine 已提交
103
  fprintf(stderr, "%s -h | [-f <from_enc>] [-t <to_enc>] [-o <dst file>] [file]\n", arg0);
F
freemine 已提交
104 105 106
  return;
}

F
freemine 已提交
107
#define IN_SIZE     (64*1024)
F
freemine 已提交
108
#define OUT_SIZE    (8*IN_SIZE)
F
freemine 已提交
109
static int do_conv(iconv_t cnv, FILE *fin, FILE *fout) {
F
freemine 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  int r = 0;
  char src[IN_SIZE];
  size_t slen = sizeof(src);
  char dst[OUT_SIZE];
  size_t dlen = sizeof(dst);
  char *start = src;
  while (!feof(fin)) {
    slen = (size_t)(src + sizeof(src)  - start);
    size_t n = fread(start, 1, slen, fin);
    if (n>0) {
      char   *ss = src;
      size_t  sl = n;
      while (sl) {
        char   *dd = dst;
        size_t  dn = dlen;
        size_t v = iconv(cnv, &ss, &sl, &dd, &dn);
        if (v==(size_t)-1) {
          int err = errno;
          if (err == EILSEQ) {
            fprintf(stderr, "failed to convert: [%s]\n", strerror(err));
            r = -1;
            break;
          }
          if (err == EINVAL) {
            fprintf(stderr, "[%s]\n", strerror(errno));
            size_t ava = (size_t)(src + sizeof(src) - ss);
            memcpy(src, ss, ava);
            start = ss;
          } else {
            fprintf(stderr, "internal logic error: [%s]\n", strerror(errno));
            r = -1;
            break;
          }
        }
F
freemine 已提交
144
        n = fwrite(dst, 1, (size_t)(dd-dst), fout);
F
freemine 已提交
145 146 147 148 149 150 151 152 153 154 155 156
        if (n<dd-dst) {
          fprintf(stderr, "failed to write: [%s]\n", strerror(errno));
          r = -1;
          break;
        }
      }
      if (r) break;
    }
  }
  return r ? -1 : 0;
}