]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGConfigFile.cpp
Latest round of JSBSim updates.
[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 extern short debug_lvl;
28
29 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 CLASS IMPLEMENTATION
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32
33 FGConfigFile::FGConfigFile(string cfgFileName)
34 {
35   cfgfile.open(cfgFileName.c_str());
36   CommentsOn = false;
37   CurrentIndex = 0;
38   Opened = true;
39   if (cfgfile.is_open()) GetNextConfigLine();
40   else Opened = false;
41
42   if (debug_lvl & 2) cout << "Instantiated: FGConfigFile" << endl;
43 }
44
45 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
47 FGConfigFile::~FGConfigFile()
48 {
49   cfgfile.close();
50   if (debug_lvl & 2) cout << "Destroyed:    FGConfigFile" << endl;
51 }
52
53 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54
55 string FGConfigFile::GetNextConfigLine(void)
56 {
57   do {
58     CurrentLine = GetLine();
59     if ((CurrentLine.find("<COMMENT>") != CurrentLine.npos) ||
60         (CurrentLine.find("<!--") != CurrentLine.npos)) CommentsOn = true;
61     if ((CurrentLine.find("</COMMENT>") != CurrentLine.npos) ||
62         (CurrentLine.find("-->") != CurrentLine.npos)) {
63       CommentsOn = false;
64       GetNextConfigLine();
65     }
66   } while (IsCommentLine());
67   if (CurrentLine.length() == 0) GetNextConfigLine();
68   CurrentIndex = 0;
69   return CurrentLine;
70 }
71
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73
74 string FGConfigFile::GetValue(string val)
75 {
76   unsigned int pos, p1, p2, ptest;
77
78   if (val == "") {    // this call is to return the tag value
79     pos = CurrentLine.find("<");
80     if (pos != CurrentLine.npos) { // beginning brace found "<"
81       p1 = CurrentLine.find_first_not_of(" ",pos+1);
82       if (p1 != CurrentLine.npos) { // found first character of tag
83         p2 = CurrentLine.find_first_of(" >",p1+1);
84         if (p2 == CurrentLine.npos) p2 = p1+1;
85         return CurrentLine.substr(p1,p2-p1);
86       }
87     } else {   // beginning brace "<" not found; this is a regular data line
88       pos = CurrentLine.find_first_not_of(" ");
89       if (pos != CurrentLine.npos) {  // first character in line found
90         p2 = CurrentLine.find_first_of(" ",pos+1);
91         if (p2 != CurrentLine.npos) {
92           return CurrentLine.substr(pos,p2-pos);
93         } else {
94           return CurrentLine.substr(pos,CurrentLine.length()-pos);
95         }
96       }
97     }
98   } else { // return a value for a specific tag
99     pos = CurrentLine.find(val);
100     if (pos != CurrentLine.npos) {
101       pos = CurrentLine.find("=",pos);
102       if (pos != CurrentLine.npos) {
103         ptest = CurrentLine.find_first_not_of(" ",pos+1);
104         if (ptest != CurrentLine.npos) {
105           p1 = ptest + 1;
106           if (CurrentLine[ptest] == '"') { // quoted
107             p2 = CurrentLine.find_first_of("\"",p1);
108           } else { // not quoted
109             p2 = CurrentLine.find_first_of(" ",p1);
110           }
111           if (p2 != CurrentLine.npos) {
112             return CurrentLine.substr(p1,p2-p1);
113           }
114         }
115       } else {   // "=" not found
116         pos = CurrentLine.find(val);
117         pos = CurrentLine.find_first_of(" ",pos+1);
118         ptest = CurrentLine.find_first_not_of(" ",pos+1);
119         if (ptest != CurrentLine.npos) {
120           if (CurrentLine[ptest] == '"') { // quoted
121             p1 = ptest + 1;
122             p2 = CurrentLine.find_first_of("\"",p1);
123           } else { // not quoted
124             p1 = ptest;
125             p2 = CurrentLine.find_first_of(" ",p1);
126           }
127           if (p2 != CurrentLine.npos) {
128             return CurrentLine.substr(p1,p2-p1);
129           } else {
130             p2 = CurrentLine.length();
131             return CurrentLine.substr(p1,p2-p1);
132           }
133         }
134       }
135     }
136   }
137
138   return string("");
139 }
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 string FGConfigFile::GetValue(void)
144 {
145   return GetValue("");
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 bool FGConfigFile::IsCommentLine(void)
151 {
152   if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
153   if (CommentsOn) return true;
154
155   return false;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 string FGConfigFile::GetLine(void)
161 {
162   string scratch = "";
163   int test;
164
165   while ((test = cfgfile.get()) != EOF) {
166     if (test >= 0x20) {
167       scratch += (char)test;
168     } else {
169       if ((test = cfgfile.get()) != EOF) {
170         if (test >= 0x20) cfgfile.unget();
171         break;
172       }
173     }
174   }
175   if (cfgfile.eof()) return string("EOF");
176   return scratch;
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180
181 FGConfigFile& FGConfigFile::operator>>(double& val)
182 {
183   unsigned int pos, end;
184
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();
193   return *this;
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 FGConfigFile& FGConfigFile::operator>>(float& val)
199 {
200   unsigned int pos, end;
201
202   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
203   if (pos == CurrentLine.npos) pos = CurrentLine.length();
204   end = CurrentLine.find_first_of(", ",pos+1);
205   if (end == CurrentLine.npos) end = CurrentLine.length();
206   string str = CurrentLine.substr(pos, end - pos);
207   val = strtod(str.c_str(),NULL);
208   CurrentIndex = end+1;
209   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
210   return *this;
211 }
212
213 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214
215 FGConfigFile& FGConfigFile::operator>>(int& val)
216 {
217   unsigned int pos, end;
218
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   string str = CurrentLine.substr(pos, end - pos);
224   val = atoi(str.c_str());
225   CurrentIndex = end+1;
226   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
227   return *this;
228 }
229
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231
232 FGConfigFile& FGConfigFile::operator>>(eParam& val)
233 {
234   unsigned int pos, end;
235
236   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
237   if (pos == CurrentLine.npos) pos = CurrentLine.length();
238   end = CurrentLine.find_first_of(", ",pos+1);
239   if (end == CurrentLine.npos) end = CurrentLine.length();
240   string str = CurrentLine.substr(pos, end - pos);
241   val = (eParam)atoi(str.c_str());
242   CurrentIndex = end+1;
243   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
244   return *this;
245 }
246
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248
249 FGConfigFile& FGConfigFile::operator>>(string& str)
250 {
251   unsigned int pos, end;
252
253   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
254   if (pos == CurrentLine.npos) pos = CurrentLine.length();
255   end = CurrentLine.find_first_of(", ",pos+1);
256   if (end == CurrentLine.npos) end = CurrentLine.length();
257   str = CurrentLine.substr(pos, end - pos);
258   CurrentIndex = end+1;
259   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
260   return *this;
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264
265 void FGConfigFile::ResetLineIndexToZero(void)
266 {
267   CurrentIndex = 0;
268 }
269
270 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271
272 void FGConfigFile::Debug(void)
273 {
274     //TODO: Add your source code here
275 }
276