Skip to content

Commit

Permalink
STY: Move nulldup backup definition from cp_win32.c to ncconfigure.h
Browse files Browse the repository at this point in the history
Allow definition to be used in more places.
Should probably consolidate definition a few places.
  • Loading branch information
DWesl committed Oct 29, 2022
1 parent fb02ff8 commit 4ef6874
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 67 deletions.
69 changes: 68 additions & 1 deletion include/ncconfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
66 changes: 0 additions & 66 deletions libncpoco/cp_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ef6874

Please sign in to comment.