]> git.sesse.net Git - mlt/blob - src/framework/mlt_tokeniser.c
A little debugging.
[mlt] / src / framework / mlt_tokeniser.c
1 /**
2  * \file mlt_tokeniser.c
3  * \brief string tokeniser
4  * \see mlt_tokeniser_s
5  *
6  * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /* System header files */
25 #include <stdlib.h>
26 #include <string.h>
27
28 /* Application header files */
29 #include "mlt_tokeniser.h"
30
31 /** Initialise a tokeniser.
32 */
33
34 mlt_tokeniser mlt_tokeniser_init( )
35 {
36         return calloc( 1, sizeof( mlt_tokeniser_t ) );
37 }
38
39 /** Clear the tokeniser.
40 */
41
42 static void mlt_tokeniser_clear( mlt_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 mlt_tokeniser_append( mlt_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 mlt_tokeniser_parse_new( mlt_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         mlt_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                         mlt_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 ( strchr( token, '\"' ) == NULL || token[ strlen( token ) - 1 ] == '\"' )
109                         {
110                                 mlt_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 += delimiter_size;
123                 }
124         }
125
126         /* Special case - malformed string condition */
127         if ( !strcmp( token, "" ) )
128         {
129                 count = 0 - ( count - 1 );
130                 mlt_tokeniser_append( tokeniser, token );
131         }
132
133         free( token );
134         return count;
135 }
136
137 /** Get the original input.
138 */
139
140 char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser )
141 {
142         return tokeniser->input;
143 }
144
145 /** Get the number of tokens.
146 */
147
148 int mlt_tokeniser_count( mlt_tokeniser tokeniser )
149 {
150         return tokeniser->count;
151 }
152
153 /** Get a token as a string.
154 */
155
156 char *mlt_tokeniser_get_string( mlt_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 mlt_tokeniser_close( mlt_tokeniser tokeniser )
168 {
169         mlt_tokeniser_clear( tokeniser );
170         free( tokeniser->tokens );
171         free( tokeniser );
172 }