Geant4 Cross Reference |
1 /* 2 # <<BEGIN-copyright>> 3 # <<END-copyright>> 4 */ 5 6 #include <stdlib.h> 7 #include <string.h> 8 #include <ctype.h> 9 #include <sys/types.h> 10 #include <sys/stat.h> 11 #include <fcntl.h> 12 #include <errno.h> 13 14 #if defined(WIN32) || defined(__MINGW32__) 15 #include <windows.h> 16 #define realpath( a, b ) GetFullPathName( a, PATH_MAX, b, NULL ) 17 #define strtoll _strtoi64 18 #else 19 #include <unistd.h> 20 #endif 21 22 #include "xDataTOM_importXML_private.h" 23 #include "xDataTOM_private.h" 24 25 #if defined __cplusplus 26 namespace GIDI { 27 using namespace GIDI; 28 #endif 29 30 #ifndef PATH_MAX 31 #define PATH_MAX 4096 32 #endif 33 34 int xDataTOM_smrLibraryID = smr_unknownID; 35 36 static int xDataTOME_initializeElement( statusMessageReporting *smr, xDataTOM_element *element, xDataTOM_element *parent, int ordinal, int index, 37 char const *name ); 38 static void xDataTOME_displayTree( statusMessageReporting *smr, xDataTOM_element *element, int printAttributes, int level ); 39 40 static int xDataTOM_initialize_xData( statusMessageReporting *smr, xDataTOM_xDataInfo *xDataInfo ); 41 /* 42 ************************************************************ 43 */ 44 xDataTOM_TOM *xDataTOM_importFile( statusMessageReporting *smr, const char *fileName ) { 45 /* 46 * Returns NULL is an error occurred. 47 */ 48 49 return( xDataXML_importFile( smr, fileName ) ); 50 } 51 /* 52 ************************************************************ 53 */ 54 xDataTOM_TOM *xDataTOM_mallocTOM( statusMessageReporting *smr ) { 55 /* 56 * Returns NULL is an error occurred. 57 */ 58 xDataTOM_TOM *TOM; 59 60 if( ( TOM = (xDataTOM_TOM *) smr_malloc2( smr, sizeof( xDataTOM_TOM ), 1, "xDataTOM_TOM" ) ) != NULL ) { 61 if( xDataTOM_initializeTOM( smr, TOM ) ) smr_freeMemory( (void **) &TOM ); 62 } 63 return( TOM ); 64 } 65 /* 66 ************************************************************ 67 */ 68 int xDataTOM_initializeTOM( statusMessageReporting *smr, xDataTOM_TOM *TOM ) { 69 70 TOM->fileName = NULL; 71 TOM->realFileName = NULL; 72 xDataTOME_initializeElement( smr, &(TOM->root), NULL, 0, 0, "" ); 73 return( !smr_isOk( smr ) ); 74 } 75 /* 76 ************************************************************ 77 */ 78 void *xDataTOM_freeTOM( statusMessageReporting * /*smr*/, xDataTOM_TOM **TOM ) { 79 80 xDataTOM_TOM *TOMp; 81 82 if( TOM == NULL ) return( NULL ); 83 TOMp = *TOM; 84 if( TOMp == NULL ) return( NULL ); 85 xDataTOM_releaseElement( &(TOMp->root) ); 86 smr_freeMemory( (void **) &(TOMp->fileName) ); 87 smr_freeMemory( (void **) &(TOMp->realFileName) ); 88 smr_freeMemory( (void **) TOM ); 89 return( NULL ); 90 } 91 /* 92 ************************************************************ 93 */ 94 int xDataTOM_setFileNameTOM( statusMessageReporting *smr, xDataTOM_TOM *TOM, const char *fileName ) { 95 /* 96 * Returns not zero value if error occurred. 97 */ 98 99 char realPath[PATH_MAX+1]; 100 101 smr_freeMemory( (void **) &(TOM->fileName) ); 102 smr_freeMemory( (void **) &(TOM->realFileName) ); 103 if( fileName != NULL ) { 104 if( ( TOM->fileName = smr_allocateCopyString2( smr, fileName, "fileName" ) ) == NULL ) return( 1 ); 105 if( realpath( fileName, realPath ) != NULL ) { 106 if( ( TOM->realFileName = smr_allocateCopyString2( smr, realPath, "realFileName" ) ) == NULL ) return( 1 ); 107 } 108 } 109 return( 0 ); 110 } 111 /* 112 ************************************************************ 113 */ 114 void xDataTOM_displayTree( statusMessageReporting *smr, xDataTOM_TOM *TOM, int printAttributes ) { 115 116 if( TOM->root.children != NULL ) xDataTOME_displayTree( smr, TOM->root.children, printAttributes, 0 ); 117 } 118 119 /**************************************** 120 * Element functions. 121 ****************************************/ 122 /* 123 ************************************************************ 124 */ 125 xDataTOM_element *xDataTOM_mallocElement( statusMessageReporting *smr, xDataTOM_element *parent, int ordinal, int index, char const *name ) { 126 /* 127 * Returns NULL is an error occurred. 128 */ 129 xDataTOM_element *element; 130 131 if( ( element = (xDataTOM_element *) smr_malloc2( smr, sizeof( xDataTOM_element ), 1, "xDataTOM_elelument" ) ) != NULL ) { 132 if( xDataTOME_initializeElement( smr, element, parent, ordinal, index, name ) ) smr_freeMemory( (void **) &element ); 133 } 134 return( element ); 135 } 136 /* 137 ************************************************************ 138 */ 139 void xDataTOM_freeElement( xDataTOM_element **element ) { 140 141 if( element == NULL ) return; 142 xDataTOM_releaseElement( *element ); 143 smr_freeMemory( (void **) element ); 144 } 145 /* 146 ************************************************************ 147 */ 148 void xDataTOM_releaseElement( xDataTOM_element *element ) { 149 150 xDataTOM_element *child, *nextChild; 151 152 if( element == NULL ) return; 153 xDataTOMAL_release( &(element->attributes) ); 154 for( child = element->children; child != NULL; child = nextChild ) { 155 nextChild = child->next; 156 xDataTOM_freeElement( &child ); 157 } 158 if( element->xDataInfo.ID != NULL ) { 159 xDataTOM_axes_release( &(element->xDataInfo.axes) ); 160 if( strcmp( element->xDataInfo.ID, xDataTOM_XYs_ID ) == 0 ) { 161 xDataTOM_XYs_free( &(element->xDataInfo ) ); } 162 else if( strcmp( element->xDataInfo.ID, xDataTOM_regionsXYs_ID ) == 0 ) { 163 xDataTOM_regionsXYs_free( &(element->xDataInfo ) ); } 164 else if( strcmp( element->xDataInfo.ID, xDataTOM_W_XYs_ID ) == 0 ) { 165 xDataTOM_W_XYs_freeFrom_xDataInfo( &(element->xDataInfo ) ); } 166 else if( strcmp( element->xDataInfo.ID, xDataTOM_V_W_XYs_ID ) == 0 ) { 167 xDataTOM_V_W_XYs_free( &(element->xDataInfo ) ); } 168 else if( strcmp( element->xDataInfo.ID, xDataTOM_W_XYs_LegendreSeries_ID ) == 0 ) { 169 xDataTOM_W_XYs_LegendreSeries_free( &(element->xDataInfo ) ); } 170 else if( strcmp( element->xDataInfo.ID, xDataTOM_regionsW_XYs_LegendreSeries_ID ) == 0 ) { 171 xDataTOM_regionsW_XYs_LegendreSeries_free( &(element->xDataInfo ) ); } 172 else if( strcmp( element->xDataInfo.ID, xDataTOM_V_W_XYs_LegendreSeries_ID ) == 0 ) { 173 xDataTOM_V_W_XYs_LegendreSeries_free( &(element->xDataInfo ) ); } 174 else if( strcmp( element->xDataInfo.ID, xDataTOM_KalbachMann_ID ) == 0 ) { 175 xDataTOM_KalbachMann_free( &(element->xDataInfo ) ); } 176 else if( strcmp( element->xDataInfo.ID, xDataTOM_polynomial_ID ) == 0 ) { 177 xDataTOM_polynomial_free( &(element->xDataInfo ) ); } 178 else { 179 printf( "not freed for %s\n", element->xDataInfo.ID ); 180 } 181 } 182 element->parent = NULL; 183 smr_freeMemory( (void **) &(element->name) ); 184 } 185 /* 186 ************************************************************ 187 */ 188 xDataTOM_element *xDataTOM_addElementInElement( statusMessageReporting *smr, xDataTOM_element *parent, int index, char const *name ) { 189 190 xDataTOM_element *element; 191 192 if( ( element = xDataTOM_mallocElement( smr, parent, parent->numberOfChildren, index, name ) ) == NULL ) return( NULL ); 193 if( parent->children == NULL ) { 194 parent->children = element; } 195 else { 196 xDataTOM_element *last; 197 198 for( last = parent->children; last->next != NULL; last = last->next ) ; 199 last->next = element; 200 } 201 (parent->numberOfChildren)++; 202 return( element ); 203 } 204 /* 205 ************************************************************ 206 */ 207 static int xDataTOME_initializeElement( statusMessageReporting *smr, xDataTOM_element *element, xDataTOM_element *parent, int ordinal, int index, 208 char const *name ) { 209 210 element->ordinal = ordinal; 211 element->index = index; 212 element->parent = parent; 213 element->next = NULL; 214 element->name = smr_allocateCopyString2( smr, name, "element->name" ); 215 xDataTOMAL_initial( smr, &(element->attributes) ); 216 element->numberOfChildren = 0; 217 element->children = NULL; 218 return( ( xDataTOM_initialize_xData( smr, &(element->xDataInfo) ) || ( element->name == NULL ) ) ? 1 : 0 ); 219 } 220 /* 221 ************************************************************ 222 */ 223 xDataTOM_element *xDataTOM_getDocumentsElement( xDataTOM_TOM *TOM ) { 224 225 return( TOM->root.children ); 226 } 227 /* 228 ************************************************************ 229 */ 230 xDataTOM_element *xDataTOME_getFirstElement( xDataTOM_element *element ) { 231 232 if( element != NULL ) element = element->children; 233 return( element ); 234 } 235 /* 236 ************************************************************ 237 */ 238 xDataTOM_element *xDataTOME_getNextElement( xDataTOM_element *element ) { 239 240 if( element != NULL ) element = element->next; 241 return( element ); 242 } 243 /* 244 ************************************************************ 245 */ 246 xDataTOM_element *xDataTOME_getOneElementByName( statusMessageReporting *smr, xDataTOM_element *element, char const *name, int required ) { 247 248 int n = 0; 249 xDataTOM_element *child, *desired = NULL; 250 251 for( child = xDataTOME_getFirstElement( element ); child != NULL; child = xDataTOME_getNextElement( child ) ) { 252 if( strcmp( child->name, name ) == 0 ) { 253 if( n == 0 ) desired = child; 254 n++; 255 } 256 } 257 if( n == 0 ) { 258 if( required ) smr_setReportError2( smr, smr_unknownID, 1, "elements '%s' not found in element '%s'", name, element->name ); } 259 else if( n > 1 ) { 260 smr_setReportError2( smr, smr_unknownID, 1, "multiple (= %d) elements '%s' found in element '%s'", name, element->name ); 261 desired = NULL; 262 } 263 return( desired ); 264 } 265 /* 266 ************************************************************ 267 */ 268 int xDataTOM_numberOfElementsByName( statusMessageReporting * /*smr*/, xDataTOM_element *element, char const *name ) { 269 270 int n = 0; 271 xDataTOM_element *child; 272 273 for( child = xDataTOME_getFirstElement( element ); child != NULL; child = xDataTOME_getNextElement( child ) ) if( !strcmp( child->name, name ) ) n++; 274 return( n ); 275 } 276 /* 277 ************************************************************ 278 */ 279 int xDataTOME_addAttribute( statusMessageReporting *smr, xDataTOM_element *element, char const *name, char const *value ) { 280 281 return( xDataTOMAL_addAttribute( smr, &(element->attributes), name, value ) ); 282 } 283 /* 284 ************************************************************ 285 */ 286 char const *xDataTOM_getAttributesValueInElement( xDataTOM_element *element, char const *name ) { 287 288 return( xDataTOMAL_getAttributesValue( &(element->attributes), name ) ); 289 } 290 /* 291 ************************************************************ 292 */ 293 int xDataTOME_copyAttributionList( statusMessageReporting *smr, xDataTOM_attributionList *desc, xDataTOM_element *element ) { 294 295 return( xDataTOMAL_copyAttributionList( smr, desc, &(element->attributes) ) ); 296 } 297 /* 298 ************************************************************ 299 */ 300 int xDataTOME_convertAttributeToInteger( statusMessageReporting *smr, xDataTOM_element *element, char const *name, int *n ) { 301 302 return( xDataTOMAL_convertAttributeToInteger( smr, &(element->attributes), name, n ) ); 303 } 304 /* 305 ************************************************************ 306 */ 307 int xDataTOME_convertAttributeToDouble( statusMessageReporting *smr, xDataTOM_element *element, char const *name, double *d ) { 308 309 return( xDataTOMAL_convertAttributeToDouble( smr, &(element->attributes), name, d ) ); 310 } 311 /* 312 ************************************************************ 313 */ 314 int xDataTOME_getInterpolation( statusMessageReporting *smr, xDataTOM_element *element, int index, enum xDataTOM_interpolationFlag *independent, 315 enum xDataTOM_interpolationFlag *dependent, enum xDataTOM_interpolationQualifier *qualifier ) { 316 317 xDataTOM_xDataInfo *xDI = &(element->xDataInfo); 318 319 if( xDI->ID == NULL ) return( 1 ); 320 321 return( xDataTOM_axes_getInterpolation( smr, &(xDI->axes), index, independent, dependent, qualifier ) ); 322 } 323 /* 324 ************************************************************ 325 */ 326 static void xDataTOME_displayTree( statusMessageReporting *smr, xDataTOM_element *element, int printAttributes, int level ) { 327 328 int i; 329 xDataTOM_element *child; 330 331 for( i = 0; i < level; i++ ) printf( " " ); 332 printf( "/%s", element->name ); 333 if( element->index >= 0 ) printf( " (%d)", element->index ); 334 if( printAttributes ) { 335 xDataTOM_attribute *attribute; 336 337 for( attribute = element->attributes.attributes; attribute != NULL; attribute = attribute->next ) { 338 printf( " (%s, \"%s\")", attribute->name, attribute->value ); 339 } 340 } 341 printf( "\n" ); 342 for( child = xDataTOME_getFirstElement( element ); child != NULL; child = xDataTOME_getNextElement( child ) ) { 343 xDataTOME_displayTree( smr, child, printAttributes, level + 1 ); 344 } 345 } 346 347 /**************************************** 348 * Attribute functions. 349 ****************************************/ 350 /* 351 ************************************************************ 352 */ 353 void xDataTOMAL_initial( statusMessageReporting * /*smr*/, xDataTOM_attributionList *attributes ) { 354 355 attributes->number = 0; 356 attributes->attributes = NULL; 357 } 358 /* 359 ************************************************************ 360 */ 361 void xDataTOMAL_release( xDataTOM_attributionList *attributes ) { 362 363 xDataTOM_attribute *attribute, *next; 364 365 for( attribute = attributes->attributes; attribute != NULL; attribute = next ) { 366 next = attribute->next; 367 smr_freeMemory( (void **) &(attribute->name) ); 368 smr_freeMemory( (void **) &(attribute->value) ); 369 smr_freeMemory( (void **) &(attribute) ); 370 } 371 xDataTOMAL_initial( NULL, attributes ); 372 } 373 /* 374 ************************************************************ 375 */ 376 int xDataTOMAL_addAttribute( statusMessageReporting *smr, xDataTOM_attributionList *attributes, char const *name, char const *value ) { 377 378 xDataTOM_attribute *attribute; 379 380 if( ( attribute = (xDataTOM_attribute *) smr_malloc2( smr, sizeof( xDataTOM_attribute ), 1, "xDataTOM_attribute" ) ) == NULL ) return( 1 ); 381 if( ( attribute->name = smr_allocateCopyString2( smr, name, "name" ) ) == NULL ) goto err; 382 if( ( attribute->value = smr_allocateCopyString2( smr, value, "value" ) ) == NULL ) goto err; 383 if( attributes->attributes == NULL ) { 384 attributes->attributes = attribute; } 385 else { 386 xDataTOM_attribute *last; 387 388 for( last = attributes->attributes; last->next != NULL; last = last->next ) ; 389 last->next = attribute; 390 } 391 attributes->number++; 392 return( 0 ); 393 394 err: 395 smr_freeMemory( (void **) &(attribute->name) ); 396 smr_freeMemory( (void **) &(attribute->value) ); 397 smr_freeMemory( (void **) &(attribute) ); 398 return( 1 ); 399 } 400 /* 401 ************************************************************ 402 */ 403 char const *xDataTOMAL_getAttributesValue( xDataTOM_attributionList *attributes, char const *name ) { 404 405 xDataTOM_attribute *attribute; 406 407 for( attribute = attributes->attributes; attribute != NULL; attribute = attribute->next ) { 408 if( !strcmp( attribute->name, name ) ) return( attribute->value ); 409 } 410 return( NULL ); 411 } 412 /* 413 ************************************************************ 414 */ 415 int xDataTOMAL_copyAttributionList( statusMessageReporting *smr, xDataTOM_attributionList *desc, xDataTOM_attributionList *src ) { 416 417 xDataTOM_attribute *attribute; 418 419 xDataTOMAL_initial( smr, desc ); 420 for( attribute = src->attributes; attribute != NULL; attribute = attribute->next ) { 421 if( xDataTOMAL_addAttribute( smr, desc, attribute->name, attribute->value ) != 0 ) goto err; 422 423 } 424 return( 0 ); 425 426 err: 427 xDataTOMAL_release( desc ); 428 return( 1 ); 429 } 430 /* 431 ************************************************************ 432 */ 433 int xDataTOMAL_convertAttributeToInteger( statusMessageReporting *smr, xDataTOM_attributionList *attributes, char const *name, int *n ) { 434 435 char const *value = xDataTOMAL_getAttributesValue( attributes, name ); 436 char *e; 437 438 if( value != NULL ) { 439 *n = (int) strtoll( value, &e, 10 ); 440 if( *e == 0 ) return( 0 ); 441 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "could not convert attribute %s's value = '%s' to an integer", name, value ); } 442 else { 443 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "no attribute named '%s'", name ); 444 } 445 return( 1 ); 446 } 447 /* 448 ************************************************************ 449 */ 450 int xDataTOMAL_convertAttributeToDouble( statusMessageReporting *smr, xDataTOM_attributionList *attributes, char const *name, double *d ) { 451 452 char const *value = xDataTOMAL_getAttributesValue( attributes, name ); 453 char *e; 454 455 if( value != NULL ) { 456 *d = strtod( value, &e ); 457 if( *e == 0 ) return( 0 ); 458 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "could not convert attribute %s's values = '%s' to a double", name, value ); } 459 else { 460 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "no attribute named '%s'", name ); 461 } 462 return( 1 ); 463 } 464 465 466 /**************************************** 467 * xData functions. 468 ****************************************/ 469 /* 470 ************************************************************ 471 */ 472 static int xDataTOM_initialize_xData( statusMessageReporting * /*smr*/, xDataTOM_xDataInfo * /*xDataInfo*/ ) { 473 474 return( 0 ); 475 } 476 /* 477 ************************************************************ 478 */ 479 void *xData_initializeData( statusMessageReporting *smr, xDataTOM_element *TE, char const *ID, size_t size ) { 480 481 xDataTOM_xDataInfo *xDI = &(TE->xDataInfo); 482 483 xDI->data = NULL; 484 xDI->ID = ID; 485 xDI->element = TE; 486 return( xDI->data = (void *) smr_malloc2( smr, size, 1, "xDI->data" ) ); 487 } 488 /* 489 ************************************************************ 490 */ 491 int xDataTOM_isXDataID( xDataTOM_element *TE, char const *ID ) { 492 493 xDataTOM_xDataInfo *xDI = &(TE->xDataInfo); 494 495 if( xDI->ID != NULL ) { 496 return( !strcmp( xDI->ID, ID ) ); 497 } 498 499 return( 0 ); 500 } 501 /* 502 ************************************************************ 503 */ 504 xDataTOM_xDataInfo *xDataTOME_getXData( xDataTOM_element *TE ) { 505 506 if( TE->xDataInfo.ID == NULL ) return( NULL ); 507 return( &(TE->xDataInfo) ); 508 } 509 /* 510 ************************************************************ 511 */ 512 void *xDataTOME_getXDataIfID( statusMessageReporting *smr, xDataTOM_element *TE, char const *ID ) { 513 514 xDataTOM_xDataInfo *xDI = xDataTOME_getXData( TE ); 515 516 if( xDI == NULL ) { 517 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "element '%s' does not have xData", TE->name ); 518 return( NULL ); 519 } 520 if( strcmp( ID, xDI->ID ) ) { 521 smr_setReportError2( smr, xDataTOM_smrLibraryID, 1, "xData has ID = '%s' not '%s' for element %s", xDI->ID, ID, TE->name ); 522 return( NULL ); 523 } 524 return( xDI->data ); 525 526 } 527 528 #if defined __cplusplus 529 } 530 #endif 531