annotate openmp/runtime/src/kmp_str.cpp @ 152:e8a9b4f4d755

pull from 146
author anatofuz
date Wed, 11 Mar 2020 18:29:16 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 /*
anatofuz
parents:
diff changeset
2 * kmp_str.cpp -- String manipulation routines.
anatofuz
parents:
diff changeset
3 */
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
8 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "kmp_str.h"
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 #include <stdarg.h> // va_*
anatofuz
parents:
diff changeset
16 #include <stdio.h> // vsnprintf()
anatofuz
parents:
diff changeset
17 #include <stdlib.h> // malloc(), realloc()
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 #include "kmp.h"
anatofuz
parents:
diff changeset
20 #include "kmp_i18n.h"
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 /* String buffer.
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 Usage:
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 // Declare buffer and initialize it.
anatofuz
parents:
diff changeset
27 kmp_str_buf_t buffer;
anatofuz
parents:
diff changeset
28 __kmp_str_buf_init( & buffer );
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 // Print to buffer.
anatofuz
parents:
diff changeset
31 __kmp_str_buf_print(& buffer, "Error in file \"%s\" line %d\n", "foo.c", 12);
anatofuz
parents:
diff changeset
32 __kmp_str_buf_print(& buffer, " <%s>\n", line);
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 // Use buffer contents. buffer.str is a pointer to data, buffer.used is a
anatofuz
parents:
diff changeset
35 // number of printed characters (not including terminating zero).
anatofuz
parents:
diff changeset
36 write( fd, buffer.str, buffer.used );
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 // Free buffer.
anatofuz
parents:
diff changeset
39 __kmp_str_buf_free( & buffer );
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 // Alternatively, you can detach allocated memory from buffer:
anatofuz
parents:
diff changeset
42 __kmp_str_buf_detach( & buffer );
anatofuz
parents:
diff changeset
43 return buffer.str; // That memory should be freed eventually.
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 Notes:
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 * Buffer users may use buffer.str and buffer.used. Users should not change
anatofuz
parents:
diff changeset
48 any fields of buffer directly.
anatofuz
parents:
diff changeset
49 * buffer.str is never NULL. If buffer is empty, buffer.str points to empty
anatofuz
parents:
diff changeset
50 string ("").
anatofuz
parents:
diff changeset
51 * For performance reasons, buffer uses stack memory (buffer.bulk) first. If
anatofuz
parents:
diff changeset
52 stack memory is exhausted, buffer allocates memory on heap by malloc(), and
anatofuz
parents:
diff changeset
53 reallocates it by realloc() as amount of used memory grows.
anatofuz
parents:
diff changeset
54 * Buffer doubles amount of allocated memory each time it is exhausted.
anatofuz
parents:
diff changeset
55 */
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 // TODO: __kmp_str_buf_print() can use thread local memory allocator.
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 #define KMP_STR_BUF_INVARIANT(b) \
anatofuz
parents:
diff changeset
60 { \
anatofuz
parents:
diff changeset
61 KMP_DEBUG_ASSERT((b)->str != NULL); \
anatofuz
parents:
diff changeset
62 KMP_DEBUG_ASSERT((b)->size >= sizeof((b)->bulk)); \
anatofuz
parents:
diff changeset
63 KMP_DEBUG_ASSERT((b)->size % sizeof((b)->bulk) == 0); \
anatofuz
parents:
diff changeset
64 KMP_DEBUG_ASSERT((unsigned)(b)->used < (b)->size); \
anatofuz
parents:
diff changeset
65 KMP_DEBUG_ASSERT( \
anatofuz
parents:
diff changeset
66 (b)->size == sizeof((b)->bulk) ? (b)->str == &(b)->bulk[0] : 1); \
anatofuz
parents:
diff changeset
67 KMP_DEBUG_ASSERT((b)->size > sizeof((b)->bulk) ? (b)->str != &(b)->bulk[0] \
anatofuz
parents:
diff changeset
68 : 1); \
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 void __kmp_str_buf_clear(kmp_str_buf_t *buffer) {
anatofuz
parents:
diff changeset
72 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
73 if (buffer->used > 0) {
anatofuz
parents:
diff changeset
74 buffer->used = 0;
anatofuz
parents:
diff changeset
75 buffer->str[0] = 0;
anatofuz
parents:
diff changeset
76 }
anatofuz
parents:
diff changeset
77 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
78 } // __kmp_str_buf_clear
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 void __kmp_str_buf_reserve(kmp_str_buf_t *buffer, int size) {
anatofuz
parents:
diff changeset
81 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
82 KMP_DEBUG_ASSERT(size >= 0);
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 if (buffer->size < (unsigned int)size) {
anatofuz
parents:
diff changeset
85 // Calculate buffer size.
anatofuz
parents:
diff changeset
86 do {
anatofuz
parents:
diff changeset
87 buffer->size *= 2;
anatofuz
parents:
diff changeset
88 } while (buffer->size < (unsigned int)size);
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 // Enlarge buffer.
anatofuz
parents:
diff changeset
91 if (buffer->str == &buffer->bulk[0]) {
anatofuz
parents:
diff changeset
92 buffer->str = (char *)KMP_INTERNAL_MALLOC(buffer->size);
anatofuz
parents:
diff changeset
93 if (buffer->str == NULL) {
anatofuz
parents:
diff changeset
94 KMP_FATAL(MemoryAllocFailed);
anatofuz
parents:
diff changeset
95 }
anatofuz
parents:
diff changeset
96 KMP_MEMCPY_S(buffer->str, buffer->size, buffer->bulk, buffer->used + 1);
anatofuz
parents:
diff changeset
97 } else {
anatofuz
parents:
diff changeset
98 buffer->str = (char *)KMP_INTERNAL_REALLOC(buffer->str, buffer->size);
anatofuz
parents:
diff changeset
99 if (buffer->str == NULL) {
anatofuz
parents:
diff changeset
100 KMP_FATAL(MemoryAllocFailed);
anatofuz
parents:
diff changeset
101 }
anatofuz
parents:
diff changeset
102 }
anatofuz
parents:
diff changeset
103 }
anatofuz
parents:
diff changeset
104
anatofuz
parents:
diff changeset
105 KMP_DEBUG_ASSERT(buffer->size > 0);
anatofuz
parents:
diff changeset
106 KMP_DEBUG_ASSERT(buffer->size >= (unsigned)size);
anatofuz
parents:
diff changeset
107 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
108 } // __kmp_str_buf_reserve
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 void __kmp_str_buf_detach(kmp_str_buf_t *buffer) {
anatofuz
parents:
diff changeset
111 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // If internal bulk is used, allocate memory and copy it.
anatofuz
parents:
diff changeset
114 if (buffer->size <= sizeof(buffer->bulk)) {
anatofuz
parents:
diff changeset
115 buffer->str = (char *)KMP_INTERNAL_MALLOC(buffer->size);
anatofuz
parents:
diff changeset
116 if (buffer->str == NULL) {
anatofuz
parents:
diff changeset
117 KMP_FATAL(MemoryAllocFailed);
anatofuz
parents:
diff changeset
118 }
anatofuz
parents:
diff changeset
119 KMP_MEMCPY_S(buffer->str, buffer->size, buffer->bulk, buffer->used + 1);
anatofuz
parents:
diff changeset
120 }
anatofuz
parents:
diff changeset
121 } // __kmp_str_buf_detach
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 void __kmp_str_buf_free(kmp_str_buf_t *buffer) {
anatofuz
parents:
diff changeset
124 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
125 if (buffer->size > sizeof(buffer->bulk)) {
anatofuz
parents:
diff changeset
126 KMP_INTERNAL_FREE(buffer->str);
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128 buffer->str = buffer->bulk;
anatofuz
parents:
diff changeset
129 buffer->size = sizeof(buffer->bulk);
anatofuz
parents:
diff changeset
130 buffer->used = 0;
anatofuz
parents:
diff changeset
131 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
132 } // __kmp_str_buf_free
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, int len) {
anatofuz
parents:
diff changeset
135 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
136 KMP_DEBUG_ASSERT(str != NULL);
anatofuz
parents:
diff changeset
137 KMP_DEBUG_ASSERT(len >= 0);
anatofuz
parents:
diff changeset
138 __kmp_str_buf_reserve(buffer, buffer->used + len + 1);
anatofuz
parents:
diff changeset
139 KMP_MEMCPY(buffer->str + buffer->used, str, len);
anatofuz
parents:
diff changeset
140 buffer->str[buffer->used + len] = 0;
anatofuz
parents:
diff changeset
141 buffer->used += len;
anatofuz
parents:
diff changeset
142 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
143 } // __kmp_str_buf_cat
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src) {
anatofuz
parents:
diff changeset
146 KMP_DEBUG_ASSERT(dest);
anatofuz
parents:
diff changeset
147 KMP_DEBUG_ASSERT(src);
anatofuz
parents:
diff changeset
148 KMP_STR_BUF_INVARIANT(dest);
anatofuz
parents:
diff changeset
149 KMP_STR_BUF_INVARIANT(src);
anatofuz
parents:
diff changeset
150 if (!src->str || !src->used)
anatofuz
parents:
diff changeset
151 return;
anatofuz
parents:
diff changeset
152 __kmp_str_buf_reserve(dest, dest->used + src->used + 1);
anatofuz
parents:
diff changeset
153 KMP_MEMCPY(dest->str + dest->used, src->str, src->used);
anatofuz
parents:
diff changeset
154 dest->str[dest->used + src->used] = 0;
anatofuz
parents:
diff changeset
155 dest->used += src->used;
anatofuz
parents:
diff changeset
156 KMP_STR_BUF_INVARIANT(dest);
anatofuz
parents:
diff changeset
157 } // __kmp_str_buf_catbuf
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 // Return the number of characters written
anatofuz
parents:
diff changeset
160 int __kmp_str_buf_vprint(kmp_str_buf_t *buffer, char const *format,
anatofuz
parents:
diff changeset
161 va_list args) {
anatofuz
parents:
diff changeset
162 int rc;
anatofuz
parents:
diff changeset
163 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 for (;;) {
anatofuz
parents:
diff changeset
166 int const free = buffer->size - buffer->used;
anatofuz
parents:
diff changeset
167 int size;
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 // Try to format string.
anatofuz
parents:
diff changeset
170 {
anatofuz
parents:
diff changeset
171 /* On Linux* OS Intel(R) 64, vsnprintf() modifies args argument, so vsnprintf()
anatofuz
parents:
diff changeset
172 crashes if it is called for the second time with the same args. To prevent
anatofuz
parents:
diff changeset
173 the crash, we have to pass a fresh intact copy of args to vsnprintf() on each
anatofuz
parents:
diff changeset
174 iteration.
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 Unfortunately, standard va_copy() macro is not available on Windows* OS.
anatofuz
parents:
diff changeset
177 However, it seems vsnprintf() does not modify args argument on Windows* OS.
anatofuz
parents:
diff changeset
178 */
anatofuz
parents:
diff changeset
179
anatofuz
parents:
diff changeset
180 #if !KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
181 va_list _args;
anatofuz
parents:
diff changeset
182 va_copy(_args, args); // Make copy of args.
anatofuz
parents:
diff changeset
183 #define args _args // Substitute args with its copy, _args.
anatofuz
parents:
diff changeset
184 #endif // KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
185 rc = KMP_VSNPRINTF(buffer->str + buffer->used, free, format, args);
anatofuz
parents:
diff changeset
186 #if !KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
187 #undef args // Remove substitution.
anatofuz
parents:
diff changeset
188 va_end(_args);
anatofuz
parents:
diff changeset
189 #endif // KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 // No errors, string has been formatted.
anatofuz
parents:
diff changeset
193 if (rc >= 0 && rc < free) {
anatofuz
parents:
diff changeset
194 buffer->used += rc;
anatofuz
parents:
diff changeset
195 break;
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 // Error occurred, buffer is too small.
anatofuz
parents:
diff changeset
199 if (rc >= 0) {
anatofuz
parents:
diff changeset
200 // C99-conforming implementation of vsnprintf returns required buffer size
anatofuz
parents:
diff changeset
201 size = buffer->used + rc + 1;
anatofuz
parents:
diff changeset
202 } else {
anatofuz
parents:
diff changeset
203 // Older implementations just return -1. Double buffer size.
anatofuz
parents:
diff changeset
204 size = buffer->size * 2;
anatofuz
parents:
diff changeset
205 }
anatofuz
parents:
diff changeset
206
anatofuz
parents:
diff changeset
207 // Enlarge buffer.
anatofuz
parents:
diff changeset
208 __kmp_str_buf_reserve(buffer, size);
anatofuz
parents:
diff changeset
209
anatofuz
parents:
diff changeset
210 // And try again.
anatofuz
parents:
diff changeset
211 }
anatofuz
parents:
diff changeset
212
anatofuz
parents:
diff changeset
213 KMP_DEBUG_ASSERT(buffer->size > 0);
anatofuz
parents:
diff changeset
214 KMP_STR_BUF_INVARIANT(buffer);
anatofuz
parents:
diff changeset
215 return rc;
anatofuz
parents:
diff changeset
216 } // __kmp_str_buf_vprint
anatofuz
parents:
diff changeset
217
anatofuz
parents:
diff changeset
218 // Return the number of characters written
anatofuz
parents:
diff changeset
219 int __kmp_str_buf_print(kmp_str_buf_t *buffer, char const *format, ...) {
anatofuz
parents:
diff changeset
220 int rc;
anatofuz
parents:
diff changeset
221 va_list args;
anatofuz
parents:
diff changeset
222 va_start(args, format);
anatofuz
parents:
diff changeset
223 rc = __kmp_str_buf_vprint(buffer, format, args);
anatofuz
parents:
diff changeset
224 va_end(args);
anatofuz
parents:
diff changeset
225 return rc;
anatofuz
parents:
diff changeset
226 } // __kmp_str_buf_print
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 /* The function prints specified size to buffer. Size is expressed using biggest
anatofuz
parents:
diff changeset
229 possible unit, for example 1024 is printed as "1k". */
anatofuz
parents:
diff changeset
230 void __kmp_str_buf_print_size(kmp_str_buf_t *buf, size_t size) {
anatofuz
parents:
diff changeset
231 char const *names[] = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"};
anatofuz
parents:
diff changeset
232 int const units = sizeof(names) / sizeof(char const *);
anatofuz
parents:
diff changeset
233 int u = 0;
anatofuz
parents:
diff changeset
234 if (size > 0) {
anatofuz
parents:
diff changeset
235 while ((size % 1024 == 0) && (u + 1 < units)) {
anatofuz
parents:
diff changeset
236 size = size / 1024;
anatofuz
parents:
diff changeset
237 ++u;
anatofuz
parents:
diff changeset
238 }
anatofuz
parents:
diff changeset
239 }
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241 __kmp_str_buf_print(buf, "%" KMP_SIZE_T_SPEC "%s", size, names[u]);
anatofuz
parents:
diff changeset
242 } // __kmp_str_buf_print_size
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 void __kmp_str_fname_init(kmp_str_fname_t *fname, char const *path) {
anatofuz
parents:
diff changeset
245 fname->path = NULL;
anatofuz
parents:
diff changeset
246 fname->dir = NULL;
anatofuz
parents:
diff changeset
247 fname->base = NULL;
anatofuz
parents:
diff changeset
248
anatofuz
parents:
diff changeset
249 if (path != NULL) {
anatofuz
parents:
diff changeset
250 char *slash = NULL; // Pointer to the last character of dir.
anatofuz
parents:
diff changeset
251 char *base = NULL; // Pointer to the beginning of basename.
anatofuz
parents:
diff changeset
252 fname->path = __kmp_str_format("%s", path);
anatofuz
parents:
diff changeset
253 // Original code used strdup() function to copy a string, but on Windows* OS
anatofuz
parents:
diff changeset
254 // Intel(R) 64 it causes assertion id debug heap, so I had to replace
anatofuz
parents:
diff changeset
255 // strdup with __kmp_str_format().
anatofuz
parents:
diff changeset
256 if (KMP_OS_WINDOWS) {
anatofuz
parents:
diff changeset
257 __kmp_str_replace(fname->path, '\\', '/');
anatofuz
parents:
diff changeset
258 }
anatofuz
parents:
diff changeset
259 fname->dir = __kmp_str_format("%s", fname->path);
anatofuz
parents:
diff changeset
260 slash = strrchr(fname->dir, '/');
anatofuz
parents:
diff changeset
261 if (KMP_OS_WINDOWS &&
anatofuz
parents:
diff changeset
262 slash == NULL) { // On Windows* OS, if slash not found,
anatofuz
parents:
diff changeset
263 char first = TOLOWER(fname->dir[0]); // look for drive.
anatofuz
parents:
diff changeset
264 if ('a' <= first && first <= 'z' && fname->dir[1] == ':') {
anatofuz
parents:
diff changeset
265 slash = &fname->dir[1];
anatofuz
parents:
diff changeset
266 }
anatofuz
parents:
diff changeset
267 }
anatofuz
parents:
diff changeset
268 base = (slash == NULL ? fname->dir : slash + 1);
anatofuz
parents:
diff changeset
269 fname->base = __kmp_str_format("%s", base); // Copy basename
anatofuz
parents:
diff changeset
270 *base = 0; // and truncate dir.
anatofuz
parents:
diff changeset
271 }
anatofuz
parents:
diff changeset
272
anatofuz
parents:
diff changeset
273 } // kmp_str_fname_init
anatofuz
parents:
diff changeset
274
anatofuz
parents:
diff changeset
275 void __kmp_str_fname_free(kmp_str_fname_t *fname) {
anatofuz
parents:
diff changeset
276 __kmp_str_free(&fname->path);
anatofuz
parents:
diff changeset
277 __kmp_str_free(&fname->dir);
anatofuz
parents:
diff changeset
278 __kmp_str_free(&fname->base);
anatofuz
parents:
diff changeset
279 } // kmp_str_fname_free
anatofuz
parents:
diff changeset
280
anatofuz
parents:
diff changeset
281 int __kmp_str_fname_match(kmp_str_fname_t const *fname, char const *pattern) {
anatofuz
parents:
diff changeset
282 int dir_match = 1;
anatofuz
parents:
diff changeset
283 int base_match = 1;
anatofuz
parents:
diff changeset
284
anatofuz
parents:
diff changeset
285 if (pattern != NULL) {
anatofuz
parents:
diff changeset
286 kmp_str_fname_t ptrn;
anatofuz
parents:
diff changeset
287 __kmp_str_fname_init(&ptrn, pattern);
anatofuz
parents:
diff changeset
288 dir_match = strcmp(ptrn.dir, "*/") == 0 ||
anatofuz
parents:
diff changeset
289 (fname->dir != NULL && __kmp_str_eqf(fname->dir, ptrn.dir));
anatofuz
parents:
diff changeset
290 base_match = strcmp(ptrn.base, "*") == 0 ||
anatofuz
parents:
diff changeset
291 (fname->base != NULL && __kmp_str_eqf(fname->base, ptrn.base));
anatofuz
parents:
diff changeset
292 __kmp_str_fname_free(&ptrn);
anatofuz
parents:
diff changeset
293 }
anatofuz
parents:
diff changeset
294
anatofuz
parents:
diff changeset
295 return dir_match && base_match;
anatofuz
parents:
diff changeset
296 } // __kmp_str_fname_match
anatofuz
parents:
diff changeset
297
anatofuz
parents:
diff changeset
298 kmp_str_loc_t __kmp_str_loc_init(char const *psource, int init_fname) {
anatofuz
parents:
diff changeset
299 kmp_str_loc_t loc;
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 loc._bulk = NULL;
anatofuz
parents:
diff changeset
302 loc.file = NULL;
anatofuz
parents:
diff changeset
303 loc.func = NULL;
anatofuz
parents:
diff changeset
304 loc.line = 0;
anatofuz
parents:
diff changeset
305 loc.col = 0;
anatofuz
parents:
diff changeset
306
anatofuz
parents:
diff changeset
307 if (psource != NULL) {
anatofuz
parents:
diff changeset
308 char *str = NULL;
anatofuz
parents:
diff changeset
309 char *dummy = NULL;
anatofuz
parents:
diff changeset
310 char *line = NULL;
anatofuz
parents:
diff changeset
311 char *col = NULL;
anatofuz
parents:
diff changeset
312
anatofuz
parents:
diff changeset
313 // Copy psource to keep it intact.
anatofuz
parents:
diff changeset
314 loc._bulk = __kmp_str_format("%s", psource);
anatofuz
parents:
diff changeset
315
anatofuz
parents:
diff changeset
316 // Parse psource string: ";file;func;line;col;;"
anatofuz
parents:
diff changeset
317 str = loc._bulk;
anatofuz
parents:
diff changeset
318 __kmp_str_split(str, ';', &dummy, &str);
anatofuz
parents:
diff changeset
319 __kmp_str_split(str, ';', &loc.file, &str);
anatofuz
parents:
diff changeset
320 __kmp_str_split(str, ';', &loc.func, &str);
anatofuz
parents:
diff changeset
321 __kmp_str_split(str, ';', &line, &str);
anatofuz
parents:
diff changeset
322 __kmp_str_split(str, ';', &col, &str);
anatofuz
parents:
diff changeset
323
anatofuz
parents:
diff changeset
324 // Convert line and col into numberic values.
anatofuz
parents:
diff changeset
325 if (line != NULL) {
anatofuz
parents:
diff changeset
326 loc.line = atoi(line);
anatofuz
parents:
diff changeset
327 if (loc.line < 0) {
anatofuz
parents:
diff changeset
328 loc.line = 0;
anatofuz
parents:
diff changeset
329 }
anatofuz
parents:
diff changeset
330 }
anatofuz
parents:
diff changeset
331 if (col != NULL) {
anatofuz
parents:
diff changeset
332 loc.col = atoi(col);
anatofuz
parents:
diff changeset
333 if (loc.col < 0) {
anatofuz
parents:
diff changeset
334 loc.col = 0;
anatofuz
parents:
diff changeset
335 }
anatofuz
parents:
diff changeset
336 }
anatofuz
parents:
diff changeset
337 }
anatofuz
parents:
diff changeset
338
anatofuz
parents:
diff changeset
339 __kmp_str_fname_init(&loc.fname, init_fname ? loc.file : NULL);
anatofuz
parents:
diff changeset
340
anatofuz
parents:
diff changeset
341 return loc;
anatofuz
parents:
diff changeset
342 } // kmp_str_loc_init
anatofuz
parents:
diff changeset
343
anatofuz
parents:
diff changeset
344 void __kmp_str_loc_free(kmp_str_loc_t *loc) {
anatofuz
parents:
diff changeset
345 __kmp_str_fname_free(&loc->fname);
anatofuz
parents:
diff changeset
346 __kmp_str_free(&(loc->_bulk));
anatofuz
parents:
diff changeset
347 loc->file = NULL;
anatofuz
parents:
diff changeset
348 loc->func = NULL;
anatofuz
parents:
diff changeset
349 } // kmp_str_loc_free
anatofuz
parents:
diff changeset
350
anatofuz
parents:
diff changeset
351 /* This function is intended to compare file names. On Windows* OS file names
anatofuz
parents:
diff changeset
352 are case-insensitive, so functions performs case-insensitive comparison. On
anatofuz
parents:
diff changeset
353 Linux* OS it performs case-sensitive comparison. Note: The function returns
anatofuz
parents:
diff changeset
354 *true* if strings are *equal*. */
anatofuz
parents:
diff changeset
355 int __kmp_str_eqf( // True, if strings are equal, false otherwise.
anatofuz
parents:
diff changeset
356 char const *lhs, // First string.
anatofuz
parents:
diff changeset
357 char const *rhs // Second string.
anatofuz
parents:
diff changeset
358 ) {
anatofuz
parents:
diff changeset
359 int result;
anatofuz
parents:
diff changeset
360 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
361 result = (_stricmp(lhs, rhs) == 0);
anatofuz
parents:
diff changeset
362 #else
anatofuz
parents:
diff changeset
363 result = (strcmp(lhs, rhs) == 0);
anatofuz
parents:
diff changeset
364 #endif
anatofuz
parents:
diff changeset
365 return result;
anatofuz
parents:
diff changeset
366 } // __kmp_str_eqf
anatofuz
parents:
diff changeset
367
anatofuz
parents:
diff changeset
368 /* This function is like sprintf, but it *allocates* new buffer, which must be
anatofuz
parents:
diff changeset
369 freed eventually by __kmp_str_free(). The function is very convenient for
anatofuz
parents:
diff changeset
370 constructing strings, it successfully replaces strdup(), strcat(), it frees
anatofuz
parents:
diff changeset
371 programmer from buffer allocations and helps to avoid buffer overflows.
anatofuz
parents:
diff changeset
372 Examples:
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 str = __kmp_str_format("%s", orig); //strdup() doesn't care about buffer size
anatofuz
parents:
diff changeset
375 __kmp_str_free( & str );
anatofuz
parents:
diff changeset
376 str = __kmp_str_format( "%s%s", orig1, orig2 ); // strcat(), doesn't care
anatofuz
parents:
diff changeset
377 // about buffer size.
anatofuz
parents:
diff changeset
378 __kmp_str_free( & str );
anatofuz
parents:
diff changeset
379 str = __kmp_str_format( "%s/%s.txt", path, file ); // constructing string.
anatofuz
parents:
diff changeset
380 __kmp_str_free( & str );
anatofuz
parents:
diff changeset
381
anatofuz
parents:
diff changeset
382 Performance note:
anatofuz
parents:
diff changeset
383 This function allocates memory with malloc() calls, so do not call it from
anatofuz
parents:
diff changeset
384 performance-critical code. In performance-critical code consider using
anatofuz
parents:
diff changeset
385 kmp_str_buf_t instead, since it uses stack-allocated buffer for short
anatofuz
parents:
diff changeset
386 strings.
anatofuz
parents:
diff changeset
387
anatofuz
parents:
diff changeset
388 Why does this function use malloc()?
anatofuz
parents:
diff changeset
389 1. __kmp_allocate() returns cache-aligned memory allocated with malloc().
anatofuz
parents:
diff changeset
390 There are no reasons in using __kmp_allocate() for strings due to extra
anatofuz
parents:
diff changeset
391 overhead while cache-aligned memory is not necessary.
anatofuz
parents:
diff changeset
392 2. __kmp_thread_malloc() cannot be used because it requires pointer to thread
anatofuz
parents:
diff changeset
393 structure. We need to perform string operations during library startup
anatofuz
parents:
diff changeset
394 (for example, in __kmp_register_library_startup()) when no thread
anatofuz
parents:
diff changeset
395 structures are allocated yet.
anatofuz
parents:
diff changeset
396 So standard malloc() is the only available option.
anatofuz
parents:
diff changeset
397 */
anatofuz
parents:
diff changeset
398
anatofuz
parents:
diff changeset
399 char *__kmp_str_format( // Allocated string.
anatofuz
parents:
diff changeset
400 char const *format, // Format string.
anatofuz
parents:
diff changeset
401 ... // Other parameters.
anatofuz
parents:
diff changeset
402 ) {
anatofuz
parents:
diff changeset
403 va_list args;
anatofuz
parents:
diff changeset
404 int size = 512;
anatofuz
parents:
diff changeset
405 char *buffer = NULL;
anatofuz
parents:
diff changeset
406 int rc;
anatofuz
parents:
diff changeset
407
anatofuz
parents:
diff changeset
408 // Allocate buffer.
anatofuz
parents:
diff changeset
409 buffer = (char *)KMP_INTERNAL_MALLOC(size);
anatofuz
parents:
diff changeset
410 if (buffer == NULL) {
anatofuz
parents:
diff changeset
411 KMP_FATAL(MemoryAllocFailed);
anatofuz
parents:
diff changeset
412 }
anatofuz
parents:
diff changeset
413
anatofuz
parents:
diff changeset
414 for (;;) {
anatofuz
parents:
diff changeset
415 // Try to format string.
anatofuz
parents:
diff changeset
416 va_start(args, format);
anatofuz
parents:
diff changeset
417 rc = KMP_VSNPRINTF(buffer, size, format, args);
anatofuz
parents:
diff changeset
418 va_end(args);
anatofuz
parents:
diff changeset
419
anatofuz
parents:
diff changeset
420 // No errors, string has been formatted.
anatofuz
parents:
diff changeset
421 if (rc >= 0 && rc < size) {
anatofuz
parents:
diff changeset
422 break;
anatofuz
parents:
diff changeset
423 }
anatofuz
parents:
diff changeset
424
anatofuz
parents:
diff changeset
425 // Error occurred, buffer is too small.
anatofuz
parents:
diff changeset
426 if (rc >= 0) {
anatofuz
parents:
diff changeset
427 // C99-conforming implementation of vsnprintf returns required buffer
anatofuz
parents:
diff changeset
428 // size.
anatofuz
parents:
diff changeset
429 size = rc + 1;
anatofuz
parents:
diff changeset
430 } else {
anatofuz
parents:
diff changeset
431 // Older implementations just return -1.
anatofuz
parents:
diff changeset
432 size = size * 2;
anatofuz
parents:
diff changeset
433 }
anatofuz
parents:
diff changeset
434
anatofuz
parents:
diff changeset
435 // Enlarge buffer and try again.
anatofuz
parents:
diff changeset
436 buffer = (char *)KMP_INTERNAL_REALLOC(buffer, size);
anatofuz
parents:
diff changeset
437 if (buffer == NULL) {
anatofuz
parents:
diff changeset
438 KMP_FATAL(MemoryAllocFailed);
anatofuz
parents:
diff changeset
439 }
anatofuz
parents:
diff changeset
440 }
anatofuz
parents:
diff changeset
441
anatofuz
parents:
diff changeset
442 return buffer;
anatofuz
parents:
diff changeset
443 } // func __kmp_str_format
anatofuz
parents:
diff changeset
444
anatofuz
parents:
diff changeset
445 void __kmp_str_free(char **str) {
anatofuz
parents:
diff changeset
446 KMP_DEBUG_ASSERT(str != NULL);
anatofuz
parents:
diff changeset
447 KMP_INTERNAL_FREE(*str);
anatofuz
parents:
diff changeset
448 *str = NULL;
anatofuz
parents:
diff changeset
449 } // func __kmp_str_free
anatofuz
parents:
diff changeset
450
anatofuz
parents:
diff changeset
451 /* If len is zero, returns true iff target and data have exact case-insensitive
anatofuz
parents:
diff changeset
452 match. If len is negative, returns true iff target is a case-insensitive
anatofuz
parents:
diff changeset
453 substring of data. If len is positive, returns true iff target is a
anatofuz
parents:
diff changeset
454 case-insensitive substring of data or vice versa, and neither is shorter than
anatofuz
parents:
diff changeset
455 len. */
anatofuz
parents:
diff changeset
456 int __kmp_str_match(char const *target, int len, char const *data) {
anatofuz
parents:
diff changeset
457 int i;
anatofuz
parents:
diff changeset
458 if (target == NULL || data == NULL) {
anatofuz
parents:
diff changeset
459 return FALSE;
anatofuz
parents:
diff changeset
460 }
anatofuz
parents:
diff changeset
461 for (i = 0; target[i] && data[i]; ++i) {
anatofuz
parents:
diff changeset
462 if (TOLOWER(target[i]) != TOLOWER(data[i])) {
anatofuz
parents:
diff changeset
463 return FALSE;
anatofuz
parents:
diff changeset
464 }
anatofuz
parents:
diff changeset
465 }
anatofuz
parents:
diff changeset
466 return ((len > 0) ? i >= len : (!target[i] && (len || !data[i])));
anatofuz
parents:
diff changeset
467 } // __kmp_str_match
anatofuz
parents:
diff changeset
468
anatofuz
parents:
diff changeset
469 int __kmp_str_match_false(char const *data) {
anatofuz
parents:
diff changeset
470 int result =
anatofuz
parents:
diff changeset
471 __kmp_str_match("false", 1, data) || __kmp_str_match("off", 2, data) ||
anatofuz
parents:
diff changeset
472 __kmp_str_match("0", 1, data) || __kmp_str_match(".false.", 2, data) ||
anatofuz
parents:
diff changeset
473 __kmp_str_match(".f.", 2, data) || __kmp_str_match("no", 1, data) ||
anatofuz
parents:
diff changeset
474 __kmp_str_match("disabled", 0, data);
anatofuz
parents:
diff changeset
475 return result;
anatofuz
parents:
diff changeset
476 } // __kmp_str_match_false
anatofuz
parents:
diff changeset
477
anatofuz
parents:
diff changeset
478 int __kmp_str_match_true(char const *data) {
anatofuz
parents:
diff changeset
479 int result =
anatofuz
parents:
diff changeset
480 __kmp_str_match("true", 1, data) || __kmp_str_match("on", 2, data) ||
anatofuz
parents:
diff changeset
481 __kmp_str_match("1", 1, data) || __kmp_str_match(".true.", 2, data) ||
anatofuz
parents:
diff changeset
482 __kmp_str_match(".t.", 2, data) || __kmp_str_match("yes", 1, data) ||
anatofuz
parents:
diff changeset
483 __kmp_str_match("enabled", 0, data);
anatofuz
parents:
diff changeset
484 return result;
anatofuz
parents:
diff changeset
485 } // __kmp_str_match_true
anatofuz
parents:
diff changeset
486
anatofuz
parents:
diff changeset
487 void __kmp_str_replace(char *str, char search_for, char replace_with) {
anatofuz
parents:
diff changeset
488 char *found = NULL;
anatofuz
parents:
diff changeset
489
anatofuz
parents:
diff changeset
490 found = strchr(str, search_for);
anatofuz
parents:
diff changeset
491 while (found) {
anatofuz
parents:
diff changeset
492 *found = replace_with;
anatofuz
parents:
diff changeset
493 found = strchr(found + 1, search_for);
anatofuz
parents:
diff changeset
494 }
anatofuz
parents:
diff changeset
495 } // __kmp_str_replace
anatofuz
parents:
diff changeset
496
anatofuz
parents:
diff changeset
497 void __kmp_str_split(char *str, // I: String to split.
anatofuz
parents:
diff changeset
498 char delim, // I: Character to split on.
anatofuz
parents:
diff changeset
499 char **head, // O: Pointer to head (may be NULL).
anatofuz
parents:
diff changeset
500 char **tail // O: Pointer to tail (may be NULL).
anatofuz
parents:
diff changeset
501 ) {
anatofuz
parents:
diff changeset
502 char *h = str;
anatofuz
parents:
diff changeset
503 char *t = NULL;
anatofuz
parents:
diff changeset
504 if (str != NULL) {
anatofuz
parents:
diff changeset
505 char *ptr = strchr(str, delim);
anatofuz
parents:
diff changeset
506 if (ptr != NULL) {
anatofuz
parents:
diff changeset
507 *ptr = 0;
anatofuz
parents:
diff changeset
508 t = ptr + 1;
anatofuz
parents:
diff changeset
509 }
anatofuz
parents:
diff changeset
510 }
anatofuz
parents:
diff changeset
511 if (head != NULL) {
anatofuz
parents:
diff changeset
512 *head = h;
anatofuz
parents:
diff changeset
513 }
anatofuz
parents:
diff changeset
514 if (tail != NULL) {
anatofuz
parents:
diff changeset
515 *tail = t;
anatofuz
parents:
diff changeset
516 }
anatofuz
parents:
diff changeset
517 } // __kmp_str_split
anatofuz
parents:
diff changeset
518
anatofuz
parents:
diff changeset
519 /* strtok_r() is not available on Windows* OS. This function reimplements
anatofuz
parents:
diff changeset
520 strtok_r(). */
anatofuz
parents:
diff changeset
521 char *__kmp_str_token(
anatofuz
parents:
diff changeset
522 char *str, // String to split into tokens. Note: String *is* modified!
anatofuz
parents:
diff changeset
523 char const *delim, // Delimiters.
anatofuz
parents:
diff changeset
524 char **buf // Internal buffer.
anatofuz
parents:
diff changeset
525 ) {
anatofuz
parents:
diff changeset
526 char *token = NULL;
anatofuz
parents:
diff changeset
527 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
528 // On Windows* OS there is no strtok_r() function. Let us implement it.
anatofuz
parents:
diff changeset
529 if (str != NULL) {
anatofuz
parents:
diff changeset
530 *buf = str; // First call, initialize buf.
anatofuz
parents:
diff changeset
531 }
anatofuz
parents:
diff changeset
532 *buf += strspn(*buf, delim); // Skip leading delimiters.
anatofuz
parents:
diff changeset
533 if (**buf != 0) { // Rest of the string is not yet empty.
anatofuz
parents:
diff changeset
534 token = *buf; // Use it as result.
anatofuz
parents:
diff changeset
535 *buf += strcspn(*buf, delim); // Skip non-delimiters.
anatofuz
parents:
diff changeset
536 if (**buf != 0) { // Rest of the string is not yet empty.
anatofuz
parents:
diff changeset
537 **buf = 0; // Terminate token here.
anatofuz
parents:
diff changeset
538 *buf += 1; // Advance buf to start with the next token next time.
anatofuz
parents:
diff changeset
539 }
anatofuz
parents:
diff changeset
540 }
anatofuz
parents:
diff changeset
541 #else
anatofuz
parents:
diff changeset
542 // On Linux* OS and OS X*, strtok_r() is available. Let us use it.
anatofuz
parents:
diff changeset
543 token = strtok_r(str, delim, buf);
anatofuz
parents:
diff changeset
544 #endif
anatofuz
parents:
diff changeset
545 return token;
anatofuz
parents:
diff changeset
546 } // __kmp_str_token
anatofuz
parents:
diff changeset
547
anatofuz
parents:
diff changeset
548 int __kmp_str_to_int(char const *str, char sentinel) {
anatofuz
parents:
diff changeset
549 int result, factor;
anatofuz
parents:
diff changeset
550 char const *t;
anatofuz
parents:
diff changeset
551
anatofuz
parents:
diff changeset
552 result = 0;
anatofuz
parents:
diff changeset
553
anatofuz
parents:
diff changeset
554 for (t = str; *t != '\0'; ++t) {
anatofuz
parents:
diff changeset
555 if (*t < '0' || *t > '9')
anatofuz
parents:
diff changeset
556 break;
anatofuz
parents:
diff changeset
557 result = (result * 10) + (*t - '0');
anatofuz
parents:
diff changeset
558 }
anatofuz
parents:
diff changeset
559
anatofuz
parents:
diff changeset
560 switch (*t) {
anatofuz
parents:
diff changeset
561 case '\0': /* the current default for no suffix is bytes */
anatofuz
parents:
diff changeset
562 factor = 1;
anatofuz
parents:
diff changeset
563 break;
anatofuz
parents:
diff changeset
564 case 'b':
anatofuz
parents:
diff changeset
565 case 'B': /* bytes */
anatofuz
parents:
diff changeset
566 ++t;
anatofuz
parents:
diff changeset
567 factor = 1;
anatofuz
parents:
diff changeset
568 break;
anatofuz
parents:
diff changeset
569 case 'k':
anatofuz
parents:
diff changeset
570 case 'K': /* kilo-bytes */
anatofuz
parents:
diff changeset
571 ++t;
anatofuz
parents:
diff changeset
572 factor = 1024;
anatofuz
parents:
diff changeset
573 break;
anatofuz
parents:
diff changeset
574 case 'm':
anatofuz
parents:
diff changeset
575 case 'M': /* mega-bytes */
anatofuz
parents:
diff changeset
576 ++t;
anatofuz
parents:
diff changeset
577 factor = (1024 * 1024);
anatofuz
parents:
diff changeset
578 break;
anatofuz
parents:
diff changeset
579 default:
anatofuz
parents:
diff changeset
580 if (*t != sentinel)
anatofuz
parents:
diff changeset
581 return (-1);
anatofuz
parents:
diff changeset
582 t = "";
anatofuz
parents:
diff changeset
583 factor = 1;
anatofuz
parents:
diff changeset
584 }
anatofuz
parents:
diff changeset
585
anatofuz
parents:
diff changeset
586 if (result > (INT_MAX / factor))
anatofuz
parents:
diff changeset
587 result = INT_MAX;
anatofuz
parents:
diff changeset
588 else
anatofuz
parents:
diff changeset
589 result *= factor;
anatofuz
parents:
diff changeset
590
anatofuz
parents:
diff changeset
591 return (*t != 0 ? 0 : result);
anatofuz
parents:
diff changeset
592 } // __kmp_str_to_int
anatofuz
parents:
diff changeset
593
anatofuz
parents:
diff changeset
594 /* The routine parses input string. It is expected it is a unsigned integer with
anatofuz
parents:
diff changeset
595 optional unit. Units are: "b" for bytes, "kb" or just "k" for kilobytes, "mb"
anatofuz
parents:
diff changeset
596 or "m" for megabytes, ..., "yb" or "y" for yottabytes. :-) Unit name is
anatofuz
parents:
diff changeset
597 case-insensitive. The routine returns 0 if everything is ok, or error code:
anatofuz
parents:
diff changeset
598 -1 in case of overflow, -2 in case of unknown unit. *size is set to parsed
anatofuz
parents:
diff changeset
599 value. In case of overflow *size is set to KMP_SIZE_T_MAX, in case of unknown
anatofuz
parents:
diff changeset
600 unit *size is set to zero. */
anatofuz
parents:
diff changeset
601 void __kmp_str_to_size( // R: Error code.
anatofuz
parents:
diff changeset
602 char const *str, // I: String of characters, unsigned number and unit ("b",
anatofuz
parents:
diff changeset
603 // "kb", etc).
anatofuz
parents:
diff changeset
604 size_t *out, // O: Parsed number.
anatofuz
parents:
diff changeset
605 size_t dfactor, // I: The factor if none of the letters specified.
anatofuz
parents:
diff changeset
606 char const **error // O: Null if everything is ok, error message otherwise.
anatofuz
parents:
diff changeset
607 ) {
anatofuz
parents:
diff changeset
608
anatofuz
parents:
diff changeset
609 size_t value = 0;
anatofuz
parents:
diff changeset
610 size_t factor = 0;
anatofuz
parents:
diff changeset
611 int overflow = 0;
anatofuz
parents:
diff changeset
612 int i = 0;
anatofuz
parents:
diff changeset
613 int digit;
anatofuz
parents:
diff changeset
614
anatofuz
parents:
diff changeset
615 KMP_DEBUG_ASSERT(str != NULL);
anatofuz
parents:
diff changeset
616
anatofuz
parents:
diff changeset
617 // Skip spaces.
anatofuz
parents:
diff changeset
618 while (str[i] == ' ' || str[i] == '\t') {
anatofuz
parents:
diff changeset
619 ++i;
anatofuz
parents:
diff changeset
620 }
anatofuz
parents:
diff changeset
621
anatofuz
parents:
diff changeset
622 // Parse number.
anatofuz
parents:
diff changeset
623 if (str[i] < '0' || str[i] > '9') {
anatofuz
parents:
diff changeset
624 *error = KMP_I18N_STR(NotANumber);
anatofuz
parents:
diff changeset
625 return;
anatofuz
parents:
diff changeset
626 }
anatofuz
parents:
diff changeset
627 do {
anatofuz
parents:
diff changeset
628 digit = str[i] - '0';
anatofuz
parents:
diff changeset
629 overflow = overflow || (value > (KMP_SIZE_T_MAX - digit) / 10);
anatofuz
parents:
diff changeset
630 value = (value * 10) + digit;
anatofuz
parents:
diff changeset
631 ++i;
anatofuz
parents:
diff changeset
632 } while (str[i] >= '0' && str[i] <= '9');
anatofuz
parents:
diff changeset
633
anatofuz
parents:
diff changeset
634 // Skip spaces.
anatofuz
parents:
diff changeset
635 while (str[i] == ' ' || str[i] == '\t') {
anatofuz
parents:
diff changeset
636 ++i;
anatofuz
parents:
diff changeset
637 }
anatofuz
parents:
diff changeset
638
anatofuz
parents:
diff changeset
639 // Parse unit.
anatofuz
parents:
diff changeset
640 #define _case(ch, exp) \
anatofuz
parents:
diff changeset
641 case ch: \
anatofuz
parents:
diff changeset
642 case ch - ('a' - 'A'): { \
anatofuz
parents:
diff changeset
643 size_t shift = (exp)*10; \
anatofuz
parents:
diff changeset
644 ++i; \
anatofuz
parents:
diff changeset
645 if (shift < sizeof(size_t) * 8) { \
anatofuz
parents:
diff changeset
646 factor = (size_t)(1) << shift; \
anatofuz
parents:
diff changeset
647 } else { \
anatofuz
parents:
diff changeset
648 overflow = 1; \
anatofuz
parents:
diff changeset
649 } \
anatofuz
parents:
diff changeset
650 } break;
anatofuz
parents:
diff changeset
651 switch (str[i]) {
anatofuz
parents:
diff changeset
652 _case('k', 1); // Kilo
anatofuz
parents:
diff changeset
653 _case('m', 2); // Mega
anatofuz
parents:
diff changeset
654 _case('g', 3); // Giga
anatofuz
parents:
diff changeset
655 _case('t', 4); // Tera
anatofuz
parents:
diff changeset
656 _case('p', 5); // Peta
anatofuz
parents:
diff changeset
657 _case('e', 6); // Exa
anatofuz
parents:
diff changeset
658 _case('z', 7); // Zetta
anatofuz
parents:
diff changeset
659 _case('y', 8); // Yotta
anatofuz
parents:
diff changeset
660 // Oops. No more units...
anatofuz
parents:
diff changeset
661 }
anatofuz
parents:
diff changeset
662 #undef _case
anatofuz
parents:
diff changeset
663 if (str[i] == 'b' || str[i] == 'B') { // Skip optional "b".
anatofuz
parents:
diff changeset
664 if (factor == 0) {
anatofuz
parents:
diff changeset
665 factor = 1;
anatofuz
parents:
diff changeset
666 }
anatofuz
parents:
diff changeset
667 ++i;
anatofuz
parents:
diff changeset
668 }
anatofuz
parents:
diff changeset
669 if (!(str[i] == ' ' || str[i] == '\t' || str[i] == 0)) { // Bad unit
anatofuz
parents:
diff changeset
670 *error = KMP_I18N_STR(BadUnit);
anatofuz
parents:
diff changeset
671 return;
anatofuz
parents:
diff changeset
672 }
anatofuz
parents:
diff changeset
673
anatofuz
parents:
diff changeset
674 if (factor == 0) {
anatofuz
parents:
diff changeset
675 factor = dfactor;
anatofuz
parents:
diff changeset
676 }
anatofuz
parents:
diff changeset
677
anatofuz
parents:
diff changeset
678 // Apply factor.
anatofuz
parents:
diff changeset
679 overflow = overflow || (value > (KMP_SIZE_T_MAX / factor));
anatofuz
parents:
diff changeset
680 value *= factor;
anatofuz
parents:
diff changeset
681
anatofuz
parents:
diff changeset
682 // Skip spaces.
anatofuz
parents:
diff changeset
683 while (str[i] == ' ' || str[i] == '\t') {
anatofuz
parents:
diff changeset
684 ++i;
anatofuz
parents:
diff changeset
685 }
anatofuz
parents:
diff changeset
686
anatofuz
parents:
diff changeset
687 if (str[i] != 0) {
anatofuz
parents:
diff changeset
688 *error = KMP_I18N_STR(IllegalCharacters);
anatofuz
parents:
diff changeset
689 return;
anatofuz
parents:
diff changeset
690 }
anatofuz
parents:
diff changeset
691
anatofuz
parents:
diff changeset
692 if (overflow) {
anatofuz
parents:
diff changeset
693 *error = KMP_I18N_STR(ValueTooLarge);
anatofuz
parents:
diff changeset
694 *out = KMP_SIZE_T_MAX;
anatofuz
parents:
diff changeset
695 return;
anatofuz
parents:
diff changeset
696 }
anatofuz
parents:
diff changeset
697
anatofuz
parents:
diff changeset
698 *error = NULL;
anatofuz
parents:
diff changeset
699 *out = value;
anatofuz
parents:
diff changeset
700 } // __kmp_str_to_size
anatofuz
parents:
diff changeset
701
anatofuz
parents:
diff changeset
702 void __kmp_str_to_uint( // R: Error code.
anatofuz
parents:
diff changeset
703 char const *str, // I: String of characters, unsigned number.
anatofuz
parents:
diff changeset
704 kmp_uint64 *out, // O: Parsed number.
anatofuz
parents:
diff changeset
705 char const **error // O: Null if everything is ok, error message otherwise.
anatofuz
parents:
diff changeset
706 ) {
anatofuz
parents:
diff changeset
707 size_t value = 0;
anatofuz
parents:
diff changeset
708 int overflow = 0;
anatofuz
parents:
diff changeset
709 int i = 0;
anatofuz
parents:
diff changeset
710 int digit;
anatofuz
parents:
diff changeset
711
anatofuz
parents:
diff changeset
712 KMP_DEBUG_ASSERT(str != NULL);
anatofuz
parents:
diff changeset
713
anatofuz
parents:
diff changeset
714 // Skip spaces.
anatofuz
parents:
diff changeset
715 while (str[i] == ' ' || str[i] == '\t') {
anatofuz
parents:
diff changeset
716 ++i;
anatofuz
parents:
diff changeset
717 }
anatofuz
parents:
diff changeset
718
anatofuz
parents:
diff changeset
719 // Parse number.
anatofuz
parents:
diff changeset
720 if (str[i] < '0' || str[i] > '9') {
anatofuz
parents:
diff changeset
721 *error = KMP_I18N_STR(NotANumber);
anatofuz
parents:
diff changeset
722 return;
anatofuz
parents:
diff changeset
723 }
anatofuz
parents:
diff changeset
724 do {
anatofuz
parents:
diff changeset
725 digit = str[i] - '0';
anatofuz
parents:
diff changeset
726 overflow = overflow || (value > (KMP_SIZE_T_MAX - digit) / 10);
anatofuz
parents:
diff changeset
727 value = (value * 10) + digit;
anatofuz
parents:
diff changeset
728 ++i;
anatofuz
parents:
diff changeset
729 } while (str[i] >= '0' && str[i] <= '9');
anatofuz
parents:
diff changeset
730
anatofuz
parents:
diff changeset
731 // Skip spaces.
anatofuz
parents:
diff changeset
732 while (str[i] == ' ' || str[i] == '\t') {
anatofuz
parents:
diff changeset
733 ++i;
anatofuz
parents:
diff changeset
734 }
anatofuz
parents:
diff changeset
735
anatofuz
parents:
diff changeset
736 if (str[i] != 0) {
anatofuz
parents:
diff changeset
737 *error = KMP_I18N_STR(IllegalCharacters);
anatofuz
parents:
diff changeset
738 return;
anatofuz
parents:
diff changeset
739 }
anatofuz
parents:
diff changeset
740
anatofuz
parents:
diff changeset
741 if (overflow) {
anatofuz
parents:
diff changeset
742 *error = KMP_I18N_STR(ValueTooLarge);
anatofuz
parents:
diff changeset
743 *out = (kmp_uint64)-1;
anatofuz
parents:
diff changeset
744 return;
anatofuz
parents:
diff changeset
745 }
anatofuz
parents:
diff changeset
746
anatofuz
parents:
diff changeset
747 *error = NULL;
anatofuz
parents:
diff changeset
748 *out = value;
anatofuz
parents:
diff changeset
749 } // __kmp_str_to_unit
anatofuz
parents:
diff changeset
750
anatofuz
parents:
diff changeset
751 // end of file //