diff options
Diffstat (limited to 'src/GeneralizedPolynomial-Uniform/util.c')
-rw-r--r-- | src/GeneralizedPolynomial-Uniform/util.c | 94 |
1 files changed, 63 insertions, 31 deletions
diff --git a/src/GeneralizedPolynomial-Uniform/util.c b/src/GeneralizedPolynomial-Uniform/util.c index f4715e2..03186f0 100644 --- a/src/GeneralizedPolynomial-Uniform/util.c +++ b/src/GeneralizedPolynomial-Uniform/util.c @@ -14,9 +14,15 @@ #include <limits.h> #include <stdlib.h> #include <stdio.h> +#include <string.h> -#ifndef LOCALINTERP_STANDALONE_BUILD +#ifdef LOCALINTERP_STANDALONE_BUILD + #define WANT_STRLCPY +#else #include "cctk.h" + #define WANT_ZERO_INT_ARRAY + #define WANT_DECODE_N_PARTS + #define WANT_STRLCPY #endif #include "InterpLocalUniform.h" @@ -29,36 +35,7 @@ static const char *rcsid = "$Header$"; /******************************************************************************/ -/*@@ - @routine LocalInterp_zero_int_array - @date 23 Oct 2001 - @author Jonathan Thornburg <jthorn@aei.mpg.de> - @desc This function sets a CCTK_INT array to all zeros. - @enddesc - - @var N - @vdesc The number of elements in the array. - @vtype CCTK_INT N - @endvar - - @var array - @vdesc (A pointer to) the array - @vtype CCTK_INT array[] - @vio out - @endvar - @@*/ -void LocalInterp_zero_int_array(int N, CCTK_INT array[]) -{ -int i; - - for (i = 0 ; i < N ; ++i) - { - array[i] = 0; - } -} - -/******************************************************************************/ - +#ifdef WANT_DECODE_N_PARTS /*@@ @routine LocalInterp_decode_N_parts @date 22 Jan 2002 @@ -107,3 +84,58 @@ case CCTK_VARIABLE_FPOINTER: return 0; default: return -1; } } +#endif /* WANT_DECODE_N_PARTS */ + +/******************************************************************************/ + +#ifdef WANT_STRLCPY +/*@@ + @routine LocalInterp_Strlcpy + @date 1.Feb.2003 + @author Jonathan Thornburg <jthorn@aei.mpg.de> + @desc This function implements the strlcpy() function + described in + http://www.openbsd.org/papers/strlcpy-paper.ps + + The strlcpy(3) function copies up to size-1 characters + from the null-terminated string src to dst , followed + by a null character (so dst is always null-terminated). + + strlcpy(3) is a replacement for strncpy(3). In comparison + to strncpy(3), strlcpy(3) is safer and easier to use (it + guarantees null termination of the destination buffer), + and faster (it doesn't have to fill the entire buffer with + null characters). + @enddesc + + @var dst + @vdesc A non-null pointer to the destination buffer. + @vtype char* dst + @endvar + + @var src + @vdesc A non-null pointer to the source string. + @vtype const char* dst + @endvar + + @var dst_size + @vdesc The size of the destination buffer. + @vtype size_t dst_size + @endvar + + @returntype size_t + @returndesc This function returns strlen(src). + @endreturndesc + @@*/ +size_t LocalInterp_Strlcpy(char* dst, const char* src, size_t dst_size) +{ +const size_t src_size = strlen(src); +if (src_size < dst_size) + then strcpy(dst, src); + else { + strncpy(dst, src, dst_size-1); + dst[dst_size-1] = '\0'; + } +return src_size; +} +#endif /* WANT_STRLCPY */ |