]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2005 João Abecasis | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See | |
5 | // accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | ||
9 | #include <fstream> | |
10 | #include <iostream> | |
11 | #include <iterator> | |
b32b8144 FG |
12 | #include <cstring> |
13 | #include <vector> | |
7c673cae FG |
14 | |
15 | #include <boost/spirit/include/classic_scanner.hpp> | |
16 | #include <boost/spirit/include/classic_primitives.hpp> | |
17 | ||
18 | namespace spirit = boost::spirit::classic; | |
19 | ||
20 | typedef std::istream_iterator<char, char> iterator; | |
21 | typedef spirit::scanner<iterator> scanner; | |
22 | ||
23 | int main(int argc, char * argv[]) | |
24 | { | |
b32b8144 FG |
25 | std::vector<char*> args; |
26 | bool usage_error = false; | |
27 | ||
28 | for (int i = 1; i < argc; ++i) { | |
29 | if (std::strncmp(argv[i], "--", 2) == 0) { | |
30 | if (strcmp(argv[i], "--strict") == 0) { | |
31 | // Ignore --strict because the build file accidentally | |
32 | // uses it. Why yes, this is a horrible hack. | |
33 | } else { | |
34 | std::cerr << "ERROR: Invalid flag: " << argv[i] << std::endl; | |
35 | usage_error = true; | |
36 | } | |
37 | } else { | |
38 | args.push_back(argv[i]); | |
39 | } | |
40 | } | |
41 | ||
42 | if (!usage_error && args.size() != 2) | |
7c673cae FG |
43 | { |
44 | std::cerr << "ERROR: Wrong number of arguments." << std::endl; | |
b32b8144 FG |
45 | usage_error = true; |
46 | } | |
7c673cae | 47 | |
b32b8144 FG |
48 | if (usage_error) { |
49 | std::cout << "Usage:\n\t" << argv[0] << " file1 file2" << std::endl; | |
7c673cae FG |
50 | return 1; |
51 | } | |
52 | ||
53 | std::ifstream | |
b32b8144 FG |
54 | file1(args[0], std::ios_base::binary | std::ios_base::in), |
55 | file2(args[1], std::ios_base::binary | std::ios_base::in); | |
7c673cae FG |
56 | |
57 | if (!file1 || !file2) | |
58 | { | |
59 | std::cerr << "ERROR: Unable to open one or both files." << std::endl; | |
60 | return 2; | |
61 | } | |
62 | ||
63 | file1.unsetf(std::ios_base::skipws); | |
64 | file2.unsetf(std::ios_base::skipws); | |
65 | ||
66 | iterator | |
67 | iter_file1(file1), | |
68 | iter_file2(file2); | |
69 | ||
70 | scanner | |
71 | scan1(iter_file1, iterator()), | |
72 | scan2(iter_file2, iterator()); | |
73 | ||
74 | std::size_t line = 1, column = 1; | |
75 | ||
76 | while (!scan1.at_end() && !scan2.at_end()) | |
77 | { | |
78 | if (spirit::eol_p.parse(scan1)) | |
79 | { | |
80 | if (!spirit::eol_p.parse(scan2)) | |
81 | { | |
82 | std::cout << "Files differ at line " << line << ", column " << | |
83 | column << '.' << std::endl; | |
84 | return 3; | |
85 | } | |
86 | ||
87 | ++line, column = 1; | |
88 | continue; | |
89 | } | |
90 | ||
91 | if (*scan1 != *scan2) | |
92 | { | |
93 | std::cout << "Files differ at line " << line << ", column " << | |
94 | column << '.' << std::endl; | |
95 | return 4; | |
96 | } | |
97 | ||
98 | ++scan1, ++scan2, ++column; | |
99 | } | |
100 | ||
101 | if (scan1.at_end() != scan2.at_end()) | |
102 | { | |
103 | std::cout << "Files differ in length." << std::endl; | |
104 | return 5; | |
105 | } | |
106 | } |