Commit b39f721f authored by Reinhard Prix's avatar Reinhard Prix
Browse files

some cleanup and 'modernization' in StringVector module

   - use C99 __func__ instead of explicit function names
   - use XLAL?alloc() functions
   - more explicit error-messages in 'elementary' XLAL-failures
Original: 5e32915b143bd2ffcc99fd0daae861a06f743a23
parent 3a212e9a
......@@ -25,8 +25,6 @@
* \brief Creation/destruction/manipulation API for 'StringVector' type objects,
* which are just LAL 'vectors' of CHAR * pointers.
*
* $Id$
*
*/
#ifndef _STRINGVECTOR_H /* Double-include protection. */
......@@ -44,7 +42,6 @@ extern "C" {
#include <lal/LALRCSID.h>
/*---------- DEFINES ----------*/
NRCSID( STRINGVECTORH, "$Id$" );
/*----- Error-codes -----*/
/*---------- exported types ----------*/
......
......@@ -24,8 +24,6 @@
* \ingroup factories
* \brief Creation/destruction/manipulation functions for 'LALStringVector' type objects.
*
* Revision: $Id$
*
*/
/*---------- INCLUDES ----------*/
......@@ -45,8 +43,6 @@ static CHAR *deblank_string ( const CHAR *start, UINT4 len );
const LALStringVector empty_LALStringVector;
/*---------- Global variables ----------*/
NRCSID( STRINGVECTORC, "$Id$");
/*---------- internal prototypes ----------*/
......@@ -63,29 +59,36 @@ XLALAppendString2Vector (LALStringVector *vect, /**< input string-vector to app
const CHAR *string /**< string to append */
)
{
const CHAR *fn = "XLALAppendString2Vector()";
const CHAR *fn = __func__;
UINT4 oldlen;
LALStringVector *ret;
if ( !string ) {
XLALPrintError ("\n%s: NULL string passed to append\n\n", fn );
XLALPrintError ("\n%s: NULL 'string' passed to append\n\n", fn );
XLAL_ERROR_NULL ( fn, XLAL_EINVAL );
}
if ( ! vect )
ret = XLALCreateStringVector ( string, NULL ); /* special case: NULL string-vector to append to */
{ /* special case: NULL string-vector to append to */
if ( (ret = XLALCreateStringVector ( string, NULL )) == NULL) {
XLALPrintError ("%s: XLALCreateStringVector() failed!\n", fn );
XLAL_ERROR_NULL ( fn, XLAL_EFUNC );
}
}
else
{
ret = vect;
oldlen = ret->length;
if ( (ret->data = LALRealloc ( ret->data, (oldlen + 1)*sizeof( *ret->data ) )) == NULL ) {
if ( (ret->data = XLALRealloc ( ret->data, (oldlen + 1)*sizeof( *ret->data ) )) == NULL ) {
XLALPrintError ("%s: XLALRealloc(%d) failed!\n", fn, oldlen + 1 );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
ret->length ++;
if ( (ret->data[oldlen] = LALCalloc(1, strlen(string) + 1 )) == NULL ) {
if ( (ret->data[oldlen] = XLALCalloc(1, strlen(string) + 1 )) == NULL ) {
XLALPrintError ("%s: XLALCalloc(%d) failed!\n", fn, strlen(string) + 1 );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
......@@ -107,28 +110,31 @@ XLALAppendString2Vector (LALStringVector *vect, /**< input string-vector to app
LALStringVector *
XLALCreateStringVector ( const CHAR *str1, ... )
{
const CHAR *fn = "XLALCreateStringVector()";
const CHAR *fn = __func__;
LALStringVector *ret;
const CHAR *next;
va_list ap;
if ( !str1 ) {
XLALPrintError ("%s: invalid NULL input string 'str1'\n", fn );
XLAL_ERROR_NULL (fn, XLAL_EINVAL );
}
size_t len;
/* set up return vector of strings, and handle first argument */
if ( (ret = LALCalloc ( 1, sizeof(*ret) )) == NULL ) {
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
if ( (ret->data = LALCalloc ( 1, sizeof(ret->data[0]) )) == NULL) {
LALFree ( ret );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
if ( (ret->data[0] = LALCalloc ( strlen(str1)+1, sizeof(CHAR) )) == NULL ) {
LALFree ( ret->data );
LALFree ( ret );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
len = sizeof(*ret);
if ( (ret = XLALCalloc ( 1, len )) == NULL )
goto failed;
len = sizeof(ret->data[0]);
if ( (ret->data = XLALCalloc ( 1, len )) == NULL)
goto failed;
len = strlen(str1)+1;
if ( (ret->data[0] = XLALCalloc ( len, sizeof(CHAR) )) == NULL )
goto failed;
strcpy ( ret->data[0], str1 );
ret->length ++;
......@@ -138,9 +144,13 @@ XLALCreateStringVector ( const CHAR *str1, ... )
while ( (next = va_arg(ap, const CHAR *)) != NULL )
{
ret->length ++;
if ( (ret->data = LALRealloc ( ret->data, ret->length * sizeof(ret->data[0]))) == NULL )
len = ret->length * sizeof(ret->data[0]);
if ( (ret->data = XLALRealloc ( ret->data, len)) == NULL )
goto failed;
if ( (ret->data[ret->length-1] = LALCalloc( strlen(next)+1, sizeof(CHAR) )) == NULL )
len = strlen(next)+1;
if ( (ret->data[ret->length-1] = XLALCalloc( len, sizeof(CHAR) )) == NULL )
goto failed;
strcpy ( ret->data[ret->length-1], next );
......@@ -153,6 +163,7 @@ XLALCreateStringVector ( const CHAR *str1, ... )
failed:
va_end(ap);
XLALPrintError ("%s: failed to allocate '%d' bytes\n", fn, len );
XLALDestroyStringVector ( ret );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
......@@ -173,13 +184,13 @@ XLALDestroyStringVector ( LALStringVector *vect )
for ( i=0; i < vect->length; i++ )
{
if ( vect->data[i] )
LALFree ( vect->data[i] );
XLALFree ( vect->data[i] );
}
LALFree ( vect->data );
XLALFree ( vect->data );
}
LALFree ( vect );
XLALFree ( vect );
return;
......@@ -200,8 +211,10 @@ static int StringCompare (const void *p1, const void *p2)
int
XLALSortStringVector (LALStringVector *strings)
{
const CHAR *fn = "XLALSortStringVector()";
const CHAR *fn = __func__;
if ( !strings || strings->length == 0 ) {
XLALPrintError ("%s: invalid empty or zero-length input 'strings'\n", fn );
XLAL_ERROR ( fn, XLAL_EINVAL );
}
......@@ -219,7 +232,7 @@ XLALSortStringVector (LALStringVector *strings)
LALStringVector *
XLALParseCSV2StringVector ( const CHAR *CSVlist )
{
const CHAR *fn = "XLALParseCSV2StringVector";
const CHAR *fn = __func__;
UINT4 counter;
const CHAR *start, *tmp;
CHAR **data = NULL;
......@@ -228,18 +241,20 @@ XLALParseCSV2StringVector ( const CHAR *CSVlist )
if ( !CSVlist )
return NULL;
size_t len;
/* prepare return string-vector */
if ( ( ret = LALCalloc ( 1, sizeof( *ret )) ) == NULL )
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
len = sizeof( *ret );
if ( ( ret = XLALCalloc ( 1, len ) ) == NULL )
goto failed;
start = CSVlist;
counter = 0;
do
{
UINT4 len;
/* extend string-array */
if ( ( data = LALRealloc ( data, (counter+1) * sizeof(CHAR*) )) == NULL )
len = (counter+1) * sizeof(CHAR*);
if ( ( data = XLALRealloc ( data, len )) == NULL )
goto failed;
/* determine string-length of next value */
......@@ -249,9 +264,10 @@ XLALParseCSV2StringVector ( const CHAR *CSVlist )
len = strlen ( start );
/* allocate space for that value in string-array */
if ( (data[counter] = deblank_string ( start, len ) ) == NULL )
goto failed;
if ( (data[counter] = deblank_string ( start, len ) ) == NULL ) {
XLALDestroyStringVector ( ret );
XLAL_ERROR_NULL ( fn, XLAL_EFUNC );
}
counter ++;
} while ( tmp && (start = tmp + 1) );
......@@ -263,6 +279,7 @@ XLALParseCSV2StringVector ( const CHAR *CSVlist )
return ( ret );
failed:
XLALPrintError ("%s: failed to allocate %d bytes\n", fn, len );
XLALDestroyStringVector ( ret );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
......@@ -275,13 +292,17 @@ XLALParseCSV2StringVector ( const CHAR *CSVlist )
CHAR *
deblank_string ( const CHAR *start, UINT4 len )
{
const char *fn = __func__;
const CHAR *blank_chars = " \t\n";
const CHAR *pos0, *pos1;
UINT4 newlen;
CHAR *ret;
if ( !start || !len )
return NULL;
if ( !start || !len ) {
XLALPrintError ("%s: invalid NULL input 'start' or len=0\n", fn );
XLAL_ERROR_NULL ( fn, XLAL_EINVAL );
}
/* clip from beginning */
pos0 = start;
......@@ -294,11 +315,15 @@ deblank_string ( const CHAR *start, UINT4 len )
pos1 --;
newlen = pos1 - pos0 + 1;
if ( !newlen )
return NULL;
if ( !newlen ) {
XLALPrintError ("%s: something went wrong here .. probably a coding mistake.\n", fn );
XLAL_ERROR_NULL ( fn, XLAL_EFAILED );
}
if ( (ret = LALCalloc(1, newlen + 1)) == NULL )
return NULL;
if ( (ret = XLALCalloc(1, newlen + 1)) == NULL ) {
XLALPrintError ("%s: failed to XLALCalloc(1, %d )\n", fn, newlen + 1 );
XLAL_ERROR_NULL ( fn, XLAL_ENOMEM );
}
strncpy ( ret, pos0, newlen );
ret[ newlen ] = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment