]> git.sesse.net Git - mlt/blob - mlt/src/framework/mlt_repository.c
Factory implementation
[mlt] / mlt / src / framework / mlt_repository.c
1 /*
2  * repository.c -- provides a map between service and shared objects
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "mlt_repository.h"
22 #include "mlt_properties.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dlfcn.h>
27 #include <string.h>
28
29 struct mlt_repository_s
30 {
31         struct mlt_properties_s parent;
32         mlt_properties object_list;
33 };
34
35 static char *construct_full_file( char *output, char *prefix, char *file )
36 {
37         strcpy( output, prefix );
38         if ( prefix[ strlen( prefix ) - 1 ] != '/' )
39                 strcat( output, "/" );
40         strcat( output, file );
41         return output;
42 }
43
44 static char *chomp( char *input )
45 {
46         if ( input[ strlen( input ) - 1 ] == '\n' )
47                 input[ strlen( input ) - 1 ] = '\0';
48         return input;
49 }
50
51 static mlt_properties construct_object( char *prefix, char *id )
52 {
53         mlt_properties output = calloc( sizeof( struct mlt_properties_s ), 1 );
54         mlt_properties_init( output, NULL );
55         mlt_properties_set( output, "prefix", prefix );
56         mlt_properties_set( output, "id", id );
57         return output;
58 }
59
60 static mlt_properties construct_service( mlt_properties object, char *id )
61 {
62         mlt_properties output = calloc( sizeof( struct mlt_properties_s ), 1 );
63         mlt_properties_init( output, NULL );
64         mlt_properties_set_data( output, "object", object, 0, NULL, NULL );
65         mlt_properties_set( output, "id", id );
66         return output;
67 }
68
69 void *construct_instance( mlt_properties service_properties, char *symbol, void *input )
70 {
71         // Extract the service
72         char *service = mlt_properties_get( service_properties, "id" );
73
74         // Get the object properties
75         void *object_properties = mlt_properties_get_data( service_properties, "object", NULL );
76
77         // Get the dlopen'd object
78         void *object = mlt_properties_get_data( object_properties, "dlopen", NULL );
79
80         // Get the dlsym'd symbol
81         void *( *symbol_ptr )( char *, void * ) = mlt_properties_get_data( object_properties, symbol, NULL );
82
83         // Check that we have object and open if we don't
84         if ( object == NULL )
85         {
86                 char full_file[ 512 ];
87
88                 // Get the prefix and id of the shared object
89                 char *prefix = mlt_properties_get( object_properties, "prefix" );
90                 char *file = mlt_properties_get( object_properties, "id" );
91
92                 // Construct the full file
93                 construct_full_file( full_file, prefix, file );
94
95                 // Open the shared object
96                 object = dlopen( full_file, RTLD_NOW | RTLD_GLOBAL );
97
98                 // Set it on the properties
99                 mlt_properties_set_data( object_properties, "dlopen", object, 0, ( void (*)( void * ) )dlclose, NULL );
100         }
101
102         // Now check if we have this symbol pointer
103         if ( object != NULL && symbol_ptr == NULL )
104         {
105                 // Construct it now
106                 symbol_ptr = dlsym( object, symbol );
107
108                 // Set it on the properties
109                 mlt_properties_set_data( object_properties, "dlsym", symbol_ptr, 0, NULL, NULL );
110         }
111
112         // Construct the service
113         return symbol_ptr != NULL ? symbol_ptr( service, input ) : NULL;
114 }
115
116 void destroy_properties( void *arg )
117 {
118         mlt_properties_close( arg );
119         free( arg );
120 }
121
122 mlt_repository mlt_repository_init( mlt_properties object_list, char *prefix, char *data, char *symbol )
123 {
124         char full_file[ 512 ];
125         FILE *file;
126
127         // Construct the repository
128         mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
129         mlt_properties_init( &this->parent, NULL );
130
131         // Add the symbol to THIS repository properties.
132         mlt_properties_set( &this->parent, "_symbol", symbol );
133
134         // Asociate the repository to the global object_list
135         this->object_list = object_list;
136
137         // Construct full file
138         construct_full_file( full_file, prefix, data );
139
140         // Open the file
141         file = fopen( full_file, "r" );
142
143         // Parse the contents
144         if ( file != NULL )
145         {
146                 char full[ 512 ];
147                 char service[ 256 ];
148                 char object[ 256 ];
149
150                 while( fgets( full, 512, file ) )
151                 {
152                         chomp( full );
153
154                         if ( full[ 0 ] != '#' && full[ 0 ] != '\0' && sscanf( full, "%s %s", service, object ) == 2 )
155                         {
156                                 // Get the object properties first
157                                 mlt_properties object_properties = mlt_properties_get_data( object_list, object, NULL );
158
159                                 // If their are no properties, create them now
160                                 if ( object_properties == NULL )
161                                 {
162                                         // Construct the object
163                                         object_properties = construct_object( prefix, object );
164
165                                         // Add it to the object list
166                                         mlt_properties_set_data( object_list, object, object_properties, 0, destroy_properties, NULL );
167                                 }
168
169                                 // Now construct a property for the service
170                                 mlt_properties service_properties = construct_service( object_properties, service );
171
172                                 // Add it to the repository
173                                 mlt_properties_set_data( &this->parent, service, service_properties, 0, destroy_properties, NULL );
174                         }
175                 }
176
177                 // Close the file
178                 fclose( file );
179         }
180
181         return this;
182 }
183
184 void *mlt_repository_fetch( mlt_repository this, char *service, void *input )
185 {
186         // Get the service properties
187         mlt_properties service_properties = mlt_properties_get_data( &this->parent, service, NULL );
188
189         // If the service exists
190         if ( service_properties != NULL )
191         {
192                 // Get the symbol that is used to generate this service
193                 char *symbol = mlt_properties_get( &this->parent, "_symbol" );
194
195                 // Now get an instance of the service
196                 return construct_instance( service_properties, symbol, input );
197         }
198
199         return NULL;
200 }
201
202 void mlt_repository_close( mlt_repository this )
203 {
204         mlt_properties_close( &this->parent );
205         free( this );
206 }
207
208