/src/jasper/src/libjasper/base/jas_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1999-2000 Image Power, Inc. and the University of |
3 | | * British Columbia. |
4 | | * Copyright (c) 2001-2002 Michael David Adams. |
5 | | * All rights reserved. |
6 | | */ |
7 | | |
8 | | /* __START_OF_JASPER_LICENSE__ |
9 | | * |
10 | | * JasPer License Version 2.0 |
11 | | * |
12 | | * Copyright (c) 2001-2006 Michael David Adams |
13 | | * Copyright (c) 1999-2000 Image Power, Inc. |
14 | | * Copyright (c) 1999-2000 The University of British Columbia |
15 | | * |
16 | | * All rights reserved. |
17 | | * |
18 | | * Permission is hereby granted, free of charge, to any person (the |
19 | | * "User") obtaining a copy of this software and associated documentation |
20 | | * files (the "Software"), to deal in the Software without restriction, |
21 | | * including without limitation the rights to use, copy, modify, merge, |
22 | | * publish, distribute, and/or sell copies of the Software, and to permit |
23 | | * persons to whom the Software is furnished to do so, subject to the |
24 | | * following conditions: |
25 | | * |
26 | | * 1. The above copyright notices and this permission notice (which |
27 | | * includes the disclaimer below) shall be included in all copies or |
28 | | * substantial portions of the Software. |
29 | | * |
30 | | * 2. The name of a copyright holder shall not be used to endorse or |
31 | | * promote products derived from the Software without specific prior |
32 | | * written permission. |
33 | | * |
34 | | * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS |
35 | | * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
36 | | * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS |
37 | | * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
38 | | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
39 | | * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO |
40 | | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL |
41 | | * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING |
42 | | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
43 | | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
44 | | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE |
45 | | * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE |
46 | | * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. |
47 | | * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS |
48 | | * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL |
49 | | * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS |
50 | | * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE |
51 | | * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE |
52 | | * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL |
53 | | * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, |
54 | | * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL |
55 | | * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH |
56 | | * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, |
57 | | * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH |
58 | | * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY |
59 | | * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. |
60 | | * |
61 | | * __END_OF_JASPER_LICENSE__ |
62 | | */ |
63 | | |
64 | | /* |
65 | | * String Library |
66 | | * |
67 | | * $Id$ |
68 | | */ |
69 | | |
70 | | /******************************************************************************\ |
71 | | * Includes |
72 | | \******************************************************************************/ |
73 | | |
74 | | #define JAS_FOR_INTERNAL_USE_ONLY |
75 | | |
76 | | #include "jasper/jas_string.h" |
77 | | #include "jasper/jas_malloc.h" |
78 | | #include "jasper/jas_debug.h" |
79 | | |
80 | | #include <string.h> |
81 | | |
82 | | /******************************************************************************\ |
83 | | * Miscellaneous Functions |
84 | | \******************************************************************************/ |
85 | | |
86 | | /* This function is equivalent to the popular but non-standard (and |
87 | | not-always-available) strdup function. */ |
88 | | |
89 | | char *jas_strdup(const char *s) |
90 | 403k | { |
91 | 403k | size_t n; |
92 | 403k | char *p; |
93 | 403k | n = strlen(s) + 1; |
94 | 403k | if (!(p = jas_malloc(n))) { |
95 | 0 | return 0; |
96 | 0 | } |
97 | 403k | strcpy(p, s); |
98 | 403k | return p; |
99 | 403k | } |
100 | | |
101 | | char *jas_strtok(char *s, const char *delim, char **save_ptr) |
102 | 152k | { |
103 | 152k | #if 1 |
104 | 152k | char *result; |
105 | 152k | char *end; |
106 | 152k | if (!s) { |
107 | 76.0k | s = *save_ptr; |
108 | 76.0k | } |
109 | 152k | if (*s != '\0') { |
110 | 76.0k | s += strspn(s, delim); |
111 | 76.0k | if (*s != '\0') { |
112 | 76.0k | end = s + strcspn(s, delim); |
113 | 76.0k | if (*end == '\0') { |
114 | 76.0k | *save_ptr = end; |
115 | 76.0k | } else { |
116 | 0 | *end = '\0'; |
117 | 0 | *save_ptr = end + 1; |
118 | 0 | } |
119 | 76.0k | result = s; |
120 | 76.0k | } else { |
121 | 0 | *save_ptr = s; |
122 | 0 | result = 0; |
123 | 0 | } |
124 | 76.0k | } else { |
125 | 76.0k | *save_ptr = s; |
126 | 76.0k | result = 0; |
127 | 76.0k | } |
128 | 152k | return result; |
129 | | #else |
130 | | return strtok_r(str, delim, save_ptr); |
131 | | #endif |
132 | 152k | } |
133 | | |
134 | | JAS_EXPORT |
135 | | int jas_stringtokenize(const char *string, const char *delim, |
136 | | char ***tokens_buf, size_t *max_tokens_buf, size_t *num_tokens_buf) |
137 | 76.0k | { |
138 | 76.0k | char **tokens = 0; |
139 | 76.0k | size_t max_tokens = 0; |
140 | 76.0k | size_t num_tokens = 0; |
141 | 76.0k | char **new_tokens; |
142 | 76.0k | size_t new_max_tokens; |
143 | 76.0k | char *buffer = 0; |
144 | 76.0k | int ret = 0; |
145 | 76.0k | char *token = 0; |
146 | | |
147 | 76.0k | if (!(buffer = jas_strdup(string))) { |
148 | 0 | ret = -1; |
149 | 0 | goto done; |
150 | 0 | } |
151 | | //new_max_tokens = 1; |
152 | 76.0k | new_max_tokens = 0; |
153 | 76.0k | if (new_max_tokens > 0) { |
154 | 0 | if (!(tokens = jas_malloc(new_max_tokens * sizeof(char *)))) { |
155 | 0 | ret = -1; |
156 | 0 | goto done; |
157 | 0 | } |
158 | 0 | max_tokens = new_max_tokens; |
159 | 0 | } |
160 | | |
161 | 76.0k | bool first = true; |
162 | 76.0k | char *saveptr = 0; |
163 | 152k | for (;;) { |
164 | 152k | char *cp; |
165 | 152k | if (!(cp = jas_strtok(first ? buffer : 0, delim, &saveptr))) { |
166 | 76.0k | break; |
167 | 76.0k | } |
168 | 76.0k | first = false; |
169 | 76.0k | if (!(token = jas_strdup(cp))) { |
170 | 0 | ret = -1; |
171 | 0 | goto done; |
172 | 0 | } |
173 | 76.0k | if (num_tokens == max_tokens) { |
174 | 76.0k | new_max_tokens = max_tokens ? 2 * max_tokens : 1; |
175 | 76.0k | if (!(new_tokens = jas_realloc(tokens, new_max_tokens * |
176 | 76.0k | sizeof(char *)))) { |
177 | 0 | ret = -1; |
178 | 0 | goto done; |
179 | 0 | } |
180 | 76.0k | tokens = new_tokens; |
181 | 76.0k | max_tokens = new_max_tokens; |
182 | 76.0k | } |
183 | 76.0k | assert(num_tokens < max_tokens); |
184 | 76.0k | tokens[num_tokens] = token; |
185 | 76.0k | token = 0; |
186 | 76.0k | ++num_tokens; |
187 | 76.0k | } |
188 | | |
189 | 76.0k | done: |
190 | 76.0k | if (buffer) { |
191 | 76.0k | jas_free(buffer); |
192 | 76.0k | } |
193 | 76.0k | if (ret && tokens) { |
194 | 0 | for (int i = 0; i < num_tokens; ++i) { |
195 | 0 | jas_free(tokens[i]); |
196 | 0 | } |
197 | 0 | jas_free(tokens); |
198 | 0 | tokens = 0; |
199 | 0 | max_tokens = 0; |
200 | 0 | num_tokens = 0; |
201 | 0 | } |
202 | 76.0k | if (token) { |
203 | 0 | jas_free(token); |
204 | 0 | } |
205 | 76.0k | if (!ret) { |
206 | 76.0k | *tokens_buf = tokens; |
207 | 76.0k | *max_tokens_buf = max_tokens; |
208 | 76.0k | *num_tokens_buf = num_tokens; |
209 | 76.0k | } |
210 | 76.0k | if (jas_get_debug_level() >= 100) { |
211 | 0 | jas_eprintf("tokens %p; max_tokens %zu; num_tokens %zu\n", |
212 | 0 | JAS_CAST(void *, tokens), max_tokens, num_tokens); |
213 | 0 | for (int i = 0; i < num_tokens; ++i) { |
214 | 0 | jas_eprintf("[%d] = %s\n", i, tokens[i]); |
215 | 0 | } |
216 | 0 | } |
217 | 76.0k | return ret; |
218 | 76.0k | } |