8 #include "MCGIDI_map.h"
9 #include <xDataTOM_importXML_private.h>
17 #define realpath( a, b ) GetFullPathName( a, PATH_MAX, b, NULL )
22 #if defined __cplusplus
27 static MCGIDI_mapEntry *
_MCGIDI_map_addEntry( statusMessageReporting *smr, MCGIDI_map *map,
enum MCGIDI_mapEntry_type type,
const char *schema,
const char *path,
28 const char *evaluation,
const char *projectile,
const char *target );
30 int projectile_PoPID,
int target_PoPID );
32 int projectile_PoPID,
int target_PoPID );
33 static int _MCGIDI_map_walkTree2( statusMessageReporting *smr, MCGIDI_map *map,
int level,
int (*handler)( MCGIDI_mapEntry *entry,
int level,
void *
userData),
37 #if defined __cplusplus
42 #if defined __cplusplus
53 if( ( map = (MCGIDI_map *) smr_malloc2( smr,
sizeof( MCGIDI_map ), 0,
"map" ) ) == NULL )
return( NULL );
62 memset( map, 0,
sizeof( MCGIDI_map ) );
63 map->status = MCGIDI_map_status_Ok;
65 map->smrUserInterface.map = map;
67 map->mapFileName = NULL;
68 map->numberOfEntries = 0;
69 map->mapEntries = NULL;
75 char const *aliases[] = {
"Co58m1",
"Ag110m1",
"Cd115m1",
"Te127m1",
"Te129m1",
"Pm148m1",
"Ho166m1",
"Am242m1",
"Am244m1",
"Es254m1" };
76 char const *targets[] = {
"Co58_e1",
"Ag110_e2",
"Cd115_e1",
"Te127_e2",
"Te129_e1",
"Pm148_e2",
"Ho166_e1",
"Am242_e2",
"Am244_e1",
"Es254_e2" };
77 int i1, n1 =
sizeof( aliases ) /
sizeof( aliases[1] );
80 for( i1 = 0; i1 < n1; i1++ ) {
93 MCGIDI_map *
MCGIDI_map_readFile( statusMessageReporting *smr,
const char *basePath,
const char *mapFileName ) {
98 xDataXML_document *doc;
99 xDataXML_element *element;
100 xDataXML_element *child;
102 const char *evaluation, *projectile, *targetName, *path, *schema;
107 if( ( basePath == NULL ) || ( mapFileName[0] ==
'/' ) ) {
108 strcpy( realPath, mapFileName ); }
110 strcpy( realPath, basePath );
111 strcat( realPath,
"/" );
112 strcat( realPath, mapFileName );
114 if( realpath( realPath, p ) == NULL ) {
115 smr_setReportError2( smr, smr_unknownID, MCGIDI_map_status_mapParsing,
"No map file %s\n", mapFileName );
118 n = (int) strlen( p ) + 2;
119 if( ( map->path = (
char *) smr_malloc2( smr, 2 * n, 0,
"map->path" ) ) == NULL )
return( (MCGIDI_map *)
MCGIDI_map_free( NULL, map ) );
120 map->mapFileName = &(map->path[n + 1]);
121 strcpy( map->mapFileName, p );
122 strcpy( map->path, p );
123 if( ( p = strrchr( map->path,
'/' ) ) != NULL ) {
126 strcpy( map->path,
"." );
133 if( strcmp( child->name,
"path" ) == 0 ) {
135 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"path missing path attribute" );
139 else if( strcmp( child->name,
"target" ) == 0 ) {
141 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"target missing 'schema' attribute" );
145 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"target missing 'path' attribute" );
149 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"target missing 'evaluation' attribute" );
153 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"target missing 'projectile' attribute" );
157 smr_setReportError3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"target missing 'target' attribute" );
162 smr_setReportError3( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_mapParsing,
"invalid element = %s", child->name );
184 MCGIDI_mapEntry *entry, *next;
187 for( entry = map->mapEntries; entry != NULL; entry = next ) {
189 if( entry->schema != NULL )
smr_freeMemory( (
void **) &(entry->schema) );
190 if( entry->path != NULL )
smr_freeMemory( (
void **) &(entry->path) );
191 if( entry->evaluation != NULL )
smr_freeMemory( (
void **) &(entry->evaluation) );
192 if( entry->projectile != NULL )
smr_freeMemory( (
void **) &(entry->projectile) );
193 if( entry->targetName != NULL )
smr_freeMemory( (
void **) &(entry->targetName) );
197 map->numberOfEntries = 0;
198 map->mapEntries = NULL;
199 map->status = MCGIDI_map_status_Ok;
206 return( map->mapEntries );
213 return( entry->next );
218 int MCGIDI_map_addTarget( statusMessageReporting *smr, MCGIDI_map *map,
const char *schema,
const char *path,
const char *evaluation,
const char *projectile,
const char *target ) {
220 return(
_MCGIDI_map_addEntry( smr, map, MCGIDI_mapEntry_type_target, schema, path, evaluation, projectile, target ) != NULL );
227 MCGIDI_mapEntry *entry =
_MCGIDI_map_addEntry( smr, map, MCGIDI_mapEntry_type_path, NULL, path, NULL, NULL, NULL );
229 if( entry != NULL ) {
230 if( ( entry->map =
MCGIDI_map_readFile( smr, map->path, entry->path ) ) == NULL ) entry = NULL;
232 return( entry != NULL );
237 static MCGIDI_mapEntry *
_MCGIDI_map_addEntry( statusMessageReporting *smr, MCGIDI_map *map,
enum MCGIDI_mapEntry_type type,
const char *schema,
238 const char *path,
const char *evaluation,
const char *projectile,
const char *targetName ) {
241 MCGIDI_mapEntry *entry;
243 if( ( entry = (MCGIDI_mapEntry * ) smr_malloc2( smr,
sizeof( MCGIDI_mapEntry ), 1,
"entry" ) ) == NULL )
return( NULL );
247 entry->schema = NULL;
249 entry->evaluation = NULL;
250 entry->projectile = NULL;
251 entry->targetName = NULL;
252 entry->globalPoPsIndexProjectile = entry->globalPoPsIndexTarget = -1;
256 if( ( entry->path = (
char *) smr_malloc2( smr, strlen( path ) + 1, 0,
"path" ) ) == NULL )
goto err;
257 strcpy( entry->path, path );
260 if( evaluation != NULL ) {
261 if( ( entry->evaluation = (
char *) smr_malloc2( smr, strlen( evaluation ) + 1, 0,
"evaluation" ) ) == NULL )
goto err;
262 strcpy( entry->evaluation, evaluation );
265 if( projectile != NULL ) {
267 if( ( entry->projectile = (
char *) smr_malloc2( smr, strlen( projectile ) + 1, 0,
"projectile" ) ) == NULL )
goto err;
268 strcpy( entry->projectile, projectile );
271 if( targetName != NULL ) {
273 if( ( entry->targetName = (
char *) smr_malloc2( smr, strlen( targetName ) + 1, 0,
"target" ) ) == NULL )
goto err;
274 strcpy( entry->targetName, targetName );
277 if( schema != NULL ) {
278 if( ( entry->schema = (
char *) smr_malloc2( smr, strlen( schema ) + 1, 0,
"schema" ) ) == NULL )
goto err;
279 strcpy( entry->schema, schema );
282 if( map->mapEntries == NULL ) {
283 map->mapEntries = entry; }
285 for( p = map->mapEntries; p->next != NULL; p = p->next );
288 map->numberOfEntries++;
303 int projectile_PoPID,
int target_PoPID ) {
311 if( !
smr_isOk( smr ) )
return( NULL );
312 if( map->status != MCGIDI_map_status_Ok )
return( NULL );
315 if( ( path == NULL ) &&
smr_isOk( smr ) ) {
316 if( evaluation == NULL ) {
317 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, 1,
"target %s for projectile %s not found",
318 targetName, projectileName ); }
320 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, 1,
"target %s for projectile %s and evaluation %s not found",
321 targetName, projectileName, evaluation );
330 int projectile_PoPID,
int target_PoPID ) {
332 MCGIDI_mapEntry *entry;
336 if( evaluation != NULL ) {
337 if( strlen( evaluation ) == 0 ) evaluation = NULL;
340 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
341 switch( entry->type ) {
342 case MCGIDI_mapEntry_type_target :
343 if( ( projectile_PoPID == entry->globalPoPsIndexProjectile ) && ( target_PoPID == entry->globalPoPsIndexTarget ) ) {
344 if( evaluation == NULL ) {
347 status = strcmp( evaluation, entry->evaluation ) == 0;
350 n = (int) strlen( map->path ) + 1 + (int) strlen( entry->path ) + 1;
351 if( ( path = (
char * ) smr_malloc2( smr, n, 0,
"path" ) ) == NULL )
return( NULL );
352 strcpy( path, map->path );
354 if( entry->path[0] ==
'/' ) {
355 strcpy( path, entry->path ); }
357 strcat( path, entry->path );
363 case MCGIDI_mapEntry_type_path :
367 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_UnknownType,
"unknown type = %d", entry->type );
376 char *
MCGIDI_map_findTarget( statusMessageReporting *smr, MCGIDI_map *map,
const char *evaluation,
const char *projectile,
const char *targetName ) {
378 int projectile_PoPID, target_PoPID;
393 MCGIDI_map *mapAllOfTarget;
395 if( map->status != MCGIDI_map_status_Ok )
return( NULL );
396 if( ( mapAllOfTarget =
MCGIDI_map_new( smr ) ) == NULL )
return( NULL );
398 if( ( status != 0 ) ) mapAllOfTarget = (MCGIDI_map *)
MCGIDI_map_free( smr, mapAllOfTarget );
399 return( mapAllOfTarget );
405 int projectile_PoPID,
int target_PoPID ) {
407 MCGIDI_mapEntry *entry;
409 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
410 switch( entry->type ) {
411 case MCGIDI_mapEntry_type_target :
412 if( ( projectile_PoPID == entry->globalPoPsIndexProjectile ) && ( target_PoPID == entry->globalPoPsIndexTarget ) ) {
413 if(
_MCGIDI_map_addEntry( smr, mapAllOfTarget, entry->type, entry->schema, entry->path, entry->evaluation, entry->projectile,
414 entry->targetName ) == NULL )
return( 1 );
417 case MCGIDI_mapEntry_type_path :
421 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_UnknownType,
"unknown type = %d", entry->type );
432 int projectile_PoPID, target_PoPID;
445 if( endPath[0] ==
'/' ) {
446 if( ( path = (
char *) smr_malloc2( smr, strlen( endPath ) + 1, 0,
"path" ) ) == NULL )
return( NULL );
449 if( ( path = (
char *) smr_malloc2( smr, strlen( map->path ) + strlen( endPath ) + 2, 0,
"path" ) ) == NULL )
return( NULL );
450 strcpy( path, map->path );
453 strcat( path, endPath );
462 MCGIDI_map *map = target->parent;
464 switch( target->type ) {
465 case MCGIDI_mapEntry_type_target :
468 case MCGIDI_mapEntry_type_path :
469 smr_setReportInfo3p( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_UnknownType,
"path type not allowed" );
472 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_UnknownType,
"unknown type = %d", target->type );
480 static int _MCGIDI_map_walkTree2( statusMessageReporting *smr, MCGIDI_map *map,
int level,
int (*handler)( MCGIDI_mapEntry *entry,
int level,
void *
userData),
483 MCGIDI_mapEntry *entry;
485 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
486 if( handler( entry, level, userData ) != 0 )
return( 1 );
487 if( entry->type == MCGIDI_mapEntry_type_path )
if(
_MCGIDI_map_walkTree2( smr, entry->map, level + 1, handler, userData ) != 0 )
return( 1 );
494 int MCGIDI_map_walkTree( statusMessageReporting *smr, MCGIDI_map *map,
int (*handler)( MCGIDI_mapEntry *entry,
int level,
void *
userData),
void *userData ) {
503 MCGIDI_mapEntry *entry;
505 char targetFormat[] =
"<target schema=\"%s\" evaluation=\"%s\" projectile=\"%s\" target=\"%s\" path=\"%s\"/>\n";
506 char pathFormat[] =
"<path projectile=\"%s\" path=\"%s\"/>\n";
507 char start[] =
"<map>\n";
508 char end[] =
"</map>";
509 int n = 0, nStart = (int) strlen( start ), nEnd = (int) strlen( end );
510 int nTarget = (int) strlen( targetFormat ) - 10, nPath = (int) strlen( pathFormat ) - 4;
512 if( map->status != MCGIDI_map_status_Ok )
return( NULL );
514 n = nStart + nEnd + 1;
515 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
516 switch( entry->type ) {
517 case MCGIDI_mapEntry_type_target :
518 n += (int) ( strlen( entry->schema ) + strlen( entry->path ) + strlen( entry->evaluation ) + strlen( entry->projectile ) + strlen( entry->targetName ) + nTarget );
520 case MCGIDI_mapEntry_type_path :
521 n += (int ) strlen( entry->path ) + (int ) strlen( entry->projectile ) + nPath;
524 smr_setReportInfo3( smr, &(map->smrUserInterface), smr_unknownID, MCGIDI_map_status_UnknownType,
"unknown type = %d", entry->type );
529 if( ( s = (
char *) smr_malloc2( smr, n, 0,
"xml string" ) ) == NULL )
return( NULL );
533 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
534 switch( entry->type ) {
535 case MCGIDI_mapEntry_type_target :
536 sprintf( p, targetFormat, entry->schema, entry->evaluation, entry->projectile, entry->targetName, entry->path );
538 case MCGIDI_mapEntry_type_path :
539 sprintf( p, pathFormat, entry->projectile, entry->path );
556 MCGIDI_mapEntry *entry;
558 int n = (int ) strlen( sLevel ) / 4;
560 if( map->status != MCGIDI_map_status_Ok ) {
561 fprintf( f,
"Bad map status = %d\n", map->status );
564 if( level < n ) sLevel[4 * level] = 0;
565 fprintf( f,
"%smap->path = %s\n", sLevel, map->path );
566 fprintf( f,
"%smap->mapFileName = %s\n", sLevel, map->mapFileName );
567 for( entry = map->mapEntries; entry != NULL; entry = entry->next ) {
568 switch( entry->type ) {
569 case MCGIDI_mapEntry_type_target :
570 fprintf( f,
"%sType = target: schema = %s: evaluation = %s: projectile = %s: target = %s: path = %s\n", sLevel, entry->schema,
571 entry->evaluation, entry->projectile, entry->targetName, entry->path );
573 case MCGIDI_mapEntry_type_path :
574 fprintf( f,
"%sType = path: path = %s\n", sLevel, entry->path );
578 fprintf( f,
"%sUnknown type = %d\n", sLevel, entry->type );
587 MCGIDI_map_smr *smrUserInterface = (MCGIDI_map_smr *) userData;
592 #if defined __cplusplus
static char * _MCGIDI_map_smrUserInterface(void *userData)
char const * xDataXML_getAttributesValueInElement(xDataXML_element *element, char const *name)
PoP * PoPs_addAliasIfNeeded(statusMessageReporting *smr, char const *name, char const *alias)
MCGIDI_map * MCGIDI_map_findAllOfTarget(statusMessageReporting *smr, MCGIDI_map *map, const char *projectile, const char *targetName)
char * MCGIDI_map_findTarget(statusMessageReporting *smr, MCGIDI_map *map, const char *evaluation, const char *projectile, const char *targetName)
char * MCGIDI_map_getTargetsFullPath(statusMessageReporting *smr, MCGIDI_mapEntry *target)
static char * _MCGIDI_map_findTargetViaPoPIDs2(statusMessageReporting *smr, MCGIDI_map *map, const char *evaluation, int projectile_PoPID, int target_PoPID)
char * MCGIDI_map_getFullPath(statusMessageReporting *smr, MCGIDI_map *map, const char *endPath)
MCGIDI_map * MCGIDI_map_new(statusMessageReporting *smr)
MCGIDI_map * MCGIDI_map_findAllOfTargetViaPoPIDs(statusMessageReporting *smr, MCGIDI_map *map, int projectile_PoPID, int target_PoPID)
int MCGIDI_map_walkTree(statusMessageReporting *smr, MCGIDI_map *map, int(*handler)(MCGIDI_mapEntry *entry, int level, void *userData), void *userData)
void * MCGIDI_map_free(statusMessageReporting *smr, MCGIDI_map *map)
char * smr_allocateFormatMessage(char const *fmt,...)
char * MCGIDI_map_findTargetViaPoPIDs(statusMessageReporting *smr, MCGIDI_map *map, const char *evaluation, int projectile_PoPID, int target_PoPID)
xDataXML_document * xDataXML_importFile2(statusMessageReporting *smr, char const *fileName)
int MCGIDI_map_addPath(statusMessageReporting *smr, MCGIDI_map *map, const char *path)
static MCGIDI_mapEntry * _MCGIDI_map_addEntry(statusMessageReporting *smr, MCGIDI_map *map, enum MCGIDI_mapEntry_type type, const char *schema, const char *path, const char *evaluation, const char *projectile, const char *target)
int smr_isOk(statusMessageReporting *smr)
int lPoPs_addParticleIfNeeded(statusMessageReporting *smr, char const *name, char const *special)
MCGIDI_mapEntry * MCGIDI_map_getFirstEntry(MCGIDI_map *map)
void * smr_freeMemory(void **p)
char const * PoPs_getName_atIndex(statusMessageReporting *smr, int index)
int MCGIDI_map_addTarget(statusMessageReporting *smr, MCGIDI_map *map, const char *schema, const char *path, const char *evaluation, const char *projectile, const char *target)
MCGIDI_map * MCGIDI_map_readFile(statusMessageReporting *smr, const char *basePath, const char *mapFileName)
void * xDataXML_freeDoc(statusMessageReporting *smr, xDataXML_document *doc)
xDataXML_element * xDataXML_getNextElement(xDataXML_element *element)
void MCGIDI_map_release(statusMessageReporting *smr, MCGIDI_map *map)
MCGIDI_mapEntry * MCGIDI_map_getNextEntry(MCGIDI_mapEntry *entry)
int MCGIDI_map_initialize(statusMessageReporting *smr, MCGIDI_map *map)
char * MCGIDI_map_toXMLString(statusMessageReporting *smr, MCGIDI_map *map)
xDataXML_element * xDataXML_getFirstElement(xDataXML_element *element)
void MCGIDI_map_simpleWrite(FILE *f, MCGIDI_map *map)
xDataXML_element * xDataXML_getDocumentsElement(xDataXML_document *doc)
static int _MCGIDI_map_walkTree2(statusMessageReporting *smr, MCGIDI_map *map, int level, int(*handler)(MCGIDI_mapEntry *entry, int level, void *userData), void *userData)
static int _MCGIDI_map_findAllOfTargetViaPoPIDs2(statusMessageReporting *smr, MCGIDI_map *mapAllOfTarget, MCGIDI_map *map, int projectile_PoPID, int target_PoPID)
static void _MCGIDI_map_simpleWrite2(FILE *f, MCGIDI_map *map, int level)