]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGConfigFile.cpp
Sync with current JSBSim devel 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 static const char *IdSrc = "$Header$";
25 static const char *IdHdr = "ID_CONFIGFILE";
26
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 CLASS IMPLEMENTATION
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
30
31 FGConfigFile::FGConfigFile(string cfgFileName)
32 {
33   cfgfile.open(cfgFileName.c_str());
34   CommentsOn = false;
35   CurrentIndex = 0;
36   Opened = true;
37   if (cfgfile.is_open()) GetNextConfigLine();
38   else Opened = false;
39 }
40
41
42 FGConfigFile::~FGConfigFile(void)
43 {
44   cfgfile.close();
45 }
46
47
48 string FGConfigFile::GetNextConfigLine(void)
49 {
50   do {
51     CurrentLine = GetLine();
52     if (CurrentLine.find("<COMMENT>") != CurrentLine.npos) CommentsOn = true;
53     if (CurrentLine.find("</COMMENT>") != CurrentLine.npos) {
54       CommentsOn = false;
55       GetNextConfigLine();
56     }
57   } while (IsCommentLine());
58   if (CurrentLine.length() == 0) GetNextConfigLine();
59   CurrentIndex = 0;
60   return CurrentLine;
61 }
62
63
64 string FGConfigFile::GetValue(string val)
65 {
66   unsigned int pos, p1, p2, ptest;
67
68   if (val == "") {    // this call is to return the tag value
69     pos = CurrentLine.find("<");
70     if (pos != CurrentLine.npos) { // beginning brace found "<"
71       p1 = CurrentLine.find_first_not_of(" ",pos+1);
72       if (p1 != CurrentLine.npos) { // found first character of tag
73         p2 = CurrentLine.find_first_of(" >",p1+1);
74         if (p2 == CurrentLine.npos) p2 = p1+1;
75         return CurrentLine.substr(p1,p2-p1);
76       }
77     } else {   // beginning brace "<" not found; this is a regular data line
78       pos = CurrentLine.find_first_not_of(" ");
79       if (pos != CurrentLine.npos) {  // first character in line found
80         p2 = CurrentLine.find_first_of(" ",pos+1);
81         if (p2 != CurrentLine.npos) {
82           return CurrentLine.substr(pos,p2-pos);
83         } else {
84           return CurrentLine.substr(pos,CurrentLine.length()-pos);
85         }
86       }
87     }
88   } else { // return a value for a specific tag
89     pos = CurrentLine.find(val);
90     if (pos != CurrentLine.npos) {
91       pos = CurrentLine.find("=",pos);
92       if (pos != CurrentLine.npos) {
93         ptest = CurrentLine.find_first_not_of(" ",pos+1);
94         if (ptest != CurrentLine.npos) {
95           p1 = ptest + 1;
96           if (CurrentLine[ptest] == '"') { // quoted
97             p2 = CurrentLine.find_first_of("\"",p1);
98           } else { // not quoted
99             p2 = CurrentLine.find_first_of(" ",p1);
100           }
101           if (p2 != CurrentLine.npos) {
102             return CurrentLine.substr(p1,p2-p1);
103           }
104         }
105       } else {   // "=" not found
106         pos = CurrentLine.find(val);
107         pos = CurrentLine.find_first_of(" ",pos+1);
108         ptest = CurrentLine.find_first_not_of(" ",pos+1);
109         if (ptest != CurrentLine.npos) {
110           if (CurrentLine[ptest] == '"') { // quoted
111             p1 = ptest + 1;
112             p2 = CurrentLine.find_first_of("\"",p1);
113           } else { // not quoted
114             p1 = ptest;
115             p2 = CurrentLine.find_first_of(" ",p1);
116           }
117           if (p2 != CurrentLine.npos) {
118             return CurrentLine.substr(p1,p2-p1);
119           } else {
120             p2 = CurrentLine.length();
121             return CurrentLine.substr(p1,p2-p1);
122           }
123         }
124       }
125     }
126   }
127
128   return string("");
129 }
130
131
132 string FGConfigFile::GetValue(void)
133 {
134   return GetValue("");
135 }
136
137
138 bool FGConfigFile::IsCommentLine(void)
139 {
140   if (CurrentLine[0] == '/' && CurrentLine[1] == '/') return true;
141   if (CommentsOn) return true;
142
143   return false;
144 }
145
146
147 string FGConfigFile::GetLine(void)
148 {
149   string scratch = "";
150   unsigned int test;
151
152   while ((test = cfgfile.get()) != EOF) {
153     if (test >= 0x20) {
154       scratch += (char)test;
155     } else {
156       if ((test = cfgfile.get()) != EOF) {
157         if (test >= 0x20) cfgfile.unget();
158         break;
159       }
160     }
161   }
162   if (cfgfile.eof()) return string("EOF");
163   return scratch;
164 }
165
166 FGConfigFile& FGConfigFile::operator>>(double& val)
167 {
168   unsigned int pos, end;
169
170   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
171   if (pos == CurrentLine.npos) pos = CurrentLine.length();
172   end = CurrentLine.find_first_of(", ",pos+1);
173   if (end == CurrentLine.npos) end = CurrentLine.length();
174   string str = CurrentLine.substr(pos, end - pos);
175   val = strtod(str.c_str(),NULL);
176   CurrentIndex = end+1;
177   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
178   return *this;
179 }
180
181 FGConfigFile& FGConfigFile::operator>>(float& 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 FGConfigFile& FGConfigFile::operator>>(int& val)
197 {
198   unsigned int pos, end;
199
200   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
201   if (pos == CurrentLine.npos) pos = CurrentLine.length();
202   end = CurrentLine.find_first_of(", ",pos+1);
203   if (end == CurrentLine.npos) end = CurrentLine.length();
204   string str = CurrentLine.substr(pos, end - pos);
205   val = atoi(str.c_str());
206   CurrentIndex = end+1;
207   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
208   return *this;
209 }
210
211 FGConfigFile& FGConfigFile::operator>>(eParam& val)
212 {
213   unsigned int pos, end;
214
215   pos = CurrentLine.find_first_not_of(", ",CurrentIndex);
216   if (pos == CurrentLine.npos) pos = CurrentLine.length();
217   end = CurrentLine.find_first_of(", ",pos+1);
218   if (end == CurrentLine.npos) end = CurrentLine.length();
219   string str = CurrentLine.substr(pos, end - pos);
220   val = (eParam)atoi(str.c_str());
221   CurrentIndex = end+1;
222   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
223   return *this;
224 }
225
226 FGConfigFile& FGConfigFile::operator>>(string& str)
227 {
228   unsigned int pos, end;
229
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   str = CurrentLine.substr(pos, end - pos);
235   CurrentIndex = end+1;
236   if (CurrentIndex >= CurrentLine.length()) GetNextConfigLine();
237   return *this;
238 }
239
240
241 void FGConfigFile::ResetLineIndexToZero(void)
242 {
243   CurrentIndex = 0;
244 }
245