| 1 |
/* |
| 2 |
Copyright (C) 1993, 1994, RSNA and Washington University |
| 3 |
|
| 4 |
The software and supporting documentation for the Radiological |
| 5 |
Society of North America (RSNA) 1993, 1994 Digital Imaging and |
| 6 |
Communications in Medicine (DICOM) Demonstration were developed |
| 7 |
at the |
| 8 |
Electronic Radiology Laboratory |
| 9 |
Mallinckrodt Institute of Radiology |
| 10 |
Washington University School of Medicine |
| 11 |
510 S. Kingshighway Blvd. |
| 12 |
St. Louis, MO 63110 |
| 13 |
as part of the 1993, 1994 DICOM Central Test Node project for, and |
| 14 |
under contract with, the Radiological Society of North America. |
| 15 |
|
| 16 |
THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND NEITHER RSNA NOR |
| 17 |
WASHINGTON UNIVERSITY MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS |
| 18 |
PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR |
| 19 |
USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY |
| 20 |
SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF |
| 21 |
THE SOFTWARE IS WITH THE USER. |
| 22 |
|
| 23 |
Copyright of the software and supporting documentation is |
| 24 |
jointly owned by RSNA and Washington University, and free access |
| 25 |
is hereby granted as a license to use this software, copy this |
| 26 |
software and prepare derivative works based upon this software. |
| 27 |
However, any distribution of this software source code or |
| 28 |
supporting documentation or derivative works (source code and |
| 29 |
supporting documentation) must include the three paragraphs of |
| 30 |
the copyright notice. |
| 31 |
*/ |
| 32 |
/* Copyright marker. Copyright will be inserted above. Do not remove */ |
| 33 |
|
| 34 |
/* |
| 35 |
** Electronic Radiology Laboratory |
| 36 |
** Mallinckrodt Institute of Radiology |
| 37 |
** Washington University School of Medicine |
| 38 |
** |
| 39 |
** Module Name(s): UTL_RegexMatch, UTL_ConvertRegex, |
| 40 |
** UTL_ConvertDatetoLong, UTL_ConvertLongtoDate, |
| 41 |
** UTL_ConvertTimetoFloat, UTL_ConvertFloattoTime, |
| 42 |
** UTL_SqueezeBlanks, UTL_DateMatch, UTL_TimeMatch |
| 43 |
** UTL_GetDicomDate, UTL_GetDicomTime |
| 44 |
** |
| 45 |
** Author, Date: David E. Beecher, March 1994 |
| 46 |
** Intent: Miscellaneous functions that may be useful in |
| 47 |
** a number of different areas. |
| 48 |
** |
| 49 |
** Last Update: $Author: smm $, $Date: 2002-05-13 20:33:01 $ |
| 50 |
** Source File: $RCSfile: utility.c,v $ |
| 51 |
** Revision: $Revision: 1.26 $ |
| 52 |
** Status: $State: Exp $ |
| 53 |
*/ |
| 54 |
|
| 55 |
#include "ctn_os.h" |
| 56 |
|
| 57 |
#include "dicom.h" |
| 58 |
#include "lst.h" |
| 59 |
#include "condition.h" |
| 60 |
|
| 61 |
#include "utility.h" |
| 62 |
#ifndef _MSC_VER |
| 63 |
/* UTL_RegexMatch |
| 64 |
** |
| 65 |
** Purpose: |
| 66 |
** Perform a DICOM regular expression match with the specified string, stm. |
| 67 |
** |
| 68 |
** Parameter Dictionary: |
| 69 |
** char *regex: |
| 70 |
** The DICOM regular expression to try and match. |
| 71 |
** char *stm: |
| 72 |
** The input string to match. |
| 73 |
** |
| 74 |
** Return Values: |
| 75 |
** UTL_MATCH: The input string matched the regular expression. |
| 76 |
** UTL_NOMATCH: The input string did not match the regular expression. |
| 77 |
** |
| 78 |
** Algorithm: |
| 79 |
** A simple function to perform a DICOM regular expression match with the |
| 80 |
** specified string, stm. The sematics of the DICOM patterns must be altered |
| 81 |
** slightly to work correctly with regex under unix...more information may |
| 82 |
** be found below. |
| 83 |
** |
| 84 |
*/ |
| 85 |
|
| 86 |
CONDITION |
| 87 |
UTL_RegexMatch(char *regex, char *stm) |
| 88 |
{ |
| 89 |
#ifdef USEREGCOMP |
| 90 |
|
| 91 |
int |
| 92 |
ret, |
| 93 |
regexReturn; |
| 94 |
char |
| 95 |
*new_rstring; |
| 96 |
regex_t preg; |
| 97 |
char errorBuff[256]; |
| 98 |
regmatch_t pmatch; |
| 99 |
|
| 100 |
new_rstring = UTL_ConvertRegex(regex); |
| 101 |
|
| 102 |
regexReturn = regcomp(&preg, new_rstring, 0); |
| 103 |
if (regexReturn != 0) { |
| 104 |
regerror(regexReturn, &preg, errorBuff, sizeof(errorBuff)); |
| 105 |
fprintf(stderr, "%d\n", regexReturn); |
| 106 |
fprintf(stderr, "%s\n", errorBuff); |
| 107 |
|
| 108 |
free(new_rstring); |
| 109 |
return (UTL_NOMATCH); |
| 110 |
} else { |
| 111 |
ret = regexec(&preg, stm, 1, &pmatch, 0); |
| 112 |
|
| 113 |
switch (ret) { |
| 114 |
case 0: |
| 115 |
free(new_rstring); |
| 116 |
return (UTL_MATCH); |
| 117 |
break; |
| 118 |
default: |
| 119 |
free(new_rstring); |
| 120 |
return (UTL_NOMATCH); |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
#else |
| 125 |
int |
| 126 |
ret; |
| 127 |
char |
| 128 |
*new_rstring; |
| 129 |
|
| 130 |
new_rstring = UTL_ConvertRegex(regex); |
| 131 |
if (re_comp(new_rstring) != (char *) 0) { |
| 132 |
free(new_rstring); |
| 133 |
return (UTL_NOMATCH); |
| 134 |
} else { |
| 135 |
ret = re_exec(stm); |
| 136 |
switch (ret) { |
| 137 |
case 0: |
| 138 |
case -1: |
| 139 |
free(new_rstring); |
| 140 |
return (UTL_NOMATCH); |
| 141 |
break; |
| 142 |
case 1: |
| 143 |
free(new_rstring); |
| 144 |
return (UTL_MATCH); |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
/* UTL_ConvertRegex |
| 152 |
** |
| 153 |
** Purpose: |
| 154 |
** This function converts a DICOM "regular expression" to the proper |
| 155 |
** regex semantics under unix. |
| 156 |
** |
| 157 |
** Parameter Dictionary: |
| 158 |
** char *regex: |
| 159 |
** The DICOM regular expression to convert. |
| 160 |
** |
| 161 |
** Return Values: |
| 162 |
** char *: The converted regular expression which expresses DICOM pattern |
| 163 |
** matching in regex semantics. |
| 164 |
** |
| 165 |
** Notes: |
| 166 |
** This routine needs to return a string of unknown length. Since we |
| 167 |
** don't want to burden the caller with having to remember to free the |
| 168 |
** string after it has been used, we simply reuse the same piece of storage |
| 169 |
** and realloc it when necessary to increase the size. |
| 170 |
** |
| 171 |
** Algorithm: |
| 172 |
** Simple function to convert a DICOM "regular expression" to the proper |
| 173 |
** regex semantics under unix. DICOM has only 2 meta characters, "*" for 0 |
| 174 |
** or more occurrences, and "?" for a single character. The "*" must be |
| 175 |
** converted to ".*" for regex while the "?" must be converted to ".". |
| 176 |
** Other special characters to regex like "[", "]", and "." must also |
| 177 |
** be escaped with the "\". The DICOM escape character is assumed to be "\". |
| 178 |
*/ |
| 179 |
char * |
| 180 |
UTL_ConvertRegex(char *regex) |
| 181 |
{ |
| 182 |
|
| 183 |
char |
| 184 |
*new_regex = (char *) NULL; |
| 185 |
int |
| 186 |
malloced_size = 0; |
| 187 |
int |
| 188 |
i, |
| 189 |
j, |
| 190 |
escape_on; |
| 191 |
|
| 192 |
if (new_regex == (char *) NULL) { |
| 193 |
malloced_size = REGEX_SIZE; |
| 194 |
if ((new_regex = (char *) malloc(malloced_size)) == (char *) NULL) { |
| 195 |
return ((char *) NULL); |
| 196 |
} |
| 197 |
} |
| 198 |
i = j = 0; |
| 199 |
escape_on = OFF; |
| 200 |
new_regex[j++] = '^'; |
| 201 |
while (regex[i] != '\000') { |
| 202 |
switch (regex[i]) { |
| 203 |
case '*': /* Transform the "*" to ".*" or "\*" if |
| 204 |
* escaped */ |
| 205 |
switch (escape_on) { |
| 206 |
case OFF: |
| 207 |
new_regex[j++] = '.'; |
| 208 |
break; |
| 209 |
case ON: |
| 210 |
new_regex[j++] = '\\'; |
| 211 |
escape_on = OFF; |
| 212 |
break; |
| 213 |
} |
| 214 |
new_regex[j++] = '*'; |
| 215 |
i++; |
| 216 |
break; |
| 217 |
case '?': /* Transform the "?" to "." or "?" if escaped */ |
| 218 |
switch (escape_on) { |
| 219 |
case OFF: |
| 220 |
new_regex[j++] = '.'; |
| 221 |
break; |
| 222 |
case ON: |
| 223 |
new_regex[j++] = '?'; |
| 224 |
escape_on = OFF; |
| 225 |
break; |
| 226 |
} |
| 227 |
i++; |
| 228 |
break; |
| 229 |
case '\\': /* Note that we have seen the escape |
| 230 |
* character */ |
| 231 |
switch (escape_on) { |
| 232 |
case OFF: |
| 233 |
escape_on = ON; |
| 234 |
break; |
| 235 |
case ON: |
| 236 |
escape_on = OFF; |
| 237 |
new_regex[j++] = '\\'; |
| 238 |
new_regex[j++] = '\\'; |
| 239 |
break; |
| 240 |
} |
| 241 |
i++; |
| 242 |
break; |
| 243 |
case '.': |
| 244 |
case '[': /* These are special to regex and need to be |
| 245 |
* escaped */ |
| 246 |
case ']': |
| 247 |
new_regex[j++] = '\\'; |
| 248 |
new_regex[j++] = regex[i++]; |
| 249 |
escape_on = OFF; |
| 250 |
break; |
| 251 |
default: /* Leave the "\" in at this juncture */ |
| 252 |
switch (escape_on) { |
| 253 |
case ON: |
| 254 |
new_regex[j++] = '\\'; |
| 255 |
escape_on = OFF; |
| 256 |
break; |
| 257 |
case OFF: |
| 258 |
break; |
| 259 |
} |
| 260 |
new_regex[j++] = regex[i++]; |
| 261 |
break; |
| 262 |
} |
| 263 |
if (j >= (malloced_size - 2)) { |
| 264 |
malloced_size += REGEX_SIZE; |
| 265 |
if ((new_regex = (char *) realloc(new_regex, malloced_size)) == |
| 266 |
(char *) NULL) { |
| 267 |
return ((char *) NULL); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
new_regex[j++] = '$'; |
| 272 |
new_regex[j] = '\000'; |
| 273 |
return (new_regex); |
| 274 |
} |
| 275 |
#endif |
| 276 |
/* UTL_ConvertDatetoLong |
| 277 |
** Convert a Dicom date to a long for comparision ease. |
| 278 |
*/ |
| 279 |
long |
| 280 |
UTL_ConvertDatetoLong(const char *date) |
| 281 |
{ |
| 282 |
|
| 283 |
char |
| 284 |
year[5], |
| 285 |
month[3], |
| 286 |
day[3]; |
| 287 |
|
| 288 |
strncpy(year, date, 4); |
| 289 |
year[4] = '\000'; |
| 290 |
strncpy(month, date + 4, 2); |
| 291 |
month[2] = '\000'; |
| 292 |
strncpy(day, date + 6, 2); |
| 293 |
day[2] = '\000'; |
| 294 |
|
| 295 |
return ((atol(year) * 10000) + (atol(month) * 100) + atol(day)); |
| 296 |
} |
| 297 |
|
| 298 |
/* UTL_ConvertLongtoDate |
| 299 |
** Convert a long to a Dicom date. |
| 300 |
*/ |
| 301 |
void |
| 302 |
UTL_ConvertLongtoDate(long ld, char *date) |
| 303 |
{ |
| 304 |
|
| 305 |
int |
| 306 |
year, |
| 307 |
month, |
| 308 |
day; |
| 309 |
|
| 310 |
year = ld / 10000; |
| 311 |
ld -= (year * 10000); |
| 312 |
month = ld / 100; |
| 313 |
ld -= (month * 100); |
| 314 |
day = ld; |
| 315 |
|
| 316 |
sprintf(date, "%04d%02d%02d", year, month, day); |
| 317 |
|
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
/* UTL_ConvertTimetoFloat |
| 322 |
** Convert a Dicom time to a floating point number for comparision ease. |
| 323 |
*/ |
| 324 |
double |
| 325 |
UTL_ConvertTimetoFloat(const char *time) |
| 326 |
{ |
| 327 |
|
| 328 |
size_t |
| 329 |
i; |
| 330 |
char |
| 331 |
hour[3], |
| 332 |
minute[3], |
| 333 |
second[3], |
| 334 |
fracsec[7]; |
| 335 |
const char *p; |
| 336 |
double |
| 337 |
divisor, |
| 338 |
hh, |
| 339 |
mm, |
| 340 |
ss, |
| 341 |
fs; |
| 342 |
|
| 343 |
hh = mm = ss = fs = 0.0; |
| 344 |
hour[0] = minute[0] = second[0] = fracsec[0] = '\000'; |
| 345 |
|
| 346 |
p = time; |
| 347 |
/* |
| 348 |
* Just a brute force way to tear down a Dicom time...not very pretty, |
| 349 |
* but it works... We are not guaranteed to have every field present as |
| 350 |
* we are in the date... |
| 351 |
*/ |
| 352 |
hour[0] = *p++; |
| 353 |
hour[1] = *p++; |
| 354 |
hour[2] = '\000'; |
| 355 |
if (isdigit(*p)) { |
| 356 |
minute[0] = *p++; |
| 357 |
minute[1] = *p++; |
| 358 |
minute[2] = '\000'; |
| 359 |
if (isdigit(*p)) { |
| 360 |
second[0] = *p++; |
| 361 |
second[1] = *p++; |
| 362 |
second[2] = '\000'; |
| 363 |
if (*p == '.') { |
| 364 |
p++; |
| 365 |
fracsec[0] = *p++; |
| 366 |
if ((*p != '\000') && (isdigit(*p))) { |
| 367 |
fracsec[1] = *p++; |
| 368 |
if ((*p != '\000') && (isdigit(*p))) { |
| 369 |
fracsec[2] = *p++; |
| 370 |
if ((*p != '\000') && (isdigit(*p))) { |
| 371 |
fracsec[3] = *p++; |
| 372 |
if ((*p != '\000') && (isdigit(*p))) { |
| 373 |
fracsec[4] = *p++; |
| 374 |
if ((*p != '\000') && (isdigit(*p))) { |
| 375 |
fracsec[5] = *p++; |
| 376 |
fracsec[6] = '\000'; |
| 377 |
} else |
| 378 |
fracsec[5] = '\000'; |
| 379 |
} else |
| 380 |
fracsec[4] = '\000'; |
| 381 |
} else |
| 382 |
fracsec[3] = '\000'; |
| 383 |
} else |
| 384 |
fracsec[2] = '\000'; |
| 385 |
} else |
| 386 |
fracsec[1] = '\000'; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
hh = atof(hour); |
| 391 |
mm = atof(minute); |
| 392 |
ss = atof(second); |
| 393 |
divisor = 1; |
| 394 |
for (i = 0; i < strlen(fracsec); i++) |
| 395 |
divisor *= 10; |
| 396 |
fs = atof(fracsec) / divisor; |
| 397 |
|
| 398 |
return ((hh * 3600.0) + (mm * 60.0) + ss + fs); |
| 399 |
} |
| 400 |
|
| 401 |
/* UTL_ConvertFloattoTime |
| 402 |
** Convert a floating point number to a Dicom time. |
| 403 |
*/ |
| 404 |
void |
| 405 |
UTL_ConvertFloattoTime(double dt, char *time) |
| 406 |
{ |
| 407 |
int |
| 408 |
hour, |
| 409 |
minute, |
| 410 |
second, |
| 411 |
fracsec; |
| 412 |
|
| 413 |
hour = (int) (dt / 3600.0); |
| 414 |
dt -= (hour * 3600); |
| 415 |
|
| 416 |
minute = (int) (dt / 60.); |
| 417 |
dt -= (minute * 60); |
| 418 |
|
| 419 |
second = (int) dt; |
| 420 |
dt -= second; |
| 421 |
|
| 422 |
fracsec = (int) ((dt * 1000000) + 0.5); |
| 423 |
|
| 424 |
sprintf(time, "%02d%02d%02d.%06d", hour, minute, second, fracsec); |
| 425 |
|
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
/* UTL_SqueezeBlanks |
| 431 |
** |
| 432 |
*/ |
| 433 |
void |
| 434 |
UTL_SqueezeBlanks(char *s) |
| 435 |
{ |
| 436 |
|
| 437 |
char |
| 438 |
*t1, |
| 439 |
*t2; |
| 440 |
|
| 441 |
t1 = t2 = s; |
| 442 |
while (*t2 != '\000') { |
| 443 |
if (*t2 != ' ') { |
| 444 |
*t1 = *t2; |
| 445 |
t1++; |
| 446 |
} |
| 447 |
t2++; |
| 448 |
} |
| 449 |
*t1 = '\000'; |
| 450 |
|
| 451 |
return; |
| 452 |
} |
| 453 |
/* UTL_DateMatch |
| 454 |
** Match a date range as specified in the Dicom standard |
| 455 |
*/ |
| 456 |
CONDITION |
| 457 |
UTL_DateMatch(char *datestring, char *stm) |
| 458 |
{ |
| 459 |
|
| 460 |
int |
| 461 |
match; |
| 462 |
char |
| 463 |
*ndate; |
| 464 |
long |
| 465 |
start_date, |
| 466 |
end_date, |
| 467 |
date_in_question; |
| 468 |
|
| 469 |
if ((ndate = (char *) malloc(strlen(datestring) + 1)) == (char *) NULL) |
| 470 |
return (UTL_NOMATCH); |
| 471 |
|
| 472 |
strcpy(ndate, datestring); |
| 473 |
UTL_SqueezeBlanks(ndate); |
| 474 |
UTL_SqueezeBlanks(stm); |
| 475 |
|
| 476 |
match = 0; |
| 477 |
if (strchr(ndate, (int) '-') == (char *) NULL) { |
| 478 |
if (strcmp(ndate, stm) == 0) |
| 479 |
match = 1; |
| 480 |
} else { |
| 481 |
date_in_question = UTL_ConvertDatetoLong(stm); |
| 482 |
if (ndate[0] == '-') { |
| 483 |
end_date = UTL_ConvertDatetoLong(ndate + 1); |
| 484 |
if (date_in_question <= end_date) |
| 485 |
match = 1; |
| 486 |
} else if (ndate[strlen(ndate) - 1] == '-') { |
| 487 |
start_date = UTL_ConvertDatetoLong(ndate); |
| 488 |
if (date_in_question >= start_date) |
| 489 |
match = 1; |
| 490 |
} else { |
| 491 |
start_date = UTL_ConvertDatetoLong(ndate); |
| 492 |
end_date = UTL_ConvertDatetoLong(strchr(ndate, (int) '-') + 1); |
| 493 |
if ((date_in_question >= start_date) && |
| 494 |
(date_in_question <= end_date)) |
| 495 |
match = 1; |
| 496 |
} |
| 497 |
} |
| 498 |
free(ndate); |
| 499 |
if (match) |
| 500 |
return (UTL_MATCH); |
| 501 |
else |
| 502 |
return (UTL_NOMATCH); |
| 503 |
} |
| 504 |
/* UTL_TimeMatch |
| 505 |
** Match a time range as specified in the Dicom standard |
| 506 |
*/ |
| 507 |
CONDITION |
| 508 |
UTL_TimeMatch(char *timestring, char *stm) |
| 509 |
{ |
| 510 |
|
| 511 |
int |
| 512 |
match; |
| 513 |
char |
| 514 |
*ntime; |
| 515 |
double |
| 516 |
start_time, |
| 517 |
end_time, |
| 518 |
time_in_question; |
| 519 |
|
| 520 |
if ((ntime = (char *) malloc(strlen(timestring) + 2)) == (char *) NULL) |
| 521 |
return (UTL_NOMATCH); |
| 522 |
|
| 523 |
strcpy(ntime, timestring); |
| 524 |
UTL_SqueezeBlanks(ntime); |
| 525 |
UTL_SqueezeBlanks(stm); |
| 526 |
|
| 527 |
match = 0; |
| 528 |
if (strchr(ntime, (int) '-') == (char *) NULL) { |
| 529 |
if (strcmp(ntime, stm) == 0) |
| 530 |
match = 1; |
| 531 |
} else { |
| 532 |
time_in_question = UTL_ConvertTimetoFloat(stm); |
| 533 |
if (ntime[0] == '-') { |
| 534 |
end_time = UTL_ConvertTimetoFloat(ntime + 1); |
| 535 |
if (time_in_question <= end_time) |
| 536 |
match = 1; |
| 537 |
} else if (ntime[strlen(ntime) - 1] == '-') { |
| 538 |
start_time = UTL_ConvertTimetoFloat(ntime); |
| 539 |
if (time_in_question >= start_time) |
| 540 |
match = 1; |
| 541 |
} else { |
| 542 |
start_time = UTL_ConvertTimetoFloat(ntime); |
| 543 |
end_time = UTL_ConvertTimetoFloat(strchr(ntime, (int) '-') + 1); |
| 544 |
if ((time_in_question >= start_time) && |
| 545 |
(time_in_question <= end_time)) |
| 546 |
match = 1; |
| 547 |
} |
| 548 |
} |
| 549 |
free(ntime); |
| 550 |
if (match) |
| 551 |
return (UTL_MATCH); |
| 552 |
else |
| 553 |
return (UTL_NOMATCH); |
| 554 |
} |
| 555 |
/* |
| 556 |
** UTL_GetDicomDate |
| 557 |
** Get the current date and store as a Dicom date. |
| 558 |
*/ |
| 559 |
void |
| 560 |
UTL_GetDicomDate(char *datestr) |
| 561 |
{ |
| 562 |
|
| 563 |
struct tm |
| 564 |
*tf; |
| 565 |
time_t |
| 566 |
loctime; |
| 567 |
|
| 568 |
loctime = time((time_t *) NULL); |
| 569 |
tf = localtime(&loctime); |
| 570 |
|
| 571 |
sprintf(datestr, "%04d%02d%02d", (tf->tm_year) + 1900, (tf->tm_mon) + 1, tf->tm_mday); |
| 572 |
return; |
| 573 |
|
| 574 |
} |
| 575 |
/* |
| 576 |
** UTL_GetDicomTime |
| 577 |
** Get the current time and store as a Dicom time. |
| 578 |
*/ |
| 579 |
void |
| 580 |
UTL_GetDicomTime(char *timestr) |
| 581 |
{ |
| 582 |
|
| 583 |
struct tm |
| 584 |
*tf; |
| 585 |
time_t |
| 586 |
loctime; |
| 587 |
|
| 588 |
loctime = time((time_t *) NULL); |
| 589 |
tf = localtime(&loctime); |
| 590 |
|
| 591 |
sprintf(timestr, "%02d%02d%02d.%06d", (tf->tm_hour), (tf->tm_min), (tf->tm_sec), 0); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
#ifdef _MSC_VER |
| 596 |
typedef struct { |
| 597 |
char key[10]; |
| 598 |
struct _timeb t; |
| 599 |
} UTL_TIMESTRUCTURE; |
| 600 |
#else |
| 601 |
typedef struct { |
| 602 |
char key[10]; |
| 603 |
struct timeval t; |
| 604 |
} UTL_TIMESTRUCTURE; |
| 605 |
#endif |
| 606 |
|
| 607 |
void * |
| 608 |
UTL_GetTimeStamp() |
| 609 |
{ |
| 610 |
UTL_TIMESTRUCTURE *t; |
| 611 |
|
| 612 |
t = calloc(1, sizeof(*t)); |
| 613 |
if (t == NULL) |
| 614 |
return NULL; |
| 615 |
|
| 616 |
strcpy(t->key, "UTL STAMP"); |
| 617 |
|
| 618 |
#ifdef _MSC_VER |
| 619 |
_ftime(&t->t); |
| 620 |
#elif (TIMEOFDAYARGS == 2) |
| 621 |
gettimeofday(&t->t, NULL); |
| 622 |
#else |
| 623 |
gettimeofday(&t->t); |
| 624 |
#endif |
| 625 |
|
| 626 |
return t; |
| 627 |
} |
| 628 |
|
| 629 |
double |
| 630 |
UTL_DeltaTime(void *timeStamp) |
| 631 |
{ |
| 632 |
#ifdef _MSC_VER |
| 633 |
struct _timeb timeNow; |
| 634 |
#else |
| 635 |
struct timeval timeNow; |
| 636 |
#endif |
| 637 |
UTL_TIMESTRUCTURE *t; |
| 638 |
double delta = 0.; |
| 639 |
|
| 640 |
#ifdef _MSC_VER |
| 641 |
_ftime(&timeNow); |
| 642 |
#elif (TIMEOFDAYARGS == 2) |
| 643 |
gettimeofday(&timeNow, NULL); |
| 644 |
#else |
| 645 |
gettimeofday(&timeNow); |
| 646 |
#endif |
| 647 |
|
| 648 |
t = (UTL_TIMESTRUCTURE *) timeStamp; |
| 649 |
if (t == NULL) |
| 650 |
return -1.0; |
| 651 |
|
| 652 |
if (strcmp(t->key, "UTL STAMP") != 0) |
| 653 |
return -1.0; |
| 654 |
|
| 655 |
#ifdef _MSC_VER |
| 656 |
delta = timeNow.time - t->t.time; |
| 657 |
delta += (timeNow.millitm - t->t.millitm) / 1000.; |
| 658 |
#else |
| 659 |
delta = timeNow.tv_sec - t->t.tv_sec; |
| 660 |
delta += (timeNow.tv_usec - t->t.tv_usec) / 1000000.; |
| 661 |
#endif |
| 662 |
|
| 663 |
return delta; |
| 664 |
} |
| 665 |
|
| 666 |
void |
| 667 |
UTL_ReleaseTimeStamp(void *timeStamp) |
| 668 |
{ |
| 669 |
UTL_TIMESTRUCTURE *t; |
| 670 |
|
| 671 |
t = (UTL_TIMESTRUCTURE *) timeStamp; |
| 672 |
if (t == NULL) |
| 673 |
return; |
| 674 |
|
| 675 |
if (strcmp(t->key, "UTL STAMP") != 0) |
| 676 |
return; |
| 677 |
|
| 678 |
free(timeStamp); |
| 679 |
} |
| 680 |
|
| 681 |
CONDITION |
| 682 |
UTL_VerifyCreatePath(const char *path) |
| 683 |
{ |
| 684 |
int i; |
| 685 |
#ifdef _MSC_VER |
| 686 |
struct _stat buf; |
| 687 |
#else |
| 688 |
struct stat buf; |
| 689 |
#endif |
| 690 |
char |
| 691 |
*p, |
| 692 |
temp[1024]; |
| 693 |
int flag = 0; |
| 694 |
static int statCount = 0; |
| 695 |
|
| 696 |
#ifdef _MSC_VER |
| 697 |
statCount++; |
| 698 |
i = _stat(path, &buf); |
| 699 |
#else |
| 700 |
i = stat(path, &buf); |
| 701 |
#endif |
| 702 |
|
| 703 |
|
| 704 |
if (i == 0) { |
| 705 |
#ifdef _MSC_VER |
| 706 |
flag = ((buf.st_mode & _S_IFDIR) != 0); |
| 707 |
#else |
| 708 |
flag = (S_ISDIR(buf.st_mode)); |
| 709 |
#endif |
| 710 |
if (flag) |
| 711 |
return UTL_NORMAL; |
| 712 |
else |
| 713 |
return UTL_PATHNOTDIR; |
| 714 |
} |
| 715 |
p = temp; |
| 716 |
|
| 717 |
while (*path != '\0') { |
| 718 |
*p++ = *path++; |
| 719 |
while (*path != '/' && *path != '\\' && *path != '\0') { |
| 720 |
#ifdef _MSC_VER |
| 721 |
if (*path == ':') { |
| 722 |
*p++ = *path++; |
| 723 |
if (*path == '\0') /* We should not get C:\0, but test |
| 724 |
* it */ |
| 725 |
break; |
| 726 |
} |
| 727 |
#endif |
| 728 |
*p++ = *path++; |
| 729 |
} |
| 730 |
|
| 731 |
*p = '\0'; |
| 732 |
#ifdef _MSC_VER |
| 733 |
statCount++; |
| 734 |
i = _stat(temp, &buf); |
| 735 |
#else |
| 736 |
i = stat(temp, &buf); |
| 737 |
#endif |
| 738 |
|
| 739 |
if (i == 0) { |
| 740 |
#ifdef _MSC_VER |
| 741 |
flag = ((buf.st_mode & _S_IFDIR) != 0); |
| 742 |
#else |
| 743 |
flag = (S_ISDIR(buf.st_mode)); |
| 744 |
#endif |
| 745 |
if (!flag) |
| 746 |
return UTL_PATHNOTDIR; |
| 747 |
} else { |
| 748 |
#ifdef _MSC_VER |
| 749 |
int e1; |
| 750 |
e1 = errno; |
| 751 |
memset(&buf, 0, sizeof(buf)); |
| 752 |
/*fprintf(stderr, "Stat Count = %d\n", statCount);*/ |
| 753 |
statCount++; |
| 754 |
i = _stat(temp, &buf); |
| 755 |
e1 = errno; |
| 756 |
i = _mkdir(temp); |
| 757 |
#else |
| 758 |
i = mkdir(temp, 0777); |
| 759 |
#endif |
| 760 |
if (i != 0) { |
| 761 |
int e1; |
| 762 |
e1 = errno; |
| 763 |
fprintf(stderr, "Stat Count = %d\n", statCount); |
| 764 |
perror(temp); |
| 765 |
return UTL_FILECREATEFAILED; |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
return UTL_NORMAL; |
| 770 |
} |
| 771 |
|
| 772 |
CTNBOOLEAN UTL_IsDirectory(const char* path) |
| 773 |
{ |
| 774 |
int i; |
| 775 |
#ifdef _MSC_VER |
| 776 |
struct _stat buf; |
| 777 |
#else |
| 778 |
struct stat buf; |
| 779 |
#endif |
| 780 |
|
| 781 |
int flag = 0; |
| 782 |
|
| 783 |
#ifdef _MSC_VER |
| 784 |
i = _stat(path, &buf); |
| 785 |
#else |
| 786 |
i = stat(path, &buf); |
| 787 |
#endif |
| 788 |
|
| 789 |
|
| 790 |
if (i == 0) { |
| 791 |
#ifdef _MSC_VER |
| 792 |
flag = ((buf.st_mode & _S_IFDIR) != 0); |
| 793 |
#else |
| 794 |
flag = (S_ISDIR(buf.st_mode)); |
| 795 |
#endif |
| 796 |
if (flag) |
| 797 |
return TRUE; |
| 798 |
} |
| 799 |
return FALSE; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
CONDITION UTL_ScanDirectory(const char* path, |
| 805 |
LST_HEAD** lst) |
| 806 |
{ |
| 807 |
UTL_FILEITEM* item = 0; |
| 808 |
|
| 809 |
#ifdef _WIN32 |
| 810 |
long hFile = 0; |
| 811 |
struct _finddata_t fileInfo; |
| 812 |
char directoryText[1024]; |
| 813 |
*lst = LST_Create(); |
| 814 |
strcpy(directoryText, path); |
| 815 |
strcat(directoryText, "/*"); |
| 816 |
if( (hFile = _findfirst(directoryText, &fileInfo)) == -1L) |
| 817 |
return 0; |
| 818 |
|
| 819 |
item = malloc(sizeof(*item)); |
| 820 |
strcpy(item->path, fileInfo.name); |
| 821 |
LST_Enqueue(lst, item); |
| 822 |
|
| 823 |
while(_findnext(hFile, &fileInfo) == 0) { |
| 824 |
item = malloc(sizeof(*item)); |
| 825 |
strcpy(item->path, fileInfo.name); |
| 826 |
LST_Enqueue(lst, item); |
| 827 |
} |
| 828 |
_findclose(hFile); |
| 829 |
|
| 830 |
#else |
| 831 |
DIR* dirp; |
| 832 |
struct dirent* dp; |
| 833 |
|
| 834 |
*lst = LST_Create(); |
| 835 |
dirp = opendir(path); |
| 836 |
if (dirp == 0) |
| 837 |
return 0; |
| 838 |
|
| 839 |
while ((dp = readdir(dirp)) != NULL) { |
| 840 |
item = malloc(sizeof(*item)); |
| 841 |
strcpy(item->path, dp->d_name); |
| 842 |
LST_Enqueue(lst, item); |
| 843 |
} |
| 844 |
closedir(dirp); |
| 845 |
#endif |
| 846 |
|
| 847 |
return UTL_NORMAL; |
| 848 |
} |
| 849 |
|
| 850 |
static char* UTL_configFile = 0; |
| 851 |
static LST_HEAD* UTL_configList = 0; |
| 852 |
typedef struct { |
| 853 |
void* reserved[2]; |
| 854 |
char *pName; |
| 855 |
char *pValue; |
| 856 |
} CONFIG_ITEM; |
| 857 |
|
| 858 |
CONDITION UTL_ReadConfigFile( ) |
| 859 |
{ |
| 860 |
FILE* f; |
| 861 |
char buf[1024]; |
| 862 |
|
| 863 |
if (UTL_configList != 0) |
| 864 |
return UTL_NORMAL; |
| 865 |
|
| 866 |
UTL_configList = LST_Create(); |
| 867 |
if (UTL_configList == NULL) |
| 868 |
return 0; |
| 869 |
|
| 870 |
if (UTL_configFile == 0) |
| 871 |
return UTL_NORMAL; |
| 872 |
|
| 873 |
if (UTL_configFile[0] == '\0') |
| 874 |
return UTL_NORMAL; |
| 875 |
|
| 876 |
f = fopen(UTL_configFile, "r"); |
| 877 |
if (f == NULL) |
| 878 |
return 0; |
| 879 |
|
| 880 |
while (fgets(buf, sizeof(buf), f) != NULL) { |
| 881 |
char* token1; |
| 882 |
char* token2; |
| 883 |
CONFIG_ITEM* item; |
| 884 |
|
| 885 |
if (buf[0] == '#') continue; |
| 886 |
if (buf[0] == '\n') continue; |
| 887 |
token1 = strtok(buf, " \t\n"); |
| 888 |
token2 = strtok(0, " \t\n"); |
| 889 |
if (token2 == NULL) continue; |
| 890 |
|
| 891 |
item = (CONFIG_ITEM*)malloc(sizeof(*item) + strlen(token1) + |
| 892 |
strlen(token2) + 2); |
| 893 |
item->pName = ((char*)item) + sizeof(*item); |
| 894 |
strcpy(item->pName, token1); |
| 895 |
item->pValue = item->pName + strlen(token1) + 1; |
| 896 |
strcpy(item->pValue, token2); |
| 897 |
|
| 898 |
LST_Enqueue(&UTL_configList, item); |
| 899 |
} |
| 900 |
|
| 901 |
fclose(f); |
| 902 |
|
| 903 |
return UTL_NORMAL; |
| 904 |
} |
| 905 |
|
| 906 |
CONDITION UTL_SetConfigFile(const char* configFile) |
| 907 |
{ |
| 908 |
if (UTL_configFile != 0) { |
| 909 |
CTN_FREE(UTL_configFile); |
| 910 |
} |
| 911 |
|
| 912 |
if (configFile == 0 || configFile[0] == '\0') { |
| 913 |
char* p = getenv("CTN_TARGET"); |
| 914 |
if (p == NULL) { |
| 915 |
return UTL_NO_CTN_TARGET; |
| 916 |
} |
| 917 |
UTL_configFile = (char*) malloc(strlen(p) + strlen("/runtime/ctn_cfg.txt") + 1); |
| 918 |
strcpy(UTL_configFile, p); |
| 919 |
strcat(UTL_configFile, "/runtime/ctn_cfg.txt"); |
| 920 |
} else { |
| 921 |
UTL_configFile = (char*) malloc(strlen(configFile)+1); |
| 922 |
strcpy(UTL_configFile, configFile); |
| 923 |
} |
| 924 |
|
| 925 |
return UTL_NORMAL; |
| 926 |
} |
| 927 |
|
| 928 |
CONDITION UTL_TestConfigFile(const char* configFile) |
| 929 |
{ |
| 930 |
return UTL_NORMAL; |
| 931 |
} |
| 932 |
char* UTL_GetConfigParameter(const char* paramName) |
| 933 |
{ |
| 934 |
CONDITION cond; |
| 935 |
char nameCopy[256]; |
| 936 |
CONFIG_ITEM* item; |
| 937 |
int idx; |
| 938 |
|
| 939 |
cond = UTL_ReadConfigFile( ); |
| 940 |
if (cond != UTL_NORMAL) |
| 941 |
return NULL; |
| 942 |
|
| 943 |
item = LST_Head(&UTL_configList); |
| 944 |
if (item == NULL) |
| 945 |
return NULL; |
| 946 |
|
| 947 |
(void) LST_Position(&UTL_configList, item); |
| 948 |
while(item != NULL) { |
| 949 |
if (strcmp(item->pName, paramName) == 0) |
| 950 |
return item->pValue; |
| 951 |
|
| 952 |
item = LST_Next(&UTL_configList); |
| 953 |
} |
| 954 |
|
| 955 |
strcpy(nameCopy, paramName); |
| 956 |
idx = strlen(nameCopy) - 1; |
| 957 |
while (idx > 0) { |
| 958 |
if (nameCopy[idx] == '/') { |
| 959 |
nameCopy[idx] = '\0'; |
| 960 |
idx = -1; |
| 961 |
break; |
| 962 |
} else { |
| 963 |
idx--; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
if (idx < 0) { |
| 968 |
return UTL_GetConfigParameter(nameCopy); |
| 969 |
} else { |
| 970 |
return NULL; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
char** |
| 975 |
UTL_ExpandToPointerArray(const char* inputText, |
| 976 |
const char* delimiters, |
| 977 |
int* numberOfEntries) |
| 978 |
{ |
| 979 |
int idx; |
| 980 |
int memorySize = 0; |
| 981 |
int arrayIndex = 0; |
| 982 |
char** array; |
| 983 |
char* outputPtr; |
| 984 |
char* token; |
| 985 |
|
| 986 |
*numberOfEntries = 1; |
| 987 |
for (idx = 0; inputText[idx] != '\0'; idx++) { |
| 988 |
int j; |
| 989 |
for (j = 0; delimiters[j] != '\0'; j++) { |
| 990 |
if (inputText[idx] == delimiters[j]) { |
| 991 |
(*numberOfEntries)++; |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
memorySize = (sizeof(char*)) * (*numberOfEntries); |
| 998 |
memorySize += strlen(inputText) + 1; |
| 999 |
|
| 1000 |
array = (char**)CTN_MALLOC(memorySize); |
| 1001 |
outputPtr = ((char*) array) + ((sizeof(char*)) * (*numberOfEntries)); |
| 1002 |
strcpy(outputPtr, inputText); |
| 1003 |
|
| 1004 |
token = strtok(outputPtr, delimiters); |
| 1005 |
while(token != NULL) { |
| 1006 |
array[arrayIndex++] = token; |
| 1007 |
token = strtok(NULL, delimiters); |
| 1008 |
} |
| 1009 |
|
| 1010 |
return array; |
| 1011 |
} |
| 1012 |
|
| 1013 |
CTNBOOLEAN UTL_IsFile(const char* path) |
| 1014 |
{ |
| 1015 |
int i; |
| 1016 |
CTNBOOLEAN rtnValue = FALSE; |
| 1017 |
|
| 1018 |
#ifdef _WIN32 |
| 1019 |
struct _stat buf; |
| 1020 |
|
| 1021 |
i = _stat(path, &buf); |
| 1022 |
if (i == 0) { |
| 1023 |
rtnValue = ((buf.st_mode & _S_IFREG) != 0); |
| 1024 |
} |
| 1025 |
#else |
| 1026 |
struct stat buf; |
| 1027 |
i = stat(path, &buf); |
| 1028 |
if (i == 0) { |
| 1029 |
rtnValue = (S_ISREG(buf.st_mode)); |
| 1030 |
} |
| 1031 |
#endif |
| 1032 |
|
| 1033 |
return rtnValue; |
| 1034 |
} |
| 1035 |
|
| 1036 |
CONDITION UTL_DeleteFile(const char* path) |
| 1037 |
{ |
| 1038 |
int i = 0; |
| 1039 |
|
| 1040 |
i = unlink(path); |
| 1041 |
|
| 1042 |
if (i == 0) |
| 1043 |
return UTL_NORMAL; |
| 1044 |
|
| 1045 |
return COND_PushCondition(UTL_DELETEFILEFAILED, ""); |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
CONDITION |
| 1050 |
UTL_FileSize(const char* path, U32* size) |
| 1051 |
{ |
| 1052 |
int status; |
| 1053 |
struct stat im_stat; |
| 1054 |
|
| 1055 |
status = stat(path, &im_stat); |
| 1056 |
if (status < 0) { |
| 1057 |
*size = 0; |
| 1058 |
return 0; |
| 1059 |
} else { |
| 1060 |
*size = im_stat.st_size; |
| 1061 |
return UTL_NORMAL; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|