1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 | /*********************************************
Reactor Geometry Generator
Argonne National Laboratory
Contains CParser class implementation.
*********************************************/
#include <cctype>
#include "meshkit/parser.hpp"
#include <stdlib.h>
CParser::CParser ()
// ---------------------------------------------------------------------------
// Function: default constructor
// Input: none
// Output: none
// ---------------------------------------------------------------------------
{
}
CParser::~CParser ()
// ---------------------------------------------------------------------------
// Function: destructor
// Input: none
// Output: none
// ---------------------------------------------------------------------------
{
}
bool CParser::ReadNextLine (std::ifstream& FileInput, int& nLineNum,
std::string& szInputString, const int MAXCHARS,
const std::string& szComment, bool bLowerCase)
// ---------------------------------------------------------------------------
// Function: reads the next line skipping over the comment lines
// and converts all alphabets to lower case if requested
// Input: file istream, line #, string to hold the input line,
// max. # of characters expected in each input line,
// comment character(s) at the beginning of a comment line,
// lowercase conversion option
// Output: updated values of line # and the string
// return value is true if successful
// false if an error state is encountered
// Restriction: Cannot read a line over 256 characters
// ---------------------------------------------------------------------------
{
int flag = 0;
int flag1 =0;
bool bWhSpc = false;<--- The scope of the variable 'bWhSpc' can be reduced.
int tokenfound = 1;
const int MAXCH = 2000;
char szInp[MAXCH];
char szTemp [MAXCH];
std::vector<std::string> tokens;
// enough capacity to read and store?
if (MAXCHARS > MAXCH)
return false;
// comment character(s)
int nCLen = static_cast<int>(szComment.length());
// read the line (skip over comment lines)
for(;;)
{
++nLineNum;
FileInput.getline (szInp, MAXCHARS);
// // end-of-file?
// if (FileInput.eof())
// return false;
if (FileInput.fail())
FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
// unrecoverable error?
if (FileInput.bad())
return false;
// successful read
szInputString = szInp;
GetTokens(szInputString, " ", tokens);
bWhSpc = EatWhiteSpace(szInputString);
if ((szInputString.substr(0,nCLen) != szComment)&& (bWhSpc ==false)){
szInputString = szInp;
GetTokens(szInputString, " ", tokens);
for(unsigned int i=0; i< tokens.size(); i++){
std::string temptoken = tokens[i];
if (temptoken == "&")
flag1 = 1;
}
//Filter the comment tokens
// FilterComment(szInputString, szComment);
//if "&" is found continue to read the next line
std::string szTempString = szInputString;
// check if line is continued &
while(flag1 ==1 && tokenfound == 1){
GetTokens(szTempString, " ", tokens);
for(unsigned int i=1; i<=tokens.size(); i++){
std::string temptoken = tokens[i-1];
if (temptoken == "&"){
tokenfound = 1;
flag = 1;
}
else{
if(flag==1)
flag = 1;//do nothing token already found
else
tokenfound = 0;
}
}
if(tokenfound ==1){
++nLineNum;
RemoveToken(szInputString);
//- getting more tokens and add to the existing
FileInput.getline (szTemp, MAXCHARS);
// end-of-file?
if (FileInput.eof())
return false;
if (FileInput.fail())
FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
// unrecoverable error?
if (FileInput.bad())
return false;
// successful read
szTempString = szTemp;
FilterComment(szTempString, szComment);
szInputString+=" ";
szInputString+=szTemp;
}
else{
break;//while loop ents
}
flag = 0;
}
// while loop ends
// convert to lower case?
if (bLowerCase){
for (int i=0; i < static_cast<int>(szInputString.length()); i++)
szInputString[i] = tolower(szInputString[i]);
}
break;
}
}
return true;
}
void CParser::GetTokens (const std::string& input,
const std::string& delims,
std::vector<std::string>& tokens)
// ----------------------------------------------------------------------------
// Function: Parses the input line and gets the tokens
// Input: string, delimiters
// Output: vector containing the tokens
// ----------------------------------------------------------------------------
{
std::string::size_type beg_index, end_index;
// clear the vector that will store the tokens
tokens.clear();
// get location of the first character that is not a delimiter
beg_index = input.find_first_not_of(delims);
// loop while the beginning index is not the end of string
while (beg_index != std::string::npos)
{
// get location of the next delimiter
end_index = input.find_first_of (delims, beg_index);
// if this location is the end of string then adjust the value
// as string length
if (end_index == std::string::npos) end_index = input.length();
// save the string between beg_index and end_index
tokens.push_back (input.substr(beg_index,end_index-beg_index));
// get location of the next character that is not a delimiter
beg_index = input.find_first_not_of (delims, end_index);
}
}
void CParser:: FilterComment (std::string& input, const std::string& szComment)
// ----------------------------------------------------------------------------
// Function: Parses the input line and gets the tokens
// Input: string, delimiters
// Output: vector containing the tokens
// ----------------------------------------------------------------------------
{
// remove comment from the line obtained
unsigned int i;
std::vector<std::string> tokens;
std::string tempInput;
GetTokens(input, " ", tokens);
for(i=0; i<tokens.size(); i++){
std::string temptoken = tokens[i];
if(temptoken == szComment){
break;
}
else{
tempInput+=temptoken;
if(i!=(tokens.size()-1)){ //indent{
if (tokens[i+1]!=szComment)
tempInput+=" ";
}
else{
tempInput+=""; // no indent
}
}
}
input = tempInput.c_str();
}
void CParser:: RemoveToken (std::string& input)
// ----------------------------------------------------------------------------
// Function: Parses the input line and gets the tokens
// Input: string, delimiters
// Output: vector containing the tokens
// ----------------------------------------------------------------------------
{
// remove comment from the line obtained
unsigned int i;
std::vector<std::string> tokens;
std::string tempInput;
GetTokens(input, " ", tokens);
for(i=0; i<tokens.size(); i++){
std::string temptoken = tokens[i];
if(temptoken == "&"){
break;
}
else{
tempInput+=temptoken;
if(i!=(tokens.size()-1)){ //indent{
if (tokens[i+1]!="&")
tempInput+=" ";
}
else{
tempInput+=""; // no indent
}
}
}
input = tempInput.c_str();
}
bool CParser:: EatWhiteSpace (std::string& input)
// ----------------------------------------------------------------------------
// Function: Parses the input line and gets the tokens
// Input: string, delimiters
// Output: vector containing the tokens
// ----------------------------------------------------------------------------
{
// remove comment from the line obtained
unsigned int i;
std::vector<std::string> tokens;
std::string tempInput;<--- Unused variable: tempInput
GetTokens(input, " ", tokens);
for(i=0; i<tokens.size(); i++){
std::string temptoken = tokens[i];
if(temptoken != " " && temptoken !="!"){
return false;
break; <--- Consecutive return, break, continue, goto or throw statements are unnecessary.
}
else if(temptoken =="!"){
return true;
break;<--- Consecutive return, break, continue, goto or throw statements are unnecessary.
}
}
return false;
}
|