4 * This file defines the string_tokenize interface
5 * Time-stamp: "2007-11-12 20:40:36 bkorb"
7 * This file is part of AutoOpts, a companion to AutoGen.
8 * AutoOpts is free software.
9 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
11 * AutoOpts is available under any one of two licenses. The license
12 * in use must be one of these two and the choice is under the control
13 * of the user of the license.
15 * The GNU Lesser General Public License, version 3 or later
16 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
18 * The Modified Berkeley Software Distribution License
19 * See the file "COPYING.mbsd"
21 * These files have the following md5sums:
23 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
24 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
25 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
31 #define cc_t const unsigned char
32 #define ch_t unsigned char
34 /* = = = START-STATIC-FORWARD = = = */
35 /* static forward declarations maintained by mk-fwd */
37 copy_cooked( ch_t
** ppDest
, char const ** ppSrc
);
40 copy_raw( ch_t
** ppDest
, char const ** ppSrc
);
41 /* = = = END-STATIC-FORWARD = = = */
44 copy_cooked( ch_t
** ppDest
, char const ** ppSrc
)
46 ch_t
* pDest
= (ch_t
*)*ppDest
;
47 const ch_t
* pSrc
= (const ch_t
*)(*ppSrc
+ 1);
52 case NUL
: *ppSrc
= NULL
; return;
55 pSrc
+= ao_string_cook_escape_char( (char*)pSrc
, (char*)&ch
, 0x7F );
66 *ppDest
= (ch_t
*)pDest
; /* next spot for storing character */
67 *ppSrc
= (char const *)pSrc
; /* char following closing quote */
72 copy_raw( ch_t
** ppDest
, char const ** ppSrc
)
74 ch_t
* pDest
= *ppDest
;
75 cc_t
* pSrc
= (cc_t
*) (*ppSrc
+ 1);
80 case NUL
: *ppSrc
= NULL
; return;
84 * *Four* escapes are handled: newline removal, escape char
85 * quoting and apostrophe quoting
88 case NUL
: *ppSrc
= NULL
; return;
90 if (*(++pSrc
) == '\n')
114 *ppDest
= pDest
; /* next spot for storing character */
115 *ppSrc
= (char const *) pSrc
; /* char following closing quote */
119 /*=export_func ao_string_tokenize
121 * what: tokenize an input string
123 * arg: + char const* + string + string to be tokenized +
125 * ret_type: token_list_t*
126 * ret_desc: pointer to a structure that lists each token
130 * This function will convert one input string into a list of strings.
131 * The list of strings is derived by separating the input based on
132 * white space separation. However, if the input contains either single
133 * or double quote characters, then the text after that character up to
134 * a matching quote will become the string in the list.
136 * The returned pointer should be deallocated with @code{free(3C)} when
137 * are done using the data. The data are placed in a single block of
138 * allocated memory. Do not deallocate individual token/strings.
140 * The structure pointed to will contain at least these two fields:
143 * The number of tokens found in the input string.
145 * An array of @code{tkn_ct + 1} pointers to substring tokens, with
146 * the last pointer set to NULL.
149 * There are two types of quoted strings: single quoted (@code{'}) and
150 * double quoted (@code{"}). Singly quoted strings are fairly raw in that
151 * escape characters (@code{\\}) are simply another character, except when
152 * preceding the following characters:
154 * @code{\\} double backslashes reduce to one
155 * @code{'} incorporates the single quote into the string
156 * @code{\n} suppresses both the backslash and newline character
159 * Double quote strings are formed according to the rules of string
160 * constants in ANSI-C programs.
164 * #include <stdlib.h>
166 * token_list_t* ptl = ao_string_tokenize( some_string )
167 * for (ix = 0; ix < ptl->tkn_ct; ix++)
168 * do_something_with_tkn( ptl->tkn_list[ix] );
171 * Note that everything is freed with the one call to @code{free(3C)}.
174 * NULL is returned and @code{errno} will be set to indicate the problem:
177 * @code{EINVAL} - There was an unterminated quoted string.
179 * @code{ENOENT} - The input string was empty.
181 * @code{ENOMEM} - There is not enough memory.
185 ao_string_tokenize( char const* str
)
187 int max_token_ct
= 1; /* allow for trailing NUL on string */
190 if (str
== NULL
) goto bogus_str
;
193 * Trim leading white space. Use "ENOENT" and a NULL return to indicate
194 * an empty string was passed.
196 while (IS_WHITESPACE_CHAR(*str
)) str
++;
204 * Take an approximate count of tokens. If no quoted strings are used,
205 * it will be accurate. If quoted strings are used, it will be a little
206 * high and we'll squander the space for a few extra pointers.
209 cc_t
* pz
= (cc_t
*)str
;
213 while (! IS_WHITESPACE_CHAR(*++pz
))
214 if (*pz
== NUL
) goto found_nul
;
215 while (IS_WHITESPACE_CHAR(*pz
)) pz
++;
216 } while (*pz
!= NUL
);
222 res
= malloc( sizeof(*res
) + strlen(str
) + (max_token_ct
* sizeof(ch_t
*)) );
229 * Now copy each token into the output buffer.
232 ch_t
* pzDest
= (ch_t
*)(res
->tkn_list
+ (max_token_ct
+ 1));
236 res
->tkn_list
[ res
->tkn_ct
++ ] = pzDest
;
239 if (IS_WHITESPACE_CHAR(ch
)) {
241 while (IS_WHITESPACE_CHAR(*++str
)) ;
247 copy_cooked( &pzDest
, &str
);
253 if (IS_WHITESPACE_CHAR(*str
))
254 goto found_white_space
;
258 copy_raw( &pzDest
, &str
);
264 if (IS_WHITESPACE_CHAR(*str
))
265 goto found_white_space
;
278 * NUL terminate the last token and see if we have any more tokens.
281 } while (*str
!= NUL
);
283 res
->tkn_list
[ res
->tkn_ct
] = NULL
;
294 main( int argc
, char** argv
)
297 printf("USAGE: %s arg [ ... ]\n", *argv
);
301 char* arg
= *(++argv
);
302 token_list_t
* p
= ao_string_tokenize( arg
);
304 printf( "Parsing string ``%s'' failed:\n\terrno %d (%s)\n",
305 arg
, errno
, strerror( errno
));
308 printf( "Parsed string ``%s''\ninto %d tokens:\n", arg
, p
->tkn_ct
);
310 printf( " %3d: ``%s''\n", ix
+1, p
->tkn_list
[ix
] );
311 } while (++ix
< p
->tkn_ct
);
322 * c-file-style: "stroustrup"
323 * indent-tabs-mode: nil
325 * end of autoopts/tokenize.c */