--- trunk/SHAPES/SHAPE.cpp 2004/06/24 15:31:52 1295 +++ trunk/SHAPES/SHAPE.cpp 2004/06/24 19:06:47 1300 @@ -35,7 +35,7 @@ void SHAPE::readSHAPEfile(const char *fname) { printf("Could not open SHAPE file %s\n", fname); exit(-1); } - + findBegin( "ShapeInfo" ); eof_test = fgets( readLine, sizeof(readLine), shapeFile ); @@ -44,8 +44,7 @@ void SHAPE::readSHAPEfile(const char *fname) { // toss comment lines if( readLine[0] != '!' && readLine[0] != '#' ){ - foo = strtok(readLine, " ,;\t"); - if (!strcasecmp(foo, "end")) break; + if (isEndLine(readLine)) break; nTokens = count_tokens(readLine, " ,;\t"); if (nTokens < 5) { @@ -54,7 +53,7 @@ void SHAPE::readSHAPEfile(const char *fname) { } foo = strtok(readLine, " ,;\t"); - setType(foo); + setType(TrimSpaces(foo)); foo = strtok(NULL, " ,;\t"); mass = atof(foo); foo = strtok(NULL, " ,;\t"); @@ -70,14 +69,15 @@ void SHAPE::readSHAPEfile(const char *fname) { findBegin( "ContactFunctions" ); + eof_test = fgets( readLine, sizeof(readLine), shapeFile ); + while( eof_test != NULL ){ // toss comment lines if( readLine[0] != '!' && readLine[0] != '#' ){ - foo = strtok(readLine, " ,;\t"); - if (!strcasecmp(foo, "end")) break; + if (isEndLine(readLine)) break; - nTokens = count_tokens(readLine, " ,;\t"); + nTokens = count_tokens(readLine, " ,;\t"); if (nTokens != 4) { printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); exit(-1); @@ -107,17 +107,17 @@ void SHAPE::readSHAPEfile(const char *fname) { } findBegin( "RangeFunctions" ); + eof_test = fgets( readLine, sizeof(readLine), shapeFile ); while( eof_test != NULL ){ // toss comment lines if( readLine[0] != '!' && readLine[0] != '#' ){ - foo = strtok(readLine, " ,;\t"); - if (!strcasecmp(foo, "end")) break; + if (isEndLine(readLine)) break; nTokens = count_tokens(readLine, " ,;\t"); if (nTokens != 4) { - printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); + printf("incorrect number of tokens on sFunc line in SHAPE file\n"); exit(-1); } @@ -145,17 +145,17 @@ void SHAPE::readSHAPEfile(const char *fname) { } findBegin( "StrengthFunctions" ); + eof_test = fgets( readLine, sizeof(readLine), shapeFile ); while( eof_test != NULL ){ // toss comment lines if( readLine[0] != '!' && readLine[0] != '#' ){ - - foo = strtok(readLine, " ,;\t"); - if (!strcasecmp(foo, "end")) break; + + if (isEndLine(readLine)) break; nTokens = count_tokens(readLine, " ,;\t"); if (nTokens != 4) { - printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); + printf("incorrect number of tokens on epsFunc line in SHAPE file\n"); exit(-1); } @@ -203,10 +203,6 @@ void SHAPE::findBegin( char* startText ){ } while( !foundText ){ - while( eof_test != NULL) { - eof_test = fgets( readLine, sizeof(readLine), shapeFile ); - lineNum++; - } if( eof_test == NULL ){ printf("Error fast forwarding file at line %d: " @@ -215,10 +211,10 @@ void SHAPE::findBegin( char* startText ){ } the_token = strtok( readLine, " ,;\t" ); - if (!strcasecmp("begin", the_token)) { - the_token = strtok( NULL, " ,;\t" ); - foundText = !strcmp( startText, the_token ); + if (!strcasecmp("begin", the_token)) { + the_token = TrimSpaces(strtok( NULL, " ,;\t" )); + foundText = !strcasecmp( startText, the_token ); } @@ -236,22 +232,113 @@ int SHAPE::count_tokens(char *line, char *delimiters) } } -int SHAPE::count_tokens(char *line, char *delimiters) { -/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ +int SHAPE::isEndLine(char *line) { + char *working_line; + char *foo; + working_line = strdup(line); + + foo = strtok(working_line, " ,;\t"); + + if (!strcasecmp(foo, "end")) return 1; + + return 0; +} + + +int SHAPE::count_tokens(char *line, char *delimiters) { + /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ + char *working_line; /* WORKING COPY OF LINE. */ int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ char *strtok_ptr; /* POINTER FOR STRTOK. */ - + strtok_ptr= working_line= strdup(line); - + ntokens=0; while (strtok(strtok_ptr,delimiters)!=NULL) { ntokens++; strtok_ptr=NULL; } - + free(working_line); return(ntokens); } + + +/** + * Removes left and right spaces from a string + * + * @param str String to trim + * + * @return char* to the trimed string + */ +char * SHAPE::TrimSpaces(char *str) { + size_t len; + char *right, *left; + + if (strlen(str) == 0) return(str); + + /* Trim whitespace from left side */ + for (left = str; isspace(*left); left++); + + /* Trim whitespace from right side */ + if ((len = strlen(left))) + { + right = left + (len - 1); + + while (isspace(*right)) + { + *right = '\0'; + right--; + } + } + + /* Only do the str copy if their was spaces to the left */ + if (left != str) + strcpy(str, left); + + return (str); +} + + +double SHAPE::getSigmaAt(double costheta, double phi) { + + vector::iterator sigmaIter; + double sigma; + + sigma = 0.0; + + for(sigmaIter = sigmaFuncs.begin(); sigmaIter != sigmaFuncs.end(); + ++sigmaIter) + sigma += (*sigmaIter)->getValueAt(costheta, phi); + + return sigma; +} + +double SHAPE::getSAt(double costheta, double phi) { + + vector::iterator sIter; + double s; + + s = 0.0; + + for(sIter = sFuncs.begin(); sIter != sFuncs.end(); ++sIter) + s += (*sIter)->getValueAt(costheta, phi); + + return s; +} + +double SHAPE::getEpsAt(double costheta, double phi) { + + vector::iterator epsIter; + double eps; + + eps = 0.0; + + for(epsIter = epsFuncs.begin(); epsIter != epsFuncs.end(); ++epsIter) + eps += (*epsIter)->getValueAt(costheta, phi); + + return eps; +}