| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
#include <vjConfig.h> |
|---|
| 36 |
#include <ctype.h> |
|---|
| 37 |
#include <Config/vjParseUtil.h> |
|---|
| 38 |
#include <Kernel/vjDebug.h> |
|---|
| 39 |
#include <Config/vjConfigTokens.h> |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
bool readString (std::istream &in, char *buffer, int size, bool *quoted) { |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
size = size-5; |
|---|
| 53 |
int i; |
|---|
| 54 |
bool retval = false; |
|---|
| 55 |
char c, vj; |
|---|
| 56 |
buffer[0] = '\0'; |
|---|
| 57 |
|
|---|
| 58 |
if (quoted) |
|---|
| 59 |
*quoted = false; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
for (;;) { |
|---|
| 63 |
if (!in.get(c)) |
|---|
| 64 |
break; |
|---|
| 65 |
if (isspace(c)) |
|---|
| 66 |
continue; |
|---|
| 67 |
if (c == '#') { |
|---|
| 68 |
while (c != '\n') |
|---|
| 69 |
in.get(c); |
|---|
| 70 |
continue; |
|---|
| 71 |
} |
|---|
| 72 |
if ((c == '/') && in.get(vj)) { |
|---|
| 73 |
|
|---|
| 74 |
if (vj == '/') { |
|---|
| 75 |
|
|---|
| 76 |
while (c != '\n') |
|---|
| 77 |
if (!in.get(c)) |
|---|
| 78 |
break; |
|---|
| 79 |
continue; |
|---|
| 80 |
} |
|---|
| 81 |
else if (vj == '*') { |
|---|
| 82 |
|
|---|
| 83 |
while (true) { |
|---|
| 84 |
if (!in.get(vj)) |
|---|
| 85 |
break; |
|---|
| 86 |
if (vj == '*') { |
|---|
| 87 |
in.get(vj); |
|---|
| 88 |
if (vj == '/') |
|---|
| 89 |
break; |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
continue; |
|---|
| 93 |
} |
|---|
| 94 |
else |
|---|
| 95 |
in.putback(vj); |
|---|
| 96 |
} |
|---|
| 97 |
break; |
|---|
| 98 |
} |
|---|
| 99 |
buffer[0] = c; |
|---|
| 100 |
|
|---|
| 101 |
if ((buffer[0] == '{') || (buffer[0] == '}')) { |
|---|
| 102 |
buffer[1] = '\0'; |
|---|
| 103 |
retval = true; |
|---|
| 104 |
} |
|---|
| 105 |
else if (buffer[0] == '"') { |
|---|
| 106 |
|
|---|
| 107 |
if (quoted) |
|---|
| 108 |
*quoted = true; |
|---|
| 109 |
for (i = 0; i < size; i++) { |
|---|
| 110 |
in.get(buffer[i]); |
|---|
| 111 |
if (buffer[i] == '"') { |
|---|
| 112 |
buffer[i] = '\0'; |
|---|
| 113 |
break; |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
if (i == size) { |
|---|
| 117 |
i--; |
|---|
| 118 |
while (in.get(buffer[i]) && (buffer[i] != '"')) |
|---|
| 119 |
; |
|---|
| 120 |
buffer[i] = '\0'; |
|---|
| 121 |
vjDEBUG (vjDBG_ERROR,0) << clrOutNORM(clrRED, "ERROR:") << " Truncated string in config file: '" |
|---|
| 122 |
<< buffer << "'\n" << vjDEBUG_FLUSH; |
|---|
| 123 |
} |
|---|
| 124 |
retval = true; |
|---|
| 125 |
} |
|---|
| 126 |
else { |
|---|
| 127 |
|
|---|
| 128 |
for (i = 1; i < size-1; i++) { |
|---|
| 129 |
in.get(buffer[i]); |
|---|
| 130 |
if (buffer[i] == '}') { |
|---|
| 131 |
|
|---|
| 132 |
in.putback(buffer[i]); |
|---|
| 133 |
break; |
|---|
| 134 |
} |
|---|
| 135 |
if (isspace(buffer[i])) |
|---|
| 136 |
break; |
|---|
| 137 |
} |
|---|
| 138 |
buffer[i] = '\0'; |
|---|
| 139 |
retval = true; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
if (!retval) |
|---|
| 143 |
buffer[0] = '\0'; |
|---|
| 144 |
return retval; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
VarType readType (std::istream &in) { |
|---|
| 150 |
char str[256]; |
|---|
| 151 |
|
|---|
| 152 |
if (!readString (in, str, 256)) |
|---|
| 153 |
return T_INVALID; |
|---|
| 154 |
|
|---|
| 155 |
if (!strcasecmp (str, int_TOKEN)) |
|---|
| 156 |
return T_INT; |
|---|
| 157 |
if (!strcasecmp (str, integer_TOKEN)) |
|---|
| 158 |
return T_INT; |
|---|
| 159 |
if (!strcasecmp (str, float_TOKEN)) |
|---|
| 160 |
return T_FLOAT; |
|---|
| 161 |
if (!strcasecmp (str, bool_TOKEN)) |
|---|
| 162 |
return T_BOOL; |
|---|
| 163 |
if (!strcasecmp (str, boolean_TOKEN)) |
|---|
| 164 |
return T_BOOL; |
|---|
| 165 |
if (!strcasecmp (str, string_TOKEN)) |
|---|
| 166 |
return T_STRING; |
|---|
| 167 |
if (!strcasecmp (str, distance_TOKEN)) |
|---|
| 168 |
return T_DISTANCE; |
|---|
| 169 |
if (!strcasecmp (str, chunk_TOKEN)) |
|---|
| 170 |
return T_CHUNK; |
|---|
| 171 |
if (!strcasecmp (str, embeddedchunk_TOKEN)) |
|---|
| 172 |
return T_EMBEDDEDCHUNK; |
|---|
| 173 |
|
|---|
| 174 |
return T_INVALID; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
char *typeString (VarType t) { |
|---|
| 180 |
switch (t) { |
|---|
| 181 |
case T_INT: |
|---|
| 182 |
return "Int"; |
|---|
| 183 |
case T_BOOL: |
|---|
| 184 |
return "Bool"; |
|---|
| 185 |
case T_FLOAT: |
|---|
| 186 |
return "Float"; |
|---|
| 187 |
case T_STRING: |
|---|
| 188 |
return "String"; |
|---|
| 189 |
case T_CHUNK: |
|---|
| 190 |
return "Chunk"; |
|---|
| 191 |
case T_EMBEDDEDCHUNK: |
|---|
| 192 |
return "EmbeddedChunk"; |
|---|
| 193 |
default: |
|---|
| 194 |
return "Unrecognized_Type"; |
|---|
| 195 |
} |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
char *unitString (CfgUnit t) { |
|---|
| 201 |
switch (t) { |
|---|
| 202 |
case U_Feet: |
|---|
| 203 |
return "Feet"; |
|---|
| 204 |
case U_Inches: |
|---|
| 205 |
return "Inches"; |
|---|
| 206 |
case U_Meters: |
|---|
| 207 |
return "Meters"; |
|---|
| 208 |
case U_Centimeters: |
|---|
| 209 |
return "Centimeters"; |
|---|
| 210 |
default: |
|---|
| 211 |
return "Invalid Unit Type"; |
|---|
| 212 |
} |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
float toFeet (float val, CfgUnit unit) { |
|---|
| 216 |
switch (unit) { |
|---|
| 217 |
case U_Feet: |
|---|
| 218 |
return val; |
|---|
| 219 |
case U_Inches: |
|---|
| 220 |
return (val/12); |
|---|
| 221 |
case U_Meters: |
|---|
| 222 |
return (val*3.28084); |
|---|
| 223 |
case U_Centimeters: |
|---|
| 224 |
return (val*0.0328084); |
|---|
| 225 |
default: |
|---|
| 226 |
return val; |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
bool vjstrcasecmp (const std::string& a, const std::string& b) { |
|---|
| 232 |
if (a.size() != b.size()) |
|---|
| 233 |
return true; |
|---|
| 234 |
for (unsigned int i = 0; i < a.size(); i++) |
|---|
| 235 |
if (toupper(a[i]) != toupper(b[i])) |
|---|
| 236 |
return true; |
|---|
| 237 |
return false; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
bool vjstrncasecmp (const std::string& a, const std::string& b, int _n) { |
|---|
| 242 |
|
|---|
| 243 |
int n = VJ_MIN2 (a.size(), b.size()); |
|---|
| 244 |
if (_n >= 0) |
|---|
| 245 |
n = VJ_MIN2 (n, _n); |
|---|
| 246 |
|
|---|
| 247 |
for (int i = 0; i < n; i++) |
|---|
| 248 |
if (toupper(a[i]) != toupper(b[i])) |
|---|
| 249 |
return true; |
|---|
| 250 |
return false; |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
bool vjstrncmp (const std::string& a, const std::string& b, int _n) { |
|---|
| 256 |
|
|---|
| 257 |
int n = VJ_MIN2 (a.size(), b.size()); |
|---|
| 258 |
if (_n >= 0) |
|---|
| 259 |
n = VJ_MIN2 (n, _n); |
|---|
| 260 |
|
|---|
| 261 |
for (int i = 0; i < n; i++) |
|---|
| 262 |
if (a[i] != b[i]) |
|---|
| 263 |
return true; |
|---|
| 264 |
return false; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
std::string replaceEnvVars (const std::string& s) { |
|---|
| 273 |
unsigned int i, j; |
|---|
| 274 |
int lastpos = 0; |
|---|
| 275 |
std::string result = ""; |
|---|
| 276 |
for (i = 0; i < s.length(); i++) { |
|---|
| 277 |
if (s[i] == '$') { |
|---|
| 278 |
|
|---|
| 279 |
result += std::string(s, lastpos, i - lastpos); |
|---|
| 280 |
i++; |
|---|
| 281 |
if (s[i] == '{') { |
|---|
| 282 |
for (j = i; j < s.length(); j++) |
|---|
| 283 |
if (s[j] == '}') |
|---|
| 284 |
break; |
|---|
| 285 |
std::string var(s,i+1,j-i-1); |
|---|
| 286 |
|
|---|
| 287 |
std::string res; |
|---|
| 288 |
char* env = getenv(var.c_str()); |
|---|
| 289 |
if (env) |
|---|
| 290 |
res = env; |
|---|
| 291 |
result += res; |
|---|
| 292 |
i = j+1; |
|---|
| 293 |
lastpos = i; |
|---|
| 294 |
} |
|---|
| 295 |
else { |
|---|
| 296 |
for (j = i; j < s.length(); j++) |
|---|
| 297 |
if (s[j] == '/' || s[j] == '\\') |
|---|
| 298 |
break; |
|---|
| 299 |
std::string var(s,i,j-i); |
|---|
| 300 |
|
|---|
| 301 |
std::string res; |
|---|
| 302 |
char* env = getenv(var.c_str()); |
|---|
| 303 |
if (env) |
|---|
| 304 |
res = env; |
|---|
| 305 |
result += res; |
|---|
| 306 |
i = j; |
|---|
| 307 |
lastpos = i; |
|---|
| 308 |
} |
|---|
| 309 |
} |
|---|
| 310 |
} |
|---|
| 311 |
result += std::string(s, lastpos, s.length() - lastpos); |
|---|
| 312 |
return result; |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
bool isAbsolutePathName (const std::string& n) { |
|---|
| 319 |
#ifdef WIN32 |
|---|
| 320 |
return ((n.length() > 0) && (n[0] == '\\')) |
|---|
| 321 |
|| ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\')); |
|---|
| 322 |
#else |
|---|
| 323 |
return (n.length() > 0) && (n[0] == '/'); |
|---|
| 324 |
#endif |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
std::string demangleFileName (const std::string& n, std::string parentfile) { |
|---|
| 330 |
|
|---|
| 331 |
std::string fname = replaceEnvVars (n); |
|---|
| 332 |
|
|---|
| 333 |
if (!isAbsolutePathName(fname)) { |
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
int lastslash = 0; |
|---|
| 339 |
for (unsigned int i = 0; i < parentfile.length(); i++) { |
|---|
| 340 |
if (parentfile[i] == '/') |
|---|
| 341 |
lastslash = i; |
|---|
| 342 |
#ifdef WIN32 |
|---|
| 343 |
if (parentfile[i] == '\\') |
|---|
| 344 |
lastslash = i; |
|---|
| 345 |
#endif |
|---|
| 346 |
} |
|---|
| 347 |
if (lastslash) { |
|---|
| 348 |
std::string s(parentfile, 0, lastslash+1); |
|---|
| 349 |
fname = s + fname; |
|---|
| 350 |
} |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
return fname; |
|---|
| 354 |
} |
|---|