| 1 |
/************************************************************************* |
| 2 |
* * |
| 3 |
* Vega FEM Simulation Library Version 3.1 * |
| 4 |
* * |
| 5 |
* "volumetricMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 |
* All rights reserved. * |
| 7 |
* * |
| 8 |
* Code author: Jernej Barbic * |
| 9 |
* http://www.jernejbarbic.com/code * |
| 10 |
* * |
| 11 |
* Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, * |
| 12 |
* Doug L. James, Jovan Popovic * |
| 13 |
* * |
| 14 |
* Funding: National Science Foundation, Link Foundation, * |
| 15 |
* Singapore-MIT GAMBIT Game Lab, * |
| 16 |
* Zumberge Research and Innovation Fund at USC * |
| 17 |
* * |
| 18 |
* This library is free software; you can redistribute it and/or * |
| 19 |
* modify it under the terms of the BSD-style license that is * |
| 20 |
* included with this library in the file LICENSE.txt * |
| 21 |
* * |
| 22 |
* This library is distributed in the hope that it will be useful, * |
| 23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 25 |
* LICENSE.TXT for more details. * |
| 26 |
* * |
| 27 |
*************************************************************************/ |
| 28 |
|
| 29 |
#include <string.h> |
| 30 |
#include "volumetricMeshParser.h" |
| 31 |
|
| 32 |
VolumetricMeshParser::VolumetricMeshParser(const char * includeToken_) |
| 33 |
{ |
| 34 |
fin = NULL; |
| 35 |
//fileStackDepth = -1; |
| 36 |
fileStack.empty(); |
| 37 |
if (includeToken_ == NULL) |
| 38 |
{ |
| 39 |
includeTokenLength = 9; |
| 40 |
strcpy(includeToken, "*INCLUDE "); |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
includeTokenLength = strlen(includeToken_); |
| 45 |
strcpy(includeToken, includeToken_); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
int VolumetricMeshParser::open(const char * filename) |
| 50 |
{ |
| 51 |
// extract directory name and filename |
| 52 |
// seek for last '/' in filename |
| 53 |
// if no '/', then directory name is "." |
| 54 |
// else, everything before '/' is directory name, and everything after is filename |
| 55 |
|
| 56 |
const char * iter = filename; |
| 57 |
const char * lastPos = NULL; |
| 58 |
|
| 59 |
while (*iter != '\0') |
| 60 |
{ |
| 61 |
if ((*iter == '/') || (*iter == '\\')) |
| 62 |
{ |
| 63 |
lastPos = iter; |
| 64 |
} |
| 65 |
iter++; |
| 66 |
} |
| 67 |
|
| 68 |
// lastPos is NULL if no slash found, otherwise it points to the last slash |
| 69 |
if (lastPos == NULL) |
| 70 |
{ |
| 71 |
directoryName[0] = '.'; |
| 72 |
directoryName[1] = '\0'; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
iter = filename; |
| 77 |
int charPos = 0; |
| 78 |
while (iter != lastPos) |
| 79 |
{ |
| 80 |
directoryName[charPos] = *iter; |
| 81 |
charPos++; |
| 82 |
iter++; |
| 83 |
} |
| 84 |
directoryName[charPos] = '\0'; |
| 85 |
} |
| 86 |
|
| 87 |
// now, directoryName has been extracted |
| 88 |
fin = fopen(filename, "r"); |
| 89 |
if (!fin) |
| 90 |
return 1; |
| 91 |
|
| 92 |
//fileStackDepth=0; |
| 93 |
fileStack.push_back(fin); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
VolumetricMeshParser::~VolumetricMeshParser() |
| 99 |
{ |
| 100 |
close(); |
| 101 |
} |
| 102 |
|
| 103 |
void VolumetricMeshParser::rewindToStart() |
| 104 |
{ |
| 105 |
if (fileStack.empty()) // no files currently opened |
| 106 |
return; |
| 107 |
|
| 108 |
while (fileStack.size() > 1) |
| 109 |
{ |
| 110 |
fclose(fileStack[fileStack.size() - 1]); |
| 111 |
fileStack.pop_back(); |
| 112 |
} |
| 113 |
|
| 114 |
// now, we have fileStackDepth == 0 |
| 115 |
fin = fileStack[0]; |
| 116 |
rewind(fin); |
| 117 |
} |
| 118 |
|
| 119 |
void VolumetricMeshParser::close() |
| 120 |
{ |
| 121 |
while (fileStack.size() > 0) |
| 122 |
{ |
| 123 |
fclose(fileStack[fileStack.size() - 1]); |
| 124 |
fileStack.pop_back(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void VolumetricMeshParser::beautifyLine(char * s, int numRetainedSpaces, int removeWhitespace_) |
| 129 |
{ |
| 130 |
if (removeWhitespace_) |
| 131 |
removeWhitespace(s, numRetainedSpaces); |
| 132 |
|
| 133 |
//upperCase(s); |
| 134 |
|
| 135 |
// remove trailing '\n' |
| 136 |
char * pos = s; |
| 137 |
while( *pos != '\0' ) |
| 138 |
pos++; |
| 139 |
|
| 140 |
if ( pos != s ) // string is not zero length |
| 141 |
{ |
| 142 |
if( *(pos-1) == '\n' ) |
| 143 |
*(pos-1) = '\0'; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
char * VolumetricMeshParser::getNextLine(char * s, int numRetainedSpaces, int removeWhitespace_) |
| 148 |
{ |
| 149 |
char * code; |
| 150 |
do |
| 151 |
{ |
| 152 |
while (((code = fgets(s, 4096, fin)) == NULL) && (fileStack.size() > 1)) // if EOF, pop previous file from stack |
| 153 |
{ |
| 154 |
fclose(fin); |
| 155 |
fileStack.pop_back(); |
| 156 |
fin = fileStack[fileStack.size() - 1]; |
| 157 |
} |
| 158 |
|
| 159 |
if (code == NULL) // reached end of main file |
| 160 |
{ |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
} |
| 164 |
while ((s[0] == '#') || (s[0] == 13) || (s[0] == 10)); // ignore comments and blank lines |
| 165 |
|
| 166 |
beautifyLine(s, numRetainedSpaces, removeWhitespace_); |
| 167 |
|
| 168 |
// handle the case where input is specified via *INCLUDE |
| 169 |
while(strncmp(s, includeToken, includeTokenLength) == 0) |
| 170 |
{ |
| 171 |
// open up the new file |
| 172 |
char * newFile = &(s[includeTokenLength]); |
| 173 |
char newFileCompleteName[4096]; |
| 174 |
sprintf(newFileCompleteName,"%s/%s",directoryName,newFile); |
| 175 |
|
| 176 |
FILE * finNew = fopen(newFileCompleteName, "r"); |
| 177 |
|
| 178 |
if (!finNew) |
| 179 |
{ |
| 180 |
printf("Error: couldn't open include file %s.\n", newFileCompleteName); |
| 181 |
//exit(1); |
| 182 |
close(); |
| 183 |
throw -1; |
| 184 |
} |
| 185 |
|
| 186 |
if ((code = fgets(s,4096,finNew)) != NULL) // new file is not empty |
| 187 |
{ |
| 188 |
beautifyLine(s, numRetainedSpaces, removeWhitespace_); |
| 189 |
|
| 190 |
// register the new file |
| 191 |
fileStack.push_back(finNew); |
| 192 |
fin = finNew; |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
fclose(finNew); |
| 197 |
printf("Warning: include file is empty.\n"); |
| 198 |
code = getNextLine(s, numRetainedSpaces); |
| 199 |
beautifyLine(s, numRetainedSpaces, removeWhitespace_); |
| 200 |
return code; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
//printf("%s\n", s); |
| 205 |
|
| 206 |
return code; |
| 207 |
} |
| 208 |
|
| 209 |
// convert string to uppercase |
| 210 |
void VolumetricMeshParser::upperCase(char * s) |
| 211 |
{ |
| 212 |
char caseDifference = 'A' - 'a'; |
| 213 |
for(unsigned int i=0; i<strlen(s); i++) |
| 214 |
{ |
| 215 |
if ((s[i] >= 'a') && (s[i] <= 'z')) |
| 216 |
s[i] += caseDifference; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// erases whitespace from string s |
| 221 |
// retains the first "numRetainedSpaces" spaces |
| 222 |
void VolumetricMeshParser::removeWhitespace(char * s, int numRetainedSpaces) |
| 223 |
{ |
| 224 |
char * p = s; |
| 225 |
while (*p != 0) |
| 226 |
{ |
| 227 |
while (1) |
| 228 |
{ |
| 229 |
bool eraseCharacter = (*p == ' '); |
| 230 |
for(int i=1; i<=numRetainedSpaces; i++) |
| 231 |
{ |
| 232 |
if (*(p + i) == 0) |
| 233 |
{ |
| 234 |
eraseCharacter = false; |
| 235 |
break; |
| 236 |
} |
| 237 |
eraseCharacter = (eraseCharacter && ((p == s) || (*(p + i) == ' '))); // always erase spaces at the beginning of line |
| 238 |
} |
| 239 |
|
| 240 |
if (!eraseCharacter) |
| 241 |
break; |
| 242 |
|
| 243 |
// erase the empty space |
| 244 |
char * q = p; |
| 245 |
while (*q != 0) // move all subsequent characters to the left |
| 246 |
{ |
| 247 |
*q = *(q + 1); // copy the next character into the current place |
| 248 |
q++; |
| 249 |
} |
| 250 |
} |
| 251 |
p++; |
| 252 |
} |
| 253 |
} |
| 254 |
|