Parent: [40a590] (diff)

Child: [ae8ff5] (diff)

Download this file

utf8iter.cpp    84 lines (74 with data), 1.7 kB

 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
#ifndef lint
static char rcsid[] = "@(#$Id: utf8iter.cpp,v 1.2 2005-02-11 11:20:02 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
#include <stdio.h>
#include <string>
#include <iostream>
#include <list>
#include <vector>
#include "debuglog.h"
using namespace std;
#include "utf8iter.h"
#include "readfile.h"
int main(int argc, char **argv)
{
if (argc != 3) {
cerr << "Usage: utf8iter infile outfile" << endl;
exit(1);
}
const char *infile = argv[1];
const char *outfile = argv[2];
string in;
if (!file_to_string(infile, in)) {
cerr << "Cant read file\n" << endl;
exit(1);
}
vector<unsigned int>ucsout1;
string out, out1;
Utf8Iter it(in);
FILE *fp = fopen(outfile, "w");
if (fp == 0) {
fprintf(stderr, "cant create %s\n", outfile);
exit(1);
}
int nchars = 0;
for (;!it.eof(); it++) {
unsigned int value = *it;
if (value == (unsigned int)-1) {
fprintf(stderr, "Conversion error occurred\n");
exit(1);
}
ucsout1.push_back(value);
fwrite(&value, 4, 1, fp);
if (!it.appendchartostring(out))
break;
out1 += it;
nchars++;
}
fprintf(stderr, "nchars1 %d\n", nchars);
if (in != out) {
fprintf(stderr, "error: out != in\n");
exit(1);
}
if (in != out1) {
fprintf(stderr, "error: out1 != in\n");
exit(1);
}
vector<unsigned int>ucsout2;
it.rewind();
for (int i = 0; ; i++) {
unsigned int value;
if ((value = it[i]) == (unsigned int)-1) {
fprintf(stderr, "%d chars\n", i);
break;
}
it++;
ucsout2.push_back(value);
}
if (ucsout1 != ucsout2) {
fprintf(stderr, "error: ucsout1 != ucsout2\n");
exit(1);
}
fclose(fp);
exit(0);
}