ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/SHAPES/SHAPE.cpp
(Generate patch)

Comparing trunk/SHAPES/SHAPE.cpp (file contents):
Revision 1299 by gezelter, Thu Jun 24 15:56:05 2004 UTC vs.
Revision 1300 by gezelter, Thu Jun 24 19:06:47 2004 UTC

# Line 35 | Line 35 | void SHAPE::readSHAPEfile(const char *fname) {
35      printf("Could not open SHAPE file %s\n", fname);
36      exit(-1);
37    }
38 <  
38 >
39    findBegin( "ShapeInfo" );
40  
41    eof_test = fgets( readLine, sizeof(readLine), shapeFile );
# Line 44 | Line 44 | void SHAPE::readSHAPEfile(const char *fname) {
44      // toss comment lines
45      if( readLine[0] != '!' && readLine[0] != '#' ){
46        
47 <      foo = strtok(readLine, " ,;\t");
48 <      if (!strcasecmp(foo, "end")) break;
47 >      if (isEndLine(readLine)) break;
48  
49        nTokens = count_tokens(readLine, " ,;\t");
50        if (nTokens < 5) {
# Line 54 | Line 53 | void SHAPE::readSHAPEfile(const char *fname) {
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");
# Line 70 | Line 69 | void SHAPE::readSHAPEfile(const char *fname) {
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");
78 <      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);
# Line 107 | Line 107 | void SHAPE::readSHAPEfile(const char *fname) {
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");
116 <      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  
# Line 145 | Line 145 | void SHAPE::readSHAPEfile(const char *fname) {
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");
154 <      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  
# Line 203 | Line 203 | void SHAPE::findBegin( char* startText ){
203    }
204    
205    while( !foundText ){
206    while( eof_test != NULL) {
207      eof_test = fgets( readLine, sizeof(readLine), shapeFile );
208      lineNum++;
209    }
206      
207      if( eof_test == NULL ){
208        printf("Error fast forwarding file at line %d: "
# Line 215 | Line 211 | void SHAPE::findBegin( char* startText ){
211      }
212  
213      the_token = strtok( readLine, " ,;\t" );
218    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  
# Line 236 | Line 232 | int SHAPE::count_tokens(char *line, char *delimiters)
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines