-
Notifications
You must be signed in to change notification settings - Fork 2
/
diff.cc
221 lines (190 loc) · 4.7 KB
/
diff.cc
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* This file is part of the Advance project.
*
* Copyright (C) 2002 Andrea Mazzoleni
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "portable.h"
#include "rom.h"
#include "game.h"
#include "except.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
/**
* Check if A include B.
*/
bool include(const rom_by_name_set& A, const rom_by_name_set& B)
{
rom_by_crc_set Ac;
for(rom_by_name_set::const_iterator i=A.begin();i!=A.end();++i)
Ac.insert(*i);
for(rom_by_name_set::const_iterator i=B.begin();i!=B.end();++i) {
rom_by_crc_set::const_iterator j = Ac.find(*i);
if (j==Ac.end())
return false;
}
return true;
}
std::ostream& output_info(std::ostream& os, const rom& A)
{
os << "rom ( name " << A.name_get() << " size " << std::dec << A.size_get();
if (A.nodump_get())
os << " flags nodump";
else
os << " crc " << std::hex << setw(8) << setfill('0') << A.crc_get();
os << " )";
return os;
}
std::ostream& output_info(std::ostream &os, const game& A)
{
os << "game (\n";
os << "\tname " << A.name_get() << "\n";
for(rom_by_name_set::const_iterator i=A.rs_get().begin();i!=A.rs_get().end();++i) {
os << "\t";
output_info(os, *i);
os << "\n";
}
os << ")\n";
return os;
}
std::ostream& output_xml(std::ostream& os, const rom& A)
{
os << "<rom name=\"" << A.name_get() << "\" size=\"" << std::dec << A.size_get() << "\"";
if (A.nodump_get())
os << " status=\"nodump\"";
else
os << " crc=\"" << std::hex << setw(8) << setfill('0') << A.crc_get() << "\"";
os << "/>";
return os;
}
std::ostream& output_xml(std::ostream &os, const game& A)
{
os << "\t<game name=\"" << A.name_get() << "\">\n";
for(rom_by_name_set::const_iterator i=A.rs_get().begin();i!=A.rs_get().end();++i) {
os << "\t\t";
output_xml(os, *i);
os << "\n";
}
os << "\t</game>\n";
return os;
}
void version()
{
std::cout << PACKAGE " v" VERSION " by Andrea Mazzoleni" << std::endl;
}
void usage()
{
version();
cout << "Usage: advdiff [-i] info1.lst/xml info2.lst/xml" << endl;
cout << endl;
cout << "Options:" << endl;
cout << " " SWITCH_GETOPT_LONG("-i, --info ", "-i") " Output in info format" << endl;
}
#if HAVE_GETOPT_LONG
struct option long_options[] = {
{"info", 0, 0, 'i'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{0, 0, 0, 0}
};
#endif
#define OPTIONS "ihV"
void process(int argc, char* argv[])
{
gamearchive g0;
gamearchive g1;
bool opt_info;
opt_info = false;
if (argc <= 1) {
usage();
return;
}
int c = 0;
opterr = 0; // don't print errors
while ((c =
#if HAVE_GETOPT_LONG
getopt_long(argc, argv, OPTIONS, long_options, 0))
#else
getopt(argc, argv, OPTIONS))
#endif
!= EOF) {
switch (c) {
case 'i' :
opt_info = true;
break;
case 'h' :
usage();
return;
case 'V' :
version();
return;
default: {
// not optimal code for g++ 2.95.3
string opt;
opt = (char)optopt;
throw error() << "Unknown option `" << opt << "'";
}
}
}
if (argc - optind < 2)
throw error() << "Missing input files";
if (argc - optind > 2)
throw error() << "Too many input files";
string f0 = argv[optind+0];
string f1 = argv[optind+1];
ifstream ff0(f0.c_str(), ios::in | ios::binary);
if (!ff0)
throw error() << "Failed open of " << f0;
g0.load(ff0);
ff0.close();
ifstream ff1(f1.c_str(), ios::in | ios::binary);
if (!ff1)
throw error() << "Failed open of " << f1;
g1.load(ff1);
ff1.close();
if (!opt_info)
std::cout << "<mame>\n";
gamearchive::const_iterator i;
for(i=g1.begin();i!=g1.end();++i) {
gamearchive::const_iterator j = g0.find(*i);
if (j==g0.end() || !include(j->rs_get(), i->rs_get())) {
if (opt_info)
output_info(std::cout, *i) << "\n";
else
output_xml(std::cout, *i);
}
}
if (!opt_info)
std::cout << "</mame>\n";
}
int main(int argc, char* argv[])
{
try {
process(argc, argv);
} catch (error& e) {
cerr << e << endl;
exit(EXIT_FAILURE);
} catch (std::bad_alloc) {
cerr << "Low memory" << endl;
exit(EXIT_FAILURE);
} catch (...) {
cerr << "Unknown error" << endl;
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}