+ Fixes
[opsoft.git] / silentbob / gclib / src / dsplit.cxx
blob9b58fed5d6087164de6726e63c83412d62063e58
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib.h>
8 #undef __export
9 #define __export
11 /* 2006-06-07 */
12 __export int Dsplit (char * STR, char *ch, DList * to_list)
14 DList * ret = to_list;
15 char * S;
16 char * ptr = STR;
17 int ch_len;
18 int count = 0;
20 if (!STR || !ch || !ret)
21 return -1;
23 ch_len = strlen (ch);
24 while((S = strstr (ptr, ch)) && S) {
25 ret->add_tail (Dstrndup (ptr, S - ptr));
26 S += ch_len;
27 ptr = S;
28 ++count;
31 if (strlen (ptr)) {
32 ret->add_tail (strdup (ptr));
33 ++count;
36 return count;
39 __export int Dsplit (char * buf, size_t buflen, char *str, EList * to_list)
41 char * S;
42 char * ptr = buf;
43 int str_len;
44 int count = 0;
45 char * last;
47 if (!buf || !str || !to_list)
48 return -1;
50 last = &buf[buflen-1];
51 str_len = strlen (str);
52 while((S = Dmid_memmem (ptr, last, str, str_len)) && S) {
53 *to_list << Dstrndup (ptr, S - ptr);
54 S += str_len;
55 ptr = S;
56 ++count;
59 if (ptr <= last) {
60 *to_list << Dstrndup (ptr, last-ptr+1);
61 ++count;
64 return count;