]> git.sesse.net Git - mlt/blob - src/framework/mlt_tokeniser.c
added mlt_tokeniser
[mlt] / src / framework / mlt_tokeniser.c
1 /*
2  * mlt_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 "mlt_tokeniser.h"
27
28 /** Initialise a tokeniser.
29 */
30
31 mlt_tokeniser mlt_tokeniser_init( )
32 {
33         return calloc( 1, sizeof( mlt_tokeniser_t ) );
34 }
35
36 /** Clear the tokeniser.
37 */
38
39 static void mlt_tokeniser_clear( mlt_tokeniser tokeniser )
40 {
41         int index = 0;
42         for ( index = 0; index < tokeniser->count; index ++ )
43                 free( tokeniser->tokens[ index ] );
44         tokeniser->count = 0;
45         free( tokeniser->input );
46         tokeniser->input = NULL;
47 }
48
49 /** Append a string to the tokeniser.
50 */
51
52 static int mlt_tokeniser_append( mlt_tokeniser tokeniser, char *token )
53 {
54         int error = 0;
55
56         if ( tokeniser->count == tokeniser->size )
57         {
58                 tokeniser->size += 20;
59                 tokeniser->tokens = realloc( tokeniser->tokens, tokeniser->size * sizeof( char * ) );
60         }
61
62         if ( tokeniser->tokens != NULL )
63         {
64                 tokeniser->tokens[ tokeniser->count ++ ] = strdup( token );
65         }
66         else
67         {
68                 tokeniser->count = 0;
69                 error = -1;
70         }
71         return error;
72 }
73
74 /** Parse a string by splitting on the delimiter provided.
75 */
76
77 int mlt_tokeniser_parse_new( mlt_tokeniser tokeniser, char *string, char *delimiter )
78 {
79         int count = 0;
80         int length = strlen( string );
81         int delimiter_size = strlen( delimiter );
82         int index = 0;
83         char *token = strdup( string );
84
85         mlt_tokeniser_clear( tokeniser );
86         tokeniser->input = strdup( string );
87         strcpy( token, "" );
88
89         for ( index = 0; index < length; )
90         {
91                 char *start = string + index;
92                 char *end = strstr( start, delimiter );
93
94                 if ( end == NULL )
95                 {
96                         strcat( token, start );
97                         mlt_tokeniser_append( tokeniser, token );
98                         index = length;
99                         count ++;
100                 }
101                 else if ( start != end )
102                 {
103                         strncat( token, start, end - start );
104                         index += end - start;
105                         if ( token[ 0 ] != '\"' || ( token[ 0 ] == '\"' && token[ strlen( token ) - 1 ] == '\"' ) )
106                         {
107                                 mlt_tokeniser_append( tokeniser, token );
108                                 strcpy( token, "" );
109                                 count ++;
110                         }
111                         else while ( strncmp( string + index, delimiter, delimiter_size ) == 0 )
112                         {
113                                 strncat( token, delimiter, delimiter_size );
114                                 index += delimiter_size;
115                         }
116                 }
117                 else
118                 {
119                         index += strlen( delimiter );
120                 }
121         }
122
123         /* Special case - malformed string condition */
124         if ( !strcmp( token, "" ) )
125         {
126                 count = 0 - ( count - 1 );
127                 mlt_tokeniser_append( tokeniser, token );
128         }
129                 
130         free( token );
131         return count;
132 }
133
134 /** Get the original input.
135 */
136
137 char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser )
138 {
139         return tokeniser->input;
140 }
141
142 /** Get the number of tokens.
143 */
144
145 int mlt_tokeniser_count( mlt_tokeniser tokeniser )
146 {
147         return tokeniser->count;
148 }
149
150 /** Get a token as a string.
151 */
152
153 char *mlt_tokeniser_get_string( mlt_tokeniser tokeniser, int index )
154 {
155         if ( index < tokeniser->count )
156                 return tokeniser->tokens[ index ];
157         else
158                 return NULL;
159 }
160
161 /** Close the tokeniser.
162 */
163
164 void mlt_tokeniser_close( mlt_tokeniser tokeniser )
165 {
166         mlt_tokeniser_clear( tokeniser );
167         free( tokeniser->tokens );
168         free( tokeniser );
169 }