1 |
// Renegade Map Selector |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// RenegadeConfig.cpp |
6 |
|
7 |
#include "RenegadeConfig.h" |
8 |
|
9 |
bool RenegadeConfig::load() |
10 |
{ |
11 |
ifstream fin(file.c_str()); |
12 |
|
13 |
if (!fin.is_open()) return false; |
14 |
|
15 |
do |
16 |
{ |
17 |
string line; |
18 |
getline(fin, line); |
19 |
|
20 |
if (line[line.length() - 1] == '\r') |
21 |
{ |
22 |
line.erase(line.length() - 1); |
23 |
linux_ = true; |
24 |
} |
25 |
|
26 |
if (line.find("MapName=") == 0) |
27 |
{ |
28 |
text << "MapName=" << (fin.good() ? "\n" : ""); |
29 |
} |
30 |
else if (line.find("MapName") == 0) |
31 |
{ |
32 |
if (line.find("MapName00=") == 0) |
33 |
{ |
34 |
text << "MapName00=" << (fin.good() ? "\n" : ""); |
35 |
} |
36 |
|
37 |
maps.push_back(line.substr(10)); |
38 |
} |
39 |
else |
40 |
{ |
41 |
text << line << (fin.good() ? "\n" : ""); |
42 |
} |
43 |
} |
44 |
while (fin.good()); |
45 |
|
46 |
fin.close(); |
47 |
|
48 |
if (debug) |
49 |
{ |
50 |
cerr << "linux_ = " << (linux_ ? "true" : "false") << "\nmaps = {\n"; |
51 |
|
52 |
for (unsigned index = 0; index < maps.size(); index++) |
53 |
{ |
54 |
cerr << " [" << index << "] = " << maps[index] << "\n"; |
55 |
} |
56 |
|
57 |
cerr << "}\n"; |
58 |
} |
59 |
|
60 |
return true; |
61 |
} |
62 |
|
63 |
void RenegadeConfig::save() |
64 |
{ |
65 |
ofstream fout(file.c_str()); |
66 |
|
67 |
if (debug) |
68 |
{ |
69 |
cerr << "maps = {\n"; |
70 |
|
71 |
for (unsigned index = 0; index < maps.size(); index++) |
72 |
{ |
73 |
cerr << " [" << index << "] = " << maps[index] << "\n"; |
74 |
} |
75 |
|
76 |
cerr << "}\n"; |
77 |
} |
78 |
|
79 |
do |
80 |
{ |
81 |
string line; |
82 |
getline(text, line); |
83 |
|
84 |
if (line == "MapName=") |
85 |
{ |
86 |
fout << "MapName=" << maps[0] << (text.good() ? (linux_ ? "\r\n" : |
87 |
"\n") : ""); |
88 |
} |
89 |
else if (line == "MapName00=") |
90 |
{ |
91 |
for (unsigned index = 0; index < maps.size(); index++) |
92 |
{ |
93 |
char* number = new char[3]; |
94 |
sprintf(number, "%02u", index); |
95 |
|
96 |
fout << "MapName" << number << "=" << maps[index] << (index + 1 |
97 |
< maps.size() || text.good() ? (linux_ ? "\r\n" : "\n") : |
98 |
""); |
99 |
|
100 |
delete [] number; |
101 |
} |
102 |
} |
103 |
else |
104 |
{ |
105 |
fout << line << (text.good() ? (linux_ ? "\r\n" : "\n") : ""); |
106 |
} |
107 |
} |
108 |
while (text.good()); |
109 |
|
110 |
fout.close(); |
111 |
} |