| 1 | gezelter | 1290 | #include "SHAPE.hpp" | 
| 2 |  |  |  | 
| 3 |  |  | SHAPE::SHAPE(void) { | 
| 4 |  |  | int i, j; | 
| 5 |  |  |  | 
| 6 |  |  | mass = 0.0; | 
| 7 |  |  | for (i=0; i<3; i++) | 
| 8 |  |  | for (j=0; j<3; j++) | 
| 9 |  |  | I[i][j] = 0.0; | 
| 10 |  |  |  | 
| 11 |  |  | } | 
| 12 |  |  |  | 
| 13 |  |  | SHAPE::~SHAPE(void) { | 
| 14 |  |  | } | 
| 15 |  |  |  | 
| 16 |  |  | void SHAPE::getI(double theI[3][3]) { | 
| 17 |  |  | int i, j; | 
| 18 |  |  |  | 
| 19 |  |  | for (i=0; i<3; i++) | 
| 20 |  |  | for (j=0; j<3; j++) | 
| 21 |  |  | theI[i][j] = I[i][j]; | 
| 22 |  |  | } | 
| 23 |  |  |  | 
| 24 |  |  |  | 
| 25 |  |  | void SHAPE::readSHAPEfile(const char *fname) { | 
| 26 |  |  |  | 
| 27 |  |  | int lineNum, nTokens; | 
| 28 |  |  | char* eof_test; | 
| 29 |  |  | char* foo; | 
| 30 |  |  | char readLine[500]; | 
| 31 |  |  | SHFunc* shf; | 
| 32 |  |  |  | 
| 33 |  |  | shapeFile = fopen(fname, "r"); | 
| 34 |  |  | if (shapeFile == NULL) { | 
| 35 |  |  | printf("Could not open SHAPE file %s\n", fname); | 
| 36 |  |  | exit(-1); | 
| 37 |  |  | } | 
| 38 | gezelter | 1295 |  | 
| 39 |  |  | findBegin( "ShapeInfo" ); | 
| 40 |  |  |  | 
| 41 | gezelter | 1290 | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 42 | gezelter | 1295 |  | 
| 43 | gezelter | 1290 | while( eof_test != NULL ){ | 
| 44 |  |  | // toss comment lines | 
| 45 |  |  | if( readLine[0] != '!' && readLine[0] != '#' ){ | 
| 46 | gezelter | 1295 |  | 
| 47 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 48 |  |  | if (!strcasecmp(foo, "end")) break; | 
| 49 | gezelter | 1290 |  | 
| 50 |  |  | nTokens = count_tokens(readLine, " ,;\t"); | 
| 51 |  |  | if (nTokens < 5) { | 
| 52 | gezelter | 1295 | printf("Not enough data on shapeInfo line in SHAPE file.\n"); | 
| 53 | gezelter | 1290 | exit(-1); | 
| 54 |  |  | } | 
| 55 |  |  |  | 
| 56 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 57 |  |  | setType(foo); | 
| 58 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 59 |  |  | mass = atof(foo); | 
| 60 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 61 |  |  | I[0][0] = atof(foo); | 
| 62 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 63 |  |  | I[1][1] = atof(foo); | 
| 64 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 65 |  |  | I[2][2] = atof(foo); | 
| 66 |  |  | break; | 
| 67 |  |  | } | 
| 68 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 69 |  |  | } | 
| 70 |  |  |  | 
| 71 |  |  | findBegin( "ContactFunctions" ); | 
| 72 |  |  |  | 
| 73 |  |  | while( eof_test != NULL ){ | 
| 74 |  |  | // toss comment lines | 
| 75 |  |  | if( readLine[0] != '!' && readLine[0] != '#' ){ | 
| 76 |  |  |  | 
| 77 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 78 |  |  | if (!strcasecmp(foo, "end")) break; | 
| 79 |  |  |  | 
| 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); | 
| 84 |  |  | } | 
| 85 |  |  |  | 
| 86 |  |  | shf = new SHFunc(); | 
| 87 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 88 |  |  | shf->setL(atoi(foo)); | 
| 89 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 90 |  |  | shf->setM(atoi(foo)); | 
| 91 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 92 |  |  | if (!strcasecmp(foo, "cos")) | 
| 93 |  |  | shf->makeCosFunc(); | 
| 94 |  |  | else { | 
| 95 |  |  | if (!strcasecmp(foo, "sin")) | 
| 96 |  |  | shf->makeSinFunc(); | 
| 97 |  |  | else { | 
| 98 |  |  | printf("unknown  function type in shape file!"); | 
| 99 |  |  | exit(-1); | 
| 100 |  |  | } | 
| 101 |  |  | } | 
| 102 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 103 |  |  | shf->setCoefficient(atof(foo)); | 
| 104 |  |  | sigmaFuncs.push_back(shf); | 
| 105 |  |  | } | 
| 106 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 107 |  |  | } | 
| 108 |  |  |  | 
| 109 |  |  | findBegin( "RangeFunctions" ); | 
| 110 |  |  |  | 
| 111 |  |  | while( eof_test != NULL ){ | 
| 112 |  |  | // toss comment lines | 
| 113 |  |  | if( readLine[0] != '!' && readLine[0] != '#' ){ | 
| 114 |  |  |  | 
| 115 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 116 |  |  | if (!strcasecmp(foo, "end")) 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"); | 
| 121 |  |  | exit(-1); | 
| 122 |  |  | } | 
| 123 |  |  |  | 
| 124 |  |  | shf = new SHFunc(); | 
| 125 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 126 |  |  | shf->setL(atoi(foo)); | 
| 127 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 128 |  |  | shf->setM(atoi(foo)); | 
| 129 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 130 |  |  | if (!strcasecmp(foo, "cos")) | 
| 131 |  |  | shf->makeCosFunc(); | 
| 132 |  |  | else { | 
| 133 |  |  | if (!strcasecmp(foo, "sin")) | 
| 134 |  |  | shf->makeSinFunc(); | 
| 135 |  |  | else { | 
| 136 |  |  | printf("unknown  function type in shape file!"); | 
| 137 |  |  | exit(-1); | 
| 138 |  |  | } | 
| 139 |  |  | } | 
| 140 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 141 |  |  | shf->setCoefficient(atof(foo)); | 
| 142 |  |  | sFuncs.push_back(shf); | 
| 143 |  |  | } | 
| 144 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 145 |  |  | } | 
| 146 |  |  |  | 
| 147 |  |  | findBegin( "StrengthFunctions" ); | 
| 148 |  |  |  | 
| 149 |  |  | while( eof_test != NULL ){ | 
| 150 |  |  | // toss comment lines | 
| 151 |  |  | if( readLine[0] != '!' && readLine[0] != '#' ){ | 
| 152 |  |  |  | 
| 153 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 154 |  |  | if (!strcasecmp(foo, "end")) 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"); | 
| 159 |  |  | exit(-1); | 
| 160 |  |  | } | 
| 161 |  |  |  | 
| 162 |  |  | shf = new SHFunc(); | 
| 163 |  |  | foo = strtok(readLine, " ,;\t"); | 
| 164 |  |  | shf->setL(atoi(foo)); | 
| 165 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 166 |  |  | shf->setM(atoi(foo)); | 
| 167 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 168 |  |  | if (!strcasecmp(foo, "cos")) | 
| 169 |  |  | shf->makeCosFunc(); | 
| 170 |  |  | else { | 
| 171 |  |  | if (!strcasecmp(foo, "sin")) | 
| 172 |  |  | shf->makeSinFunc(); | 
| 173 |  |  | else { | 
| 174 |  |  | printf("unknown  function type in shape file!"); | 
| 175 |  |  | exit(-1); | 
| 176 |  |  | } | 
| 177 |  |  | } | 
| 178 |  |  | foo = strtok(NULL, " ,;\t"); | 
| 179 |  |  | shf->setCoefficient(atof(foo)); | 
| 180 |  |  | epsFuncs.push_back(shf); | 
| 181 |  |  | } | 
| 182 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 183 |  |  | } | 
| 184 |  |  | fclose(shapeFile); | 
| 185 |  |  | } | 
| 186 |  |  |  | 
| 187 |  |  | void SHAPE::findBegin( char* startText ){ | 
| 188 |  |  |  | 
| 189 |  |  | int foundText = 0; | 
| 190 |  |  | int lineNum; | 
| 191 |  |  | char* the_token; | 
| 192 |  |  | char* eof_test; | 
| 193 |  |  | char readLine[500]; | 
| 194 |  |  |  | 
| 195 |  |  | rewind( shapeFile ); | 
| 196 |  |  | lineNum = 0; | 
| 197 |  |  |  | 
| 198 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 199 |  |  | lineNum++; | 
| 200 |  |  | if( eof_test == NULL ){ | 
| 201 |  |  | printf( "Error fast forwarding SHAPE file: file is empty.\n"); | 
| 202 |  |  | exit(-1); | 
| 203 |  |  | } | 
| 204 |  |  |  | 
| 205 |  |  | while( !foundText ){ | 
| 206 |  |  | while( eof_test != NULL) { | 
| 207 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 208 |  |  | lineNum++; | 
| 209 |  |  | } | 
| 210 |  |  |  | 
| 211 |  |  | if( eof_test == NULL ){ | 
| 212 |  |  | printf("Error fast forwarding file at line %d: " | 
| 213 |  |  | "file ended unexpectedly.\n", lineNum); | 
| 214 |  |  | exit(-1); | 
| 215 |  |  | } | 
| 216 |  |  |  | 
| 217 |  |  | the_token = strtok( readLine, " ,;\t" ); | 
| 218 |  |  | if (!strcasecmp("begin", the_token)) { | 
| 219 |  |  |  | 
| 220 |  |  | the_token = strtok( NULL, " ,;\t" ); | 
| 221 |  |  | foundText = !strcmp( startText, the_token ); | 
| 222 |  |  |  | 
| 223 |  |  | } | 
| 224 |  |  |  | 
| 225 |  |  | if( !foundText ){ | 
| 226 |  |  | eof_test = fgets( readLine, sizeof(readLine), shapeFile ); | 
| 227 |  |  | lineNum++; | 
| 228 |  |  |  | 
| 229 |  |  |  | 
| 230 |  |  | if (eof_test == NULL) { | 
| 231 |  |  | printf("Error fast forwarding file at line %d: " | 
| 232 |  |  | "file ended unexpectedly.\n", lineNum); | 
| 233 |  |  | exit(-1); | 
| 234 |  |  | } | 
| 235 |  |  | } | 
| 236 |  |  | } | 
| 237 |  |  | } | 
| 238 |  |  |  | 
| 239 |  |  | int SHAPE::count_tokens(char *line, char *delimiters) { | 
| 240 |  |  | /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ | 
| 241 |  |  |  | 
| 242 |  |  | char *working_line;   /* WORKING COPY OF LINE. */ | 
| 243 |  |  | int ntokens;          /* NUMBER OF TOKENS FOUND IN LINE. */ | 
| 244 |  |  | char *strtok_ptr;     /* POINTER FOR STRTOK. */ | 
| 245 |  |  |  | 
| 246 |  |  | strtok_ptr= working_line= strdup(line); | 
| 247 |  |  |  | 
| 248 |  |  | ntokens=0; | 
| 249 |  |  | while (strtok(strtok_ptr,delimiters)!=NULL) | 
| 250 |  |  | { | 
| 251 |  |  | ntokens++; | 
| 252 |  |  | strtok_ptr=NULL; | 
| 253 |  |  | } | 
| 254 |  |  |  | 
| 255 |  |  | free(working_line); | 
| 256 |  |  | return(ntokens); | 
| 257 |  |  | } |