1 /*******************************************************************************
6 Purpose: Config file read-in class
10 --------------------------------------------------------------------------------
13 --------------------------------------------------------------------------------
14 03/16/2000 JSB Created
16 ********************************************************************************
18 *******************************************************************************/
20 #include "FGConfigFile.h"
24 /*******************************************************************************
25 ************************************ CODE **************************************
26 *******************************************************************************/
28 FGConfigFile::FGConfigFile(string cfgFileName)
30 cfgfile.open(cfgFileName.c_str());
37 FGConfigFile::~FGConfigFile(void)
43 string FGConfigFile::GetNextConfigLine(void)
46 CurrentLine = GetLine();
47 if (CurrentLine.find("<COMMENT>") != CurrentLine.npos) CommentsOn = true;
48 if (CurrentLine.find("</COMMENT>") != CurrentLine.npos) {
52 } while (IsCommentLine());
53 if (CurrentLine.length() == 0) GetNextConfigLine();
59 string FGConfigFile::GetValue(string val)
61 unsigned int pos, p1, p2, ptest;
63 if (val == "") { // this call is to return the tag value
64 pos = CurrentLine.find("<");
65 if (pos != CurrentLine.npos) { // beginning brace found "<"
66 p1 = CurrentLine.find_first_not_of(" ",pos+1);
67 if (p1 != CurrentLine.npos) { // found first character of tag
68 p2 = CurrentLine.find_first_of(" >",p1+1);
69 if (p2 == CurrentLine.npos) p2 = p1+1;
70 return CurrentLine.substr(p1,p2-p1);
72 } else { // beginning brace "<" not found; this is a regular data line
73 pos = CurrentLine.find_first_not_of(" ");
74 if (pos != CurrentLine.npos) { // first character in line found
75 p2 = CurrentLine.find_first_of(" ",pos+1);
76 if (p2 != CurrentLine.npos) {
77 return CurrentLine.substr(pos,p2-pos);
79 return CurrentLine.substr(pos,CurrentLine.length()-pos);
83 } else { // return a value for a specific tag
84 pos = CurrentLine.find(val);
85 if (pos != CurrentLine.npos) {
86 pos = CurrentLine.find("=",pos);
87 if (pos != CurrentLine.npos) {
88 ptest = CurrentLine.find_first_not_of(" ",pos+1);
89 if (ptest != CurrentLine.npos) {
91 if (CurrentLine[ptest] == '"') { // quoted
92 p2 = CurrentLine.find_first_of("\"",p1);
93 } else { // not quoted
94 p2 = CurrentLine.find_first_of(" ",p1);
96 if (p2 != CurrentLine.npos) {
97 return CurrentLine.substr(p1,p2-p1);
100 } else { // "=" not found
101 pos = CurrentLine.find(val);
102 pos = CurrentLine.find_first_of(" ",pos+1);
103 ptest = CurrentLine.find_first_not_of(" ",pos+1);
104 if (ptest != CurrentLine.npos) {
105 if (CurrentLine[ptest] == '"') { // quoted
107 p2 = CurrentLine.find_first_of("\"",p1);
108 } else { // not quoted
110 p2 = CurrentLine.find_first_of(" ",p1);
112 if (p2 != CurrentLine.npos) {
113 return CurrentLine.substr(p1,p2-p1);
115 p2 = CurrentLine.length();
116 return CurrentLine.substr(p1,p2-p1);
127 string FGConfigFile::GetValue(void)
133 bool FGConfigFile::IsCommentLine(void)
135 if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
136 if (CommentsOn) return true;
142 string FGConfigFile::GetLine(void)
147 while ((test = cfgfile.get()) != EOF) {
149 scratch += (char)test;
151 if ((test = cfgfile.get()) != EOF) {
152 if (test >= 0x20) cfgfile.unget();
157 if (cfgfile.eof()) return string("EOF");
161 FGConfigFile& FGConfigFile::operator>>(double& val)
163 unsigned int pos, end;
165 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
166 if (pos == CurrentLine.npos) pos = CurrentLine.length();
167 end = CurrentLine.find_first_of(", ",pos+1);
168 if (end == CurrentLine.npos) end = CurrentLine.length();
169 string str = CurrentLine.substr(pos, end - pos);
170 val = strtod(str.c_str(),NULL);
171 CurrentIndex = end+1;
173 if (CurrentIndex >= CurrentLine.length())
179 FGConfigFile& FGConfigFile::operator>>(float& val)
181 unsigned int pos, end;
183 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
184 if (pos == CurrentLine.npos) pos = CurrentLine.length();
185 end = CurrentLine.find_first_of(", ",pos+1);
186 if (end == CurrentLine.npos) end = CurrentLine.length();
187 string str = CurrentLine.substr(pos, end - pos);
188 val = strtod(str.c_str(),NULL);
189 CurrentIndex = end+1;
191 if (CurrentIndex >= CurrentLine.length())
197 FGConfigFile& FGConfigFile::operator>>(int& val)
199 unsigned int pos, end;
201 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
202 if (pos == CurrentLine.npos) pos = CurrentLine.length();
203 end = CurrentLine.find_first_of(", ",pos+1);
204 if (end == CurrentLine.npos) end = CurrentLine.length();
205 string str = CurrentLine.substr(pos, end - pos);
206 val = atoi(str.c_str());
207 CurrentIndex = end+1;
209 if (CurrentIndex >= CurrentLine.length())
215 FGConfigFile& FGConfigFile::operator>>(string& str)
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 str = CurrentLine.substr(pos, end - pos);
224 CurrentIndex = end+1;
226 if (CurrentIndex >= CurrentLine.length())
233 void FGConfigFile::ResetLineIndexToZero(void)