summaryrefslogtreecommitdiff
path: root/utilities/normcode.cpp
blob: 4a5bbf6e4d9ab6663b4cd5938cb72e5e3aeee536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*  normcode.cpp
 *  Transcodes Latin-1/UTF-8 to canonically normalized UTF-8/SCSU
 */

#include <iostream>
#include <string>
#include <fstream>


#include "unicode/utypes.h"
#include "unicode/convert.h"
#include "unicode/ustring.h"
#include "unicode/normlzr.h"

#define BUFSIZE 65536

int main (char argc, char** argv) {

  if (argc < 5) {
    cout << "usage: " << argv[0] << " -[8|l] <input file> -[8|s] <outputfile> -[c|d]" << endl;
    cout << "The first argument should be -8 if input is UTF-8 or -l if it is Latin-1/Codepage 1252." << endl;
    cout << "The third argument should be -8 for UTF-8 output or -s for SCSU output." << endl;
    cout << "The fifth arg should be -c if you want to pre-compose or -d if you want to decompose (default)." << endl;
    return -1;
  }

  fstream inf, outf;
  char* buffer = new char[BUFSIZE];
  UnicodeString source, norm;
  UErrorCode err;
  int32_t buf32;

  UnicodeConverter latin("Latin-1", err);
  if (U_FAILURE(err)) cout << "Latin-1 converter error" << endl;
  UnicodeConverter scsu("SCSU", err);
  if (U_FAILURE(err)) cout << "SCSU converter error" << endl;
  UnicodeConverter utf8("UTF-8", err);
  if (U_FAILURE(err)) cout << "UTF-8 converter error" << endl;

  inf.open(argv[2], fstream::in);
  outf.open(argv[4], fstream::out);

  while (!inf.eof()) {
    inf.getline(buffer, BUFSIZE);
    buf32 = inf.gcount();
    if (buf32 == 0) {
      outf << endl;
      continue;
    }
    buffer[buf32-1] = '\n';
    buffer[buf32] = 0;

    if (argv[1][1] == 'l') {
      latin.toUnicodeString(source, buffer, buf32, err);
      if (U_FAILURE(err)) cout << "Latin-1 conversion error" << endl;
    } else {
      utf8.toUnicodeString(source, buffer, buf32, err);
      if (U_FAILURE(err)) cout << "UTF-8 conversion error" << endl;
    }

    if (argc > 5) {
      if (argv[5][1] == 'c') {
	Normalizer::normalize(source, UNORM_NFC, 0, norm, err); //canonical composition
      } else {
	Normalizer::normalize(source, UNORM_NFD, 0, norm, err); //canonical decomposition
      }
    } else {
      Normalizer::normalize(source, UNORM_NFD, 0, norm, err); //canonical decomposition
    }
    
    buf32 = BUFSIZE;
    if (argv[3][1] == 's') {
      scsu.fromUnicodeString((char*)buffer, buf32, norm, err);
      if (U_FAILURE(err)) cout << "SCSU conversion error" << endl;
      //      scsu.toUnicodeString(norm, buffer, buf32, err);
      //      buf32 = BUFSIZE;
      //      utf8.fromUnicodeString((char*)buffer, buf32, norm, err);
    } else {
      utf8.fromUnicodeString((char*)buffer, buf32, norm, err);
      if (U_FAILURE(err)) cout << "UTF-8 conversion error" << endl;
    }

    if (U_SUCCESS(err) && !inf.eof()) {
      buffer[buf32] = 0;
    }
    
    outf.write(buffer, buf32);
  }

  outf.close();
  inf.close();

  delete buffer;

  return 0;
}