Geant4 Cross Reference |
1 #include <stdio.h> 1 #include <stdio.h> 2 #include <stdlib.h> 2 #include <stdlib.h> 3 #include <string.h> 3 #include <string.h> 4 #include <stdarg.h> 4 #include <stdarg.h> 5 5 6 #ifdef WIN32 6 #ifdef WIN32 7 /*#define va_copy(dst, src) ((dst) = (src))*/ 7 /*#define va_copy(dst, src) ((dst) = (src))*/ 8 #endif 8 #endif 9 9 10 #include "statusMessageReporting.h" 10 #include "statusMessageReporting.h" 11 11 12 #if defined __cplusplus 12 #if defined __cplusplus 13 namespace GIDI { 13 namespace GIDI { 14 using namespace GIDI; 14 using namespace GIDI; 15 #endif 15 #endif 16 16 17 #define SMR_InitialMessageSize 1024 17 #define SMR_InitialMessageSize 1024 18 #define SMR_IncrementMessageSize 1024 18 #define SMR_IncrementMessageSize 1024 19 19 20 static int smrIsSetup = 0; 20 static int smrIsSetup = 0; 21 static char smr_mallocFailed[] = "statusMessag 21 static char smr_mallocFailed[] = "statusMessageReporting could not allocate memory for message"; 22 static char statusStringOk[] = "Ok", statusSt 22 static char statusStringOk[] = "Ok", statusStringInfo[] = "Info", 23 statusStringWarning[] = "Warning", statusS 23 statusStringWarning[] = "Warning", statusStringError[] = "Error", statusStringInvalid[] = "Invalid"; 24 24 25 static int numberOfRegisteredLibraries = 0; 25 static int numberOfRegisteredLibraries = 0; 26 static char unknownLibrary[] = "unknownID"; 26 static char unknownLibrary[] = "unknownID"; 27 static char tooManyLibrary[] = "tooManyIDs"; 27 static char tooManyLibrary[] = "tooManyIDs"; 28 static char invalidLibrary[] = "invalidID"; 28 static char invalidLibrary[] = "invalidID"; 29 static char errnoLibrary[] = "errnoID"; 29 static char errnoLibrary[] = "errnoID"; 30 static char smrLibrary[] = "statusMessageRepor 30 static char smrLibrary[] = "statusMessageReporting"; 31 static char *registeredLibraries[smr_maximumNu 31 static char *registeredLibraries[smr_maximumNumberOfRegisteredLibraries]; 32 32 33 static statusMessageReport *smr_reportNew( voi 33 static statusMessageReport *smr_reportNew( void ); 34 static int smr_reportInitialize( statusMessage 34 static int smr_reportInitialize( statusMessageReport *report ); 35 static void smr_reportRelease( statusMessageRe 35 static void smr_reportRelease( statusMessageReport *report ); 36 static int smr_setReport( statusMessageReporti 36 static int smr_setReport( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, 37 enum smr_status status, char const *fmt, v 37 enum smr_status status, char const *fmt, va_list *args ); 38 static int smr_setAllocationFailure( statusMes 38 static int smr_setAllocationFailure( statusMessageReport *report, char const *file, int line, char const *function, char const *fmt, va_list *args ); 39 /* 39 /* 40 ============================================== 40 ============================================================ 41 */ 41 */ 42 int smr_setup( void ) { 42 int smr_setup( void ) { 43 43 44 int i; 44 int i; 45 45 46 if( smrIsSetup ) return( 0 ); 46 if( smrIsSetup ) return( 0 ); 47 smrIsSetup = 1; 47 smrIsSetup = 1; 48 for( i = 0; i < smr_maximumNumberOfRegiste 48 for( i = 0; i < smr_maximumNumberOfRegisteredLibraries; ++i ) registeredLibraries[i] = NULL; 49 registeredLibraries[smr_unknownID] = unkno 49 registeredLibraries[smr_unknownID] = unknownLibrary; 50 ++numberOfRegisteredLibraries; 50 ++numberOfRegisteredLibraries; 51 registeredLibraries[smr_tooManyIDs] = tooM 51 registeredLibraries[smr_tooManyIDs] = tooManyLibrary; 52 ++numberOfRegisteredLibraries; 52 ++numberOfRegisteredLibraries; 53 registeredLibraries[smr_invalidID] = inval 53 registeredLibraries[smr_invalidID] = invalidLibrary; 54 ++numberOfRegisteredLibraries; 54 ++numberOfRegisteredLibraries; 55 registeredLibraries[smr_errnoID] = errnoLi 55 registeredLibraries[smr_errnoID] = errnoLibrary; 56 ++numberOfRegisteredLibraries; 56 ++numberOfRegisteredLibraries; 57 registeredLibraries[smr_smrID] = smrLibrar 57 registeredLibraries[smr_smrID] = smrLibrary; 58 ++numberOfRegisteredLibraries; 58 ++numberOfRegisteredLibraries; 59 return( 1 ); 59 return( 1 ); 60 } 60 } 61 /* 61 /* 62 ============================================== 62 ============================================================ 63 */ 63 */ 64 int smr_cleanup( void ) { 64 int smr_cleanup( void ) { 65 65 66 int i; 66 int i; 67 67 68 if( smrIsSetup == 0 ) return( 0 ); 68 if( smrIsSetup == 0 ) return( 0 ); 69 for( i = smr_smrID + 1; i < numberOfRegist 69 for( i = smr_smrID + 1; i < numberOfRegisteredLibraries; ++i ) smr_freeMemory( (void **) &(registeredLibraries[i]) ); 70 numberOfRegisteredLibraries = 0; 70 numberOfRegisteredLibraries = 0; 71 smrIsSetup = 0; 71 smrIsSetup = 0; 72 72 73 return( 0 ); 73 return( 0 ); 74 } 74 } 75 /* 75 /* 76 ============================================== 76 ============================================================ 77 */ 77 */ 78 int smr_registerLibrary( char const *libraryNa 78 int smr_registerLibrary( char const *libraryName ) { 79 79 80 int i; 80 int i; 81 81 82 if( smrIsSetup == 0 ) return( -1 ); 82 if( smrIsSetup == 0 ) return( -1 ); 83 if( numberOfRegisteredLibraries == smr_max 83 if( numberOfRegisteredLibraries == smr_maximumNumberOfRegisteredLibraries ) return( smr_tooManyIDs ); 84 for( i = 0; i < numberOfRegisteredLibrarie 84 for( i = 0; i < numberOfRegisteredLibraries; ++i ) { /* Check if name is already registered. */ 85 if( strcmp( libraryName, registeredLib 85 if( strcmp( libraryName, registeredLibraries[i] ) == 0 ) return( i ); 86 } 86 } 87 if( ( registeredLibraries[numberOfRegister 87 if( ( registeredLibraries[numberOfRegisteredLibraries] = strdup( libraryName ) ) == NULL ) return( -2 ); 88 ++numberOfRegisteredLibraries; 88 ++numberOfRegisteredLibraries; 89 return( numberOfRegisteredLibraries - 1 ); 89 return( numberOfRegisteredLibraries - 1 ); 90 } 90 } 91 /* 91 /* 92 ============================================== 92 ============================================================ 93 */ 93 */ 94 int smr_numberOfRegisteredLibraries( void ) { 94 int smr_numberOfRegisteredLibraries( void ) { 95 95 96 return( numberOfRegisteredLibraries ); 96 return( numberOfRegisteredLibraries ); 97 } 97 } 98 /* 98 /* 99 ============================================== 99 ============================================================ 100 */ 100 */ 101 char const *smr_getRegisteredLibrariesName( in 101 char const *smr_getRegisteredLibrariesName( int ID ) { 102 102 103 if( ( ID < 0 ) || ( ID >= smr_maximumNumbe 103 if( ( ID < 0 ) || ( ID >= smr_maximumNumberOfRegisteredLibraries ) ) return( NULL ); 104 return( registeredLibraries[ID] ); 104 return( registeredLibraries[ID] ); 105 } 105 } 106 /* 106 /* 107 ============================================== 107 ============================================================ 108 */ 108 */ 109 statusMessageReporting *smr_new( statusMessage 109 statusMessageReporting *smr_new( statusMessageReporting *smr, enum smr_status verbosity, int append ) { 110 110 111 statusMessageReporting *new_SMR; 111 statusMessageReporting *new_SMR; 112 112 113 if( ( new_SMR = (statusMessageReporting *) 113 if( ( new_SMR = (statusMessageReporting *) smr_malloc2( smr, sizeof( statusMessageReporting ), 0, "new_SMR" ) ) == NULL ) return( NULL ); 114 smr_initialize( new_SMR, verbosity, append 114 smr_initialize( new_SMR, verbosity, append ); 115 return( new_SMR ); 115 return( new_SMR ); 116 } 116 } 117 /* 117 /* 118 ============================================== 118 ============================================================ 119 */ 119 */ 120 int smr_initialize( statusMessageReporting *sm 120 int smr_initialize( statusMessageReporting *smr, enum smr_status verbosity, int append ) { 121 121 122 if( smr == NULL ) return( 0 ); 122 if( smr == NULL ) return( 0 ); 123 smr->verbosity = verbosity; 123 smr->verbosity = verbosity; 124 smr->append = append; 124 smr->append = append; 125 smr_reportInitialize( &(smr->report) ); 125 smr_reportInitialize( &(smr->report) ); 126 return( 0 ); 126 return( 0 ); 127 } 127 } 128 /* 128 /* 129 ============================================== 129 ============================================================ 130 */ 130 */ 131 statusMessageReporting *smr_clone( statusMessa 131 statusMessageReporting *smr_clone( statusMessageReporting *smr ) { 132 132 133 if( smr == NULL ) return( NULL ); 133 if( smr == NULL ) return( NULL ); 134 return( smr_new( NULL, smr->verbosity, smr 134 return( smr_new( NULL, smr->verbosity, smr->append ) ); 135 } 135 } 136 /* 136 /* 137 ============================================== 137 ============================================================ 138 */ 138 */ 139 void smr_release( statusMessageReporting *smr 139 void smr_release( statusMessageReporting *smr ) { 140 140 141 statusMessageReport *current, *next, *firs 141 statusMessageReport *current, *next, *first = smr_firstReport( smr ); 142 142 143 if( smr == NULL ) return; 143 if( smr == NULL ) return; 144 for( current = first; current != NULL; cur 144 for( current = first; current != NULL; current = next ) { 145 next = smr_nextReport( current ); 145 next = smr_nextReport( current ); 146 smr_reportRelease( current ); 146 smr_reportRelease( current ); 147 if( current != first ) smr_freeMemory( 147 if( current != first ) smr_freeMemory( (void **) ¤t ); 148 } 148 } 149 smr_initialize( smr, smr->verbosity, smr-> 149 smr_initialize( smr, smr->verbosity, smr->append ); 150 } 150 } 151 /* 151 /* 152 ============================================== 152 ============================================================ 153 */ 153 */ 154 void *smr_free( statusMessageReporting **smr ) 154 void *smr_free( statusMessageReporting **smr ) { 155 155 156 if( smr == NULL ) return( NULL ); 156 if( smr == NULL ) return( NULL ); 157 if( *smr != NULL ) { 157 if( *smr != NULL ) { 158 smr_release( *smr ); 158 smr_release( *smr ); 159 smr_freeMemory( (void **) smr ); 159 smr_freeMemory( (void **) smr ); 160 } 160 } 161 return( *smr ); 161 return( *smr ); 162 } 162 } 163 /* 163 /* 164 ============================================== 164 ============================================================ 165 */ 165 */ 166 static statusMessageReport *smr_reportNew( voi 166 static statusMessageReport *smr_reportNew( void ) { 167 167 168 statusMessageReport *report; 168 statusMessageReport *report; 169 169 170 if( ( report = (statusMessageReport *) smr 170 if( ( report = (statusMessageReport *) smr_malloc2( NULL, sizeof( statusMessageReport ), 0, "report" ) ) == NULL ) return( NULL ); 171 smr_reportInitialize( report ); 171 smr_reportInitialize( report ); 172 return( report ); 172 return( report ); 173 } 173 } 174 /* 174 /* 175 ============================================== 175 ============================================================ 176 */ 176 */ 177 static int smr_reportInitialize( statusMessage 177 static int smr_reportInitialize( statusMessageReport *report ) { 178 178 179 report->next = NULL; 179 report->next = NULL; 180 report->status = smr_status_Ok; 180 report->status = smr_status_Ok; 181 report->libraryID = smr_unknownID; 181 report->libraryID = smr_unknownID; 182 report->code = smr_codeNULL; 182 report->code = smr_codeNULL; 183 report->line = -1; 183 report->line = -1; 184 report->fileName[0] = 0; 184 report->fileName[0] = 0; 185 report->function[0] = 0; 185 report->function[0] = 0; 186 report->message = NULL; 186 report->message = NULL; 187 return( 0 ); 187 return( 0 ); 188 } 188 } 189 /* 189 /* 190 ============================================== 190 ============================================================ 191 */ 191 */ 192 static void smr_reportRelease( statusMessageRe 192 static void smr_reportRelease( statusMessageReport *report ) { 193 193 194 if( report->message != NULL ) { 194 if( report->message != NULL ) { 195 if( report->message != smr_mallocFaile 195 if( report->message != smr_mallocFailed ) smr_freeMemory( (void **) &(report->message) ); 196 } 196 } 197 smr_reportInitialize( report ); 197 smr_reportInitialize( report ); 198 } 198 } 199 /* 199 /* 200 ============================================== 200 ============================================================ 201 */ 201 */ 202 static int smr_setReport( statusMessageReporti 202 static int smr_setReport( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, 203 enum smr_status status, char const *fmt, v 203 enum smr_status status, char const *fmt, va_list *args ) { 204 204 205 char *userMsg; 205 char *userMsg; 206 statusMessageReport *report, *next; 206 statusMessageReport *report, *next; 207 207 208 if( smr == NULL ) return( 0 ); 208 if( smr == NULL ) return( 0 ); 209 if( (int) status < (int) smr->verbosity ) 209 if( (int) status < (int) smr->verbosity ) return( 0 ); 210 if( status == smr_status_Ok ) return( 0 ); 210 if( status == smr_status_Ok ) return( 0 ); 211 if( ( smr->report.status != smr_status_Ok 211 if( ( smr->report.status != smr_status_Ok ) && smr->append ) { 212 if( ( report = smr_reportNew( ) ) == N 212 if( ( report = smr_reportNew( ) ) == NULL ) return( smr_setAllocationFailure( NULL, file, line, function, fmt, args ) ); 213 for( next = smr_firstReport( smr ); ne 213 for( next = smr_firstReport( smr ); next->next != NULL; next = next->next ) ; 214 next->next = report; } 214 next->next = report; } 215 else { 215 else { 216 if( status <= smr->report.status ) ret 216 if( status <= smr->report.status ) return( 0 ); 217 smr_release( smr ); 217 smr_release( smr ); 218 report = &(smr->report); 218 report = &(smr->report); 219 } 219 } 220 report->status = status; 220 report->status = status; 221 if( ( libraryID < 0 ) || ( libraryID >= nu 221 if( ( libraryID < 0 ) || ( libraryID >= numberOfRegisteredLibraries ) ) libraryID = smr_invalidID; 222 report->libraryID = libraryID; 222 report->libraryID = libraryID; 223 report->code = code; 223 report->code = code; 224 report->line = line; 224 report->line = line; 225 if( file != NULL ) strncpy( report->fileNa 225 if( file != NULL ) strncpy( report->fileName, file, smr_maximumFileNameSize ); 226 report->fileName[smr_maximumFileNameSize] 226 report->fileName[smr_maximumFileNameSize] = 0; 227 if( function != NULL ) strncpy( report->fu 227 if( function != NULL ) strncpy( report->function, function, smr_maximumFileNameSize ); 228 report->function[smr_maximumFileNameSize] 228 report->function[smr_maximumFileNameSize] = 0; 229 229 230 if( ( report->message = smr_vallocateForma 230 if( ( report->message = smr_vallocateFormatMessage( fmt, args ) ) == NULL ) return( smr_setAllocationFailure( report, file, line, function, fmt, args ) ); 231 if( userInterface != NULL ) { 231 if( userInterface != NULL ) { 232 if( ( userMsg = (*(smr_userInterface * 232 if( ( userMsg = (*(smr_userInterface *) userInterface)( (void *) userInterface ) ) != NULL ) { 233 int userSize = (int) strlen( userM 233 int userSize = (int) strlen( userMsg ); 234 if( ( report->message = (char *) s 234 if( ( report->message = (char *) smr_realloc2( NULL, report->message, strlen( report->message ) + userSize + 2, "report->message" ) ) == NULL ) { 235 free( userMsg ); 235 free( userMsg ); 236 return( smr_setAllocationFailu 236 return( smr_setAllocationFailure( report, file, line, function, fmt, args ) ); 237 } 237 } 238 strcat( report->message, userMsg ) 238 strcat( report->message, userMsg ); 239 free( userMsg ); 239 free( userMsg ); 240 } 240 } 241 } 241 } 242 return( 0 ); 242 return( 0 ); 243 } 243 } 244 /* 244 /* 245 ============================================== 245 ============================================================ 246 */ 246 */ 247 static int smr_setAllocationFailure( statusMes 247 static int smr_setAllocationFailure( statusMessageReport *report, char const *file, int line, char const *function, char const *fmt, va_list *args ) { 248 248 249 vfprintf( stderr, fmt, *args ); 249 vfprintf( stderr, fmt, *args ); 250 va_end( *args ); 250 va_end( *args ); 251 fprintf( stderr, "\nAt line %d of %s in fu 251 fprintf( stderr, "\nAt line %d of %s in function %s\n", line, file, function ); 252 if( report != NULL ) { 252 if( report != NULL ) { 253 report->status = smr_status_Error; 253 report->status = smr_status_Error; 254 report->message = (char *) smr_mallocF 254 report->message = (char *) smr_mallocFailed; 255 return( 1 ); 255 return( 1 ); 256 } 256 } 257 return( -1 ); 257 return( -1 ); 258 } 258 } 259 /* 259 /* 260 ============================================== 260 ============================================================ 261 */ 261 */ 262 int smr_setReportInfo( statusMessageReporting 262 int smr_setReportInfo( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, ... ) { 263 263 264 int status; 264 int status; 265 va_list args; 265 va_list args; 266 266 267 va_start( args, fmt ); 267 va_start( args, fmt ); 268 status = smr_setReport( smr, userInterface 268 status = smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Info, fmt, &args ); 269 va_end( args ); 269 va_end( args ); 270 return( status ); 270 return( status ); 271 } 271 } 272 /* 272 /* 273 ============================================== 273 ============================================================ 274 */ 274 */ 275 int smr_vsetReportInfo( statusMessageReporting 275 int smr_vsetReportInfo( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args ) { 276 276 277 return( smr_setReport( smr, userInterface, 277 return( smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Info, fmt, args ) ); 278 } 278 } 279 /* 279 /* 280 ============================================== 280 ============================================================ 281 */ 281 */ 282 int smr_setReportWarning( statusMessageReporti 282 int smr_setReportWarning( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, ... ) { 283 283 284 int status; 284 int status; 285 va_list args; 285 va_list args; 286 286 287 va_start( args, fmt ); 287 va_start( args, fmt ); 288 status = smr_setReport( smr, userInterface 288 status = smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Warning, fmt, &args ); 289 va_end( args ); 289 va_end( args ); 290 return( status ); 290 return( status ); 291 } 291 } 292 /* 292 /* 293 ============================================== 293 ============================================================ 294 */ 294 */ 295 int smr_vsetReportWarning( statusMessageReport 295 int smr_vsetReportWarning( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args ) { 296 296 297 return( smr_setReport( smr, userInterface, 297 return( smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Warning, fmt, args ) ); 298 } 298 } 299 /* 299 /* 300 ============================================== 300 ============================================================ 301 */ 301 */ 302 int smr_setReportError( statusMessageReporting 302 int smr_setReportError( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, ... ) { 303 303 304 int status; 304 int status; 305 va_list args; 305 va_list args; 306 306 307 va_start( args, fmt ); 307 va_start( args, fmt ); 308 status = smr_setReport( smr, userInterface 308 status = smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Error, fmt, &args ); 309 va_end( args ); 309 va_end( args ); 310 return( status ); 310 return( status ); 311 } 311 } 312 /* 312 /* 313 ============================================== 313 ============================================================ 314 */ 314 */ 315 int smr_vsetReportError( statusMessageReportin 315 int smr_vsetReportError( statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args ) { 316 316 317 return( smr_setReport( smr, userInterface, 317 return( smr_setReport( smr, userInterface, file, line, function, libraryID, code, smr_status_Error, fmt, args ) ); 318 } 318 } 319 /* 319 /* 320 ============================================== 320 ============================================================ 321 */ 321 */ 322 enum smr_status smr_highestStatus( statusMessa 322 enum smr_status smr_highestStatus( statusMessageReporting *smr ) { 323 323 324 enum smr_status status = smr_status_Ok; 324 enum smr_status status = smr_status_Ok; 325 statusMessageReport *report; 325 statusMessageReport *report; 326 326 327 if( smr == NULL ) return( smr_status_Ok ); 327 if( smr == NULL ) return( smr_status_Ok ); 328 for( report = smr_firstReport( smr ); repo 328 for( report = smr_firstReport( smr ); report != NULL; report = smr_nextReport( report ) ) if( report->status > status ) status = report->status; 329 return( status ); 329 return( status ); 330 } 330 } 331 /* 331 /* 332 ============================================== 332 ============================================================ 333 */ 333 */ 334 int smr_isOk( statusMessageReporting *smr ) { 334 int smr_isOk( statusMessageReporting *smr ) { 335 335 336 return( smr_highestStatus( smr ) == smr_st 336 return( smr_highestStatus( smr ) == smr_status_Ok ); 337 } 337 } 338 /* 338 /* 339 ============================================== 339 ============================================================ 340 */ 340 */ 341 int smr_isInfo( statusMessageReporting *smr ) 341 int smr_isInfo( statusMessageReporting *smr ) { 342 342 343 return( smr_highestStatus( smr ) == smr_st 343 return( smr_highestStatus( smr ) == smr_status_Info ); 344 } 344 } 345 /* 345 /* 346 ============================================== 346 ============================================================ 347 */ 347 */ 348 int smr_isWarning( statusMessageReporting *smr 348 int smr_isWarning( statusMessageReporting *smr ) { 349 349 350 return( smr_highestStatus( smr ) == smr_st 350 return( smr_highestStatus( smr ) == smr_status_Warning ); 351 } 351 } 352 /* 352 /* 353 ============================================== 353 ============================================================ 354 */ 354 */ 355 int smr_isError( statusMessageReporting *smr ) 355 int smr_isError( statusMessageReporting *smr ) { 356 356 357 return( smr_highestStatus( smr ) == smr_st 357 return( smr_highestStatus( smr ) == smr_status_Error ); 358 } 358 } 359 /* 359 /* 360 ============================================== 360 ============================================================ 361 */ 361 */ 362 int smr_isWarningOrError( statusMessageReporti 362 int smr_isWarningOrError( statusMessageReporting *smr ) { 363 363 364 enum smr_status status = smr_highestStatus 364 enum smr_status status = smr_highestStatus( smr ); 365 365 366 return( ( status == smr_status_Warning ) | 366 return( ( status == smr_status_Warning ) || ( status == smr_status_Error ) ); 367 } 367 } 368 /* 368 /* 369 ============================================== 369 ============================================================ 370 */ 370 */ 371 int smr_isReportOk( statusMessageReport *repor 371 int smr_isReportOk( statusMessageReport *report ) { 372 372 373 if( report == NULL ) return( 0 ); 373 if( report == NULL ) return( 0 ); 374 return( report->status == smr_status_Ok ); 374 return( report->status == smr_status_Ok ); 375 } 375 } 376 /* 376 /* 377 ============================================== 377 ============================================================ 378 */ 378 */ 379 int smr_isReportInfo( statusMessageReport *rep 379 int smr_isReportInfo( statusMessageReport *report ) { 380 380 381 if( report == NULL ) return( 0 ); 381 if( report == NULL ) return( 0 ); 382 return( report->status == smr_status_Info 382 return( report->status == smr_status_Info ); 383 } 383 } 384 /* 384 /* 385 ============================================== 385 ============================================================ 386 */ 386 */ 387 int smr_isReportWarning( statusMessageReport * 387 int smr_isReportWarning( statusMessageReport *report ) { 388 388 389 if( report == NULL ) return( 0 ); 389 if( report == NULL ) return( 0 ); 390 return( report->status == smr_status_Warni 390 return( report->status == smr_status_Warning ); 391 } 391 } 392 /* 392 /* 393 ============================================== 393 ============================================================ 394 */ 394 */ 395 int smr_isReportError( statusMessageReport *re 395 int smr_isReportError( statusMessageReport *report ) { 396 396 397 if( report == NULL ) return( 0 ); 397 if( report == NULL ) return( 0 ); 398 return( report->status == smr_status_Error 398 return( report->status == smr_status_Error ); 399 } 399 } 400 /* 400 /* 401 ============================================== 401 ============================================================ 402 */ 402 */ 403 int smr_isReportWarningOrError( statusMessageR 403 int smr_isReportWarningOrError( statusMessageReport *report ) { 404 404 405 if( report == NULL ) return( 0 ); 405 if( report == NULL ) return( 0 ); 406 return( ( report->status == smr_status_War 406 return( ( report->status == smr_status_Warning ) || ( report->status == smr_status_Error ) ); 407 } 407 } 408 /* 408 /* 409 ============================================== 409 ============================================================ 410 */ 410 */ 411 int smr_numberOfReports( statusMessageReportin 411 int smr_numberOfReports( statusMessageReporting *smr ) { 412 412 413 int n = 0; 413 int n = 0; 414 statusMessageReport *report; 414 statusMessageReport *report; 415 415 416 if( smr == NULL ) return( 0 ); 416 if( smr == NULL ) return( 0 ); 417 if( smr->report.status == smr_status_Ok ) 417 if( smr->report.status == smr_status_Ok ) return( 0 ); 418 for( report = smr_firstReport( smr ); repo 418 for( report = smr_firstReport( smr ); report != NULL; report = smr_nextReport( report ) ) ++n; 419 return( n ); 419 return( n ); 420 } 420 } 421 /* 421 /* 422 ============================================== 422 ============================================================ 423 */ 423 */ 424 statusMessageReport *smr_firstReport( statusMe 424 statusMessageReport *smr_firstReport( statusMessageReporting *smr ) { 425 425 426 if( smr == NULL ) return( NULL ); 426 if( smr == NULL ) return( NULL ); 427 if( smr->report.status == smr_status_Ok ) 427 if( smr->report.status == smr_status_Ok ) return( NULL ); 428 return( &(smr->report) ); 428 return( &(smr->report) ); 429 } 429 } 430 /* 430 /* 431 ============================================== 431 ============================================================ 432 */ 432 */ 433 statusMessageReport *smr_nextReport( statusMes 433 statusMessageReport *smr_nextReport( statusMessageReport *report ) { 434 434 435 if( report == NULL ) return( NULL ); 435 if( report == NULL ) return( NULL ); 436 return( report->next ); 436 return( report->next ); 437 } 437 } 438 /* 438 /* 439 ============================================== 439 ============================================================ 440 */ 440 */ 441 enum smr_status smr_getVerbosity( statusMessag 441 enum smr_status smr_getVerbosity( statusMessageReporting *smr ) { 442 442 443 if( smr == NULL ) return( smr_status_Ok ); 443 if( smr == NULL ) return( smr_status_Ok ); 444 return( smr->verbosity ); 444 return( smr->verbosity ); 445 } 445 } 446 /* 446 /* 447 ============================================== 447 ============================================================ 448 */ 448 */ 449 int smr_getAppend( statusMessageReporting *smr 449 int smr_getAppend( statusMessageReporting *smr ) { 450 450 451 if( smr == NULL ) return( 0 ); 451 if( smr == NULL ) return( 0 ); 452 return( smr->append ); 452 return( smr->append ); 453 } 453 } 454 /* 454 /* 455 ============================================== 455 ============================================================ 456 */ 456 */ 457 int smr_getLibraryID( statusMessageReport *rep 457 int smr_getLibraryID( statusMessageReport *report ) { 458 458 459 if( report == NULL ) return( 0 ); 459 if( report == NULL ) return( 0 ); 460 return( report->libraryID ); 460 return( report->libraryID ); 461 } 461 } 462 /* 462 /* 463 ============================================== 463 ============================================================ 464 */ 464 */ 465 int smr_getCode( statusMessageReport *report ) 465 int smr_getCode( statusMessageReport *report ) { 466 466 467 if( report == NULL ) return( -1 ); 467 if( report == NULL ) return( -1 ); 468 return( report->code ); 468 return( report->code ); 469 } 469 } 470 /* 470 /* 471 ============================================== 471 ============================================================ 472 */ 472 */ 473 int smr_getLine( statusMessageReport *report ) 473 int smr_getLine( statusMessageReport *report ) { 474 474 475 if( report == NULL ) return( -1 ); 475 if( report == NULL ) return( -1 ); 476 return( report->line ); 476 return( report->line ); 477 } 477 } 478 /* 478 /* 479 ============================================== 479 ============================================================ 480 */ 480 */ 481 char const *smr_getFile( statusMessageReport * 481 char const *smr_getFile( statusMessageReport *report ) { 482 482 483 if( report == NULL ) return( NULL ); 483 if( report == NULL ) return( NULL ); 484 return( report->fileName ); 484 return( report->fileName ); 485 } 485 } 486 /* 486 /* 487 ============================================== 487 ============================================================ 488 */ 488 */ 489 char const *smr_getFunction( statusMessageRepo 489 char const *smr_getFunction( statusMessageReport *report ) { 490 490 491 if( report == NULL ) return( NULL ); 491 if( report == NULL ) return( NULL ); 492 return( report->function ); 492 return( report->function ); 493 } 493 } 494 /* 494 /* 495 ============================================== 495 ============================================================ 496 */ 496 */ 497 char const *smr_getMessage( statusMessageRepor 497 char const *smr_getMessage( statusMessageReport *report ) { 498 498 499 if( report == NULL ) return( NULL ); 499 if( report == NULL ) return( NULL ); 500 return( report->message ); 500 return( report->message ); 501 } 501 } 502 /* 502 /* 503 ============================================== 503 ============================================================ 504 */ 504 */ 505 char *smr_copyMessage( statusMessageReport *re 505 char *smr_copyMessage( statusMessageReport *report ) { 506 506 507 if( report == NULL ) return( NULL ); 507 if( report == NULL ) return( NULL ); 508 if( report->status == smr_status_Ok ) retu 508 if( report->status == smr_status_Ok ) return( NULL ); 509 return( smr_allocateFormatMessage( report- 509 return( smr_allocateFormatMessage( report->message ) ); 510 } 510 } 511 /* 511 /* 512 ============================================== 512 ============================================================ 513 */ 513 */ 514 char *smr_copyFullMessage( statusMessageReport 514 char *smr_copyFullMessage( statusMessageReport *report ) { 515 515 516 if( report == NULL ) return( NULL ); 516 if( report == NULL ) return( NULL ); 517 if( report->status == smr_status_Ok ) retu 517 if( report->status == smr_status_Ok ) return( NULL ); 518 return( smr_allocateFormatMessage( "%s\nAt 518 return( smr_allocateFormatMessage( "%s\nAt line %d of %s in function %s", report->message, report->line, report->fileName, report->function ) ); 519 } 519 } 520 /* 520 /* 521 ============================================== 521 ============================================================ 522 */ 522 */ 523 void smr_print( statusMessageReporting *smr, i 523 void smr_print( statusMessageReporting *smr, int clear ) { 524 524 525 smr_write( smr, stdout, clear ); 525 smr_write( smr, stdout, clear ); 526 } 526 } 527 /* 527 /* 528 ============================================== 528 ============================================================ 529 */ 529 */ 530 void smr_write( statusMessageReporting *smr, F 530 void smr_write( statusMessageReporting *smr, FILE *f, int clear ) { 531 531 532 statusMessageReport *report; 532 statusMessageReport *report; 533 533 534 if( smr == NULL ) return; 534 if( smr == NULL ) return; 535 for( report = smr_firstReport( smr ); repo 535 for( report = smr_firstReport( smr ); report != NULL; report = smr_nextReport( report ) ) smr_reportWrite( report, f ); 536 if( clear ) smr_release( smr ); 536 if( clear ) smr_release( smr ); 537 } 537 } 538 /* 538 /* 539 ============================================== 539 ============================================================ 540 */ 540 */ 541 void smr_reportPrint( statusMessageReport *rep 541 void smr_reportPrint( statusMessageReport *report ) { 542 542 543 smr_reportWrite( report, stderr ); 543 smr_reportWrite( report, stderr ); 544 } 544 } 545 /* 545 /* 546 ============================================== 546 ============================================================ 547 */ 547 */ 548 void smr_reportWrite( statusMessageReport *rep 548 void smr_reportWrite( statusMessageReport *report, FILE *f ) { 549 549 550 if( report->message != NULL ) fprintf( f, 550 if( report->message != NULL ) fprintf( f, "%s\nAt line %d of %s in function %s\n", report->message, report->line, report->fileName, report->function ); 551 } 551 } 552 /* 552 /* 553 ============================================== 553 ============================================================ 554 */ 554 */ 555 char const *smr_statusToString( enum smr_statu 555 char const *smr_statusToString( enum smr_status status ) { 556 556 557 switch( status ) { 557 switch( status ) { 558 case smr_status_Ok : return( statusStringO 558 case smr_status_Ok : return( statusStringOk ); 559 case smr_status_Info : return( statusStrin 559 case smr_status_Info : return( statusStringInfo ); 560 case smr_status_Warning : return( statusSt 560 case smr_status_Warning : return( statusStringWarning ); 561 case smr_status_Error : return( statusStri 561 case smr_status_Error : return( statusStringError ); 562 } 562 } 563 return( statusStringInvalid ); 563 return( statusStringInvalid ); 564 } 564 } 565 /* 565 /* 566 ============================================== 566 ============================================================ 567 */ 567 */ 568 char *smr_allocateFormatMessage( char const *f 568 char *smr_allocateFormatMessage( char const *fmt, ... ) { 569 569 570 char *s; 570 char *s; 571 va_list args; 571 va_list args; 572 572 573 va_start( args, fmt ); 573 va_start( args, fmt ); 574 s = smr_vallocateFormatMessage( fmt, &args 574 s = smr_vallocateFormatMessage( fmt, &args ); 575 va_end( args ); 575 va_end( args ); 576 return( s ); 576 return( s ); 577 } 577 } 578 /* 578 /* 579 ============================================== 579 ============================================================ 580 */ 580 */ 581 char *smr_vallocateFormatMessage( char const * 581 char *smr_vallocateFormatMessage( char const *fmt, va_list *args ) { 582 582 583 int n, size = SMR_InitialMessageSize; 583 int n, size = SMR_InitialMessageSize; 584 char buffer[SMR_InitialMessageSize], *mess 584 char buffer[SMR_InitialMessageSize], *message = buffer; 585 va_list args_; 585 va_list args_; 586 586 587 while( 1 ) { 587 while( 1 ) { 588 va_copy( args_, *args ); 588 va_copy( args_, *args ); 589 n = vsnprintf( message, size, fmt, arg 589 n = vsnprintf( message, size, fmt, args_ ); 590 va_end( args_ ); 590 va_end( args_ ); 591 if( ( n > -1 ) && ( n < size ) ) break 591 if( ( n > -1 ) && ( n < size ) ) break; 592 if( n > -1 ) { /* glibc 2.1 */ 592 if( n > -1 ) { /* glibc 2.1 */ 593 size = n + 3; } 593 size = n + 3; } 594 else { /* glibc 2.0 */ 594 else { /* glibc 2.0 */ 595 size += SMR_IncrementMessageSize; 595 size += SMR_IncrementMessageSize; 596 } 596 } 597 if( message == buffer ) message = NULL 597 if( message == buffer ) message = NULL; 598 if( ( message = (char *) realloc( mess 598 if( ( message = (char *) realloc( message, size ) ) == NULL ) return( NULL ); 599 } // Loop checking, 11.06.2015, T. Koi 599 } // Loop checking, 11.06.2015, T. Koi 600 if( message == buffer ) { 600 if( message == buffer ) { 601 if( ( message = (char *) malloc( n + 1 601 if( ( message = (char *) malloc( n + 1 ) ) == NULL ) return( NULL ); 602 strcpy( message, buffer ); } 602 strcpy( message, buffer ); } 603 else { 603 else { 604 if( ( message = (char *) realloc( mess 604 if( ( message = (char *) realloc( message, n + 1 ) ) == NULL ) return( NULL ); 605 } 605 } 606 return( message ); 606 return( message ); 607 } 607 } 608 /* 608 /* 609 ============================================== 609 ============================================================ 610 */ 610 */ 611 void *smr_malloc( statusMessageReporting *smr, 611 void *smr_malloc( statusMessageReporting *smr, size_t size, int zero, char const *forItem, char const *file, int line, char const *function ) { 612 612 613 void *p = smr_realloc( smr, NULL, size, fo 613 void *p = smr_realloc( smr, NULL, size, forItem, file, line, function ); 614 size_t i; 614 size_t i; 615 char *c; 615 char *c; 616 long long *l; 616 long long *l; 617 617 618 if( ( p != NULL ) && zero ) { 618 if( ( p != NULL ) && zero ) { 619 for( i = 0, l = (long long *) p; i < s 619 for( i = 0, l = (long long *) p; i < size / sizeof( long long ); i++, l++ ) *l = 0; 620 for( i *= sizeof( long long ), c = (ch 620 for( i *= sizeof( long long ), c = (char *) l; i < size; i++, c++ ) *c = 0; 621 } 621 } 622 622 623 return( p ); 623 return( p ); 624 } 624 } 625 /* 625 /* 626 ============================================== 626 ============================================================ 627 */ 627 */ 628 void *smr_realloc( statusMessageReporting *smr 628 void *smr_realloc( statusMessageReporting *smr, void *pOld, size_t size, char const *forItem, char const *file, int line, char const *function ) { 629 629 630 void *p = realloc( pOld, size ); 630 void *p = realloc( pOld, size ); 631 631 632 if( ( p == NULL ) && ( smr != NULL ) ) { 632 if( ( p == NULL ) && ( smr != NULL ) ) { 633 smr_setReportError( smr, NULL, file, l 633 smr_setReportError( smr, NULL, file, line, function, smr_smrID, -1, " smr_realloc: failed to realloc size = %z for variable %s\n", size, forItem ); 634 } 634 } 635 return( p ); 635 return( p ); 636 } 636 } 637 /* 637 /* 638 ============================================== 638 ============================================================ 639 */ 639 */ 640 void *smr_freeMemory( void **p ) { 640 void *smr_freeMemory( void **p ) { 641 641 642 if( p == NULL ) return( NULL ); 642 if( p == NULL ) return( NULL ); 643 if( *p != NULL ) { 643 if( *p != NULL ) { 644 free( *p ); 644 free( *p ); 645 *p = NULL; 645 *p = NULL; 646 } 646 } 647 return( *p ); 647 return( *p ); 648 } 648 } 649 /* 649 /* 650 ============================================== 650 ============================================================ 651 */ 651 */ 652 char *smr_allocateCopyString( statusMessageRep 652 char *smr_allocateCopyString( statusMessageReporting *smr, char const *s, char const *forItem, char const *file, int line, char const *function ) { 653 /* 653 /* 654 * User must free returned string. 654 * User must free returned string. 655 */ 655 */ 656 char *c = strdup( s ); 656 char *c = strdup( s ); 657 657 658 if( c == NULL ) smr_setReportError( smr, N 658 if( c == NULL ) smr_setReportError( smr, NULL, file, line, function, smr_smrID, -1, " smr_allocateCopyString: strdup failed for strlen( s ) = %z for variable %s", 659 strlen( s ), forItem ); 659 strlen( s ), forItem ); 660 return( c ); 660 return( c ); 661 } 661 } 662 /* 662 /* 663 ============================================== 663 ============================================================ 664 */ 664 */ 665 char *smr_allocateCopyStringN( statusMessageRe 665 char *smr_allocateCopyStringN( statusMessageReporting *smr, char const *s, size_t n, char const *forItem, char const *file, int line, char const *function ) { 666 /* 666 /* 667 * User must free returned string. 667 * User must free returned string. 668 */ 668 */ 669 size_t l = strlen( s ); 669 size_t l = strlen( s ); 670 char *c; 670 char *c; 671 671 672 if( l > n ) l = n; 672 if( l > n ) l = n; 673 if( ( c = (char *) smr_malloc( smr, l + 1, 673 if( ( c = (char *) smr_malloc( smr, l + 1, 0, forItem, file, line, function ) ) != NULL ) { 674 strncpy( c, s, n ); 674 strncpy( c, s, n ); 675 c[l] = 0; 675 c[l] = 0; 676 } 676 } 677 /* 677 /* 678 c = strndup( s, l ); # Not standard 678 c = strndup( s, l ); # Not standard on enough systems. 679 if( c != NULL ) { 679 if( c != NULL ) { 680 c[l] = 0; } 680 c[l] = 0; } 681 else { 681 else { 682 smr_setReportError( smr, NULL, file, 682 smr_setReportError( smr, NULL, file, line, function, smr_smrID, -1, " smr_allocateCopyStringN: strndup failed for strlen( s ) = %z for variable %s", 683 strlen( s ), forItem ); 683 strlen( s ), forItem ); 684 } 684 } 685 */ 685 */ 686 return( c ); 686 return( c ); 687 } 687 } 688 688 689 #if defined __cplusplus 689 #if defined __cplusplus 690 } 690 } 691 #endif 691 #endif 692 692