]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGConfigFile.cpp
Updated JSBsim code.
[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 /*******************************************************************************
25 ************************************ CODE **************************************
26 *******************************************************************************/
27
28 FGConfigFile::FGConfigFile(string cfgFileName)
29 {
30   cfgfile.open(cfgFileName.c_str());
31   CommentsOn = false;
32   CurrentIndex = 0;
33   GetNextConfigLine();
34 }
35
36
37 FGConfigFile::~FGConfigFile(void)
38 {
39   cfgfile.close();
40 }
41
42
43 string FGConfigFile::GetNextConfigLine(void)
44 {
45   do {
46     CurrentLine = GetLine();
47     if (CurrentLine.find("<COMMENT>") != CurrentLine.npos) CommentsOn = true;
48     if (CurrentLine.find("</COMMENT>") != CurrentLine.npos) {
49       CommentsOn = false;
50       GetNextConfigLine();
51     }
52   } while (IsCommentLine());
53   if (CurrentLine.length() == 0) GetNextConfigLine();
54   CurrentIndex = 0;
55   return CurrentLine;
56 }
57
58
59 string FGConfigFile::GetValue(string val)
60 {
61   unsigned int pos, p1, p2, ptest;
62
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);
71       }
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);
78         } else {
79           return CurrentLine.substr(pos,CurrentLine.length()-pos);
80         }
81       }
82     }
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) {
90           p1 = ptest + 1;
91           if (CurrentLine[ptest] == '"') { // quoted
92             p2 = CurrentLine.find_first_of("\"",p1);
93           } else { // not quoted
94             p2 = CurrentLine.find_first_of(" ",p1);
95           }
96           if (p2 != CurrentLine.npos) {
97             return CurrentLine.substr(p1,p2-p1);
98           }
99         }
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
106             p1 = ptest + 1;
107             p2 = CurrentLine.find_first_of("\"",p1);
108           } else { // not quoted
109             p1 = ptest;
110             p2 = CurrentLine.find_first_of(" ",p1);
111           }
112           if (p2 != CurrentLine.npos) {
113             return CurrentLine.substr(p1,p2-p1);
114           } else {
115             p2 = CurrentLine.length();
116             return CurrentLine.substr(p1,p2-p1);
117           }
118         }
119       }
120     }
121   }
122
123   return string("");
124 }
125
126
127 string FGConfigFile::GetValue(void)
128 {
129   return GetValue("");
130 }
131
132
133 bool FGConfigFile::IsCommentLine(void)
134 {
135   if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
136   if (CommentsOn) return true;
137
138   return false;
139 }
140
141
142 string FGConfigFile::GetLine(void)
143 {
144   string scratch = "";
145   unsigned int test;
146
147   while ((test = cfgfile.get()) != EOF) {
148     if (test >= 0x20) {
149       scratch += (char)test;
150     } else {
151       if ((test = cfgfile.get()) != EOF) {
152         if (test >= 0x20) cfgfile.unget();
153         break;
154       }
155     }
156   }
157   if (cfgfile.eof()) return string("EOF");
158   return scratch;
159 }
160
161 FGConfigFile& FGConfigFile::operator>>(double& val)
162 {
163   unsigned int pos, end;
164
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;
172   // EXPERIMENTAL
173   if (CurrentIndex >= CurrentLine.length())
174     GetNextConfigLine();
175   // END EXPERIMENTAL
176   return *this;
177 }
178
179 FGConfigFile& FGConfigFile::operator>>(float& val)
180 {
181   unsigned int pos, end;
182
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;
190   // EXPERIMENTAL
191   if (CurrentIndex >= CurrentLine.length())
192     GetNextConfigLine();
193   // END EXPERIMENTAL
194   return *this;
195 }
196
197 FGConfigFile& FGConfigFile::operator>>(int& val)
198 {
199   unsigned int pos, end;
200
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;
208   // EXPERIMENTAL
209   if (CurrentIndex >= CurrentLine.length())
210     GetNextConfigLine();
211   // END EXPERIMENTAL
212   return *this;
213 }
214
215 FGConfigFile& FGConfigFile::operator>>(string& str)
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   str = CurrentLine.substr(pos, end - pos);
224   CurrentIndex = end+1;
225   // EXPERIMENTAL
226   if (CurrentIndex >= CurrentLine.length())
227     GetNextConfigLine();
228   // END EXPERIMENTAL
229   return *this;
230 }
231
232
233 void FGConfigFile::ResetLineIndexToZero(void)
234 {
235   CurrentIndex = 0;
236 }
237