]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UIUCModel/uiuc_parsefile.cpp
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / FDM / UIUCModel / uiuc_parsefile.cpp
1 /**********************************************************************
2
3  FILENAME:     uiuc_parsefile.cpp
4
5 ----------------------------------------------------------------------
6
7  DESCRIPTION:  Reads the input file and stores data in a list
8                gets tokens from each line of the list
9               
10 ----------------------------------------------------------------------
11
12  STATUS:       alpha version
13
14 ----------------------------------------------------------------------
15
16  REFERENCES:   
17
18 ----------------------------------------------------------------------
19
20  HISTORY:      01/30/2000   (BS) initial release
21                09/19/2002   (MSS) appended zeros to lines w/ comments
22
23 ----------------------------------------------------------------------
24
25  AUTHOR(S):    Bipin Sehgal       <bsehgal@uiuc.edu>
26                Michael Selig
27
28 ----------------------------------------------------------------------
29
30  VARIABLES:
31
32 ----------------------------------------------------------------------
33
34  INPUTS:       *
35
36 ----------------------------------------------------------------------
37
38  OUTPUTS:      *
39
40 ----------------------------------------------------------------------
41
42  CALLED BY:    *
43
44 ----------------------------------------------------------------------
45
46  CALLS TO:     *
47
48 ----------------------------------------------------------------------
49
50  COPYRIGHT:    (C) 2000 by Michael Selig
51
52  This program is free software; you can redistribute it and/or
53  modify it under the terms of the GNU General Public License
54  as published by the Free Software Foundation.
55
56  This program is distributed in the hope that it will be useful,
57  but WITHOUT ANY WARRANTY; without even the implied warranty of
58  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59  GNU General Public License for more details.
60
61  You should have received a copy of the GNU General Public License
62  along with this program; if not, write to the Free Software
63  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
64
65 **********************************************************************/
66
67
68 #include "uiuc_parsefile.h"
69
70
71 ParseFile :: ParseFile (const string fileName)
72 {
73   file.open(fileName.c_str());
74   readFile();
75 }
76
77 ParseFile :: ~ParseFile ()
78 {
79   file.close();
80 }
81
82 void ParseFile :: removeComments(string& inputLine)
83 {
84   string::size_type pos = inputLine.find_first_of(COMMENT);
85   
86   if (pos != inputLine.npos) // a "#" exists in the line 
87   {
88         if (inputLine.find_first_not_of(DELIMITERS) == pos)
89           {
90             inputLine = ""; // Complete line a comment
91           }
92         else
93           {
94             inputLine = inputLine.substr(0,pos); //Truncate the comment from the line
95             // append zeros to the input line after stripping off the comments
96             // mss added from Bipin email of 9/3/02
97             //      inputLine += " 0 0 0 0 0 0";
98           }
99   }
100 }
101
102
103 string ParseFile :: getToken(string inputLine, int tokenNo)
104 {
105   string::size_type pos = 0;
106   string::size_type pos1 = 0;
107   int tokencounter = 0;
108
109   while (tokencounter < tokenNo)
110   {
111     if ((pos1 == inputLine.npos) || (pos1 == -1) || (pos == -1) )
112           return ""; //return an empty string if tokenNo exceeds the No of tokens in the line
113         
114         inputLine = inputLine.substr(pos1 , MAXLINE);
115         pos = inputLine.find_first_not_of(DELIMITERS);
116         pos1 = inputLine.find_first_of(DELIMITERS , pos);
117         tokencounter ++;
118   }
119
120   if (pos1== -1 || pos == -1)
121     return "";
122   else
123       return inputLine.substr(pos , pos1-pos); // return the desired token 
124 }
125
126
127 void ParseFile :: storeCommands(string inputLine)
128 {
129   string::size_type pos;
130   string::size_type pos1;
131   // int wordlength;
132   string line;
133  
134   inputLine += " ";  // To take care of the case when last character is not a blank
135   pos = inputLine.find_first_not_of(DELIMITERS);
136   pos1 = inputLine.find_first_of(DELIMITERS);
137   
138   while ((pos != inputLine.npos) && (pos1 != inputLine.npos))
139   {
140         line += inputLine.substr(pos , pos1 - pos)+ " ";
141         inputLine = inputLine.substr(pos1, inputLine.size()- (pos1 - pos));
142         pos = inputLine.find_first_not_of(DELIMITERS);
143         pos1 = inputLine.find_first_of(DELIMITERS , pos);
144   }
145   
146   line += inputLine; // Add the last word to the line
147   commands.push_back(line);
148 }
149
150 //  void ParseFile :: readFile()
151 //  {
152 //    string line;
153
154 //    while (getline(file , line))
155 //    {
156 //     removeComments(line);
157 //     if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines
158 //     {
159 //      line += "     0 0 0 0 0";
160 //          storeCommands(line);
161 //     }
162 //    }
163 //  }
164
165 void ParseFile :: readFile()
166 {
167   string line;
168
169   while (getline(file , line))
170     {
171       removeComments(line);
172       if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines
173         {
174           line += "     ";
175           // append some zeros, but this is doing something strange!
176           //              line += "  0 0 0 0 0   ";
177           storeCommands(line);
178         }
179     }
180 }
181 stack ParseFile :: getCommands()
182 {
183   return commands;
184 }
185
186 //end uiuc_parsefile.cpp