added mlt_tokeniser
authorlilo_booter <lilo_booter@d19143bc-622f-0410-bfdd-b5b2a6649095>
Sat, 17 Apr 2004 10:26:27 +0000 (10:26 +0000)
committerlilo_booter <lilo_booter@d19143bc-622f-0410-bfdd-b5b2a6649095>
Sat, 17 Apr 2004 10:26:27 +0000 (10:26 +0000)
git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@285 d19143bc-622f-0410-bfdd-b5b2a6649095

src/framework/Makefile
src/framework/mlt_tokeniser.c [new file with mode: 0644]
src/framework/mlt_tokeniser.h [new file with mode: 0644]

index 501fdb9..bd364da 100644 (file)
@@ -17,7 +17,8 @@ OBJS = mlt_frame.o \
           mlt_tractor.o \
           mlt_factory.o \
           mlt_repository.o \
-          mlt_pool.o
+          mlt_pool.o \
+          mlt_tokeniser.o
 
 SRCS := $(OBJS:.o=.c)
 
@@ -41,7 +42,7 @@ clean:
 
 install:
        install -m 755 $(TARGET) $(prefix)/lib/libmlt.so
-       mkdir -p "$(prefix)/include/mlt/framework"
+       install -d "$(prefix)/include/mlt/framework"
        install -m 644 mlt_consumer.h \
                        mlt_factory.h \
                        mlt_filter.h \
@@ -60,6 +61,7 @@ install:
                        mlt_property.h \
                        mlt_service.h  \
                        mlt_transition.h \
+                       mlt_tokeniser.h \
             "$(prefix)/include/mlt/framework"
 
 ifneq ($(wildcard .depend),)
diff --git a/src/framework/mlt_tokeniser.c b/src/framework/mlt_tokeniser.c
new file mode 100644 (file)
index 0000000..053b36f
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * mlt_tokeniser.c -- String tokeniser
+ * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
+ * Author: Charles Yates <charles.yates@pandora.be>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/* System header files */
+#include <stdlib.h>
+#include <string.h>
+
+/* Application header files */
+#include "mlt_tokeniser.h"
+
+/** Initialise a tokeniser.
+*/
+
+mlt_tokeniser mlt_tokeniser_init( )
+{
+       return calloc( 1, sizeof( mlt_tokeniser_t ) );
+}
+
+/** Clear the tokeniser.
+*/
+
+static void mlt_tokeniser_clear( mlt_tokeniser tokeniser )
+{
+       int index = 0;
+       for ( index = 0; index < tokeniser->count; index ++ )
+               free( tokeniser->tokens[ index ] );
+       tokeniser->count = 0;
+       free( tokeniser->input );
+       tokeniser->input = NULL;
+}
+
+/** Append a string to the tokeniser.
+*/
+
+static int mlt_tokeniser_append( mlt_tokeniser tokeniser, char *token )
+{
+       int error = 0;
+
+       if ( tokeniser->count == tokeniser->size )
+       {
+               tokeniser->size += 20;
+               tokeniser->tokens = realloc( tokeniser->tokens, tokeniser->size * sizeof( char * ) );
+       }
+
+       if ( tokeniser->tokens != NULL )
+       {
+               tokeniser->tokens[ tokeniser->count ++ ] = strdup( token );
+       }
+       else
+       {
+               tokeniser->count = 0;
+               error = -1;
+       }
+       return error;
+}
+
+/** Parse a string by splitting on the delimiter provided.
+*/
+
+int mlt_tokeniser_parse_new( mlt_tokeniser tokeniser, char *string, char *delimiter )
+{
+       int count = 0;
+       int length = strlen( string );
+       int delimiter_size = strlen( delimiter );
+       int index = 0;
+       char *token = strdup( string );
+
+       mlt_tokeniser_clear( tokeniser );
+       tokeniser->input = strdup( string );
+       strcpy( token, "" );
+
+       for ( index = 0; index < length; )
+       {
+               char *start = string + index;
+               char *end = strstr( start, delimiter );
+
+               if ( end == NULL )
+               {
+                       strcat( token, start );
+                       mlt_tokeniser_append( tokeniser, token );
+                       index = length;
+                       count ++;
+               }
+               else if ( start != end )
+               {
+                       strncat( token, start, end - start );
+                       index += end - start;
+                       if ( token[ 0 ] != '\"' || ( token[ 0 ] == '\"' && token[ strlen( token ) - 1 ] == '\"' ) )
+                       {
+                               mlt_tokeniser_append( tokeniser, token );
+                               strcpy( token, "" );
+                               count ++;
+                       }
+                       else while ( strncmp( string + index, delimiter, delimiter_size ) == 0 )
+                       {
+                               strncat( token, delimiter, delimiter_size );
+                               index += delimiter_size;
+                       }
+               }
+               else
+               {
+                       index += strlen( delimiter );
+               }
+       }
+
+       /* Special case - malformed string condition */
+       if ( !strcmp( token, "" ) )
+       {
+               count = 0 - ( count - 1 );
+               mlt_tokeniser_append( tokeniser, token );
+       }
+               
+       free( token );
+       return count;
+}
+
+/** Get the original input.
+*/
+
+char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser )
+{
+       return tokeniser->input;
+}
+
+/** Get the number of tokens.
+*/
+
+int mlt_tokeniser_count( mlt_tokeniser tokeniser )
+{
+       return tokeniser->count;
+}
+
+/** Get a token as a string.
+*/
+
+char *mlt_tokeniser_get_string( mlt_tokeniser tokeniser, int index )
+{
+       if ( index < tokeniser->count )
+               return tokeniser->tokens[ index ];
+       else
+               return NULL;
+}
+
+/** Close the tokeniser.
+*/
+
+void mlt_tokeniser_close( mlt_tokeniser tokeniser )
+{
+       mlt_tokeniser_clear( tokeniser );
+       free( tokeniser->tokens );
+       free( tokeniser );
+}
diff --git a/src/framework/mlt_tokeniser.h b/src/framework/mlt_tokeniser.h
new file mode 100644 (file)
index 0000000..118b853
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * mlt_tokeniser.h -- String tokeniser
+ * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
+ * Author: Charles Yates <charles.yates@pandora.be>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _VALERIE_TOKENISER_H_
+#define _VALERIE_TOKENISER_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/** Structure for tokeniser.
+*/
+
+typedef struct
+{
+       char *input;
+       char **tokens;
+       int count;
+       int size;
+}
+*mlt_tokeniser, mlt_tokeniser_t;
+
+/** Remote parser API.
+*/
+
+extern mlt_tokeniser mlt_tokeniser_init( );
+extern int mlt_tokeniser_parse_new( mlt_tokeniser, char *, char * );
+extern char *mlt_tokeniser_get_input( mlt_tokeniser );
+extern int mlt_tokeniser_count( mlt_tokeniser );
+extern char *mlt_tokeniser_get_string( mlt_tokeniser, int );
+extern void mlt_tokeniser_close( mlt_tokeniser );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif