-
Notifications
You must be signed in to change notification settings - Fork 2
/
conf.cc
252 lines (224 loc) · 9.22 KB
/
conf.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* 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 "conf.h"
#include "token.h"
#include <iostream>
#include <fstream>
using namespace std;
static void expand_tree(const string& path, filepath_container& ds)
{
DIR* dir = opendir(path.c_str());
if (!dir)
throw error() << "Failed open directory '" << path << "'";
ds.insert(ds.end(), filepath(path));
struct dirent* ent = readdir(dir);
while (ent) {
string subpath = path + "/" + ent->d_name;
struct stat st;
if (stat(subpath.c_str(), &st)!=0)
throw error() << "Failed stat file " << subpath;
if (S_ISDIR(st.st_mode)) {
if (strcmp(ent->d_name, ".")!=0 && strcmp(ent->d_name, "..")!=0) {
expand_tree(subpath.c_str(), ds);
}
}
ent = readdir(dir);
}
closedir(dir);
}
config::config(const string& file, bool need_rom, bool need_sample, bool need_disk, bool need_change)
{
string cfg;
if (file.length())
cfg = file;
else
cfg = "advscan.rc";
ifstream is(cfg.c_str());
if (!is)
throw error() << "Failed open of the configuration file " << cfg;
filepath_container base_romreadonlytree;
while (!is.eof()) {
string s;
getline(is, s, '\n');
s = strip_space(s);
if (s.length() == 0)
continue;
if (s[0]=='#')
continue;
unsigned i = 0;
string tag = token_get(s, i, " \t");
token_skip(s, i, " \t");
string arg = token_get(s, i, "");
if (tag == "rom") {
unsigned j = 0;
while (j < arg.length()) {
string dir = token_get(arg, j, DIR_SEP);
if (j < arg.length() && arg[j] == DIR_SEP)
++j;
rompath.insert(rompath.end(), filepath(file_adjust(dir)));
}
} else if (tag == "sample") {
unsigned j = 0;
while (j < arg.length()) {
string dir = token_get(arg, j, DIR_SEP);
if (j < arg.length() && arg[j] == DIR_SEP)
++j;
samplepath.insert(samplepath.end(), filepath(file_adjust(dir)));
}
} else if (tag == "disk") {
unsigned j = 0;
while (j < arg.length()) {
string dir = token_get(arg, j, DIR_SEP);
if (j < arg.length() && arg[j] == DIR_SEP)
++j;
diskpath.insert(diskpath.end(), filepath(file_adjust(dir)));
}
} else if (tag == "rom_import") {
unsigned j = 0;
while (j < arg.length()) {
string dir = token_get(arg, j, DIR_SEP);
if (j < arg.length() && arg[j] == DIR_SEP)
++j;
base_romreadonlytree.insert(base_romreadonlytree.end(), filepath(file_adjust(dir)));
}
} else if (tag == "rom_unknown") {
if (romunknownpath.file_get().length())
throw error() << "Double specification of option `rom_unknown' in file " << cfg;
if (arg.length() == 0)
throw error() << "Empty specification of option `rom_unknown' in file " << cfg;
if (arg.find(DIR_SEP) != string::npos)
throw error() << "Multiple path specification in option `rom_unknown' in file " << cfg;
romunknownpath.file_set(file_adjust(arg));
} else if (tag == "sample_unknown") {
if (sampleunknownpath.file_get().length())
throw error() << "Double specification of option `sample_unknown' in file " << cfg;
if (arg.length() == 0)
throw error() << "Empty specification of option `sample_unknown' in file " << cfg;
if (arg.find(DIR_SEP) != string::npos)
throw error() << "Multiple path specification in option `sample_unknown' in file " << cfg;
sampleunknownpath.file_set(file_adjust(arg));
} else if (tag == "disk_unknown") {
if (diskunknownpath.file_get().length())
throw error() << "Double specification of option `disk_unknown' in file " << cfg;
if (arg.length() == 0)
throw error() << "Empty specification of option `disk_unknown' in file " << cfg;
if (arg.find(DIR_SEP) != string::npos)
throw error() << "Multiple path specification in option `disk_unknown' in file " << cfg;
diskunknownpath.file_set(file_adjust(arg));
} else if (tag == "rom_new") {
if (romnewpath.file_get().length())
throw error() << "Double specification of option `rom_new' in file " << cfg;
if (arg.length() == 0)
throw error() << "Empty specification of option `rom_new' in file " << cfg;
if (arg.find(DIR_SEP) != string::npos)
throw error() << "Multiple path specification in option `rom_new' in file " << cfg;
romnewpath.file_set(file_adjust(arg));
} else {
throw error() << "Unknown option `" << tag << "' in file " << cfg;
}
}
is.close();
if (need_rom) {
if (rompath.empty())
throw error() << "Missing option `rom' option in file " << cfg;
for(filepath_container::iterator i=rompath.begin();i!=rompath.end();++i) {
if (access(i->file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << i->file_get() << " on `rom' option in file " << cfg;
}
if (need_change) {
for(filepath_container::iterator i=rompath.begin();i!=rompath.end();++i) {
if (access(i->file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << i->file_get() << " on `rom' option in file " << cfg;
}
if (!romnewpath.file_get().length())
throw error() << "Missing `rom_new' option in file " << cfg;
if (access(romnewpath.file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << romnewpath.file_get() << " on `rom_new' option in file " << cfg;
if (access(romnewpath.file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << romnewpath.file_get() << " on `rom_new' option in file " << cfg;
if (!romunknownpath.file_get().length())
throw error() << "Missing `rom_unknown' option in file " << cfg;
if (access(romunknownpath.file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << romunknownpath.file_get() << " on `rom_unknown' option in file " << cfg;
if (access(romunknownpath.file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << romunknownpath.file_get() << " on `rom_unknown' option in file " << cfg;
}
}
if (need_sample) {
if (samplepath.empty())
throw error() << "Missing `sample' option in file " << cfg;
for(filepath_container::iterator i=samplepath.begin();i!=samplepath.end();++i) {
if (access(i->file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << i->file_get() << " on `sample' option in file " << cfg;
}
if (need_change) {
for(filepath_container::iterator i=samplepath.begin();i!=samplepath.end();++i) {
if (access(i->file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << i->file_get() << " on `sample' option in file " << cfg;
}
if (!sampleunknownpath.file_get().length())
throw error() << "Missing `sample_unknown' option in file " << cfg;
if (access(sampleunknownpath.file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << sampleunknownpath.file_get() << " on `sample_unknown' option in file " << cfg;
if (access(sampleunknownpath.file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << sampleunknownpath.file_get() << " on `sample_unknown' option in file " << cfg;
}
}
if (need_disk) {
if (diskpath.empty())
throw error() << "Missing `disk' option in file " << cfg;
for(filepath_container::iterator i=diskpath.begin();i!=diskpath.end();++i) {
if (access(i->file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << i->file_get() << " on `disk' option in file " << cfg;
}
if (need_change) {
for(filepath_container::iterator i=diskpath.begin();i!=diskpath.end();++i) {
if (access(i->file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << i->file_get() << " on `disk' option in file " << cfg;
}
if (!diskunknownpath.file_get().length())
throw error() << "Missing `disk_unknown' option in file " << cfg;
if (access(diskunknownpath.file_get().c_str(), F_OK) != 0)
throw error() << "Not existing dir " << diskunknownpath.file_get() << " on `disk_unknown' option in file " << cfg;
if (access(diskunknownpath.file_get().c_str(), W_OK) != 0)
throw error() << "Access denied on dir " << diskunknownpath.file_get() << " on `disk_unknown' option in file " << cfg;
}
}
// expand romreadonly recursively
filepath_container expand_romreadonlytree;
for(filepath_container::iterator i=base_romreadonlytree.begin();i!=base_romreadonlytree.end();++i) {
expand_tree(i->file_get(), expand_romreadonlytree);
}
// remove paths already in rompath
for(filepath_container::iterator i=expand_romreadonlytree.begin();i!=expand_romreadonlytree.end();++i) {
filepath_container::iterator j;
for(j=rompath.begin();j!=rompath.end();++j) {
if (file_compare(j->file_get(), i->file_get())==0)
break;
}
if (j==rompath.end()) {
romreadonlytree.insert(romreadonlytree.end(), *i);
}
}
}
config::~config()
{
}