diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-09-01 14:57:40 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-09-01 14:57:40 -0800 |
| commit | 10aa5616990bc7335a8d825bc84f9bae10bbc328 (patch) | |
| tree | 0dda5498f51918d8d4544918d058964ca2aa2bf8 /lib/libbindle | |
| parent | 70e87071e80afd819747b495e54b5aa0b74e131c (diff) | |
| download | bindletools-10aa5616990bc7335a8d825bc84f9bae10bbc328.tar.gz bindletools-10aa5616990bc7335a8d825bc84f9bae10bbc328.tar.bz2 bindletools-10aa5616990bc7335a8d825bc84f9bae10bbc328.tar.xz bindletools-10aa5616990bc7335a8d825bc84f9bae10bbc328.zip | |
libbindle: refactoring bindle_strsplit()
Diffstat (limited to 'lib/libbindle')
| -rw-r--r-- | lib/libbindle/lstrings.c | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/lib/libbindle/lstrings.c b/lib/libbindle/lstrings.c index 2d44c58..d77ccfa 100644 --- a/lib/libbindle/lstrings.c +++ b/lib/libbindle/lstrings.c | |||
| @@ -183,13 +183,14 @@ bindle_strsplit( | |||
| 183 | char *** argvp, | 183 | char *** argvp, |
| 184 | int * argcp ) | 184 | int * argcp ) |
| 185 | { | 185 | { |
| 186 | int rc; | ||
| 187 | int argc; | 186 | int argc; |
| 188 | size_t pos; | 187 | size_t pos; |
| 189 | char ** argv; | 188 | char ** argv; |
| 190 | char * line; | 189 | char * line; |
| 191 | char * bol; | 190 | char * bol; |
| 192 | char quote; | 191 | char quote; |
| 192 | size_t delim_count; | ||
| 193 | size_t size; | ||
| 193 | 194 | ||
| 194 | BindleDebugTrace(); | 195 | BindleDebugTrace(); |
| 195 | 196 | ||
| @@ -205,12 +206,36 @@ bindle_strsplit( | |||
| 205 | *argcp = 0; | 206 | *argcp = 0; |
| 206 | 207 | ||
| 207 | if (!(str)) | 208 | if (!(str)) |
| 209 | { | ||
| 210 | if ((argvp)) | ||
| 211 | { | ||
| 212 | if ((*argvp = malloc(sizeof(char *))) == NULL) | ||
| 213 | return(errno); | ||
| 214 | (*argvp)[0] = NULL; | ||
| 215 | }; | ||
| 208 | return(BNDL_SUCCESS); | 216 | return(BNDL_SUCCESS); |
| 217 | }; | ||
| 209 | 218 | ||
| 210 | if ((line = bindle_strdup(str)) == NULL) | 219 | if ((line = bindle_strdup(str)) == NULL) |
| 211 | return(ENOMEM); | 220 | return(ENOMEM); |
| 212 | bol = line; | 221 | bol = line; |
| 213 | 222 | ||
| 223 | // pre-allocate array | ||
| 224 | if ((argvp)) | ||
| 225 | { | ||
| 226 | delim_count = 2; | ||
| 227 | for(pos = 0; ((line[pos])); pos++) | ||
| 228 | if (line[pos] == delim) | ||
| 229 | delim_count++; | ||
| 230 | size = delim_count * sizeof(char *); | ||
| 231 | if ((argv = malloc(size)) == NULL) | ||
| 232 | { | ||
| 233 | free(line); | ||
| 234 | return(errno); | ||
| 235 | }; | ||
| 236 | memset(argv, 0, size); | ||
| 237 | }; | ||
| 238 | |||
| 214 | for(pos = 0; ((line[pos])); pos++) | 239 | for(pos = 0; ((line[pos])); pos++) |
| 215 | { | 240 | { |
| 216 | switch(line[pos]) | 241 | switch(line[pos]) |
| @@ -236,25 +261,31 @@ bindle_strsplit( | |||
| 236 | default: | 261 | default: |
| 237 | if (line[pos] == delim) | 262 | if (line[pos] == delim) |
| 238 | { | 263 | { |
| 239 | argc++; | 264 | if ((argvp)) |
| 240 | line[pos] = '\0'; | ||
| 241 | if ((rc = bindle_strsadd(&argv, bol)) != BNDL_SUCCESS) | ||
| 242 | { | 265 | { |
| 243 | free(line); | 266 | line[pos] = '\0'; |
| 244 | bindle_strsfree(argv); | 267 | if ((argv[argc] = bindle_strdup(bol)) == NULL) |
| 245 | return(rc); | 268 | { |
| 269 | free(line); | ||
| 270 | bindle_strsfree(argv); | ||
| 271 | return(errno); | ||
| 272 | }; | ||
| 246 | }; | 273 | }; |
| 274 | argc++; | ||
| 247 | bol = &line[pos+1]; | 275 | bol = &line[pos+1]; |
| 248 | }; | 276 | }; |
| 249 | break; | 277 | break; |
| 250 | }; | 278 | }; |
| 251 | }; | 279 | }; |
| 252 | 280 | ||
| 253 | if ((rc = bindle_strsadd(&argv, bol)) != BNDL_SUCCESS) | 281 | if ((argvp)) |
| 254 | { | 282 | { |
| 255 | free(line); | 283 | if ((argv[argc] = bindle_strdup(bol)) == NULL) |
| 256 | bindle_strsfree(argv); | 284 | { |
| 257 | return(rc); | 285 | free(line); |
| 286 | bindle_strsfree(argv); | ||
| 287 | return(errno); | ||
| 288 | }; | ||
| 258 | }; | 289 | }; |
| 259 | argc++; | 290 | argc++; |
| 260 | 291 | ||
