2
Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
6
This file is part of Silo. For details, see silo.llnl.gov.
8
Redistribution and use in source and binary forms, with or without
9
modification, are permitted provided that the following conditions
12
* Redistributions of source code must retain the above copyright
13
notice, this list of conditions and the disclaimer below.
14
* Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the disclaimer (as noted
16
below) in the documentation and/or other materials provided with
18
* Neither the name of the LLNS/LLNL nor the names of its
19
contributors may be used to endorse or promote products derived
20
from this software without specific prior written permission.
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE
26
LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
27
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
This work was produced at Lawrence Livermore National Laboratory under
36
Contract No. DE-AC52-07NA27344 with the DOE.
38
Neither the United States Government nor Lawrence Livermore National
39
Security, LLC nor any of their employees, makes any warranty, express
40
or implied, or assumes any liability or responsibility for the
41
accuracy, completeness, or usefulness of any information, apparatus,
42
product, or process disclosed, or represents that its use would not
43
infringe privately-owned rights.
45
Any reference herein to any specific commercial products, process, or
46
services by trade name, trademark, manufacturer or otherwise does not
47
necessarily constitute or imply its endorsement, recommendation, or
48
favoring by the United States Government or Lawrence Livermore
49
National Security, LLC. The views and opinions of authors expressed
50
herein do not necessarily state or reflect those of the United States
51
Government or Lawrence Livermore National Security, LLC, and shall not
52
be used for advertising or product endorsement purposes.
54
#include "config.h" /* For HAVE_MEMMOVE test. */
56
* SCSTR.C - string manipulation functions
59
* Software Release #92-0043
67
#define ishexdigit(x) (isdigit(x)?(x)-'0':islower(x)?(x)+10-'a':(x)+10-'A')
70
/*-------------------------------------------------------------------------
71
* Function: lite_SC_stoi
73
* Purpose: String to integer. Returns 0 if string is NULL.
79
* Programmer: Adapted from PACT SCORE
84
*-------------------------------------------------------------------------
87
lite_SC_stoi (char *s) {
89
if (s == NULL) return(0);
90
else return (atoi(s));
94
/*-------------------------------------------------------------------------
95
* Function: lite_SC_stol
97
* Purpose: String to long integer.
101
* Failure: 0 if string is NULL
103
* Programmer: Adapted from PACT SCORE
108
*-------------------------------------------------------------------------
111
lite_SC_stol (char *s) {
113
if (s == NULL) return(0);
114
else return(atol(s));
118
/*-------------------------------------------------------------------------
119
* Function: _lite_SC_strtol
121
* Purpose: Strtol done right (since some libraries are bad)
127
* Programmer: Adapted from PACT SCORE
132
*-------------------------------------------------------------------------
135
_lite_SC_strtol (char *str, char **ptr, int base) {
142
* In case no number is formed.
144
if (ptr != (char **) 0) *ptr = str;
147
* Base is invalid -- should be a fatal error.
149
if (base < 0 || base > MBASE) return (0);
151
if (!isalnum(c = *str)) {
152
while (isspace(c)) c = *++str;
155
case '+' : c = *++str;
160
if (c != '0') base = 10;
161
else if (str[1] == 'x' || str[1] == 'X') base = 16;
166
* For any base > 10, the digits incrementally following
167
* 9 are assumed to be "abc...z" or "ABC...Z".
169
if (!isalnum(c) || (xx = ishexdigit(c)) >= base) {
170
return(0); /* no number formed */
174
* Skip over leading "0x" or "0X".
176
if ((base == 16) && (c == '0') && isxdigit(str[2]) &&
177
((str[1] == 'x') || (str[1] == 'X'))) {
182
* Accumulate neg avoids surprises near MAXLONG.
184
for (val = -ishexdigit(c);
185
isalnum(c = *++str) && (xx = ishexdigit(c)) < base; ) {
186
val = base * val - xx;
189
if (ptr != (char **) 0) *ptr = str;
191
return(neg ? val : -val);
194
/*--------------------------------------------------------------------------*/
195
/* STRING SORT ROUTINES */
196
/*--------------------------------------------------------------------------*/
199
qsort_strcmp(const void *str1, const void *str2)
201
return(strcmp(*((const char **)str1), *((const char **)str2)));
205
/*-------------------------------------------------------------------------
206
* Function: lite_SC_string_sort
208
* Purpose: Sort an array of character pointers by what they point to.
212
* Programmer: Adapted from PACT SCORE
216
* Sean Ahern, Fri Mar 2 09:42:18 PST 2001
217
* Changed this sort to be a qsort, as suggested by Dan Schikore.
219
*-------------------------------------------------------------------------*/
221
lite_SC_string_sort (char **v, int n) {
222
qsort(v, n, sizeof(char *), qsort_strcmp);