35 |
|
printf("Could not open SHAPE file %s\n", fname); |
36 |
|
exit(-1); |
37 |
|
} |
38 |
< |
rewind(shapeFile); |
39 |
< |
lineNum = 0; |
38 |
> |
|
39 |
> |
findBegin( "ShapeInfo" ); |
40 |
> |
|
41 |
|
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
42 |
< |
lineNum++; |
42 |
< |
if( eof_test == NULL ){ |
43 |
< |
printf("Error in reading SHAPE from SHAPE file at line %d.\n", |
44 |
< |
lineNum ); |
45 |
< |
exit(-1); |
46 |
< |
} |
47 |
< |
// first find the shape name: |
42 |
> |
|
43 |
|
while( eof_test != NULL ){ |
44 |
|
// toss comment lines |
45 |
|
if( readLine[0] != '!' && readLine[0] != '#' ){ |
46 |
+ |
|
47 |
+ |
if (isEndLine(readLine)) break; |
48 |
|
|
49 |
|
nTokens = count_tokens(readLine, " ,;\t"); |
50 |
|
if (nTokens < 5) { |
51 |
< |
printf("Not enough data on information line in SHAPE file.\n"); |
51 |
> |
printf("Not enough data on shapeInfo line in SHAPE file.\n"); |
52 |
|
exit(-1); |
53 |
|
} |
54 |
|
|
55 |
|
foo = strtok(readLine, " ,;\t"); |
56 |
< |
setType(foo); |
56 |
> |
setType(TrimSpaces(foo)); |
57 |
|
foo = strtok(NULL, " ,;\t"); |
58 |
|
mass = atof(foo); |
59 |
|
foo = strtok(NULL, " ,;\t"); |
65 |
|
break; |
66 |
|
} |
67 |
|
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
71 |
– |
lineNum++; |
68 |
|
} |
69 |
|
|
70 |
|
findBegin( "ContactFunctions" ); |
71 |
|
|
72 |
+ |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
73 |
+ |
|
74 |
|
while( eof_test != NULL ){ |
75 |
|
// toss comment lines |
76 |
|
if( readLine[0] != '!' && readLine[0] != '#' ){ |
77 |
|
|
78 |
< |
foo = strtok(readLine, " ,;\t"); |
81 |
< |
if (!strcasecmp(foo, "end")) break; |
78 |
> |
if (isEndLine(readLine)) break; |
79 |
|
|
80 |
< |
nTokens = count_tokens(readLine, " ,;\t"); |
80 |
> |
nTokens = count_tokens(readLine, " ,;\t"); |
81 |
|
if (nTokens != 4) { |
82 |
|
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
83 |
|
exit(-1); |
107 |
|
} |
108 |
|
|
109 |
|
findBegin( "RangeFunctions" ); |
110 |
+ |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
111 |
|
|
112 |
|
while( eof_test != NULL ){ |
113 |
|
// toss comment lines |
114 |
|
if( readLine[0] != '!' && readLine[0] != '#' ){ |
115 |
|
|
116 |
< |
foo = strtok(readLine, " ,;\t"); |
119 |
< |
if (!strcasecmp(foo, "end")) break; |
116 |
> |
if (isEndLine(readLine)) break; |
117 |
|
|
118 |
|
nTokens = count_tokens(readLine, " ,;\t"); |
119 |
|
if (nTokens != 4) { |
120 |
< |
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
120 |
> |
printf("incorrect number of tokens on sFunc line in SHAPE file\n"); |
121 |
|
exit(-1); |
122 |
|
} |
123 |
|
|
145 |
|
} |
146 |
|
|
147 |
|
findBegin( "StrengthFunctions" ); |
148 |
+ |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
149 |
|
|
150 |
|
while( eof_test != NULL ){ |
151 |
|
// toss comment lines |
152 |
|
if( readLine[0] != '!' && readLine[0] != '#' ){ |
153 |
< |
|
154 |
< |
foo = strtok(readLine, " ,;\t"); |
157 |
< |
if (!strcasecmp(foo, "end")) break; |
153 |
> |
|
154 |
> |
if (isEndLine(readLine)) break; |
155 |
|
|
156 |
|
nTokens = count_tokens(readLine, " ,;\t"); |
157 |
|
if (nTokens != 4) { |
158 |
< |
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
158 |
> |
printf("incorrect number of tokens on epsFunc line in SHAPE file\n"); |
159 |
|
exit(-1); |
160 |
|
} |
161 |
|
|
203 |
|
} |
204 |
|
|
205 |
|
while( !foundText ){ |
209 |
– |
while( eof_test != NULL) { |
210 |
– |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
211 |
– |
lineNum++; |
212 |
– |
} |
206 |
|
|
207 |
|
if( eof_test == NULL ){ |
208 |
|
printf("Error fast forwarding file at line %d: " |
211 |
|
} |
212 |
|
|
213 |
|
the_token = strtok( readLine, " ,;\t" ); |
221 |
– |
if (!strcasecmp("begin", the_token)) { |
214 |
|
|
215 |
< |
the_token = strtok( NULL, " ,;\t" ); |
216 |
< |
foundText = !strcmp( startText, the_token ); |
215 |
> |
if (!strcasecmp("begin", the_token)) { |
216 |
> |
the_token = TrimSpaces(strtok( NULL, " ,;\t" )); |
217 |
> |
foundText = !strcasecmp( startText, the_token ); |
218 |
|
|
219 |
|
} |
220 |
|
|
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
< |
int SHAPE::count_tokens(char *line, char *delimiters) { |
236 |
< |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
235 |
> |
int SHAPE::isEndLine(char *line) { |
236 |
> |
char *working_line; |
237 |
> |
char *foo; |
238 |
|
|
239 |
+ |
working_line = strdup(line); |
240 |
+ |
|
241 |
+ |
foo = strtok(working_line, " ,;\t"); |
242 |
+ |
|
243 |
+ |
if (!strcasecmp(foo, "end")) return 1; |
244 |
+ |
|
245 |
+ |
return 0; |
246 |
+ |
} |
247 |
+ |
|
248 |
+ |
|
249 |
+ |
int SHAPE::count_tokens(char *line, char *delimiters) { |
250 |
+ |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
251 |
+ |
|
252 |
|
char *working_line; /* WORKING COPY OF LINE. */ |
253 |
|
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
254 |
|
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
255 |
< |
|
255 |
> |
|
256 |
|
strtok_ptr= working_line= strdup(line); |
257 |
< |
|
257 |
> |
|
258 |
|
ntokens=0; |
259 |
|
while (strtok(strtok_ptr,delimiters)!=NULL) |
260 |
|
{ |
261 |
|
ntokens++; |
262 |
|
strtok_ptr=NULL; |
263 |
|
} |
264 |
< |
|
264 |
> |
|
265 |
|
free(working_line); |
266 |
|
return(ntokens); |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
|
270 |
+ |
/** |
271 |
+ |
* Removes left and right spaces from a string |
272 |
+ |
* |
273 |
+ |
* @param str String to trim |
274 |
+ |
* |
275 |
+ |
* @return char* to the trimed string |
276 |
+ |
*/ |
277 |
+ |
char * SHAPE::TrimSpaces(char *str) { |
278 |
+ |
size_t len; |
279 |
+ |
char *right, *left; |
280 |
+ |
|
281 |
+ |
if (strlen(str) == 0) return(str); |
282 |
+ |
|
283 |
+ |
/* Trim whitespace from left side */ |
284 |
+ |
for (left = str; isspace(*left); left++); |
285 |
+ |
|
286 |
+ |
/* Trim whitespace from right side */ |
287 |
+ |
if ((len = strlen(left))) |
288 |
+ |
{ |
289 |
+ |
right = left + (len - 1); |
290 |
+ |
|
291 |
+ |
while (isspace(*right)) |
292 |
+ |
{ |
293 |
+ |
*right = '\0'; |
294 |
+ |
right--; |
295 |
+ |
} |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
/* Only do the str copy if their was spaces to the left */ |
299 |
+ |
if (left != str) |
300 |
+ |
strcpy(str, left); |
301 |
+ |
|
302 |
+ |
return (str); |
303 |
|
} |
304 |
+ |
|
305 |
+ |
|
306 |
+ |
double SHAPE::getSigmaAt(double costheta, double phi) { |
307 |
+ |
|
308 |
+ |
vector<SHFunc*>::iterator sigmaIter; |
309 |
+ |
double sigma; |
310 |
+ |
|
311 |
+ |
sigma = 0.0; |
312 |
+ |
|
313 |
+ |
for(sigmaIter = sigmaFuncs.begin(); sigmaIter != sigmaFuncs.end(); |
314 |
+ |
++sigmaIter) |
315 |
+ |
sigma += (*sigmaIter)->getValueAt(costheta, phi); |
316 |
+ |
|
317 |
+ |
return sigma; |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
double SHAPE::getSAt(double costheta, double phi) { |
321 |
+ |
|
322 |
+ |
vector<SHFunc*>::iterator sIter; |
323 |
+ |
double s; |
324 |
+ |
|
325 |
+ |
s = 0.0; |
326 |
+ |
|
327 |
+ |
for(sIter = sFuncs.begin(); sIter != sFuncs.end(); ++sIter) |
328 |
+ |
s += (*sIter)->getValueAt(costheta, phi); |
329 |
+ |
|
330 |
+ |
return s; |
331 |
+ |
} |
332 |
+ |
|
333 |
+ |
double SHAPE::getEpsAt(double costheta, double phi) { |
334 |
+ |
|
335 |
+ |
vector<SHFunc*>::iterator epsIter; |
336 |
+ |
double eps; |
337 |
+ |
|
338 |
+ |
eps = 0.0; |
339 |
+ |
|
340 |
+ |
for(epsIter = epsFuncs.begin(); epsIter != epsFuncs.end(); ++epsIter) |
341 |
+ |
eps += (*epsIter)->getValueAt(costheta, phi); |
342 |
+ |
|
343 |
+ |
return eps; |
344 |
+ |
} |