]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UIUCModel/uiuc_parsefile.cpp
MSVC5 Compatibility tweaks.
[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   initial release
21
22 ----------------------------------------------------------------------
23
24  AUTHOR(S):    Bipin Sehgal       <bsehgal@uiuc.edu>
25
26 ----------------------------------------------------------------------
27
28  VARIABLES:
29
30 ----------------------------------------------------------------------
31
32  INPUTS:       *
33
34 ----------------------------------------------------------------------
35
36  OUTPUTS:      *
37
38 ----------------------------------------------------------------------
39
40  CALLED BY:    *
41
42 ----------------------------------------------------------------------
43
44  CALLS TO:     *
45
46 ----------------------------------------------------------------------
47
48  COPYRIGHT:    (C) 2000 by Michael Selig
49
50  This program is free software; you can redistribute it and/or
51  modify it under the terms of the GNU General Public License
52  as published by the Free Software Foundation.
53
54  This program is distributed in the hope that it will be useful,
55  but WITHOUT ANY WARRANTY; without even the implied warranty of
56  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57  GNU General Public License for more details.
58
59  You should have received a copy of the GNU General Public License
60  along with this program; if not, write to the Free Software
61  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
62  USA or view http://www.gnu.org/copyleft/gpl.html.
63
64 **********************************************************************/
65 #include <simgear/compiler.h>
66
67 #include "uiuc_parsefile.h"
68
69 FG_USING_NAMESPACE(std);
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   int 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           inputLine = ""; // Complete line a comment
90         else
91           inputLine = inputLine.substr(0,pos); //Truncate the comment from the line
92   }
93 }
94
95
96 string ParseFile :: getToken(string inputLine, int tokenNo)
97 {
98   int pos = 0;
99   int pos1 = 0;
100   int tokencounter = 0;
101
102   while (tokencounter < tokenNo)
103   {
104         if ((pos1 == inputLine.npos) || (pos1 == -1) || (pos == -1) )
105           return ""; //return an empty string if tokenNo exceeds the No of tokens in the line
106         
107         inputLine = inputLine.substr(pos1 , MAXLINE);
108         pos = inputLine.find_first_not_of(DELIMITERS);
109         pos1 = inputLine.find_first_of(DELIMITERS , pos);
110         tokencounter ++;
111   }
112
113   if (pos1== -1 || pos == -1)
114       return "";
115   else
116       return inputLine.substr(pos , pos1-pos); // return the desired token 
117 }
118
119
120 void ParseFile :: storeCommands(string inputLine)
121 {
122   int pos;
123   int pos1;
124   int wordlength;
125   string line;
126  
127   inputLine += " ";  // To take care of the case when last character is not a blank
128   pos = inputLine.find_first_not_of(DELIMITERS);
129   pos1 = inputLine.find_first_of(DELIMITERS);
130   
131   while ((pos != inputLine.npos) && (pos1 != inputLine.npos))
132   {
133         line += inputLine.substr(pos , pos1 - pos)+ " ";
134         inputLine = inputLine.substr(pos1, inputLine.size()- (pos1 - pos));
135         pos = inputLine.find_first_not_of(DELIMITERS);
136         pos1 = inputLine.find_first_of(DELIMITERS , pos);
137   }
138   
139   line += inputLine; // Add the last word to the line
140   commands.push_back(line);
141 }
142
143 void ParseFile :: readFile()
144 {
145   string line;
146
147   while (getline(file , line))
148   {
149         removeComments(line);
150         if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines
151           storeCommands(line);
152   }
153 }
154
155 stack ParseFile :: getCommands()
156 {
157   return commands;
158 }
159
160 //end uiuc_parsefile.cpp