-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
167 lines (154 loc) · 4.7 KB
/
main.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Duplo: a duplicate source code checker
* Copyright (C) 2005-2013 the Duplo developers (see the AUTHORS file).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <list>
#include <getopt.h>
#include "Duplo.h"
using namespace std;
static void usage(ostream& out, const char* name, int status)
{
out << "Usage: " << name << " [options] <file>..." << endl;
out << " or: " << name << " [options] -i <file>" << endl;
out << endl;
out << "Find duplicate source code in input files." << endl;
out << endl;
out << "Options:" << endl;
out << " -h, --help Print this help, then exit" << endl;
out << " -v, --version Print version number, then exit" << endl;
out << " -q, --quiet Do not print progress information, only results" << endl;
out << " -x, --xml Generate XML output" << endl;
out << " -l, --min-lines N Set minimum number of lines that count as block to N (default: " << MIN_BLOCK_SIZE << ")" << endl;
out << " -c, --min-chars N Set minimum number of characters that count as line to N (default: " << MIN_CHARS << ")" << endl;
out << " -p, --ignore-preprocess Ignore preprocessor directives" << endl;
out << " -n, --ignore-same-name Also compare files with the same name" << endl;
out << " -i, --input <file> Read input files from <file>, one file per line" << endl;
out << " -o, --output <file> Write final results to <file> (default: stdout)" << endl;
out << endl;
out << "More information might be available here: https://github.com/bitfehler/duplo" << endl;
exit(status);
}
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "quiet", no_argument, 0, 'q' },
{ "min-lines", required_argument, 0, 'l' },
{ "min-chars", required_argument, 0, 'c' },
{ "ignore-preprocessor", no_argument, 0, 'p' },
{ "ignore-same-name", no_argument, 0, 'n' },
{ "xml", no_argument, 0, 'x' },
{ "input", required_argument, 0, 'i' },
{ "ouput", required_argument, 0, 'o' },
{ 0, 0, 0, 0 }
};
/**
* Main routine
*
* @param argc number of arguments
* @param argv arguments
*/
int main(int argc, char* argv[])
{
int c = 0;
bool quiet = false;
int min_lines = MIN_BLOCK_SIZE;
int min_chars = MIN_CHARS;
bool ignore_preprocessor = false;
bool ignore_same_name = false;
bool xml_output = false;
char* input = 0L;
char* output = 0L;
while ((c = getopt_long(argc, argv, "hvql:c:pnxi:o:", long_options, 0L)) != -1) {
switch (c) {
case 'h':
usage(cout, argv[0], 0);
break;
case 'v':
cout << "duplo v" << VERSION << endl;
exit(0);
break;
case 'q':
quiet = true;
break;
case 'l':
min_lines = atoi(optarg);
break;
case 'c':
min_chars = atoi(optarg);
break;
case 'p':
ignore_preprocessor = true;
break;
case 'n':
ignore_same_name = true;
break;
case 'x':
xml_output = true;
break;
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case '?':
default:
usage(cerr, argv[0], 1);
}
}
vector<string> input_files;
if (optind == argc) {
if (!input) {
cerr << "Error: please either supply input files or use the -i option!" << endl;
cerr << "Try " << argv[0] << " -h for details." << endl;
exit(1);
}
ifstream ifs(input);
if (!ifs.good()) {
cerr << "Error: failed to read input files from " << input << "!" << endl;
exit(1);
}
string line;
while (getline(ifs, line)) {
input_files.push_back(line);
}
} else {
if (input) {
cerr << "Error: can only handle input files or the -i option, but not both!" << endl;
cerr << "Try " << argv[0] << " -h for details." << endl;
exit(1);
}
for (int i = optind; i < argc; ++i) {
input_files.push_back(string(argv[i]));
}
}
ostream* out = &cout;
ofstream outf;
if (output) {
outf.open(output);
if (!outf.good()) {
cerr << "Error: cannot write to output file " << output << "!" << endl;
exit(1);
}
out = &outf;
}
Duplo duplo(input_files, min_lines, min_chars, ignore_preprocessor, ignore_same_name, xml_output, quiet);
duplo.run(*out);
return 0;
}