]> mj.ucw.cz Git - libucw.git/blob - ucw/str-split.c
Merge branch 'master' into dev-sizet
[libucw.git] / ucw / str-split.c
1 /*
2  *      UCW Library -- Word Splitting
3  *
4  *      (c) 1997 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <ucw/chartype.h>
13 #include <ucw/string.h>
14
15 #include <string.h>
16
17 int
18 str_sepsplit(char *str, uint sep, char **rec, uint max)
19 {
20   uint cnt = 0;
21   while (1)
22   {
23     rec[cnt++] = str;
24     str = strchr(str, sep);
25     if (!str)
26       return cnt;
27     if (cnt >= max)
28       return -1;
29     *str++ = 0;
30   }
31 }
32
33 int
34 str_wordsplit(char *src, char **dst, uint max)
35 {
36   uint cnt = 0;
37
38   for(;;)
39     {
40       while (Cspace(*src))
41         *src++ = 0;
42       if (!*src)
43         break;
44       if (cnt >= max)
45         return -1;
46       if (*src == '"')
47         {
48           src++;
49           dst[cnt++] = src;
50           while (*src && *src != '"')
51             src++;
52           if (*src)
53             *src++ = 0;
54         }
55       else
56         {
57           dst[cnt++] = src;
58           while (*src && !Cspace(*src))
59             src++;
60         }
61     }
62   return cnt;
63 }