1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Config file read-in class
10 --------------------------------------------------------------------------------
13 --------------------------------------------------------------------------------
14 03/16/2000 JSB Created
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
20 #include "FGConfigFile.h"
24 static const char *IdSrc = "$Id$";
25 static const char *IdHdr = ID_CONFIGFILE;
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 FGConfigFile::FGConfigFile(string cfgFileName)
33 #if defined ( sgi ) && !defined( __GNUC__ )
34 cfgfile.open(cfgFileName.c_str(), ios::in );
36 cfgfile.open(cfgFileName.c_str(), ios::in | ios::binary );
41 #if defined ( sgi ) && !defined( __GNUC__ )
42 if (!cfgfile.fail() && !cfgfile.eof()) GetNextConfigLine();
44 if (cfgfile.is_open()) GetNextConfigLine();
48 if (debug_lvl & 2) cout << "Instantiated: FGConfigFile" << endl;
51 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 FGConfigFile::~FGConfigFile()
56 if (debug_lvl & 2) cout << "Destroyed: FGConfigFile" << endl;
59 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 string FGConfigFile::GetNextConfigLine(void)
66 CurrentLine = GetLine();
67 if (CurrentLine.find("<!--") != CurrentLine.npos) {
70 if (CurrentLine.find("<!--") != CurrentLine.npos)
71 CurrentLine.erase(CurrentLine.find("<!--"),4);
72 while((deblank = CurrentLine.find(" ")) != CurrentLine.npos) CurrentLine.erase(deblank,1);
73 if (CurrentLine.size() <= 2) CurrentLine = "";
76 if (CurrentLine.find("-->") != CurrentLine.npos) {
79 if (CurrentLine.find("-->") != CurrentLine.npos)
80 CurrentLine.erase(CurrentLine.find("-->"),4);
82 while((deblank = CurrentLine.find(" ")) != CurrentLine.npos) CurrentLine.erase(deblank,1);
83 if (CurrentLine.size() <= 2) CurrentLine = "";
85 CommentString += CurrentLine;
89 if (CommentsOn) CommentString += CurrentLine + "\r\n";
93 if (CurrentLine.length() == 0) GetNextConfigLine();
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 string FGConfigFile::GetCommentString(void)
102 return CommentString;
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 string FGConfigFile::GetValue(string val)
109 unsigned int pos, p1, p2, ptest;
111 if (val == "") { // this call is to return the tag value
112 pos = CurrentLine.find("<");
113 if (pos != CurrentLine.npos) { // beginning brace found "<"
114 p1 = CurrentLine.find_first_not_of(" ",pos+1);
115 if (p1 != CurrentLine.npos) { // found first character of tag
116 p2 = CurrentLine.find_first_of(" >",p1+1);
117 if (p2 == CurrentLine.npos) p2 = p1+1;
118 return CurrentLine.substr(p1,p2-p1);
120 } else { // beginning brace "<" not found; this is a regular data line
121 pos = CurrentLine.find_first_not_of(" ");
122 if (pos != CurrentLine.npos) { // first character in line found
123 p2 = CurrentLine.find_first_of(" ",pos+1);
124 if (p2 != CurrentLine.npos) {
125 return CurrentLine.substr(pos,p2-pos);
127 return CurrentLine.substr(pos,CurrentLine.length()-pos);
131 } else { // return a value for a specific tag
132 pos = CurrentLine.find(val);
133 if (pos != CurrentLine.npos) {
134 pos = CurrentLine.find("=",pos);
135 if (pos != CurrentLine.npos) {
136 ptest = CurrentLine.find_first_not_of(" ",pos+1);
137 if (ptest != CurrentLine.npos) {
139 if (CurrentLine[ptest] == '"') { // quoted
140 p2 = CurrentLine.find_first_of("\"",p1);
141 } else { // not quoted
142 p2 = CurrentLine.find_first_of(" ",p1);
144 if (p2 != CurrentLine.npos) {
145 return CurrentLine.substr(p1,p2-p1);
148 } else { // "=" not found
149 pos = CurrentLine.find(val);
150 pos = CurrentLine.find_first_of(" ",pos+1);
151 ptest = CurrentLine.find_first_not_of(" ",pos+1);
152 if (ptest != CurrentLine.npos) {
153 if (CurrentLine[ptest] == '"') { // quoted
155 p2 = CurrentLine.find_first_of("\"",p1);
156 } else { // not quoted
158 p2 = CurrentLine.find_first_of(" ",p1);
160 if (p2 != CurrentLine.npos) {
161 return CurrentLine.substr(p1,p2-p1);
163 p2 = CurrentLine.length();
164 return CurrentLine.substr(p1,p2-p1);
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 string FGConfigFile::GetValue(void)
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 string FGConfigFile::GetLine(void)
188 while ((test = cfgfile.get()) != EOF) {
190 scratch += (char)test;
192 if ((test = cfgfile.get()) != EOF) {
193 #if defined ( sgi ) && !defined( __GNUC__ )
194 if (test >= 0x20) cfgfile.putback(test);
196 if (test >= 0x20) cfgfile.unget();
202 if (cfgfile.eof()) return string("EOF");
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 FGConfigFile& FGConfigFile::operator>>(double& val)
210 unsigned int pos, end;
212 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
213 if (pos == CurrentLine.npos) pos = CurrentLine.length();
214 end = CurrentLine.find_first_of(", ",pos+1);
215 if (end == CurrentLine.npos) end = CurrentLine.length();
216 string str = CurrentLine.substr(pos, end - pos);
217 val = strtod(str.c_str(),NULL);
218 CurrentIndex = end+1;
219 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 FGConfigFile& FGConfigFile::operator>>(float& val)
227 unsigned int pos, end;
229 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
230 if (pos == CurrentLine.npos) pos = CurrentLine.length();
231 end = CurrentLine.find_first_of(", ",pos+1);
232 if (end == CurrentLine.npos) end = CurrentLine.length();
233 string str = CurrentLine.substr(pos, end - pos);
234 val = strtod(str.c_str(),NULL);
235 CurrentIndex = end+1;
236 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 FGConfigFile& FGConfigFile::operator>>(int& val)
244 unsigned int pos, end;
246 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
247 if (pos == CurrentLine.npos) pos = CurrentLine.length();
248 end = CurrentLine.find_first_of(", ",pos+1);
249 if (end == CurrentLine.npos) end = CurrentLine.length();
250 string str = CurrentLine.substr(pos, end - pos);
251 val = atoi(str.c_str());
252 CurrentIndex = end+1;
253 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 FGConfigFile& FGConfigFile::operator>>(eParam& val)
261 unsigned int pos, end;
263 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
264 if (pos == CurrentLine.npos) pos = CurrentLine.length();
265 end = CurrentLine.find_first_of(", ",pos+1);
266 if (end == CurrentLine.npos) end = CurrentLine.length();
267 string str = CurrentLine.substr(pos, end - pos);
268 val = (eParam)atoi(str.c_str());
269 CurrentIndex = end+1;
270 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 FGConfigFile& FGConfigFile::operator>>(string& str)
278 unsigned int pos, end;
280 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
281 if (pos == CurrentLine.npos) pos = CurrentLine.length();
282 end = CurrentLine.find_first_of(", ",pos+1);
283 if (end == CurrentLine.npos) end = CurrentLine.length();
284 str = CurrentLine.substr(pos, end - pos);
285 CurrentIndex = end+1;
286 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 void FGConfigFile::ResetLineIndexToZero(void)
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 void FGConfigFile::Debug(void)
301 //TODO: Add your source code here