]>
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 | ||
11fdf7f2 | 9 | #include <cstring> |
7c673cae FG |
10 | #include <fstream> |
11 | #include <iostream> | |
12 | #include <iterator> | |
b32b8144 | 13 | #include <vector> |
7c673cae | 14 | |
7c673cae | 15 | #include <boost/spirit/include/classic_primitives.hpp> |
11fdf7f2 | 16 | #include <boost/spirit/include/classic_scanner.hpp> |
7c673cae FG |
17 | |
18 | namespace spirit = boost::spirit::classic; | |
19 | ||
20 | typedef std::istream_iterator<char, char> iterator; | |
21 | typedef spirit::scanner<iterator> scanner; | |
22 | ||
11fdf7f2 | 23 | int main(int argc, char* argv[]) |
7c673cae | 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. | |
11fdf7f2 TL |
33 | } |
34 | else { | |
b32b8144 FG |
35 | std::cerr << "ERROR: Invalid flag: " << argv[i] << std::endl; |
36 | usage_error = true; | |
37 | } | |
11fdf7f2 TL |
38 | } |
39 | else { | |
b32b8144 FG |
40 | args.push_back(argv[i]); |
41 | } | |
42 | } | |
43 | ||
11fdf7f2 | 44 | if (!usage_error && args.size() != 2) { |
7c673cae | 45 | std::cerr << "ERROR: Wrong number of arguments." << std::endl; |
b32b8144 FG |
46 | usage_error = true; |
47 | } | |
7c673cae | 48 | |
b32b8144 FG |
49 | if (usage_error) { |
50 | std::cout << "Usage:\n\t" << argv[0] << " file1 file2" << std::endl; | |
7c673cae FG |
51 | return 1; |
52 | } | |
53 | ||
11fdf7f2 | 54 | std::ifstream file1(args[0], std::ios_base::binary | std::ios_base::in), |
b32b8144 | 55 | file2(args[1], std::ios_base::binary | std::ios_base::in); |
7c673cae | 56 | |
11fdf7f2 | 57 | if (!file1 || !file2) { |
7c673cae FG |
58 | std::cerr << "ERROR: Unable to open one or both files." << std::endl; |
59 | return 2; | |
60 | } | |
61 | ||
62 | file1.unsetf(std::ios_base::skipws); | |
63 | file2.unsetf(std::ios_base::skipws); | |
64 | ||
11fdf7f2 | 65 | iterator iter_file1(file1), iter_file2(file2); |
7c673cae | 66 | |
11fdf7f2 | 67 | scanner scan1(iter_file1, iterator()), scan2(iter_file2, iterator()); |
7c673cae FG |
68 | |
69 | std::size_t line = 1, column = 1; | |
70 | ||
11fdf7f2 TL |
71 | while (!scan1.at_end() && !scan2.at_end()) { |
72 | if (spirit::eol_p.parse(scan1)) { | |
73 | if (!spirit::eol_p.parse(scan2)) { | |
74 | std::cout << "Files differ at line " << line << ", column " | |
75 | << column << '.' << std::endl; | |
7c673cae FG |
76 | return 3; |
77 | } | |
78 | ||
79 | ++line, column = 1; | |
80 | continue; | |
81 | } | |
82 | ||
11fdf7f2 TL |
83 | if (*scan1 != *scan2) { |
84 | std::cout << "Files differ at line " << line << ", column " | |
85 | << column << '.' << std::endl; | |
7c673cae FG |
86 | return 4; |
87 | } | |
88 | ||
89 | ++scan1, ++scan2, ++column; | |
90 | } | |
91 | ||
11fdf7f2 | 92 | if (scan1.at_end() != scan2.at_end()) { |
7c673cae FG |
93 | std::cout << "Files differ in length." << std::endl; |
94 | return 5; | |
95 | } | |
96 | } |