1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
3 Header: FGConfigFile.h
\r
5 Date started: 03/29/00
\r
6 Purpose: Config file read-in class
\r
7 Called by: FGAircraft
\r
9 FUNCTIONAL DESCRIPTION
\r
10 --------------------------------------------------------------------------------
\r
13 --------------------------------------------------------------------------------
\r
14 03/16/2000 JSB Created
\r
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
20 #include "FGConfigFile.h"
\r
24 static const char *IdSrc = "$Id$";
\r
25 static const char *IdHdr = ID_CONFIGFILE;
\r
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
28 CLASS IMPLEMENTATION
\r
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
31 FGConfigFile::FGConfigFile(string cfgFileName)
\r
33 #if defined ( sgi ) && !defined( __GNUC__ )
\r
34 cfgfile.open(cfgFileName.c_str(), ios::in );
\r
36 cfgfile.open(cfgFileName.c_str(), ios::in | ios::binary );
\r
41 #if defined ( sgi ) && !defined( __GNUC__ )
\r
42 if (!cfgfile.fail() && !cfgfile.eof()) GetNextConfigLine();
\r
44 if (cfgfile.is_open()) GetNextConfigLine();
\r
46 else Opened = false;
\r
48 if (debug_lvl & 2) cout << "Instantiated: FGConfigFile" << endl;
\r
51 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
53 FGConfigFile::~FGConfigFile()
\r
56 if (debug_lvl & 2) cout << "Destroyed: FGConfigFile" << endl;
\r
59 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
61 string FGConfigFile::GetNextConfigLine(void)
\r
63 int deblank, not_found = string::npos;
\r
64 int comment_starts_at;
\r
65 int comment_ends_at;
\r
68 bool start_comment, end_comment;
\r
69 string CommentStringTemp;
\r
72 CurrentLine = GetLine();
\r
73 line_length = CurrentLine.length();
\r
74 comment_starts_at = CurrentLine.find("<!--");
\r
76 if (comment_starts_at >= 0) start_comment = true;
\r
77 else start_comment = false;
\r
79 comment_ends_at = CurrentLine.find("-->");
\r
81 if (comment_ends_at >= 0) end_comment = true;
\r
82 else end_comment = false;
\r
84 if (!start_comment && !end_comment) { // command comment
\r
85 if (CommentsOn) CommentStringTemp = CurrentLine;
\r
86 CommentString += CommentStringTemp + "\r\n";
\r
87 } else if (start_comment && comment_ends_at > comment_starts_at) { // <!-- ... -->
\r
89 comment_length = comment_ends_at + 2 - comment_starts_at + 1;
\r
90 LineComment = CurrentLine.substr(comment_starts_at+4, comment_length-4-3);
\r
91 CurrentLine.erase(comment_starts_at, comment_length);
\r
92 } else if ( start_comment && !end_comment) { // <!-- ...
\r
94 comment_length = line_length - comment_starts_at;
\r
95 CommentStringTemp = CurrentLine.substr(comment_starts_at+4, comment_length-4);
\r
96 CommentString = CommentStringTemp + "\r\n";
\r
97 CurrentLine.erase(comment_starts_at, comment_length);
\r
98 } else if (!start_comment && end_comment) { // ... -->
\r
100 comment_length = comment_ends_at + 2 + 1;
\r
101 CommentStringTemp = CurrentLine.substr(0, comment_length-4);
\r
102 CommentString += CommentStringTemp + "\r\n";
\r
103 CurrentLine.erase(0, comment_length);
\r
104 } else if (start_comment && comment_ends_at < comment_starts_at) { // --> command <!--
\r
105 cerr << "Old comment ends and new one starts - bad JSBSim config file form." << endl;
\r
106 CommentsOn = false;
\r
107 comment_length = comment_ends_at + 2 + 1;
\r
108 CommentStringTemp = CurrentLine.substr(0, comment_length-4);
\r
109 CommentString += CommentStringTemp + "\r\n";
\r
110 CurrentLine.erase(0, comment_length);
\r
113 } while (CommentsOn);
\r
115 if (CurrentLine.length() == 0) GetNextConfigLine();
\r
117 return CurrentLine;
\r
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
122 string FGConfigFile::GetValue(string val)
\r
124 unsigned int pos, p1, p2, ptest;
\r
126 if (val == "") { // this call is to return the tag value
\r
127 pos = CurrentLine.find("<");
\r
128 if (pos != CurrentLine.npos) { // beginning brace found "<"
\r
129 p1 = CurrentLine.find_first_not_of(" ",pos+1);
\r
130 if (p1 != CurrentLine.npos) { // found first character of tag
\r
131 p2 = CurrentLine.find_first_of(" >",p1+1);
\r
132 if (p2 == CurrentLine.npos) p2 = p1+1;
\r
133 return CurrentLine.substr(p1,p2-p1);
\r
135 } else { // beginning brace "<" not found; this is a regular data line
\r
136 pos = CurrentLine.find_first_not_of(" ");
\r
137 if (pos != CurrentLine.npos) { // first character in line found
\r
138 p2 = CurrentLine.find_first_of(" ",pos+1);
\r
139 if (p2 != CurrentLine.npos) {
\r
140 return CurrentLine.substr(pos,p2-pos);
\r
142 return CurrentLine.substr(pos,CurrentLine.length()-pos);
\r
146 } else { // return a value for a specific tag
\r
147 pos = CurrentLine.find(val);
\r
148 if (pos != CurrentLine.npos) {
\r
149 pos = CurrentLine.find("=",pos);
\r
150 if (pos != CurrentLine.npos) {
\r
151 ptest = CurrentLine.find_first_not_of(" ",pos+1);
\r
152 if (ptest != CurrentLine.npos) {
\r
154 if (CurrentLine[ptest] == '"') { // quoted
\r
155 p2 = CurrentLine.find_first_of("\"",p1);
\r
156 } else { // not quoted
\r
157 p2 = CurrentLine.find_first_of(" ",p1);
\r
159 if (p2 != CurrentLine.npos) {
\r
160 return CurrentLine.substr(p1,p2-p1);
\r
163 } else { // "=" not found
\r
164 pos = CurrentLine.find(val);
\r
165 pos = CurrentLine.find_first_of(" ",pos+1);
\r
166 ptest = CurrentLine.find_first_not_of(" ",pos+1);
\r
167 if (ptest != CurrentLine.npos) {
\r
168 if (CurrentLine[ptest] == '"') { // quoted
\r
170 p2 = CurrentLine.find_first_of("\"",p1);
\r
171 } else { // not quoted
\r
173 p2 = CurrentLine.find_first_of(" ",p1);
\r
175 if (p2 != CurrentLine.npos) {
\r
176 return CurrentLine.substr(p1,p2-p1);
\r
178 p2 = CurrentLine.length();
\r
179 return CurrentLine.substr(p1,p2-p1);
\r
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
191 string FGConfigFile::GetValue(void)
\r
193 return GetValue("");
\r
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
198 string FGConfigFile::GetLine(void)
\r
200 string scratch = "";
\r
203 while ((test = cfgfile.get()) != EOF) {
\r
204 if (test >= 0x20 || test == 0x09) {
\r
205 if (test == 0x09) {
\r
206 scratch += (char)0x20;
\r
208 scratch += (char)test;
\r
211 if ((test = cfgfile.get()) != EOF) { // get *next* character
\r
212 #if defined ( sgi ) && !defined( __GNUC__ )
\r
213 if (test >= 0x20 || test == 0x09) cfgfile.putback(test);
\r
215 if (test >= 0x20 || test == 0x09) cfgfile.unget();
\r
221 if (cfgfile.eof()) return string("EOF");
\r
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
227 FGConfigFile& FGConfigFile::operator>>(double& val)
\r
229 unsigned int pos, end;
\r
231 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
\r
232 if (pos == CurrentLine.npos) pos = CurrentLine.length();
\r
233 end = CurrentLine.find_first_of(", ",pos+1);
\r
234 if (end == CurrentLine.npos) end = CurrentLine.length();
\r
235 string str = CurrentLine.substr(pos, end - pos);
\r
236 val = strtod(str.c_str(),NULL);
\r
237 CurrentIndex = end+1;
\r
238 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
\r
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
244 FGConfigFile& FGConfigFile::operator>>(double& val)
\r
246 unsigned int pos, end;
\r
248 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
\r
249 if (pos == CurrentLine.npos) pos = CurrentLine.length();
\r
250 end = CurrentLine.find_first_of(", ",pos+1);
\r
251 if (end == CurrentLine.npos) end = CurrentLine.length();
\r
252 string str = CurrentLine.substr(pos, end - pos);
\r
253 val = strtod(str.c_str(),NULL);
\r
254 CurrentIndex = end+1;
\r
255 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
\r
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
261 FGConfigFile& FGConfigFile::operator>>(int& val)
\r
263 unsigned int pos, end;
\r
265 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
\r
266 if (pos == CurrentLine.npos) pos = CurrentLine.length();
\r
267 end = CurrentLine.find_first_of(", ",pos+1);
\r
268 if (end == CurrentLine.npos) end = CurrentLine.length();
\r
269 string str = CurrentLine.substr(pos, end - pos);
\r
270 val = atoi(str.c_str());
\r
271 CurrentIndex = end+1;
\r
272 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
\r
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
278 FGConfigFile& FGConfigFile::operator>>(eParam& val)
\r
280 unsigned int pos, end;
\r
282 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
\r
283 if (pos == CurrentLine.npos) pos = CurrentLine.length();
\r
284 end = CurrentLine.find_first_of(", ",pos+1);
\r
285 if (end == CurrentLine.npos) end = CurrentLine.length();
\r
286 string str = CurrentLine.substr(pos, end - pos);
\r
287 val = (eParam)atoi(str.c_str());
\r
288 CurrentIndex = end+1;
\r
289 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
\r
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
295 FGConfigFile& FGConfigFile::operator>>(string& str)
\r
297 unsigned int pos, end;
\r
299 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
\r
300 if (pos == CurrentLine.npos) pos = CurrentLine.length();
\r
301 end = CurrentLine.find_first_of(", ",pos+1);
\r
302 if (end == CurrentLine.npos) end = CurrentLine.length();
\r
303 str = CurrentLine.substr(pos, end - pos);
\r
304 CurrentIndex = end+1;
\r
305 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
\r
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
311 void FGConfigFile::ResetLineIndexToZero(void)
\r
316 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
318 void FGConfigFile::Debug(void)
\r
320 //TODO: Add your source code here
\r