]> git.sesse.net Git - mlt/blob - src/valerie/valerie_response.c
Merge ../mlt++
[mlt] / src / valerie / valerie_response.c
1 /*
2  * valerie_response.c -- Response
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 <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26
27 /* Application header files */
28 #include "valerie_response.h"
29
30 /** Construct a new dv response.
31 */
32
33 valerie_response valerie_response_init( )
34 {
35         valerie_response response = malloc( sizeof( valerie_response_t ) );
36         if ( response != NULL )
37                 memset( response, 0, sizeof( valerie_response_t ) );
38         return response;
39 }
40
41 /** Clone a dv response
42 */
43
44 valerie_response valerie_response_clone( valerie_response response )
45 {
46         valerie_response clone = valerie_response_init( );
47         if ( clone != NULL && response != NULL )
48         {
49                 int index = 0;
50                 for ( index = 0; index < valerie_response_count( response ); index ++ )
51                 {
52                         char *line = valerie_response_get_line( response, index );
53                         valerie_response_printf( clone, strlen( line ) + 2, "%s\n", line );
54                 }
55         }
56         return clone;
57 }
58
59 /** Get the error code associated to the response.
60 */
61
62 int valerie_response_get_error_code( valerie_response response )
63 {
64         int error_code = -1;
65         if ( response != NULL )
66         {
67                 if ( response->count > 0 )
68                 {
69                         if ( sscanf( response->array[ 0 ], "%d", &error_code ) != 1 )
70                                 error_code = 0;
71                 }
72                 else
73                 {
74                         error_code = -2;
75                 }
76         }
77         return error_code;
78 }
79
80 /** Get the error description associated to the response.
81 */
82
83 const char *valerie_response_get_error_string( valerie_response response )
84 {
85         const char *error_string = "No message specified";
86         if ( response->count > 0 )
87         {
88                 char *ptr = strchr( response->array[ 0 ], ' ' ) ;
89                 if ( ptr != NULL )
90                         error_string = ptr + 1;
91         }
92         return error_string;
93 }
94
95 /** Get a line of text at the given index. Note that the text itself is
96         terminated only with a NUL char and it is the responsibility of the
97         the user of the returned data to use a LF or CR/LF as appropriate.
98 */
99
100 char *valerie_response_get_line( valerie_response response, int index )
101 {
102         if ( index < response->count )
103                 return response->array[ index ];
104         else
105                 return NULL;
106 }
107
108 /** Return the number of lines of text in the response.
109 */
110
111 int valerie_response_count( valerie_response response )
112 {
113         if ( response != NULL )
114                 return response->count;
115         else
116                 return 0;
117 }
118
119 /** Set the error and description associated to the response.
120 */
121
122 void valerie_response_set_error( valerie_response response, int error_code, const char *error_string )
123 {
124         if ( response->count == 0 )
125         {
126                 valerie_response_printf( response, 10240, "%d %s\n", error_code, error_string );
127         }
128         else
129         {
130                 char temp[ 10240 ];
131                 int length = sprintf( temp, "%d %s", error_code, error_string );
132                 response->array[ 0 ] = realloc( response->array[ 0 ], length + 1 );
133                 strcpy( response->array[ 0 ], temp );
134         }
135 }
136
137 /** Write formatted text to the response. 
138 */
139
140 int valerie_response_printf( valerie_response response, size_t size, const char *format, ... )
141 {
142         int length = 0;
143         char *text = malloc( size );
144         if ( text != NULL )
145         {
146                 va_list list;
147                 va_start( list, format );
148                 length = vsnprintf( text, size, format, list );
149                 if ( length != 0 )
150                         valerie_response_write( response, text, length );
151                 va_end( list );
152                 free( text );
153         }
154         return length;
155 }
156
157 /** Write text to the reponse.
158 */
159
160 int valerie_response_write( valerie_response response, const char *text, int size )
161 {
162         int ret = 0;
163         const char *ptr = text;
164
165         while ( size > 0 )
166         {
167                 int index = response->count - 1;
168                 const char *lf = strchr( ptr, '\n' );
169                 int length_of_string = 0;
170
171                 /* Make sure we have space in the dynamic array. */
172                 if ( !response->append && response->count >= response->size - 1 )
173                 {
174                         response->size += 50;
175                         response->array = realloc( response->array, response->size * sizeof( char * ) );
176                 }
177
178                 /* Make sure the array is valid, or we're really in trouble */
179                 if ( response->array == NULL )
180                 {
181                         ret = 0;
182                         break;
183                 }
184
185                 /* Now, if we're appending to the previous write (ie: if it wasn't
186                    terminated by a LF), then use the index calculated above, otherwise
187                    go to the next one and ensure it's NULLed. */
188
189                 if ( !response->append )
190                 {
191                         response->array[ ++ index ] = NULL;
192                         response->count ++;
193                 }
194                 else
195                 {
196                         length_of_string = strlen( response->array[ index ] );
197                 }
198
199                 /* Now we need to know how to handle the current ptr with respect to lf. */
200                 /* TODO: tidy up and error check... sigh... tested for many, many 1000s of lines */
201
202                 if ( lf == NULL )
203                 {
204                         response->array[ index ] = realloc( response->array[ index ], length_of_string + size + 1 );
205                         memcpy( response->array[ index ] + length_of_string, ptr, size );
206                         response->array[ index ][ length_of_string + size ] = '\0';
207                         if ( ( length_of_string + size ) > 0 && response->array[ index ][ length_of_string + size - 1 ] == '\r' )
208                                 response->array[ index ][ length_of_string + size - 1 ] = '\0';
209                         size = 0;
210                         ret += size;
211                         response->append = 1;
212                 }
213                 else
214                 {
215                         int chars = lf - ptr;
216                         response->array[ index ] = realloc( response->array[ index ], length_of_string + chars + 1 );
217                         memcpy( response->array[ index ] + length_of_string, ptr, chars );
218                         response->array[ index ][ length_of_string + chars ] = '\0';
219                         if ( ( length_of_string + chars ) > 0 && response->array[ index ][ length_of_string + chars - 1 ] == '\r' )
220                                 response->array[ index ][ length_of_string + chars - 1 ] = '\0';
221                         ptr = ptr + chars + 1;
222                         size -= ( chars + 1 );
223                         response->append = 0;
224                         ret += chars + 1;
225                 }
226         }
227
228         return ret;
229 }
230
231 /** Close the response.
232 */
233
234 void valerie_response_close( valerie_response response )
235 {
236         if ( response != NULL )
237         {
238                 int index = 0;
239                 for ( index = 0; index < response->count; index ++ )
240                         free( response->array[ index ] );
241                 free( response->array );
242                 free( response );
243         }
244 }