2 * valerie_tokeniser.c -- String tokeniser
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
21 /* System header files */
25 /* Application header files */
26 #include "valerie_tokeniser.h"
28 /** Initialise a tokeniser.
31 valerie_tokeniser
valerie_tokeniser_init( )
33 valerie_tokeniser tokeniser
= malloc( sizeof( valerie_tokeniser_t
) );
34 if ( tokeniser
!= NULL
)
35 memset( tokeniser
, 0, sizeof( valerie_tokeniser_t
) );
39 /** Clear the tokeniser.
42 static void valerie_tokeniser_clear( valerie_tokeniser tokeniser
)
45 for ( index
= 0; index
< tokeniser
->count
; index
++ )
46 free( tokeniser
->tokens
[ index
] );
48 free( tokeniser
->input
);
49 tokeniser
->input
= NULL
;
52 /** Append a string to the tokeniser.
55 static int valerie_tokeniser_append( valerie_tokeniser tokeniser
, char *token
)
59 if ( tokeniser
->count
== tokeniser
->size
)
61 tokeniser
->size
+= 20;
62 tokeniser
->tokens
= realloc( tokeniser
->tokens
, tokeniser
->size
* sizeof( char * ) );
65 if ( tokeniser
->tokens
!= NULL
)
67 tokeniser
->tokens
[ tokeniser
->count
++ ] = strdup( token
);
77 /** Parse a string by splitting on the delimiter provided.
80 int valerie_tokeniser_parse_new( valerie_tokeniser tokeniser
, char *string
, char *delimiter
)
83 int length
= strlen( string
);
84 int delimiter_size
= strlen( delimiter
);
86 char *token
= strdup( string
);
88 valerie_tokeniser_clear( tokeniser
);
89 tokeniser
->input
= strdup( string
);
92 for ( index
= 0; index
< length
; )
94 char *start
= string
+ index
;
95 char *end
= strstr( start
, delimiter
);
99 strcat( token
, start
);
100 valerie_tokeniser_append( tokeniser
, token
);
104 else if ( start
!= end
)
106 strncat( token
, start
, end
- start
);
107 index
+= end
- start
;
108 if ( token
[ 0 ] != '\"' || ( token
[ 0 ] == '\"' && token
[ strlen( token
) - 1 ] == '\"' ) )
110 valerie_tokeniser_append( tokeniser
, token
);
114 else while ( strncmp( string
+ index
, delimiter
, delimiter_size
) == 0 )
116 strncat( token
, delimiter
, delimiter_size
);
117 index
+= delimiter_size
;
122 index
+= strlen( delimiter
);
126 /* Special case - malformed string condition */
127 if ( !strcmp( token
, "" ) )
129 count
= 0 - ( count
- 1 );
130 valerie_tokeniser_append( tokeniser
, token
);
137 /** Get the original input.
140 char *valerie_tokeniser_get_input( valerie_tokeniser tokeniser
)
142 return tokeniser
->input
;
145 /** Get the number of tokens.
148 int valerie_tokeniser_count( valerie_tokeniser tokeniser
)
150 return tokeniser
->count
;
153 /** Get a token as a string.
156 char *valerie_tokeniser_get_string( valerie_tokeniser tokeniser
, int index
)
158 if ( index
< tokeniser
->count
)
159 return tokeniser
->tokens
[ index
];
164 /** Close the tokeniser.
167 void valerie_tokeniser_close( valerie_tokeniser tokeniser
)
169 valerie_tokeniser_clear( tokeniser
);
170 free( tokeniser
->tokens
);