From 4ef68740a338682fa09c1b0c3c3ec693048a3396 Mon Sep 17 00:00:00 2001 From: DWesl <22566757+DWesl@users.noreply.github.com> Date: Sat, 29 Oct 2022 09:48:22 -0400 Subject: [PATCH] STY: Move nulldup backup definition from cp_win32.c to ncconfigure.h Allow definition to be used in more places. Should probably consolidate definition a few places. --- include/ncconfigure.h | 69 ++++++++++++++++++++++++++++++++++++++++++- libncpoco/cp_win32.c | 66 ----------------------------------------- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/include/ncconfigure.h b/include/ncconfigure.h index 77d34ac4c1..72010378cd 100644 --- a/include/ncconfigure.h +++ b/include/ncconfigure.h @@ -121,8 +121,75 @@ unsigned long long int strtoull(const char*, char**, int); /* handle null arguments */ #ifndef nulldup +#ifndef(HAVE_STRDUP) +/** Copy s if not NULL. + * + * Implementation in terms of strdup in + * + * @li include/ncconfigure.h + * @li include/netcdf_json.h + * @li libdap4/ncd4.h + * @li libdispatch/dfile.c + * @li libdispatch/dinfermodel.c + * @li libdispatch/drc.c + * @li libdispatch/dutil.c + * @li libdispatch/nc.c + * @li libdispatch/ncjson.c + * @li libdispatch/ncurl.c + * @li libncxml/ncxml_ezxml.c + * @li ncxml_tinyxml2.cpp + * @li libncxml/ncxml_xml2.c + * @li libnczarr/zsync.c + * @li ncdump/ocprint.c + * @li ncgen/cvt.c + * @li ncgen/ncgen.h + * @li ncgen3/ncgen.h + * @li nczarr_test/test_nczarr_utils.h + * @li oc2/ocinternal.h + * + * Declarations as extern: + * + * @li include/ncconfigure.h + * + * I'd like it to be + * static inline const char *nulldup(const char *const s); + * but that's not what's in ncconfigure.h + * + * @param s the string to duplicate + * @pre s is either NULL or a NULL-terminated string + * + * @returns NULL or the duplicated string (caller owns the new + * pointer) + * + * @throws ENOMEM if out of memory + + * + * @post returns NULL if s is NULL, or a new pointer to a + * freshly-allocated copy of s + */ +extern char *nulldup(const char* s) { + if (s != NULL) { + ssize_t result_length = strlen(s) + 1; + char *result = malloc(result_length); + if (result == NULL) { +#ifdef ENOMEM + /* C++11, POSIX? */ + errno = ENOMEM; +#else /* ENOMEM */ + errno = 1; +#endif /* ENOMEM */ + return NULL; + } + strncpy(result, s, result_length); + return result; + } else { + return NULL; + } +} +#else /* HAVE_STRDUP */ #define nulldup(s) ((s)==NULL?NULL:strdup(s)) -#endif +#endif /* HAVE_STRDUP */ +#endif /* nulldup */ #ifndef nulllen #define nulllen(s) ((s)==NULL?0:strlen(s)) diff --git a/libncpoco/cp_win32.c b/libncpoco/cp_win32.c index 232d985023..b4284ac101 100755 --- a/libncpoco/cp_win32.c +++ b/libncpoco/cp_win32.c @@ -29,72 +29,6 @@ static const char* driveletters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS /* Forward */ static int isAbsolutePath(const char* path); -#if !defined(nulldup) && !defined(HAVE_STRDUP) -/** Copy s if not NULL. - * - * Implementation in terms of strdup in - * - * @li include/ncconfigure.h - * @li include/netcdf_json.h - * @li libdap4/ncd4.h - * @li libdispatch/dfile.c - * @li libdispatch/dinfermodel.c - * @li libdispatch/drc.c - * @li libdispatch/dutil.c - * @li libdispatch/nc.c - * @li libdispatch/ncjson.c - * @li libdispatch/ncurl.c - * @li libncxml/ncxml_ezxml.c - * @li ncxml_tinyxml2.cpp - * @li libncxml/ncxml_xml2.c - * @li libnczarr/zsync.c - * @li ncdump/ocprint.c - * @li ncgen/cvt.c - * @li ncgen/ncgen.h - * @li ncgen3/ncgen.h - * @li nczarr_test/test_nczarr_utils.h - * @li oc2/ocinternal.h - * - * Declarations as extern: - * - * @li include/ncconfigure.h - * - * I'd like it to be - * static inline const char *nulldup(const char *const s); - * but that's not what's in ncconfigure.h - * - * @param s the string to duplicate - * @pre s is either NULL or a NULL-terminated string - * - * @returns NULL or the duplicated string (caller owns the new - * pointer) - * - * @throws ENOMEM if out of memory - * - * @post returns NULL if s is NULL, or a new pointer to a - * freshly-allocated copy of s - */ -extern char *nulldup(const char* s) { - if (s != NULL) { - ssize_t result_length = strlen(s) + 1; - char *result = malloc(result_length); - if (result == NULL) { -#ifdef ENOMEM - /* C++11, POSIX? */ - errno = ENOMEM; -#else - errno = 1; -#endif - return NULL; - } - strncpy(result, s, result_length); - return result; - } else { - return NULL; - } -} -#endif - /**************************************************/ int