a5dd91b977be5638cdfbb4daf8dfb98537998630
[melted] / src / valerie / valerie_tokeniser.c
1 /*
2 * valerie_tokeniser.c -- String tokeniser
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* System header files */
22 #include <stdlib.h>
23 #include <string.h>
24
25 /* Application header files */
26 #include "valerie_tokeniser.h"
27
28 /** Initialise a tokeniser.
29 */
30
31 valerie_tokeniser valerie_tokeniser_init( )
32 {
33 valerie_tokeniser tokeniser = malloc( sizeof( valerie_tokeniser_t ) );
34 if ( tokeniser != NULL )
35 memset( tokeniser, 0, sizeof( valerie_tokeniser_t ) );
36 return tokeniser;
37 }
38
39 /** Clear the tokeniser.
40 */
41
42 static void valerie_tokeniser_clear( valerie_tokeniser tokeniser )
43 {
44 int index = 0;
45 for ( index = 0; index < tokeniser->count; index ++ )
46 free( tokeniser->tokens[ index ] );
47 tokeniser->count = 0;
48 free( tokeniser->input );
49 tokeniser->input = NULL;
50 }
51
52 /** Append a string to the tokeniser.
53 */
54
55 static int valerie_tokeniser_append( valerie_tokeniser tokeniser, char *token )
56 {
57 int error = 0;
58
59 if ( tokeniser->count == tokeniser->size )
60 {
61 tokeniser->size += 20;
62 tokeniser->tokens = realloc( tokeniser->tokens, tokeniser->size * sizeof( char * ) );
63 }
64
65 if ( tokeniser->tokens != NULL )
66 {
67 tokeniser->tokens[ tokeniser->count ++ ] = strdup( token );
68 }
69 else
70 {
71 tokeniser->count = 0;
72 error = -1;
73 }
74 return error;
75 }
76
77 /** Parse a string by splitting on the delimiter provided.
78 */
79
80 int valerie_tokeniser_parse_new( valerie_tokeniser tokeniser, char *string, const char *delimiter )
81 {
82 int count = 0;
83 int length = strlen( string );
84 int delimiter_size = strlen( delimiter );
85 int index = 0;
86 char *token = strdup( string );
87
88 valerie_tokeniser_clear( tokeniser );
89 tokeniser->input = strdup( string );
90 strcpy( token, "" );
91
92 for ( index = 0; index < length; )
93 {
94 char *start = string + index;
95 char *end = strstr( start, delimiter );
96
97 if ( end == NULL )
98 {
99 strcat( token, start );
100 valerie_tokeniser_append( tokeniser, token );
101 index = length;
102 count ++;
103 }
104 else if ( start != end )
105 {
106 strncat( token, start, end - start );
107 index += end - start;
108 if ( token[ 0 ] != '\"' || ( token[ 0 ] == '\"' && token[ strlen( token ) - 1 ] == '\"' ) )
109 {
110 valerie_tokeniser_append( tokeniser, token );
111 strcpy( token, "" );
112 count ++;
113 }
114 else while ( strncmp( string + index, delimiter, delimiter_size ) == 0 )
115 {
116 strncat( token, delimiter, delimiter_size );
117 index += delimiter_size;
118 }
119 }
120 else
121 {
122 index += strlen( delimiter );
123 }
124 }
125
126 /* Special case - malformed string condition */
127 if ( !strcmp( token, "" ) )
128 {
129 count = 0 - ( count - 1 );
130 valerie_tokeniser_append( tokeniser, token );
131 }
132
133 free( token );
134 return count;
135 }
136
137 /** Get the original input.
138 */
139
140 char *valerie_tokeniser_get_input( valerie_tokeniser tokeniser )
141 {
142 return tokeniser->input;
143 }
144
145 /** Get the number of tokens.
146 */
147
148 int valerie_tokeniser_count( valerie_tokeniser tokeniser )
149 {
150 return tokeniser->count;
151 }
152
153 /** Get a token as a string.
154 */
155
156 char *valerie_tokeniser_get_string( valerie_tokeniser tokeniser, int index )
157 {
158 if ( index < tokeniser->count )
159 return tokeniser->tokens[ index ];
160 else
161 return NULL;
162 }
163
164 /** Close the tokeniser.
165 */
166
167 void valerie_tokeniser_close( valerie_tokeniser tokeniser )
168 {
169 valerie_tokeniser_clear( tokeniser );
170 free( tokeniser->tokens );
171 free( tokeniser );
172 }