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