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 extern short debug_lvl;
29 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 FGConfigFile::FGConfigFile(string cfgFileName)
35 cfgfile.open(cfgFileName.c_str(), ios::in | ios::binary );
39 if (cfgfile.is_open()) GetNextConfigLine();
42 if (debug_lvl & 2) cout << "Instantiated: FGConfigFile" << endl;
45 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 FGConfigFile::~FGConfigFile()
50 if (debug_lvl & 2) cout << "Destroyed: FGConfigFile" << endl;
53 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 string FGConfigFile::GetNextConfigLine(void)
58 CurrentLine = GetLine();
59 if ((CurrentLine.find("<COMMENT>") != CurrentLine.npos) ||
60 (CurrentLine.find("<!--") != CurrentLine.npos)) CommentsOn = true;
61 if ((CurrentLine.find("</COMMENT>") != CurrentLine.npos) ||
62 (CurrentLine.find("-->") != CurrentLine.npos)) {
66 } while (IsCommentLine());
67 if (CurrentLine.length() == 0) GetNextConfigLine();
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 string FGConfigFile::GetValue(string val)
76 unsigned int pos, p1, p2, ptest;
78 if (val == "") { // this call is to return the tag value
79 pos = CurrentLine.find("<");
80 if (pos != CurrentLine.npos) { // beginning brace found "<"
81 p1 = CurrentLine.find_first_not_of(" ",pos+1);
82 if (p1 != CurrentLine.npos) { // found first character of tag
83 p2 = CurrentLine.find_first_of(" >",p1+1);
84 if (p2 == CurrentLine.npos) p2 = p1+1;
85 return CurrentLine.substr(p1,p2-p1);
87 } else { // beginning brace "<" not found; this is a regular data line
88 pos = CurrentLine.find_first_not_of(" ");
89 if (pos != CurrentLine.npos) { // first character in line found
90 p2 = CurrentLine.find_first_of(" ",pos+1);
91 if (p2 != CurrentLine.npos) {
92 return CurrentLine.substr(pos,p2-pos);
94 return CurrentLine.substr(pos,CurrentLine.length()-pos);
98 } else { // return a value for a specific tag
99 pos = CurrentLine.find(val);
100 if (pos != CurrentLine.npos) {
101 pos = CurrentLine.find("=",pos);
102 if (pos != CurrentLine.npos) {
103 ptest = CurrentLine.find_first_not_of(" ",pos+1);
104 if (ptest != CurrentLine.npos) {
106 if (CurrentLine[ptest] == '"') { // quoted
107 p2 = CurrentLine.find_first_of("\"",p1);
108 } else { // not quoted
109 p2 = CurrentLine.find_first_of(" ",p1);
111 if (p2 != CurrentLine.npos) {
112 return CurrentLine.substr(p1,p2-p1);
115 } else { // "=" not found
116 pos = CurrentLine.find(val);
117 pos = CurrentLine.find_first_of(" ",pos+1);
118 ptest = CurrentLine.find_first_not_of(" ",pos+1);
119 if (ptest != CurrentLine.npos) {
120 if (CurrentLine[ptest] == '"') { // quoted
122 p2 = CurrentLine.find_first_of("\"",p1);
123 } else { // not quoted
125 p2 = CurrentLine.find_first_of(" ",p1);
127 if (p2 != CurrentLine.npos) {
128 return CurrentLine.substr(p1,p2-p1);
130 p2 = CurrentLine.length();
131 return CurrentLine.substr(p1,p2-p1);
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 string FGConfigFile::GetValue(void)
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 bool FGConfigFile::IsCommentLine(void)
152 if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
153 if (CommentsOn) return true;
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 string FGConfigFile::GetLine(void)
165 while ((test = cfgfile.get()) != EOF) {
167 scratch += (char)test;
169 if ((test = cfgfile.get()) != EOF) {
170 if (test >= 0x20) cfgfile.unget();
175 if (cfgfile.eof()) return string("EOF");
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 FGConfigFile& FGConfigFile::operator>>(double& val)
183 unsigned int pos, end;
185 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
186 if (pos == CurrentLine.npos) pos = CurrentLine.length();
187 end = CurrentLine.find_first_of(", ",pos+1);
188 if (end == CurrentLine.npos) end = CurrentLine.length();
189 string str = CurrentLine.substr(pos, end - pos);
190 val = strtod(str.c_str(),NULL);
191 CurrentIndex = end+1;
192 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 FGConfigFile& FGConfigFile::operator>>(float& val)
200 unsigned int pos, end;
202 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
203 if (pos == CurrentLine.npos) pos = CurrentLine.length();
204 end = CurrentLine.find_first_of(", ",pos+1);
205 if (end == CurrentLine.npos) end = CurrentLine.length();
206 string str = CurrentLine.substr(pos, end - pos);
207 val = strtod(str.c_str(),NULL);
208 CurrentIndex = end+1;
209 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
213 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 FGConfigFile& FGConfigFile::operator>>(int& val)
217 unsigned int pos, end;
219 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
220 if (pos == CurrentLine.npos) pos = CurrentLine.length();
221 end = CurrentLine.find_first_of(", ",pos+1);
222 if (end == CurrentLine.npos) end = CurrentLine.length();
223 string str = CurrentLine.substr(pos, end - pos);
224 val = atoi(str.c_str());
225 CurrentIndex = end+1;
226 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 FGConfigFile& FGConfigFile::operator>>(eParam& val)
234 unsigned int pos, end;
236 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
237 if (pos == CurrentLine.npos) pos = CurrentLine.length();
238 end = CurrentLine.find_first_of(", ",pos+1);
239 if (end == CurrentLine.npos) end = CurrentLine.length();
240 string str = CurrentLine.substr(pos, end - pos);
241 val = (eParam)atoi(str.c_str());
242 CurrentIndex = end+1;
243 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 FGConfigFile& FGConfigFile::operator>>(string& str)
251 unsigned int pos, end;
253 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
254 if (pos == CurrentLine.npos) pos = CurrentLine.length();
255 end = CurrentLine.find_first_of(", ",pos+1);
256 if (end == CurrentLine.npos) end = CurrentLine.length();
257 str = CurrentLine.substr(pos, end - pos);
258 CurrentIndex = end+1;
259 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 void FGConfigFile::ResetLineIndexToZero(void)
270 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 void FGConfigFile::Debug(void)
274 //TODO: Add your source code here