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 = "$Header$";
25 static const char *IdHdr = "ID_CONFIGFILE";
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 FGConfigFile::FGConfigFile(string cfgFileName)
33 cfgfile.open(cfgFileName.c_str());
37 if (cfgfile.is_open()) GetNextConfigLine();
42 FGConfigFile::~FGConfigFile(void)
48 string FGConfigFile::GetNextConfigLine(void)
51 CurrentLine = GetLine();
52 if (CurrentLine.find("<COMMENT>") != CurrentLine.npos) CommentsOn = true;
53 if (CurrentLine.find("</COMMENT>") != CurrentLine.npos) {
57 } while (IsCommentLine());
58 if (CurrentLine.length() == 0) GetNextConfigLine();
64 string FGConfigFile::GetValue(string val)
66 unsigned int pos, p1, p2, ptest;
68 if (val == "") { // this call is to return the tag value
69 pos = CurrentLine.find("<");
70 if (pos != CurrentLine.npos) { // beginning brace found "<"
71 p1 = CurrentLine.find_first_not_of(" ",pos+1);
72 if (p1 != CurrentLine.npos) { // found first character of tag
73 p2 = CurrentLine.find_first_of(" >",p1+1);
74 if (p2 == CurrentLine.npos) p2 = p1+1;
75 return CurrentLine.substr(p1,p2-p1);
77 } else { // beginning brace "<" not found; this is a regular data line
78 pos = CurrentLine.find_first_not_of(" ");
79 if (pos != CurrentLine.npos) { // first character in line found
80 p2 = CurrentLine.find_first_of(" ",pos+1);
81 if (p2 != CurrentLine.npos) {
82 return CurrentLine.substr(pos,p2-pos);
84 return CurrentLine.substr(pos,CurrentLine.length()-pos);
88 } else { // return a value for a specific tag
89 pos = CurrentLine.find(val);
90 if (pos != CurrentLine.npos) {
91 pos = CurrentLine.find("=",pos);
92 if (pos != CurrentLine.npos) {
93 ptest = CurrentLine.find_first_not_of(" ",pos+1);
94 if (ptest != CurrentLine.npos) {
96 if (CurrentLine[ptest] == '"') { // quoted
97 p2 = CurrentLine.find_first_of("\"",p1);
98 } else { // not quoted
99 p2 = CurrentLine.find_first_of(" ",p1);
101 if (p2 != CurrentLine.npos) {
102 return CurrentLine.substr(p1,p2-p1);
105 } else { // "=" not found
106 pos = CurrentLine.find(val);
107 pos = CurrentLine.find_first_of(" ",pos+1);
108 ptest = CurrentLine.find_first_not_of(" ",pos+1);
109 if (ptest != CurrentLine.npos) {
110 if (CurrentLine[ptest] == '"') { // quoted
112 p2 = CurrentLine.find_first_of("\"",p1);
113 } else { // not quoted
115 p2 = CurrentLine.find_first_of(" ",p1);
117 if (p2 != CurrentLine.npos) {
118 return CurrentLine.substr(p1,p2-p1);
120 p2 = CurrentLine.length();
121 return CurrentLine.substr(p1,p2-p1);
132 string FGConfigFile::GetValue(void)
138 bool FGConfigFile::IsCommentLine(void)
140 if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
141 if (CommentsOn) return true;
147 string FGConfigFile::GetLine(void)
152 while ((test = cfgfile.get()) != EOF) {
154 scratch += (char)test;
156 if ((test = cfgfile.get()) != EOF) {
157 if (test >= 0x20) cfgfile.unget();
162 if (cfgfile.eof()) return string("EOF");
166 FGConfigFile& FGConfigFile::operator>>(double& val)
168 unsigned int pos, end;
170 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
171 if (pos == CurrentLine.npos) pos = CurrentLine.length();
172 end = CurrentLine.find_first_of(", ",pos+1);
173 if (end == CurrentLine.npos) end = CurrentLine.length();
174 string str = CurrentLine.substr(pos, end - pos);
175 val = strtod(str.c_str(),NULL);
176 CurrentIndex = end+1;
177 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
181 FGConfigFile& FGConfigFile::operator>>(float& 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 FGConfigFile& FGConfigFile::operator>>(int& val)
198 unsigned int pos, end;
200 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
201 if (pos == CurrentLine.npos) pos = CurrentLine.length();
202 end = CurrentLine.find_first_of(", ",pos+1);
203 if (end == CurrentLine.npos) end = CurrentLine.length();
204 string str = CurrentLine.substr(pos, end - pos);
205 val = atoi(str.c_str());
206 CurrentIndex = end+1;
207 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
211 FGConfigFile& FGConfigFile::operator>>(eParam& val)
213 unsigned int pos, end;
215 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
216 if (pos == CurrentLine.npos) pos = CurrentLine.length();
217 end = CurrentLine.find_first_of(", ",pos+1);
218 if (end == CurrentLine.npos) end = CurrentLine.length();
219 string str = CurrentLine.substr(pos, end - pos);
220 val = (eParam)atoi(str.c_str());
221 CurrentIndex = end+1;
222 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
226 FGConfigFile& FGConfigFile::operator>>(string& str)
228 unsigned int pos, end;
230 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
231 if (pos == CurrentLine.npos) pos = CurrentLine.length();
232 end = CurrentLine.find_first_of(", ",pos+1);
233 if (end == CurrentLine.npos) end = CurrentLine.length();
234 str = CurrentLine.substr(pos, end - pos);
235 CurrentIndex = end+1;
236 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
241 void FGConfigFile::ResetLineIndexToZero(void)