]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UIUCModel/uiuc_parsefile.cpp
Updated to match changes in radiostack.[ch]xx
[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
66
67 #include "uiuc_parsefile.h"
68
69
70 ParseFile :: ParseFile (const string fileName)
71 {
72   file.open(fileName.c_str());
73   readFile();
74 }
75
76 ParseFile :: ~ParseFile ()
77 {
78   file.close();
79 }
80
81 void ParseFile :: removeComments(string& inputLine)
82 {
83   int pos = inputLine.find_first_of(COMMENT);
84   
85   if (pos != inputLine.npos) // a "#" exists in the line 
86   {
87         if (inputLine.find_first_not_of(DELIMITERS) == pos)
88           inputLine = ""; // Complete line a comment
89         else
90           inputLine = inputLine.substr(0,pos); //Truncate the comment from the line
91   }
92 }
93
94
95 string ParseFile :: getToken(string inputLine, int tokenNo)
96 {
97   int pos = 0;
98   int pos1 = 0;
99   int tokencounter = 0;
100
101   while (tokencounter < tokenNo)
102   {
103         if ((pos1 == inputLine.npos) || (pos1 == -1) || (pos == -1) )
104           return ""; //return an empty string if tokenNo exceeds the No of tokens in the line
105         
106         inputLine = inputLine.substr(pos1 , MAXLINE);
107         pos = inputLine.find_first_not_of(DELIMITERS);
108         pos1 = inputLine.find_first_of(DELIMITERS , pos);
109         tokencounter ++;
110   }
111
112   if (pos1== -1 || pos == -1)
113       return "";
114   else
115       return inputLine.substr(pos , pos1-pos); // return the desired token 
116 }
117
118
119 void ParseFile :: storeCommands(string inputLine)
120 {
121   int pos;
122   int pos1;
123   // int wordlength;
124   string line;
125  
126   inputLine += " ";  // To take care of the case when last character is not a blank
127   pos = inputLine.find_first_not_of(DELIMITERS);
128   pos1 = inputLine.find_first_of(DELIMITERS);
129   
130   while ((pos != inputLine.npos) && (pos1 != inputLine.npos))
131   {
132         line += inputLine.substr(pos , pos1 - pos)+ " ";
133         inputLine = inputLine.substr(pos1, inputLine.size()- (pos1 - pos));
134         pos = inputLine.find_first_not_of(DELIMITERS);
135         pos1 = inputLine.find_first_of(DELIMITERS , pos);
136   }
137   
138   line += inputLine; // Add the last word to the line
139   commands.push_back(line);
140 }
141
142 void ParseFile :: readFile()
143 {
144   string line;
145
146   while (getline(file , line))
147   {
148         removeComments(line);
149         if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines
150           storeCommands(line);
151   }
152 }
153
154 stack ParseFile :: getCommands()
155 {
156   return commands;
157 }
158
159 //end uiuc_parsefile.cpp