]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGConfigFile.cpp
42fbe6a32c458ed657b1c21113f1cd99af0d4a82
[flightgear.git] / src / FDM / JSBSim / FGConfigFile.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Header:       FGConfigFile.h
4  Author:       Jon Berndt
5  Date started: 03/29/00
6  Purpose:      Config file read-in class
7  Called by:    FGAircraft
8
9 FUNCTIONAL DESCRIPTION
10 --------------------------------------------------------------------------------
11
12 HISTORY
13 --------------------------------------------------------------------------------
14 03/16/2000 JSB  Created
15
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17 INCLUDES
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
19
20 #include "FGConfigFile.h"
21 #include <stdlib.h>
22 #include <math.h>
23
24 static const char *IdSrc = "$Id$";
25 static const char *IdHdr = ID_CONFIGFILE;
26
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 CLASS IMPLEMENTATION
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
30
31 FGConfigFile::FGConfigFile(string cfgFileName)
32 {
33 #if defined ( sgi ) && !defined( __GNUC__ )
34   cfgfile.open(cfgFileName.c_str(), ios::in );
35 #else
36   cfgfile.open(cfgFileName.c_str(), ios::in | ios::binary );
37 #endif
38   CommentsOn = false;
39   CurrentIndex = 0;
40   Opened = true;
41 #if defined ( sgi ) && !defined( __GNUC__ )
42    if (!cfgfile.fail() && !cfgfile.eof())  GetNextConfigLine();
43 #else
44   if (cfgfile.is_open()) GetNextConfigLine();
45 #endif
46   else Opened = false;
47
48   if (debug_lvl & 2) cout << "Instantiated: FGConfigFile" << endl;
49 }
50
51 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52
53 FGConfigFile::~FGConfigFile()
54 {
55   cfgfile.close();
56   if (debug_lvl & 2) cout << "Destroyed:    FGConfigFile" << endl;
57 }
58
59 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
61 string FGConfigFile::GetNextConfigLine(void)
62 {
63   int deblank;
64
65   do {
66     CurrentLine = GetLine();
67     if (CurrentLine.find("<!--") != CurrentLine.npos) {
68       CommentsOn = true;
69       CommentString = "";
70       if (CurrentLine.find("<!--") != CurrentLine.npos)
71         CurrentLine.erase(CurrentLine.find("<!--"),4);
72       while((deblank = CurrentLine.find(" ")) != CurrentLine.npos) CurrentLine.erase(deblank,1);
73       if (CurrentLine.size() <= 2) CurrentLine = "";
74     }
75
76     if (CurrentLine.find("-->") != CurrentLine.npos) {
77       CommentsOn = false;
78
79       if (CurrentLine.find("-->") != CurrentLine.npos)
80         CurrentLine.erase(CurrentLine.find("-->"),4);
81
82       while((deblank = CurrentLine.find(" ")) != CurrentLine.npos) CurrentLine.erase(deblank,1);
83       if (CurrentLine.size() <= 2) CurrentLine = "";
84
85       CommentString += CurrentLine;
86       GetNextConfigLine();
87     }
88
89     if (CommentsOn) CommentString += CurrentLine + "\r\n";
90
91   } while (CommentsOn);
92
93   if (CurrentLine.length() == 0) GetNextConfigLine();
94   CurrentIndex = 0;
95   return CurrentLine;
96 }
97
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
100 string FGConfigFile::GetCommentString(void)
101 {
102     return CommentString;
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 string FGConfigFile::GetValue(string val)
108 {
109   unsigned int pos, p1, p2, ptest;
110
111   if (val == "") {    // this call is to return the tag value
112     pos = CurrentLine.find("<");
113     if (pos != CurrentLine.npos) { // beginning brace found "<"
114       p1 = CurrentLine.find_first_not_of(" ",pos+1);
115       if (p1 != CurrentLine.npos) { // found first character of tag
116         p2 = CurrentLine.find_first_of(" >",p1+1);
117         if (p2 == CurrentLine.npos) p2 = p1+1;
118         return CurrentLine.substr(p1,p2-p1);
119       }
120     } else {   // beginning brace "<" not found; this is a regular data line
121       pos = CurrentLine.find_first_not_of(" ");
122       if (pos != CurrentLine.npos) {  // first character in line found
123         p2 = CurrentLine.find_first_of(" ",pos+1);
124         if (p2 != CurrentLine.npos) {
125           return CurrentLine.substr(pos,p2-pos);
126         } else {
127           return CurrentLine.substr(pos,CurrentLine.length()-pos);
128         }
129       }
130     }
131   } else { // return a value for a specific tag
132     pos = CurrentLine.find(val);
133     if (pos != CurrentLine.npos) {
134       pos = CurrentLine.find("=",pos);
135       if (pos != CurrentLine.npos) {
136         ptest = CurrentLine.find_first_not_of(" ",pos+1);
137         if (ptest != CurrentLine.npos) {
138           p1 = ptest + 1;
139           if (CurrentLine[ptest] == '"') { // quoted
140             p2 = CurrentLine.find_first_of("\"",p1);
141           } else { // not quoted
142             p2 = CurrentLine.find_first_of(" ",p1);
143           }
144           if (p2 != CurrentLine.npos) {
145             return CurrentLine.substr(p1,p2-p1);
146           }
147         }
148       } else {   // "=" not found
149         pos = CurrentLine.find(val);
150         pos = CurrentLine.find_first_of(" ",pos+1);
151         ptest = CurrentLine.find_first_not_of(" ",pos+1);
152         if (ptest != CurrentLine.npos) {
153           if (CurrentLine[ptest] == '"') { // quoted
154             p1 = ptest + 1;
155             p2 = CurrentLine.find_first_of("\"",p1);
156           } else { // not quoted
157             p1 = ptest;
158             p2 = CurrentLine.find_first_of(" ",p1);
159           }
160           if (p2 != CurrentLine.npos) {
161             return CurrentLine.substr(p1,p2-p1);
162           } else {
163             p2 = CurrentLine.length();
164             return CurrentLine.substr(p1,p2-p1);
165           }
166         }
167       }
168     }
169   }
170
171   return string("");
172 }
173
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175
176 string FGConfigFile::GetValue(void)
177 {
178   return GetValue("");
179 }
180
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182
183 string FGConfigFile::GetLine(void)
184 {
185   string scratch = "";
186   int test;
187
188   while ((test = cfgfile.get()) != EOF) {
189     if (test >= 0x20) {
190       scratch += (char)test;
191     } else {
192       if ((test = cfgfile.get()) != EOF) {
193 #if defined ( sgi ) && !defined( __GNUC__ )
194         if (test >= 0x20) cfgfile.putback(test);
195 #else
196         if (test >= 0x20) cfgfile.unget();
197 #endif
198         break;
199       }
200     }
201   }
202   if (cfgfile.eof()) return string("EOF");
203   return scratch;
204 }
205
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207
208 FGConfigFile& FGConfigFile::operator>>(double& val)
209 {
210   unsigned int pos, end;
211
212   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
213   if (pos == CurrentLine.npos) pos = CurrentLine.length();
214   end = CurrentLine.find_first_of(", ",pos+1);
215   if (end == CurrentLine.npos) end = CurrentLine.length();
216   string str = CurrentLine.substr(pos, end - pos);
217   val = strtod(str.c_str(),NULL);
218   CurrentIndex = end+1;
219   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
220   return *this;
221 }
222
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 FGConfigFile& FGConfigFile::operator>>(float& val)
226 {
227   unsigned int pos, end;
228
229   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
230   if (pos == CurrentLine.npos) pos = CurrentLine.length();
231   end = CurrentLine.find_first_of(", ",pos+1);
232   if (end == CurrentLine.npos) end = CurrentLine.length();
233   string str = CurrentLine.substr(pos, end - pos);
234   val = strtod(str.c_str(),NULL);
235   CurrentIndex = end+1;
236   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
237   return *this;
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242 FGConfigFile& FGConfigFile::operator>>(int& val)
243 {
244   unsigned int pos, end;
245
246   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
247   if (pos == CurrentLine.npos) pos = CurrentLine.length();
248   end = CurrentLine.find_first_of(", ",pos+1);
249   if (end == CurrentLine.npos) end = CurrentLine.length();
250   string str = CurrentLine.substr(pos, end - pos);
251   val = atoi(str.c_str());
252   CurrentIndex = end+1;
253   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
254   return *this;
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 FGConfigFile& FGConfigFile::operator>>(eParam& val)
260 {
261   unsigned int pos, end;
262
263   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
264   if (pos == CurrentLine.npos) pos = CurrentLine.length();
265   end = CurrentLine.find_first_of(", ",pos+1);
266   if (end == CurrentLine.npos) end = CurrentLine.length();
267   string str = CurrentLine.substr(pos, end - pos);
268   val = (eParam)atoi(str.c_str());
269   CurrentIndex = end+1;
270   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
271   return *this;
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275
276 FGConfigFile& FGConfigFile::operator>>(string& str)
277 {
278   unsigned int pos, end;
279
280   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
281   if (pos == CurrentLine.npos) pos = CurrentLine.length();
282   end = CurrentLine.find_first_of(", ",pos+1);
283   if (end == CurrentLine.npos) end = CurrentLine.length();
284   str = CurrentLine.substr(pos, end - pos);
285   CurrentIndex = end+1;
286   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
287   return *this;
288 }
289
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291
292 void FGConfigFile::ResetLineIndexToZero(void)
293 {
294   CurrentIndex = 0;
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298
299 void FGConfigFile::Debug(void)
300 {
301     //TODO: Add your source code here
302 }
303