utf8iter driver: read from stdin
This commit is contained in:
parent
d812fb8079
commit
262e7260d8
@ -56,6 +56,8 @@ void Usage() {
|
|||||||
static int op_flags;
|
static int op_flags;
|
||||||
#define OPT_v 0x2
|
#define OPT_v 0x2
|
||||||
|
|
||||||
|
FILE *infout = stdout;
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
thisprog = argv[0];
|
thisprog = argv[0];
|
||||||
@ -73,12 +75,14 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
argc--;argv++;
|
argc--;argv++;
|
||||||
}
|
}
|
||||||
|
string infile, outfile;
|
||||||
if (argc != 2) {
|
if (argc == 2) {
|
||||||
|
infile = *argv++;argc--;
|
||||||
|
outfile = *argv++;argc--;
|
||||||
|
Usage();
|
||||||
|
} else if (argc != 0) {
|
||||||
Usage();
|
Usage();
|
||||||
}
|
}
|
||||||
const char *infile = *argv++;argc--;
|
|
||||||
const char *outfile = *argv++;argc--;
|
|
||||||
string in;
|
string in;
|
||||||
if (!file_to_string(infile, in)) {
|
if (!file_to_string(infile, in)) {
|
||||||
cerr << "Cant read file\n" << endl;
|
cerr << "Cant read file\n" << endl;
|
||||||
@ -88,29 +92,35 @@ int main(int argc, char **argv)
|
|||||||
vector<unsigned int>ucsout1;
|
vector<unsigned int>ucsout1;
|
||||||
string out, out1;
|
string out, out1;
|
||||||
Utf8Iter it(in);
|
Utf8Iter it(in);
|
||||||
FILE *fp = fopen(outfile, "w");
|
FILE *fp = 0;
|
||||||
|
if (!outfile.empty()) {
|
||||||
|
fp = fopen(outfile.c_str(), "w");
|
||||||
if (fp == 0) {
|
if (fp == 0) {
|
||||||
fprintf(stderr, "cant create %s\n", outfile);
|
cerr << "Can't create " << outfile << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int nchars = 0;
|
int nchars = 0;
|
||||||
for (;!it.eof(); it++) {
|
for (;!it.eof(); it++) {
|
||||||
unsigned int value = *it;
|
unsigned int value = *it;
|
||||||
if (value == (unsigned int)-1) {
|
if (value == (unsigned int)-1) {
|
||||||
cerr << "Conversion error occurred\n" << endl;
|
cerr << "Conversion error occurred at position " << it.getBpos()
|
||||||
|
<< endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (op_flags & OPT_v) {
|
if (op_flags & OPT_v) {
|
||||||
printf("Value: 0x%x", value);
|
fprintf(infout, "Value: 0x%04x", value);
|
||||||
if (value < 0x7f)
|
if (value < 0x7f)
|
||||||
printf(" (%c) ", value);
|
fprintf(stdout, " (%c) ", value);
|
||||||
printf("\n");
|
fprintf(infout, "\n");
|
||||||
}
|
}
|
||||||
// UTF-32LE or BE array
|
// UTF-32LE or BE array
|
||||||
ucsout1.push_back(value);
|
ucsout1.push_back(value);
|
||||||
|
if (fp) {
|
||||||
// UTF-32LE or BE file
|
// UTF-32LE or BE file
|
||||||
fwrite(&value, 4, 1, fp);
|
fwrite(&value, 4, 1, fp);
|
||||||
|
}
|
||||||
|
|
||||||
// Reconstructed utf8 strings (2 methods)
|
// Reconstructed utf8 strings (2 methods)
|
||||||
if (!it.appendchartostring(out))
|
if (!it.appendchartostring(out))
|
||||||
@ -121,9 +131,11 @@ int main(int argc, char **argv)
|
|||||||
// fprintf(stderr, "%s", string(it).c_str());
|
// fprintf(stderr, "%s", string(it).c_str());
|
||||||
nchars++;
|
nchars++;
|
||||||
}
|
}
|
||||||
|
if (fp) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "nchars %d\n", nchars);
|
fprintf(infout, "Found %d Unicode characters\n", nchars);
|
||||||
if (in.compare(out)) {
|
if (in.compare(out)) {
|
||||||
fprintf(stderr, "error: out != in\n");
|
fprintf(stderr, "error: out != in\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -139,7 +151,6 @@ int main(int argc, char **argv)
|
|||||||
for (int i = 0; ; i++) {
|
for (int i = 0; ; i++) {
|
||||||
unsigned int value;
|
unsigned int value;
|
||||||
if ((value = it[i]) == (unsigned int)-1) {
|
if ((value = it[i]) == (unsigned int)-1) {
|
||||||
fprintf(stderr, "%d chars\n", i);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
it++;
|
it++;
|
||||||
@ -155,13 +166,10 @@ int main(int argc, char **argv)
|
|||||||
int ercnt;
|
int ercnt;
|
||||||
const char *encoding = "UTF-32LE"; // note : use BE on high-endian machine
|
const char *encoding = "UTF-32LE"; // note : use BE on high-endian machine
|
||||||
string ucs, ucs1;
|
string ucs, ucs1;
|
||||||
for (vector<unsigned int>::iterator it = ucsout1.begin();
|
for (const unsigned int i : ucsout1) {
|
||||||
it != ucsout1.end(); it++) {
|
|
||||||
unsigned int i = *it;
|
|
||||||
ucs.append((const char *)&i, 4);
|
ucs.append((const char *)&i, 4);
|
||||||
}
|
}
|
||||||
if (!transcode(ucs, ucs1,
|
if (!transcode(ucs, ucs1, encoding, encoding, &ercnt) || ercnt) {
|
||||||
encoding, encoding, &ercnt) || ercnt) {
|
|
||||||
fprintf(stderr, "Transcode check failed, ercount: %d\n", ercnt);
|
fprintf(stderr, "Transcode check failed, ercount: %d\n", ercnt);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -170,8 +178,7 @@ int main(int argc, char **argv)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!transcode(ucs, ucs1,
|
if (!transcode(ucs, ucs1, encoding, "UTF-8", &ercnt) || ercnt) {
|
||||||
encoding, "UTF-8", &ercnt) || ercnt) {
|
|
||||||
fprintf(stderr, "Transcode back to utf-8 check failed, ercount: %d\n",
|
fprintf(stderr, "Transcode back to utf-8 check failed, ercount: %d\n",
|
||||||
ercnt);
|
ercnt);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -182,4 +189,3 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user