~ubuntu-branches/ubuntu/trusty/silo-llnl/trusty

« back to all changes in this revision

Viewing changes to src/score/scstr.c

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2011-01-02 00:03:01 UTC
  • Revision ID: james.westby@ubuntu.com-20110102000301-9s2hfsjrkguz6h4r
Tags: upstream-4.8
ImportĀ upstreamĀ versionĀ 4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
 
3
LLNL-CODE-425250.
 
4
All rights reserved.
 
5
 
 
6
This file is part of Silo. For details, see silo.llnl.gov.
 
7
 
 
8
Redistribution and use in source and binary forms, with or without
 
9
modification, are permitted provided that the following conditions
 
10
are met:
 
11
 
 
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
 
17
     the distribution.
 
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.
 
21
 
 
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.
 
34
 
 
35
This work was produced at Lawrence Livermore National Laboratory under
 
36
Contract No.  DE-AC52-07NA27344 with the DOE.
 
37
 
 
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.
 
44
 
 
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.
 
53
*/
 
54
#include "config.h" /* For HAVE_MEMMOVE test. */
 
55
/*
 
56
 * SCSTR.C - string manipulation functions
 
57
 *
 
58
 * Source Version: 2.0
 
59
 * Software Release #92-0043
 
60
 *
 
61
 */
 
62
#include <ctype.h>
 
63
#include "score.h"
 
64
 
 
65
#define MBASE 32
 
66
 
 
67
#define ishexdigit(x) (isdigit(x)?(x)-'0':islower(x)?(x)+10-'a':(x)+10-'A')
 
68
 
 
69
 
 
70
/*-------------------------------------------------------------------------
 
71
 * Function:    lite_SC_stoi
 
72
 *
 
73
 * Purpose:     String to integer.  Returns 0 if string is NULL.
 
74
 *
 
75
 * Return:      Success:        
 
76
 *
 
77
 *              Failure:        
 
78
 *
 
79
 * Programmer:  Adapted from PACT SCORE
 
80
 *              Mar 12, 1996
 
81
 *
 
82
 * Modifications:
 
83
 *
 
84
 *-------------------------------------------------------------------------
 
85
 */
 
86
int
 
87
lite_SC_stoi (char *s) {
 
88
 
 
89
   if (s == NULL) return(0);
 
90
   else return (atoi(s));
 
91
}
 
92
 
 
93
 
 
94
/*-------------------------------------------------------------------------
 
95
 * Function:    lite_SC_stol
 
96
 *
 
97
 * Purpose:     String to long integer.
 
98
 *
 
99
 * Return:      Success:        
 
100
 *
 
101
 *              Failure:        0 if string is NULL
 
102
 *
 
103
 * Programmer:  Adapted from PACT SCORE
 
104
 *              Mar 12, 1996
 
105
 *
 
106
 * Modifications:
 
107
 *
 
108
 *-------------------------------------------------------------------------
 
109
 */
 
110
long
 
111
lite_SC_stol (char *s) {
 
112
 
 
113
   if (s == NULL) return(0);
 
114
   else return(atol(s));
 
115
}
 
116
 
 
117
 
 
118
/*-------------------------------------------------------------------------
 
119
 * Function:    _lite_SC_strtol
 
120
 *
 
121
 * Purpose:     Strtol done right (since some libraries are bad)
 
122
 *
 
123
 * Return:      Success:        
 
124
 *
 
125
 *              Failure:        
 
126
 *
 
127
 * Programmer:  Adapted from PACT SCORE
 
128
 *              Mar 12, 1996
 
129
 *
 
130
 * Modifications:
 
131
 *
 
132
 *-------------------------------------------------------------------------
 
133
 */
 
134
long int
 
135
_lite_SC_strtol (char *str, char **ptr, int base) {
 
136
 
 
137
   long val;
 
138
   int c;
 
139
   int xx, neg = 0;
 
140
 
 
141
   /*
 
142
    * In case no number is formed.
 
143
    */
 
144
   if (ptr != (char **) 0) *ptr = str;
 
145
 
 
146
   /*
 
147
    * Base is invalid -- should be a fatal error.
 
148
    */
 
149
   if (base < 0 || base > MBASE) return (0);
 
150
 
 
151
   if (!isalnum(c = *str)) {
 
152
      while (isspace(c)) c = *++str;
 
153
      switch (c) {
 
154
      case '-' : neg++;
 
155
      case '+' : c = *++str;
 
156
      }
 
157
   }
 
158
 
 
159
   if (base == 0) {
 
160
      if (c != '0') base = 10;
 
161
      else if (str[1] == 'x' || str[1] == 'X') base = 16;
 
162
      else base = 8;
 
163
   }
 
164
 
 
165
   /*
 
166
    * For any base > 10, the digits incrementally following
 
167
    * 9 are assumed to be "abc...z" or "ABC...Z".
 
168
    */
 
169
   if (!isalnum(c) || (xx = ishexdigit(c)) >= base) {
 
170
      return(0);                                       /* no number formed */
 
171
   }
 
172
 
 
173
   /*
 
174
    * Skip over leading "0x" or "0X".
 
175
    */
 
176
   if ((base == 16) && (c == '0') && isxdigit(str[2]) &&
 
177
       ((str[1] == 'x') || (str[1] == 'X'))) {
 
178
      c = *(str += 2);
 
179
   }
 
180
 
 
181
   /*
 
182
    * Accumulate neg avoids surprises near MAXLONG.
 
183
    */
 
184
   for (val = -ishexdigit(c);
 
185
        isalnum(c = *++str) && (xx = ishexdigit(c)) < base; ) {
 
186
      val = base * val - xx;
 
187
   }
 
188
 
 
189
   if (ptr != (char **) 0) *ptr = str;
 
190
 
 
191
   return(neg ? val : -val);
 
192
}
 
193
 
 
194
/*--------------------------------------------------------------------------*/
 
195
/*                           STRING SORT ROUTINES                           */
 
196
/*--------------------------------------------------------------------------*/
 
197
 
 
198
static int
 
199
qsort_strcmp(const void *str1, const void *str2)
 
200
{
 
201
   return(strcmp(*((const char **)str1), *((const char **)str2)));
 
202
}
 
203
 
 
204
 
 
205
/*-------------------------------------------------------------------------
 
206
 * Function:    lite_SC_string_sort
 
207
 *
 
208
 * Purpose:     Sort an array of character pointers by what they point to.
 
209
 *
 
210
 * Return:      void
 
211
 *
 
212
 * Programmer:  Adapted from PACT SCORE
 
213
 *              Mar 12, 1996
 
214
 *
 
215
 * Modifications:
 
216
 *      Sean Ahern, Fri Mar  2 09:42:18 PST 2001
 
217
 *      Changed this sort to be a qsort, as suggested by Dan Schikore.
 
218
 *
 
219
 *-------------------------------------------------------------------------*/
 
220
void
 
221
lite_SC_string_sort (char **v, int n) {
 
222
   qsort(v, n, sizeof(char *), qsort_strcmp);
 
223
}