-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSerializer.cpp
executable file
·214 lines (212 loc) · 6.05 KB
/
Serializer.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
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
#include "Serializer.h"
#define PRINT_SERIAL
//================================================================================================//
/**************************************************
** Serializer: Will read/write config variables **
***************************************************/
//================================================================================================//
Serializer gSerializer;
//================================================================================================//
/********************************************************
** Comments are only written if the file did not exist **
*********************************************************/
//================================================================================================//
void Serializer::PutComment(string file, string comment)
{
if(SearchFileGenerate(file))
{
ofstream f;
f.open(file.c_str(),ios::app);
f << comment << endl;
f.close();
}
}
//================================================================================================//
/************************************************************************************
** Read variable will search the file for variable and load it into the parsed var **
** If the file did not exist, it will be written instead (auto-generation) **
*************************************************************************************/
//================================================================================================//
void Serializer::ReadVariable(string file, string varname, int& value)
{
if(SearchFileGenerate(file))//need to write to the cfg
{
ofstream f;
f.open(file.c_str(),ios::app);
f << varname << " = " << value << endl;
f.close();
}
else//need to read from the config
{
ifstream f;
f.open(file.c_str());
string s;
while(!f.eof())
{
getline(f,s);
string tok;
string tok2;
size_t p = s.find('=');
if(p!=string::npos) {
tok = s.substr(0, p);
tok2 = s.substr(p+1, string::npos);
//trim tok (there must be better way to do that...)
while(tok.size() && tok[0]==' ')
tok.erase(0, 1);
if(tok.find(' ')!=string::npos)
tok.erase(tok.find(' '), string::npos);
if(tok.find('\t')!=string::npos)
tok.erase(tok.find('\t'), string::npos);
}
// stringstream(s) >> tok;
if(tok == varname)
{
// stringstream(s) >> tok >> tok2 >> value;
while(tok2.size() && tok2[0]==' ')
tok2.erase(0, 1);
if(tok2.find(' ')!=string::npos)
tok2.erase(tok2.find(' '), string::npos);
if(tok2.find('\t')!=string::npos)
tok2.erase(tok2.find('\t'), string::npos);
int tmp;
if(sscanf(tok2.c_str(), "%d", &tmp)==1)
value = tmp;
#ifdef PRINT_SERIAL
printf("%s read from file %s\n",tok.c_str(),file.c_str());
#endif
break;
}
#ifdef PRINT_SERIAL
if(f.eof())
{
printf("Failed to read %s from file %s\n",varname.c_str(),file.c_str());
}
#endif
}
f.close();
}
}
void Serializer::ReadVariable(string file, string varname, float& value)
{
if(SearchFileGenerate(file))
{
ofstream f;
f.open(file.c_str(),ios::app);
f << varname << " = " << value << endl;
f.close();
}
else//need to read from the config
{
ifstream f;
f.open(file.c_str());
string s;
while(!f.eof())
{
getline(f,s);
string tok;
string tok2;
size_t p = s.find('=');
if(p!=string::npos) {
tok = s.substr(0, p);
tok2 = s.substr(p+1, string::npos);
//trim tok
while(tok.size() && tok[0]==' ')
tok.erase(0, 1);
if(tok.find(' ')!=string::npos)
tok.erase(tok.find(' '), string::npos);
if(tok.find('\t')!=string::npos)
tok.erase(tok.find('\t'), string::npos);
}
if(tok == varname)
{
// stringstream(s) >> tok >> tok2 >> value;
while(tok2.size() && tok2[0]==' ')
tok2.erase(0, 1);
if(tok2.find(' ')!=string::npos)
tok2.erase(tok2.find(' '), string::npos);
if(tok2.find('\t')!=string::npos)
tok2.erase(tok2.find('\t'), string::npos);
float tmp;
if(sscanf(tok2.c_str(), "%f", &tmp)==1)
value = tmp;
#ifdef PRINT_SERIAL
printf("%s read from file %s\n",tok.c_str(),file.c_str());
#endif
break;
}
#ifdef PRINT_SERIAL
if(f.eof())
{
printf("Failed to read %s from file %s\n",varname.c_str(),file.c_str());
}
#endif
}
f.close();
}
}
void Serializer::ReadVariable(string file, string varname, string& value)
{
if(SearchFileGenerate(file))
{
ofstream f;
f.open(file.c_str(),ios::app);
f << varname << " = " << value << endl;
f.close();
}
else//need to read from the config
{
ifstream f;
f.open(file.c_str());
string s;
value.clear();
while(!f.eof())
{
getline(f,s);
string tok;
// stringstream(s) >> tok;
if(tok == varname)
{
string tok2;
// stringstream(s) >> tok >> tok2 >> value;
#ifdef PRINT_SERIAL
printf("%s read from file %s\n",tok.c_str(),file.c_str());
#endif
break;
}
#ifdef PRINT_SERIAL
if(f.eof())
{
printf("Failed to read %s from file %s\n",varname.c_str(),file.c_str());
}
#endif
}
f.close();
}
}
//================================================================================================//
/************************************************************************
** Search through the gen file list, see if this file needs generating **
*************************************************************************/
//================================================================================================//
bool Serializer::SearchFileGenerate(string file)
{
ifstream f;
f.open(file.c_str());
if(!f.is_open())//if the file doesnt exit, then it really need to be generated
{
m_GenFiles.push_back(file);
#ifdef PRINT_SERIAL
printf("file %s will be generated...\n",file.c_str());
#endif
return true;
}
f.close();
//ok it does exist allredy, but we might still need to continue generation
vector<string>::iterator i;
for(i=m_GenFiles.begin(); i<m_GenFiles.end(); i++)
{
if(file == (*i))
return true;//uh oh, need to write out
}
return false;//safe to read in
}