Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
tpi_IDs.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4# Produced at the Lawrence Livermore National Laboratory
5# Written by Bret R. Beck, [email protected].
6# CODE-461393
7# All rights reserved.
8#
9# This file is part of GIDI. For details, see nuclear.llnl.gov.
10# Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11#
12# Redistribution and use in source and binary forms, with or without modification,
13# are permitted provided that the following conditions are met:
14#
15# 1) Redistributions of source code must retain the above copyright notice,
16# this list of conditions and the disclaimer below.
17# 2) Redistributions in binary form must reproduce the above copyright notice,
18# this list of conditions and the disclaimer (as noted below) in the
19# documentation and/or other materials provided with the distribution.
20# 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21# used to endorse or promote products derived from this software without
22# specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27# SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34# <<END-copyright>>
35*/
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40
41#include <tpi_IDs.h>
42
43#if defined __cplusplus
44namespace GIDI {
45using namespace GIDI;
46#endif
47
48static tpi_channelID *_tpi_channelID_parse2( statusMessageReporting *smr, char const *str, char const *origStr, int isDecayChannel, char **EOP );
49static tpi_spectralID *_tpi_spectralID_parse2( statusMessageReporting *smr, char const *str, char const *origStr, char **EOP );
50/*
51***************************************************
52*/
54
56
57 //if( ( channelID = xData_malloc2( smr, sizeof( tpi_channelID ), 0, "channelID" ) ) == NULL ) return( NULL );
58 if( ( channelID = (tpi_channelID*) xData_malloc2( smr, sizeof( tpi_channelID ), 0, "channelID" ) ) == NULL ) return( NULL );
60 return( channelID );
61}
62/*
63***************************************************
64*/
65//int tpi_channelID_initialize( statusMessageReporting *smr, tpi_channelID *channelID ) {
67
68 memset( channelID, 0, sizeof( tpi_channelID ) );
69 return( 0 );
70}
71/*
72***************************************************
73*/
75
76 if( channelID != NULL ) {
78 xData_free( smr, channelID );
79 }
80 return( NULL );
81}
82/*
83***************************************************
84*/
86
87 tpi_spectralID *spectralID, *next;
88
89 for( spectralID = channelID->spectralIDs; spectralID != NULL; spectralID = next ) {
90 next = spectralID->next;
91 tpi_spectralID_free( smr, spectralID );
92 }
94 return( 0 );
95}
96/*
97***************************************************
98*/
99tpi_channelID *tpi_channelID_parse( statusMessageReporting *smr, char const *str, char **EOP ) {
100
101 return( _tpi_channelID_parse2( smr, str, str, 0, EOP ) );
102}
103/*
104***************************************************
105*/
106static tpi_channelID *_tpi_channelID_parse2( statusMessageReporting *smr, char const *str, char const *origStr, int isDecayChannel, char **EOP ) {
107
109 tpi_spectralID *spectralID, *priorSpectral;
110
111 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, (char *) str );
112 if( **EOP == 0 ) {
113 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Empty channel string to parse for '%s'", origStr );
114 return( NULL );
115 }
116 if( ( channelID = tpi_channelID_allocate( smr ) ) == NULL ) return( NULL );
117 priorSpectral= (tpi_spectralID *) &(channelID->spectralIDs);
118 while( 1 ) {
119 //if( ( spectralID = _tpi_spectralID_parse2( smr, *EOP, origStr, EOP ) ) == NULL ) return( tpi_channelID_free( smr, channelID ) );
120 if( ( spectralID = _tpi_spectralID_parse2( smr, *EOP, origStr, EOP ) ) == NULL ) return( (tpi_channelID*)tpi_channelID_free( smr, channelID ) );
121 priorSpectral->next = spectralID;
122 priorSpectral = spectralID;
123 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
124 if( **EOP == 0 ) break;
125 if( isDecayChannel && ( **EOP == ')' ) ) break;
126 if( **EOP != '+' ) {
127 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing '+' (or maybe ')') in channelID at '%s' of '%s'", *EOP, origStr );
128 //return( tpi_channelID_free( smr, channelID ) );
129 return( (tpi_channelID*)tpi_channelID_free( smr, channelID ) );
130 }
131 (*EOP)++;
132 }
133 return( channelID );
134}
135/*
136***************************************************
137*/
139
140 return( tpi_channelID_toStringSans( smr, channelID, gStr, NULL ) );
141}
142/*
143***************************************************
144*/
146
147 //char *sans[] = { "revision", NULL };
148 char *sans[] = { (char*)"revision", NULL };
149
150 return( tpi_channelID_toStringSans( smr, channelID, gStr, sans ) );
151}
152/*
153***************************************************
154*/
156
157 tpi_spectralID *spectralID;
158
159 for( spectralID = channelID->spectralIDs; spectralID != NULL; spectralID = spectralID->next ) {
160 if( ( tpi_spectralID_toStringSans( smr, spectralID, gStr, sans ) ) != 0 ) return( 1 );
161 if( spectralID->next != NULL ) {
162 if( ( gString_addTo( smr, gStr, " + " ) ) != 0 ) return( 1 );
163 }
164 }
165 return( 0 );
166}
167/*
168***************************************************
169*/
171
172 tpi_spectralID *spectralID;
173
174 //if( ( spectralID = xData_malloc2( smr, sizeof( tpi_spectralID ), 1, "spectralID" ) ) == NULL ) return( NULL );
175 if( ( spectralID = (tpi_spectralID*) xData_malloc2( smr, sizeof( tpi_spectralID ), 1, "spectralID" ) ) == NULL ) return( NULL );
176 tpi_spectralID_initialize( smr, spectralID );
177 return( spectralID );
178}
179/*
180***************************************************
181*/
182//int tpi_spectralID_initialize( statusMessageReporting *smr, tpi_spectralID *spectralID ) {
184
185 memset( spectralID, 0, sizeof( tpi_spectralID ) );
186 return( 0 );
187}
188/*
189***************************************************
190*/
192
193 if( spectralID != NULL ) {
194 tpi_spectralID_release( smr, spectralID );
195 xData_free( smr, spectralID );
196 }
197 return( NULL );
198}
199/*
200***************************************************
201*/
203
204 tpi_spectralIDQualifier *qualifier, *next;
205
206 if( spectralID->name != NULL ) free( spectralID->name );
207 for( qualifier = spectralID->qualifiers; qualifier != NULL; qualifier = next ) {
208 next = qualifier->next;
209 xData_free( smr, qualifier );
210 }
211 if( spectralID->decayChannel != NULL ) tpi_channelID_free( smr, spectralID->decayChannel );
212 tpi_spectralID_initialize( smr, spectralID );
213 return( 0 );
214}
215/*
216***************************************************
217*/
218tpi_spectralID *tpi_spectralID_parse( statusMessageReporting *smr, char const *str, char **EOP ) {
219
220 return( _tpi_spectralID_parse2( smr, str, str, EOP ) );
221}
222/*
223***************************************************
224*/
225static tpi_spectralID *_tpi_spectralID_parse2( statusMessageReporting *smr, char const *str, char const *origStr, char **EOP ) {
226
227 int breakup = 0, i1, i2, m;
228 double d;
229 char const *s, *q;
230 char *e;
231 char c, bOrC;
232 tpi_spectralID *spectralID;
233 tpi_spectralIDQualifier *qualifier, *priorQualifier = NULL;
234
235 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, str );
236 if( **EOP == 0 ) {
237 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Empty spectralID string to parse for '%s'", origStr );
238 return( NULL );
239 }
240 if( **EOP == '(' ) { /* Breakup spectralID, like '(Be_8 -> He_4[multipliticy:"2"])' */
241 breakup = 1;
242 (*EOP)++;
243 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
244 }
245 if( !isalpha( **EOP ) ) {
246 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Invalid spectralID name '%s' in '%s'", *EOP, origStr );
247 return( NULL );
248 }
249 for( s = *EOP, i1 = 0; ( isalnum( **EOP ) ) || ( **EOP == '_' ); (*EOP)++ ) i1++;
250 if( ( spectralID = tpi_spectralID_allocate( smr ) ) == NULL ) return( NULL );
251 //if( ( spectralID->name = tpi_misc_allocateAndCopyNCharacters( smr, s, i1 ) ) == NULL ) return( tpi_spectralID_free( smr, spectralID ) );
252 if( ( spectralID->name = tpi_misc_allocateAndCopyNCharacters( smr, s, i1 ) ) == NULL ) return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
253 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
254 if( **EOP == '[' ) { /* Spectral has qualifiers. */
255 priorQualifier = (tpi_spectralIDQualifier *) &(spectralID->qualifiers);
256 bOrC = '[';
257 while( **EOP == bOrC ) {
258 bOrC = ',';
259 (*EOP)++; /* Get qualifier's name. */
260 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
261 if( !isalpha( **EOP ) ) {
262 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Invalid qualifier name '%s' in '%s'", *EOP, origStr );
263 //return( tpi_spectralID_free( smr, spectralID ) );
264 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
265 }
266 for( s = *EOP, i1 = 0; ( isalnum( **EOP ) ) || ( **EOP == '_' ); (*EOP)++ ) i1++;
267
268 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP ); /* Skip qualifier's separator. */
269 if( **EOP != ':' ) {
270 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing ':' in qualifier defintion at '%s' in '%s'", *EOP, origStr );
271 //return( tpi_spectralID_free( smr, spectralID ) );
272 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
273 }
274
275 (*EOP)++; /* Get qualifier's value. */
276 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
277 if( ( **EOP != '"' ) && ( **EOP != '\'' ) ) {
278 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing start quote in qualifier defintion at '%s' in '%s'", *EOP, origStr );
279 //return( tpi_spectralID_free( smr, spectralID ) );
280 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
281 }
282 c = **EOP;
283 (*EOP)++;
284 for( q = *EOP, i2 = 0; ( **EOP != c ) && ( **EOP != 0 ); (*EOP)++ ) i2++;
285 if( **EOP != c ) {
286 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing end quote in qualifier defintion at '%s' in '%s'", *EOP, origStr );
287 //return( tpi_spectralID_free( smr, spectralID ) );
288 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
289 }
290 (*EOP)++;
291 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
292
293 //if( ( qualifier = xData_malloc2( smr, sizeof( tpi_spectralIDQualifier ) + i1 + i2 + 2, 1, "qualifier" ) ) == NULL )
294 if( ( qualifier = (tpi_spectralIDQualifier*) xData_malloc2( smr, sizeof( tpi_spectralIDQualifier ) + i1 + i2 + 2, 1, "qualifier" ) ) == NULL )
295 //return( tpi_spectralID_free( smr, spectralID ) );
296 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
297 qualifier->next = NULL;
298 qualifier->name = (char *) &(qualifier[1]);
299 qualifier->value = &(qualifier->name[i1+1]);
300 strncpy( qualifier->name, s, i1 );
301 qualifier->name[i1] = 0;
302 strncpy( qualifier->value, q, i2 );
303 qualifier->value[i2] = 0;
304
305 if( strcmp( qualifier->name, "revision" ) == 0 ) {
306 spectralID->revision = qualifier->value; }
307 else if( strcmp( qualifier->name, "multiplicity" ) == 0 ) {
308 spectralID->multiplicityStr = qualifier->value; }
309 else if( strcmp( qualifier->name, "level" ) == 0 ) {
310 spectralID->levelStr = qualifier->value;
311 }
312 priorQualifier->next = qualifier;
313 priorQualifier = qualifier;
314 }
315 if( **EOP != ']' ) {
316 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing ']' for qualifier at '%s' in '%s'", *EOP, origStr );
317 //return( tpi_spectralID_free( smr, spectralID ) );
318 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
319 }
320 (*EOP)++;
321 if( spectralID->multiplicityStr != NULL ) {
322 m = strtol( spectralID->multiplicityStr, &e, 10 );
323 if( ( *e == 0 ) && ( e != spectralID->multiplicityStr ) ) spectralID->multiplicity = m;
324 }
325 if( spectralID->levelStr != NULL ) {
326 d = strtod( spectralID->levelStr, &e );
327 if( ( *e == 0 ) && ( e != spectralID->levelStr ) ) spectralID->level = d;
328 }
329 }
330 if( breakup ) {
331 *EOP = (char *) tpi_misc_firstNonWhiteSpace( smr, *EOP );
332 if( ( **EOP != '-' ) || ( (*EOP)[1] != '>' ) ) {
333 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing '->' for breakup at '%s' in '%s'", *EOP, origStr );
334 //return( tpi_spectralID_free( smr, spectralID ) );
335 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
336 }
337 *EOP += 2;
338 if( (spectralID->decayChannel = _tpi_channelID_parse2( smr, *EOP, origStr, breakup, EOP )) == NULL )
339 //return( tpi_spectralID_free(smr, spectralID) );
340 return( (tpi_spectralID*) tpi_spectralID_free(smr, spectralID) );
341 if( **EOP != ')' ) {
342 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "Missing ')' for breakup at '%s' in '%s'", *EOP, origStr );
343 //return( tpi_spectralID_free( smr, spectralID ) );
344 return( (tpi_spectralID*) tpi_spectralID_free( smr, spectralID ) );
345 }
346 (*EOP)++;
347 }
348 return( spectralID );
349}
350/*
351***************************************************
352*/
354
355 return( tpi_spectralID_toStringSans( smr, spectralID, gStr, NULL ) );
356}
357/*
358***************************************************
359*/
361
362 //char *sans[] = { "revision", NULL };
363 char *sans[] = { (char*)"revision", NULL };
364
365 return( tpi_spectralID_toStringSans( smr, spectralID, gStr, sans ) );
366}
367/*
368***************************************************
369*/
370
371int tpi_spectralID_toStringSans( statusMessageReporting *smr, tpi_spectralID *spectralID, gString *gStr, char *sans[] ) {
372
373 tpi_spectralIDQualifier *qualifier;
374 int i;
375 char **san, *sSan[] = { NULL };
376
377 if( sans == NULL ) sans = sSan;
378 if( spectralID->decayChannel != NULL ) if( gString_addTo( smr, gStr, "(" ) != 0 ) return( 1 );
379 if( ( gString_addTo( smr, gStr, spectralID->name ) ) != 0 ) return( 1 );
380 if( spectralID->qualifiers != NULL ) {
381 for( qualifier = spectralID->qualifiers, i = 0; qualifier != NULL; qualifier = qualifier->next ) i++;
382 for( qualifier = spectralID->qualifiers; qualifier != NULL; qualifier = qualifier->next ) {
383 for( san = (char **) sans; *san != NULL; san++ ) {
384 if( strcmp( *san, qualifier->name ) == 0 ) {
385 i--;
386 break;
387 }
388 }
389 }
390 if( i > 0 ) {
391 if( gString_addTo( smr, gStr, "[" ) != 0 ) return( 1 );
392 for( qualifier = spectralID->qualifiers; qualifier != NULL; qualifier = qualifier->next ) {
393 for( san = (char **) sans; *san != NULL; san++ ) if( strcmp( *san, qualifier->name ) == 0 ) break;
394 if( *san != NULL ) continue;
395 if( gString_addTo( smr, gStr, qualifier->name ) != 0 ) return( 1 );
396 if( gString_addTo( smr, gStr, ":'" ) != 0 ) return( 1 );
397 if( gString_addTo( smr, gStr, qualifier->value ) != 0 ) return( 1 );
398 if( gString_addTo( smr, gStr, "'") != 0 ) return( 1 );
399 if( i == 1 ) {
400 if( gString_addTo( smr, gStr, "]" ) != 0 ) return( 1 ); }
401 else {
402 if( gString_addTo( smr, gStr, ", " ) != 0 ) return( 1 );
403 }
404 i--;
405 }
406 }
407 }
408 if( spectralID->decayChannel != NULL ) {
409 if( ( gString_addTo( smr, gStr, " -> " ) ) != 0 ) return( 1 );
410 if( ( tpi_channelID_toStringSans( smr, spectralID->decayChannel, gStr, sans ) ) != 0 ) return( 1 );
411 if( ( gString_addTo( smr, gStr, ")" ) ) != 0 ) return( 1 );
412 }
413 return( 0 );
414}
415/*
416***************************************************
417*/
418//char const *tpi_misc_firstNonWhiteSpace( statusMessageReporting *smr, char const *str ) {
419char const *tpi_misc_firstNonWhiteSpace( statusMessageReporting *, char const *str ) {
420
421 char const *s;
422
423 for( s = str; ( *s != 0 ) && isspace( *s ); s++ ) ;
424 return( s );
425}
426/*
427***************************************************
428*/
429char *tpi_misc_allocateAndCopyNCharacters( statusMessageReporting *smr, char const *str, int n ) {
430
431 char *s;
432
433 //if( ( s = xData_malloc2( smr, n + 1, 0, "s" ) ) == NULL ) return( NULL );
434 if( ( s = (char*) xData_malloc2( smr, n + 1, 0, "s" ) ) == NULL ) return( NULL );
435 strncpy( s, str, n );
436 s[n] = 0;
437 return( s );
438}
439
440#if defined __cplusplus
441}
442#endif
struct channelID_s channelID
int gString_addTo(statusMessageReporting *smr, gString *gStr, char const *str)
Definition: gString.cc:90
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
tpi_spectralIDQualifier * next
Definition: tpi_IDs.h:56
tpi_spectralID * next
Definition: tpi_IDs.h:62
double level
Definition: tpi_IDs.h:68
int multiplicity
Definition: tpi_IDs.h:67
char * revision
Definition: tpi_IDs.h:65
char * name
Definition: tpi_IDs.h:63
char * levelStr
Definition: tpi_IDs.h:69
tpi_spectralIDQualifier * qualifiers
Definition: tpi_IDs.h:64
tpi_channelID * decayChannel
Definition: tpi_IDs.h:70
char * multiplicityStr
Definition: tpi_IDs.h:66
int tpi_spectralID_toStringSans(statusMessageReporting *smr, tpi_spectralID *spectralID, gString *gStr, char *sans[])
Definition: tpi_IDs.cc:371
void * tpi_spectralID_free(statusMessageReporting *smr, tpi_spectralID *spectralID)
Definition: tpi_IDs.cc:191
char * tpi_misc_allocateAndCopyNCharacters(statusMessageReporting *smr, char const *str, int n)
Definition: tpi_IDs.cc:429
tpi_channelID * tpi_channelID_allocate(statusMessageReporting *smr)
Definition: tpi_IDs.cc:53
int tpi_channelID_toString(statusMessageReporting *smr, tpi_channelID *channelID, gString *gStr)
Definition: tpi_IDs.cc:138
int tpi_spectralID_toStringSanRevision(statusMessageReporting *smr, tpi_spectralID *spectralID, gString *gStr)
Definition: tpi_IDs.cc:360
tpi_spectralID * tpi_spectralID_allocate(statusMessageReporting *smr)
Definition: tpi_IDs.cc:170
tpi_spectralID * tpi_spectralID_parse(statusMessageReporting *smr, char const *str, char **EOP)
Definition: tpi_IDs.cc:218
int tpi_spectralID_toString(statusMessageReporting *smr, tpi_spectralID *spectralID, gString *gStr)
Definition: tpi_IDs.cc:353
char const * tpi_misc_firstNonWhiteSpace(statusMessageReporting *smr, char const *str)
Definition: tpi_IDs.cc:419
void * tpi_channelID_free(statusMessageReporting *smr, tpi_channelID *channelID)
Definition: tpi_IDs.cc:74
int tpi_channelID_toStringSans(statusMessageReporting *smr, tpi_channelID *channelID, gString *gStr, char *sans[])
Definition: tpi_IDs.cc:155
int tpi_spectralID_initialize(statusMessageReporting *smr, tpi_spectralID *spectralID)
Definition: tpi_IDs.cc:183
int tpi_channelID_release(statusMessageReporting *smr, tpi_channelID *channelID)
Definition: tpi_IDs.cc:85
int tpi_channelID_toStringSanRevision(statusMessageReporting *smr, tpi_channelID *channelID, gString *gStr)
Definition: tpi_IDs.cc:145
int tpi_channelID_initialize(statusMessageReporting *smr, tpi_channelID *channelID)
Definition: tpi_IDs.cc:66
tpi_channelID * tpi_channelID_parse(statusMessageReporting *smr, char const *str, char **EOP)
Definition: tpi_IDs.cc:99
int tpi_spectralID_release(statusMessageReporting *smr, tpi_spectralID *spectralID)
Definition: tpi_IDs.cc:202
void * xData_free(statusMessageReporting *smr, void *p)
Definition: xDataMisc.cc:89
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313