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();
51 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 FGConfigFile::~FGConfigFile()
59 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 string FGConfigFile::GetNextConfigLine(void)
63 int comment_starts_at;
67 bool start_comment, end_comment;
68 string CommentStringTemp;
71 CurrentLine = GetLine();
72 line_length = CurrentLine.length();
73 comment_starts_at = CurrentLine.find("<!--");
75 if (comment_starts_at >= 0) start_comment = true;
76 else start_comment = false;
78 comment_ends_at = CurrentLine.find("-->");
80 if (comment_ends_at >= 0) end_comment = true;
81 else end_comment = false;
83 if (!start_comment && !end_comment) { // command comment
84 if (CommentsOn) CommentStringTemp = CurrentLine;
85 CommentString += CommentStringTemp + "\r\n";
86 } else if (start_comment && comment_ends_at > comment_starts_at) { // <!-- ... -->
88 comment_length = comment_ends_at + 2 - comment_starts_at + 1;
89 LineComment = CurrentLine.substr(comment_starts_at+4, comment_length-4-3);
90 CurrentLine.erase(comment_starts_at, comment_length);
91 } else if ( start_comment && !end_comment) { // <!-- ...
93 comment_length = line_length - comment_starts_at;
94 CommentStringTemp = CurrentLine.substr(comment_starts_at+4, comment_length-4);
95 CommentString = CommentStringTemp + "\r\n";
96 CurrentLine.erase(comment_starts_at, comment_length);
97 } else if (!start_comment && end_comment) { // ... -->
99 comment_length = comment_ends_at + 2 + 1;
100 CommentStringTemp = CurrentLine.substr(0, comment_length-4);
101 CommentString += CommentStringTemp + "\r\n";
102 CurrentLine.erase(0, comment_length);
103 } else if (start_comment && comment_ends_at < comment_starts_at) { // --> command <!--
104 cerr << "Old comment ends and new one starts - bad JSBSim config file form." << endl;
106 comment_length = comment_ends_at + 2 + 1;
107 CommentStringTemp = CurrentLine.substr(0, comment_length-4);
108 CommentString += CommentStringTemp + "\r\n";
109 CurrentLine.erase(0, comment_length);
112 } while (CommentsOn);
114 if (CurrentLine.length() == 0) GetNextConfigLine();
119 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 string FGConfigFile::GetValue(string val)
123 unsigned int pos, p1, p2, ptest;
125 if (val == "") { // this call is to return the tag value
126 pos = CurrentLine.find("<");
127 if (pos != CurrentLine.npos) { // beginning brace found "<"
128 p1 = CurrentLine.find_first_not_of(" ",pos+1);
129 if (p1 != CurrentLine.npos) { // found first character of tag
130 p2 = CurrentLine.find_first_of(" >",p1+1);
131 if (p2 == CurrentLine.npos) p2 = p1+1;
132 return CurrentLine.substr(p1,p2-p1);
134 } else { // beginning brace "<" not found; this is a regular data line
135 pos = CurrentLine.find_first_not_of(" ");
136 if (pos != CurrentLine.npos) { // first character in line found
137 p2 = CurrentLine.find_first_of(" ",pos+1);
138 if (p2 != CurrentLine.npos) {
139 return CurrentLine.substr(pos,p2-pos);
141 return CurrentLine.substr(pos,CurrentLine.length()-pos);
145 } else { // return a value for a specific tag
146 pos = CurrentLine.find(val);
147 if (pos != CurrentLine.npos) {
148 pos = CurrentLine.find("=",pos);
149 if (pos != CurrentLine.npos) {
150 ptest = CurrentLine.find_first_not_of(" ",pos+1);
151 if (ptest != CurrentLine.npos) {
153 if (CurrentLine[ptest] == '"') { // quoted
154 p2 = CurrentLine.find_first_of("\"",p1);
155 } else { // not quoted
156 p2 = CurrentLine.find_first_of(" ",p1);
158 if (p2 != CurrentLine.npos) {
159 return CurrentLine.substr(p1,p2-p1);
162 } else { // "=" not found
163 pos = CurrentLine.find(val);
164 pos = CurrentLine.find_first_of(" ",pos+1);
165 ptest = CurrentLine.find_first_not_of(" ",pos+1);
166 if (ptest != CurrentLine.npos) {
167 if (CurrentLine[ptest] == '"') { // quoted
169 p2 = CurrentLine.find_first_of("\"",p1);
170 } else { // not quoted
172 p2 = CurrentLine.find_first_of(" ",p1);
174 if (p2 != CurrentLine.npos) {
175 return CurrentLine.substr(p1,p2-p1);
177 p2 = CurrentLine.length();
178 return CurrentLine.substr(p1,p2-p1);
188 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 string FGConfigFile::GetValue(void)
195 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 string FGConfigFile::GetLine(void)
202 while ((test = cfgfile.get()) != EOF) {
203 if (test >= 0x20 || test == 0x09) {
205 scratch += (char)0x20;
207 scratch += (char)test;
210 if ((test = cfgfile.get()) != EOF) { // get *next* character
211 #if defined ( sgi ) && !defined( __GNUC__ )
212 if (test >= 0x20 || test == 0x09) cfgfile.putback(test);
214 if (test >= 0x20 || test == 0x09) cfgfile.unget();
220 if (cfgfile.eof() && scratch.empty()) return string("EOF");
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 FGConfigFile& FGConfigFile::operator>>(double& val)
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 string str = CurrentLine.substr(pos, end - pos);
235 val = strtod(str.c_str(),NULL);
236 CurrentIndex = end+1;
241 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 FGConfigFile& FGConfigFile::operator>>(int& val)
250 unsigned int pos, end;
252 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
253 if (pos == CurrentLine.npos) pos = CurrentLine.length();
254 end = CurrentLine.find_first_of(", ",pos+1);
255 if (end == CurrentLine.npos) end = CurrentLine.length();
256 string str = CurrentLine.substr(pos, end - pos);
257 val = atoi(str.c_str());
258 CurrentIndex = end+1;
263 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 FGConfigFile& FGConfigFile::operator>>(eParam& val)
272 unsigned int pos, end;
274 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
275 if (pos == CurrentLine.npos) pos = CurrentLine.length();
276 end = CurrentLine.find_first_of(", ",pos+1);
277 if (end == CurrentLine.npos) end = CurrentLine.length();
278 string str = CurrentLine.substr(pos, end - pos);
279 val = (eParam)atoi(str.c_str());
280 CurrentIndex = end+1;
285 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 FGConfigFile& FGConfigFile::operator>>(string& str)
294 unsigned int pos, end;
296 pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
297 if (pos == CurrentLine.npos) pos = CurrentLine.length();
298 end = CurrentLine.find_first_of(", ",pos+1);
299 if (end == CurrentLine.npos) end = CurrentLine.length();
300 str = CurrentLine.substr(pos, end - pos);
301 CurrentIndex = end+1;
306 if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
311 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 void FGConfigFile::ResetLineIndexToZero(void)
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 // The bitmasked value choices are as follows:
320 // unset: In this case (the default) JSBSim would only print
321 // out the normally expected messages, essentially echoing
322 // the config files as they are read. If the environment
323 // variable is not set, debug_lvl is set to 1 internally
324 // 0: This requests JSBSim not to output any messages
326 // 1: This value explicity requests the normal JSBSim
328 // 2: This value asks for a message to be printed out when
329 // a class is instantiated
330 // 4: When this value is set, a message is displayed when a
331 // FGModel object executes its Run() method
332 // 8: When this value is set, various runtime state variables
333 // are printed out periodically
334 // 16: When set various parameters are sanity checked and
335 // a message is printed out when they go out of bounds
337 void FGConfigFile::Debug(int from)
339 if (debug_lvl <= 0) return;
341 if (debug_lvl & 1) { // Standard console startup message output
343 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
344 if (from == 0) cout << "Instantiated: FGConfigFile" << endl;
345 if (from == 1) cout << "Destroyed: FGConfigFile" << endl;
347 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
349 if (debug_lvl & 8 ) { // Runtime state variables
351 if (debug_lvl & 16) { // Sanity checking