mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-03-31 17:50:26 +08:00
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.
This commit is contained in:
parent
fb02ff8ea8
commit
4ef68740a3
@ -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))
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user