2 * valerie_response.c -- Response
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
21 /* System header files */
27 /* Application header files */
28 #include "valerie_response.h"
30 /** Construct a new dv response.
33 valerie_response valerie_response_init( )
35 valerie_response response = malloc( sizeof( valerie_response_t ) );
36 if ( response != NULL )
37 memset( response, 0, sizeof( valerie_response_t ) );
41 /** Clone a dv response
44 valerie_response valerie_response_clone( valerie_response response )
46 valerie_response clone = valerie_response_init( );
47 if ( clone != NULL && response != NULL )
50 for ( index = 0; index < valerie_response_count( response ); index ++ )
52 char *line = valerie_response_get_line( response, index );
53 valerie_response_printf( clone, strlen( line ) + 2, "%s\n", line );
59 /** Get the error code associated to the response.
62 int valerie_response_get_error_code( valerie_response response )
65 if ( response != NULL )
67 if ( response->count > 0 )
69 if ( sscanf( response->array[ 0 ], "%d", &error_code ) != 1 )
80 /** Get the error description associated to the response.
83 char *valerie_response_get_error_string( valerie_response response )
85 char *error_string = "No message specified";
86 if ( response->count > 0 )
88 char *ptr = strchr( response->array[ 0 ], ' ' ) ;
90 error_string = ptr + 1;
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.
100 char *valerie_response_get_line( valerie_response response, int index )
102 if ( index < response->count )
103 return response->array[ index ];
108 /** Return the number of lines of text in the response.
111 int valerie_response_count( valerie_response response )
113 if ( response != NULL )
114 return response->count;
119 /** Set the error and description associated to the response.
122 void valerie_response_set_error( valerie_response response, int error_code, char *error_string )
124 if ( response->count == 0 )
126 valerie_response_printf( response, 10240, "%d %s\n", error_code, error_string );
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 );
137 /** Write formatted text to the response.
140 int valerie_response_printf( valerie_response response, size_t size, char *format, ... )
143 char *text = malloc( size );
147 va_start( list, format );
148 length = vsnprintf( text, size, format, list );
150 valerie_response_write( response, text, length );
157 /** Write text to the reponse.
160 int valerie_response_write( valerie_response response, char *text, int size )
167 int index = response->count - 1;
168 char *lf = strchr( ptr, '\n' );
169 int length_of_string = 0;
171 /* Make sure we have space in the dynamic array. */
172 if ( !response->append && response->count >= response->size - 1 )
174 response->size += 50;
175 response->array = realloc( response->array, response->size * sizeof( char * ) );
178 /* Make sure the array is valid, or we're really in trouble */
179 if ( response->array == NULL )
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. */
189 if ( !response->append )
191 response->array[ ++ index ] = NULL;
196 length_of_string = strlen( response->array[ index ] );
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 */
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';
211 response->append = 1;
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;
231 /** Close the response.
234 void valerie_response_close( valerie_response response )
236 if ( response != NULL )
239 for ( index = 0; index < response->count; index ++ )
240 free( response->array[ index ] );
241 free( response->array );