00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <string.h>
00033 #include <stdlib.h>
00034 #include <stdarg.h>
00035
00036 #include <lal/XLALError.h>
00037 #include <lal/StringVector.h>
00038 #include <lal/LALMalloc.h>
00039
00040
00041
00042 static CHAR *deblank_string ( const CHAR *start, UINT4 len );
00043
00044
00045 const LALStringVector empty_LALStringVector;
00046
00047
00048 NRCSID( STRINGVECTORC, "$Id: StringVector.c,v 1.1 2008/02/22 16:11:07 reinhard Exp $");
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 LALStringVector *
00062 XLALAppendString2Vector (LALStringVector *vect,
00063 const CHAR *string
00064 )
00065 {
00066 const CHAR *fn = "XLALAppendString2Vector()";
00067 UINT4 oldlen;
00068 LALStringVector *ret;
00069
00070 if ( !string ) {
00071 XLALPrintError ("\n%s: NULL string passed to append\n\n", fn );
00072 XLAL_ERROR_NULL ( fn, XLAL_EINVAL );
00073 }
00074
00075 if ( ! vect )
00076 ret = XLALCreateStringVector ( string, NULL );
00077 else
00078 {
00079 ret = vect;
00080 oldlen = ret->length;
00081
00082 if ( (ret->data = LALRealloc ( ret->data, (oldlen + 1)*sizeof( *ret->data ) )) == NULL ) {
00083 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00084 }
00085
00086 ret->length ++;
00087
00088 if ( (ret->data[oldlen] = LALCalloc(1, strlen(string) + 1 )) == NULL ) {
00089 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00090 }
00091
00092 strcpy ( ret->data[oldlen], string );
00093 }
00094
00095 return ret;
00096
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 LALStringVector *
00108 XLALCreateStringVector ( const CHAR *str1, ... )
00109 {
00110 const CHAR *fn = "XLALCreateStringVector()";
00111 LALStringVector *ret;
00112 const CHAR *next;
00113 va_list ap;
00114
00115 if ( !str1 ) {
00116 XLAL_ERROR_NULL (fn, XLAL_EINVAL );
00117 }
00118
00119
00120 if ( (ret = LALCalloc ( 1, sizeof(*ret) )) == NULL ) {
00121 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00122 }
00123 if ( (ret->data = LALCalloc ( 1, sizeof(ret->data[0]) )) == NULL) {
00124 LALFree ( ret );
00125 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00126 }
00127 if ( (ret->data[0] = LALCalloc ( strlen(str1)+1, sizeof(CHAR) )) == NULL ) {
00128 LALFree ( ret->data );
00129 LALFree ( ret );
00130 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00131 }
00132 strcpy ( ret->data[0], str1 );
00133 ret->length ++;
00134
00135
00136 va_start(ap, str1);
00137
00138 while ( (next = va_arg(ap, const CHAR *)) != NULL )
00139 {
00140 ret->length ++;
00141 if ( (ret->data = LALRealloc ( ret->data, ret->length * sizeof(ret->data[0]))) == NULL )
00142 goto failed;
00143 if ( (ret->data[ret->length-1] = LALCalloc( strlen(next)+1, sizeof(CHAR) )) == NULL )
00144 goto failed;
00145
00146 strcpy ( ret->data[ret->length-1], next );
00147
00148 }
00149
00150 va_end(ap);
00151
00152 return ret;
00153
00154 failed:
00155 va_end(ap);
00156 XLALDestroyStringVector ( ret );
00157 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00158
00159 }
00160
00161
00162
00163 void
00164 XLALDestroyStringVector ( LALStringVector *vect )
00165 {
00166 UINT4 i;
00167
00168 if ( !vect )
00169 return;
00170
00171 if ( vect->data )
00172 {
00173 for ( i=0; i < vect->length; i++ )
00174 {
00175 if ( vect->data[i] )
00176 LALFree ( vect->data[i] );
00177 }
00178
00179 LALFree ( vect->data );
00180 }
00181
00182 LALFree ( vect );
00183
00184 return;
00185
00186 }
00187
00188
00189
00190
00191 static int StringCompare (const void *p1, const void *p2)
00192 {
00193 const char *s1 = p1;
00194 const char *s2 = p2;
00195 return (strcmp ( s1, s2 ) );
00196 }
00197
00198
00199
00200 int
00201 XLALSortStringVector (LALStringVector *strings)
00202 {
00203 const CHAR *fn = "XLALSortStringVector()";
00204 if ( !strings || strings->length == 0 ) {
00205 XLAL_ERROR ( fn, XLAL_EINVAL );
00206 }
00207
00208 qsort ( (void*)(strings->data), (size_t)(strings->length), sizeof(CHAR*), StringCompare );
00209
00210 return XLAL_SUCCESS;
00211
00212 }
00213
00214
00215
00216
00217
00218
00219 LALStringVector *
00220 XLALParseCSV2StringVector ( const CHAR *CSVlist )
00221 {
00222 const CHAR *fn = "XLALParseCSV2StringVector";
00223 UINT4 counter;
00224 const CHAR *start, *tmp;
00225 CHAR **data = NULL;
00226 LALStringVector *ret = NULL;
00227
00228 if ( !CSVlist )
00229 return NULL;
00230
00231
00232 if ( ( ret = LALCalloc ( 1, sizeof( *ret )) ) == NULL )
00233 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00234
00235 start = CSVlist;
00236 counter = 0;
00237 do
00238 {
00239 UINT4 len;
00240
00241
00242 if ( ( data = LALRealloc ( data, (counter+1) * sizeof(CHAR*) )) == NULL )
00243 goto failed;
00244
00245
00246 if ( ( tmp = strchr ( start, ',' ) ) )
00247 len = tmp - start;
00248 else
00249 len = strlen ( start );
00250
00251
00252 if ( (data[counter] = deblank_string ( start, len ) ) == NULL )
00253 goto failed;
00254
00255 counter ++;
00256
00257 } while ( tmp && (start = tmp + 1) );
00258
00259 ret -> length = counter;
00260 ret -> data = data;
00261
00262
00263 return ( ret );
00264
00265 failed:
00266 XLALDestroyStringVector ( ret );
00267 XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
00268
00269 }
00270
00271
00272
00273
00274
00275 CHAR *
00276 deblank_string ( const CHAR *start, UINT4 len )
00277 {
00278 const CHAR *blank_chars = " \t\n";
00279 const CHAR *pos0, *pos1;
00280 UINT4 newlen;
00281 CHAR *ret;
00282
00283 if ( !start || !len )
00284 return NULL;
00285
00286
00287 pos0 = start;
00288 pos1 = start + len - 1;
00289 while ( (pos0 < pos1) && strchr ( blank_chars, *pos0 ) )
00290 pos0 ++;
00291
00292
00293 while ( (pos1 >= pos0) && strchr ( blank_chars, *pos1 ) )
00294 pos1 --;
00295
00296 newlen = pos1 - pos0 + 1;
00297 if ( !newlen )
00298 return NULL;
00299
00300 if ( (ret = LALCalloc(1, newlen + 1)) == NULL )
00301 return NULL;
00302
00303 strncpy ( ret, pos0, newlen );
00304 ret[ newlen ] = 0;
00305
00306 return ret;
00307
00308 }
00309