Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
nf_stringToInt32s.c
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#include <stdlib.h>
11#include <stdint.h>
12#include <ctype.h>
13
14#include "nf_utilities.h"
15
16#define numberOfStaticInt32s ( 100 * 1000 )
17
18#ifndef INT32_MIN
19#define INT32_MIN -2147483648
20#define INT32_MAX 2147483647
21#endif
22
23static int32_t *nfu_stringToListOfInt32s_2( statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter );
24/*
25========================================================================
26*/
27int32_t *nfu_stringToListOfInt32s( statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted,
28 char **endCharacter ) {
29
30 if( strchr( "0123456789.+-eE", sep ) != NULL ) {
31 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "Invalid sep ='%c'.", sep );
32 return( NULL );
33 }
34
35 *numberConverted = 0;
36 *endCharacter = (char *) str;
37 if( isspace( sep ) ) sep = ' '; /* Make it the space character if any white space as it simplifies logic below. */
38 return( nfu_stringToListOfInt32s_2( smr, str, sep, numberConverted, endCharacter ) );
39}
40/*
41========================================================================
42*/
43static int32_t *nfu_stringToListOfInt32s_2( statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted,
44 char **endCharacter ) {
45
46 int64_t i1, i2, numberConverted_initial = *numberConverted;
47 int32_t *Int32Ptr = NULL;
48#if NFU_USEHEAP
49 int32_t *staticInt32s = (int32_t *) smr_malloc2( smr, (size_t) numberOfStaticInt32s * sizeof( int32_t ), 0, "staticInt32s" );
50 if( staticInt32s == NULL ) {
52 return( NULL );
53 }
54#else
55 int32_t staticInt32s[numberOfStaticInt32s];
56#endif
57
58 for( i1 = 0; i1 < numberOfStaticInt32s; i1++, (*numberConverted)++ ) {
59 if( *numberConverted == 0 ) {
60 if( nfu_stringToInt32( smr, str, endCharacter, &staticInt32s[i1] ) != 0 ) {
61 *endCharacter = (char *) str;
63#if NFU_USEHEAP
64 free( staticInt32s );
65#endif
66 return( NULL );
67 } }
68 else { /* Check that there is one sep character and allow for arbitrary number of white spaces. */
69 char const *str2 = str;
70
71 while( isspace( *str2 ) ) ++str2; /* Only need to check for white spaces before sep character as strtol will ignore after. */
72 if( sep != ' ' ) {
73 if( *str2 == sep ) {
74 ++str2; }
75 else {
76 str2 = str;
77 }
78 }
79 if( str < str2 ) {
80 if( nfu_stringToInt32( smr, str2, endCharacter, &staticInt32s[i1] ) != 0 ) {
81#if NFU_USEHEAP
82 free( staticInt32s );
83#endif
84 *endCharacter = (char *) str;
86 return( NULL );
87 }
88 }
89 if( str2 == (char const *) *endCharacter ) *endCharacter = (char *) str;
90 }
91 if( str == (char const *) *endCharacter ) {
92 int64_t number = *numberConverted;
93
94 if( *numberConverted == 0 ) number = 1;
95 if( ( Int32Ptr = (int32_t *) smr_malloc2( smr, (size_t) number * sizeof( int32_t ), 0, "Int32Ptr" ) ) == NULL ) {
96#if NFU_USEHEAP
97 free( staticInt32s );
98#endif
100 return( NULL );
101 }
102 break;
103 }
104 str = (char const *) *endCharacter;
105 }
106
107 if( Int32Ptr == NULL ) Int32Ptr = nfu_stringToListOfInt32s_2( smr, str, sep, numberConverted, endCharacter );
108 if( Int32Ptr != NULL ) {
109 int32_t *Int32Ptr2 = &(Int32Ptr[numberConverted_initial]);
110 char *end = *endCharacter;
111
112 for( i2 = 0; i2 < i1; i2++, Int32Ptr2++ ) *Int32Ptr2 = staticInt32s[i2];
113 while( isspace( *end ) ) ++end;
114 if( *end == 0 ) *endCharacter = end;
115 }
116
117#if NFU_USEHEAP
118 free( staticInt32s );
119#endif
120
121 return( Int32Ptr );
122}
123/*
124========================================================================
125*/
126int nfu_stringToInt32( statusMessageReporting *smr, char const *str, char **endCharacter, int32_t *value ) {
127
128 long lValue = strtol( str, endCharacter, 10 );
129
130 if( lValue < INT32_MIN ) {
131 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "int32_t underflow: %l", lValue );
132 return( -1 ); }
133 else if( lValue > INT32_MAX ) {
134 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "int32_t overflow: %l", lValue );
135 return( 1 );
136 }
137 *value = (int) lValue;
138 return( 0 );
139}
void free(voidpf ptr)
#define INT32_MAX
#define numberOfStaticInt32s
#define INT32_MIN
int32_t * nfu_stringToListOfInt32s(statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter)
int nfu_stringToInt32(statusMessageReporting *smr, char const *str, char **endCharacter, int32_t *value)
@ nfu_badInput
@ nfu_Error
int nfu_SMR_libraryID
#define smr_setReportError2(smr, libraryID, code, fmt,...)
#define smr_setReportError2p(smr, libraryID, code, fmt)
#define smr_malloc2(smr, size, zero, forItem)