-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexplode_string.c
More file actions
70 lines (56 loc) · 1.4 KB
/
explode_string.c
File metadata and controls
70 lines (56 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "cli-sub.h"
#include "err_ref.h"
#include <string.h>
#include <stdlib.h>
/* --- */
struct string_parts *explode_string( int *rc, char *string, char *delim)
{
int dlen, count, ii, err = ERR_BAD_PARMS;
char *st, *buff = 0;
struct string_parts *rez = 0;
if( string && delim) if( *string && *delim) err = RC_NORMAL;
if( err == RC_NORMAL)
{
buff = strdup( string);
if( !buff) err = ERR_MALLOC_FAILED;
else
{
rez = (struct string_parts *) malloc( sizeof *rez);
if( !rez) err = ERR_MALLOC_FAILED;
}
}
if( err == RC_NORMAL)
{
rez->np = 0;
rez->list = 0;
dlen = strlen( delim);
count = 1;
for( st = buff; *st; )
{
if( strncmp( st, delim, dlen)) st++;
else
{
count++;
*st = '\0';
st += dlen;
}
}
rez->list = (char **) malloc( count * (sizeof *(rez->list)));
if( !rez->list) err = ERR_MALLOC_FAILED;
else rez->np = count;
}
if( err == RC_NORMAL)
{
for( ii = 0; ii < count; ii++)
rez->list[ ii] = 0;
for( ii = 0, st = buff; ii < count && err == RC_NORMAL; ii++)
{
rez->list[ ii] = strdup( st);
if( !rez->list[ ii]) err = ERR_MALLOC_FAILED;
else st += strlen( st) + dlen;
}
if( buff) free( buff);
}
if( rc) *rc = err;
return rez;
}