LALString.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /** \file
00021  * \ingroup std
00022  * \author Creighton, J. D. E.
00023  * \date $Date: 2007/06/08 14:41:53 $
00024  * \brief XLAL string manipulation routines.
00025  *
00026  * $Id: LALString.c,v 1.2 2007/06/08 14:41:53 bema Exp $
00027  */
00028 
00029 #include <string.h>
00030 #include <lal/LALStdlib.h>
00031 #include <lal/LALString.h>
00032 
00033 NRCSID( LALSTRINGC, "$Id: LALString.c,v 1.2 2007/06/08 14:41:53 bema Exp $" );
00034 
00035 /** Like strcat but dynamically reallocates string with LALRealloc. */
00036 char * XLALStringAppend( char *s, const char *append )
00037 {
00038   static const char *func = "XLALStringAppend";
00039   size_t curlen;
00040   size_t newlen;
00041   if ( ! append )
00042     return s;
00043   curlen = s ? strlen( s ) : 0;
00044   newlen = curlen + strlen( append );
00045   s = LALRealloc( s, newlen + 1 );
00046   if ( ! s )
00047     XLAL_ERROR_NULL( func, XLAL_ENOMEM );
00048   strcpy( s + curlen, append );
00049   return s;
00050 }
00051 
00052 /** Like strdup but uses LAL allocation routines (free with LALFree). */
00053 char * XLALStringDuplicate( const char *s )
00054 {
00055   char *dup;
00056   dup = XLALStringAppend( NULL, s );
00057   return dup;
00058 }
00059 
00060 /** Copy sources string src to destination string dst.
00061  * Up to size - 1 characters are copied and destination string dst is
00062  * guaranteed to be NUL-terminated.
00063  * Return value is the length of source string src.  If this is greater than
00064  * or equal to the size of the destination string buffer, size, then truncation
00065  * has occurred. Should be nearly equivalent to strlcpy. */
00066 size_t XLALStringCopy( char *dst, const char *src, size_t size )
00067 {
00068   size_t srclen;
00069   if ( ! src )
00070     src = "";
00071   srclen = strlen( src );
00072   if ( ! dst || size < 1 ) /* no copy */
00073     return srclen;
00074   if ( size == 1 ) /* NUL terminate and exit */
00075   {
00076     dst[0] = 0;
00077     return srclen;
00078   }
00079   strncpy( dst, src, size - 1 );
00080   dst[size-1] = 0;
00081   return srclen;
00082 }
00083 
00084 /** Concatenate sources string src to the end of destination string dst.
00085  * Characters are added to destination string dst until the source string src
00086  * is exhausted or the length of destination string dst is size - 1 characters.
00087  * Destination string dst is guaranteed to be NUL-terminated.
00088  * Return value is the initial length of destination string dst plus the
00089  * length of source string src.  If this is greater than
00090  * or equal to the size of the destination string buffer, size, then truncation
00091  * has occurred. Should be nearly equivalent to strlcat. */
00092 size_t XLALStringConcatenate( char *dst, const char *src, size_t size )
00093 {
00094   size_t srclen;
00095   size_t dstlen;
00096   if ( ! src )
00097     src = "";
00098   srclen = strlen( src );
00099   if ( ! dst || size < 1 ) /* no copy */
00100     return srclen;
00101   if ( size == 1 ) /* NUL terminate and exit */
00102   {
00103     dst[0] = 0;
00104     return srclen;
00105   }
00106   dstlen = strlen( dst );
00107   strncat( dst, src, size - dstlen - 1 );
00108   return srclen + dstlen;
00109 }

Generated on Sat Aug 30 03:12:52 2008 for LAL by  doxygen 1.5.2