]> mj.ucw.cz Git - libucw.git/blob - lib/wordsplit.c
Rewritten ff-binary.
[libucw.git] / lib / wordsplit.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 "lib/lib.h"
12 #include "lib/chartype.h"
13
14 #include <string.h>
15
16 int
17 sepsplit(byte *str, byte sep, byte **rec, uns max)
18 {
19   uns cnt = 0;
20   while (1)
21   {
22     rec[cnt++] = str;
23     str = strchr(str, sep);
24     if (!str)
25       return cnt;
26     if (cnt >= max)
27       return -1;
28     *str++ = 0;
29   }
30 }
31
32 int
33 wordsplit(byte *src, byte **dst, uns max)
34 {
35   uns cnt = 0;
36
37   for(;;)
38     {
39       while (Cspace(*src))
40         *src++ = 0;
41       if (!*src)
42         break;
43       if (cnt >= max)
44         return -1;
45       if (*src == '"')
46         {
47           src++;
48           dst[cnt++] = src;
49           while (*src && *src != '"')
50             src++;
51           if (*src)
52             *src++ = 0;
53         }
54       else
55         {
56           dst[cnt++] = src;
57           while (*src && !Cspace(*src))
58             src++;
59         }
60     }
61   return cnt;
62 }