-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·109 lines (93 loc) · 2.74 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·109 lines (93 loc) · 2.74 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
static std::string readfile(const char filename[], const bool dots)
{
std::ifstream f(filename);
if (!f.is_open()) {
std::cerr << "error: failed to open '" << filename << "'\n";
return "";
}
std::ostringstream ret;
enum {tagname, tagdata, content} state = content;
for (char c = f.get(); f.good(); c = f.get()) {
// update state..
if (c == '<') {
state = tagname;
} else if (c == ' ' && state == tagname) {
state = tagdata;
} else if (c == '>' && (state == tagname || state == tagdata)) {
state = content;
}
// filtering input data..
if (state == tagname) {
ret << c;
} else if (state == content) {
if (c == '\n')
ret << c;
else if (dots && (c == '.' ||
c == '!' ||
c == '?' ||
c == '\"' ||
c == '>' ||
std::isdigit(c&0xff))) {
ret << c;
}
}
}
return ret.str();
}
int main(int argc, char** argv)
{
std::cout << "xmldocdiff\n";
std::string file1, file2;
bool dots = false;
if (argc == 3) {
file1 = argv[1];
file2 = argv[2];
} else if (argc == 4 && strcmp(argv[1],"--dots")==0) {
file1 = argv[2];
file2 = argv[3];
} else {
std::cout << "syntax: xmldocdiff [--dots] file1.xml file2.xml\n";
return 1;
}
const std::string data1(readfile(argv[1], dots));
const std::string data2(readfile(argv[2], dots));
/*
std::ofstream f1("dbg1.txt");
f1 << data1;
std::ofstream f2("dbg2.txt");
f2 << data2;
*/
// match data1 and data2, ignoring newlines
unsigned int line1 = 1, line2 = 1;
std::string::size_type pos1 = 0, pos2 = 0;
while (pos1 < data1.size() || pos2 < data2.size()) {
unsigned char c1 = 0;
while (pos1 < data1.size() && c1 == 0) {
c1 = data1[pos1++];
if (c1 == '\n') {
line1++;
c1 = 0;
}
}
unsigned char c2 = 0;
while (pos2 < data2.size() && c2 == 0) {
c2 = data2[pos2++];
if (c2 == '\n') {
line2++;
c2 = 0;
}
}
if (c1 != c2) {
std::cerr << "mismatch.\n"
<< " line1=" << line1 << " line2=" << line2 << "\n"
<< " data1=" << (char)c1 << " data2=" << (char)c2 << "\n";
return 1;
}
}
return 0;
}