Skip to content

Commit

Permalink
Creat PR to support refactor of code.
Browse files Browse the repository at this point in the history
re: PR #3068

Affects files:
* libdispatch/ncutil.[ch]
* libdispatch/drc.c
* include/ncutil.h
* include/ncrc.h
* include/netcdf_vutils.h
plus any files that use the above modified .h files.

Part of splitting PR 3068

1. move libdispatch/ncutil.h to include/netcdf_vutils.h and rename uses.
2. Refactor code to move and unify various functions from libdispatch/drc.c into ncutil.h and ncutil.c.
3. Modify file includes to use ncutil.h.
  • Loading branch information
DennisHeimbigner committed Feb 17, 2025
1 parent d19fca6 commit 01fe8d8
Show file tree
Hide file tree
Showing 27 changed files with 265 additions and 110 deletions.
4 changes: 2 additions & 2 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

include_HEADERS = netcdf.h netcdf_meta.h netcdf_mem.h netcdf_aux.h \
netcdf_filter.h netcdf_filter_build.h netcdf_filter_hdf5_build.h \
netcdf_dispatch.h
netcdf_dispatch.h netcdf_vutils.h

include_HEADERS += netcdf_json.h netcdf_proplist.h

Expand All @@ -25,7 +25,7 @@ nc4internal.h nctime.h nc3internal.h onstack.h ncrc.h ncauth.h \
ncoffsets.h nctestserver.h nc4dispatch.h nc3dispatch.h ncexternl.h \
ncpathmgr.h ncindex.h hdf4dispatch.h hdf5internal.h nc_provenance.h \
hdf5dispatch.h ncmodel.h isnan.h nccrc.h ncexhash.h ncxcache.h \
ncjson.h ncxml.h ncs3sdk.h ncproplist.h ncplugins.h
ncjson.h ncxml.h ncs3sdk.h ncproplist.h ncplugins.h ncutil.h

if USE_DAP
noinst_HEADERS += ncdap.h
Expand Down
20 changes: 2 additions & 18 deletions include/ncrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ and accessing rc files (e.g. .daprc).
#define NCRC_H

/* Need these support includes */
#include <stdio.h>
#include "ncuri.h"
#include "nclist.h"
#include "ncbytes.h"
#include <stdio.h>

/* getenv() keys */
#define NCRCENVIGNORE "NCRCENV_IGNORE"
Expand Down Expand Up @@ -46,6 +46,7 @@ typedef struct NCRCinfo {

/* Opaque structures */
struct NCS3INFO;
enum NCS3SVC;

#if defined(__cplusplus)
extern "C" {
Expand All @@ -67,23 +68,6 @@ EXTERNL NCRCentry* NC_rcfile_ith(NCRCinfo*,size_t);
EXTERNL void NC_rcclear(NCRCinfo* info);
EXTERNL void NC_rcclear(NCRCinfo* info);

/* From dutil.c (Might later move to e.g. nc.h */
EXTERNL int NC__testurl(const char* path, char** basenamep);
EXTERNL int NC_isLittleEndian(void);
EXTERNL char* NC_entityescape(const char* s);
EXTERNL int NC_readfile(const char* filename, NCbytes* content);
EXTERNL int NC_readfilen(const char* filename, NCbytes* content, long long len);
EXTERNL int NC_readfileF(FILE* fp, NCbytes* content, long long len);
EXTERNL int NC_writefile(const char* filename, size_t size, void* content);
EXTERNL char* NC_mktmp(const char* base);
EXTERNL int NC_getmodelist(const char* modestr, NClist** modelistp);
EXTERNL int NC_testmode(NCURI* uri, const char* tag);
EXTERNL int NC_testpathmode(const char* path, const char* tag);
EXTERNL int NC_addmodetag(NCURI* uri, const char* tag);
EXTERNL int NC_split_delim(const char* path, char delim, NClist* segments);
EXTERNL int NC_join(struct NClist* segments, char** pathp);
EXTERNL int NC_joinwith(NClist* segments, const char* sep, const char* prefix, const char* suffix, char** pathp);

#if defined(__cplusplus)
}
#endif
Expand Down
94 changes: 94 additions & 0 deletions include/ncutil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT for license information.
*/

/**
API for libdispatch/dutil.c
*/

#ifndef NCUTIL_H
#define NCUTIL_H

/**************************************************/

/* signature: void swapinline16(void* ip) */
#define swapinline16(ip) \
{ \
char b[2]; \
char* src = (char*)(ip); \
b[0] = src[1]; \
b[1] = src[0]; \
memcpy(ip, b, 2); \
}

/* signature: void swapinline32(void* ip) */
#define swapinline32(ip) \
{ \
char b[4]; \
char* src = (char*)(ip); \
b[0] = src[3]; \
b[1] = src[2]; \
b[2] = src[1]; \
b[3] = src[0]; \
memcpy(ip, b, 4); \
}

/* signature: void swapinline64(void* ip) */
#define swapinline64(ip) \
{ \
char b[8]; \
char* src = (char*)(ip); \
b[0] = src[7]; \
b[1] = src[6]; \
b[2] = src[5]; \
b[3] = src[4]; \
b[4] = src[3]; \
b[5] = src[2]; \
b[6] = src[1]; \
b[7] = src[0]; \
memcpy(ip, b, 8); \
}

/**************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/* Opaque */
struct NClist;
struct NCbytes;
struct NCURI;

EXTERNL int NC__testurl(const char* path, char** basenamep);
EXTERNL int NC_isLittleEndian(void);
EXTERNL char* NC_backslashEscape(const char* s);
EXTERNL char* NC_backslashUnescape(const char* esc);
EXTERNL char* NC_entityescape(const char* s);
EXTERNL char* NC_shellUnescape(const char* esc);
EXTERNL int NC_mktmp(const char* base, char** tmpfile);
EXTERNL int NC_readfile(const char* filename, struct NCbytes* content);
EXTERNL int NC_readfilen(const char* filename, struct NCbytes* content, long long amount);
EXTERNL int NC_readfileF(FILE* stream, struct NCbytes* content, long long amount);
EXTERNL int NC_writefile(const char* filename, size_t size, void* content);
EXTERNL int NC_getmodelist(const char* modestr, struct NClist** modelistp);
EXTERNL int NC_testpathmode(const char* path, const char* tag);
EXTERNL int NC_testmode(struct NCURI* uri, const char* tag);
EXTERNL int NC_addmodetag(struct NCURI* uri, const char* tag);
EXTERNL int NC_isinf(double x);
EXTERNL int NC_isnan(double x);
EXTERNL int NC_split_delim(const char* arg, char delim, struct NClist* segments);
EXTERNL int NC_join(struct NClist* segments, char** pathp);
EXTERNL int NC_joinwith(struct NClist* segments, const char* sep, const char* prefix, const char* suffix, char** pathp);
EXTERNL void NC_sortenvv(size_t n, char** envv);
EXTERNL void NC_sortlist(struct NClist* l);
EXTERNL void NC_freeenvv(size_t nkeys, char** keys);
EXTERNL int NC_swapatomicdata(size_t datalen, void* data, int typesize);

#if defined(__cplusplus)
}
#endif

#endif /*NCUTIL_H*/

33 changes: 19 additions & 14 deletions libdispatch/ncutil.h → include/netcdf_vutils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
/* Copyright 2018, UCAR/Unidata
See the COPYRIGHT file for more information. */

#ifndef UTILS_H
#define UTILS_H 1
#ifndef NCVUTILS_H
#define NCVUTILS_H 1

/* Define a header-only simple version of a dynamically expandable list and byte buffer */
/* To be used in code that should be independent of libnetcdf */
Expand All @@ -15,8 +15,8 @@ typedef struct VList {

typedef struct VString {
int nonextendible; /* 1 => fail if an attempt is made to extend this string*/
unsigned int alloc;
unsigned int length;
unsigned alloc;
unsigned length;
char* content;
} VString;

Expand Down Expand Up @@ -55,7 +55,7 @@ static void
vlistexpand(VList* l)
{
void** newcontent = NULL;
size_t newsz;
unsigned newsz;

if(l == NULL) return;
newsz = (l->length * 2) + 1; /* basically double allocated space */
Expand Down Expand Up @@ -132,11 +132,11 @@ static void
vsexpand(VString* vs)
{
char* newcontent = NULL;
size_t newsz;
unsigned newsz;

if(vs == NULL) return;
assert(vs->nonextendible == 0);
newsz = (vs->alloc + VSTRALLOC); /* basically double allocated space */
newsz = (vs->alloc + VSTRALLOC); /* increase allocated space */
if(vs->alloc >= newsz) return; /* space already allocated */
newcontent=(char*)calloc(1,newsz+1);/* always room for nul term */
assert(newcontent != NULL);
Expand All @@ -154,7 +154,7 @@ vsappendn(VString* vs, const char* elem, unsigned n)
{
size_t need;
assert(vs != NULL && elem != NULL);
if(n == 0) {n = strlen(elem);}
if(n == 0) {n = (unsigned)strlen(elem);}
need = vs->length + n;
if(vs->nonextendible) {
/* Space must already be available */
Expand All @@ -166,7 +166,7 @@ vsappendn(VString* vs, const char* elem, unsigned n)
memcpy(&vs->content[vs->length],elem,n);
vs->length += n;
if(!vs->nonextendible)
vs->content[vs->length] = '\0';
vs->content[vs->length] = '\0'; /* guarantee nul term */
}

static void
Expand Down Expand Up @@ -196,7 +196,12 @@ static char*
vsextract(VString* vs)
{
char* x = NULL;
if(vs == NULL || vs->content == NULL) return NULL;
if(vs == NULL) return NULL;
if(vs->content == NULL) {
/* guarantee content existence and nul terminated */
if((vs->content = calloc(1,sizeof(char)))==NULL) return NULL;
vs->length = 0;
}
x = vs->content;
vs->content = NULL;
vs->length = 0;
Expand Down Expand Up @@ -229,14 +234,14 @@ util_initialize(void)

/* Following are always "in-lined"*/
#define vlistcontents(l) ((l)==NULL?NULL:(l)->content)
#define vlistlength(l) ((l)==NULL?0:(int)(l)->length)
#define vlistlength(l) ((l)==NULL?0:(l)->length)
#define vlistclear(l) vlistsetlength(l,0)
#define vlistsetlength(l,len) do{if((l)!=NULL) (l)->length=len;} while(0)

#define vscontents(vs) ((vs)==NULL?NULL:(vs)->content)
#define vslength(vs) ((vs)==NULL?0:(int)(vs)->length)
#define vslength(vs) ((vs)==NULL?0:(vs)->length)
#define vscat(vs,s) vsappendn(vs,s,0)
#define vsclear(vs) vssetlength(vs,0)
#define vssetlength(vs,len) do{if((vs)!=NULL) (vs)->length=len;} while(0)

#endif /*UTILS_H*/
#endif /*NCVUTIL_H*/
2 changes: 1 addition & 1 deletion libdap4/d4file.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ set_curl_properties(NCD4INFO* d4info)
if(path == NULL) return NC_ENOMEM;
snprintf(path,len,"%s/nc4cookies",globalstate->tempdir);
/* Create the unique cookie file name */
newpath = NC_mktmp(path);
if((ret=NC_mktmp(path,&newpath))) goto fail;
free(path);
if(newpath == NULL) {
fprintf(stderr,"Cannot create cookie file\n");
Expand Down
1 change: 1 addition & 0 deletions libdap4/d4includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "nclog.h"
#include "ncdap.h"
#include "ncpathmgr.h"
#include "ncutil.h"

#include "d4util.h"

Expand Down
2 changes: 1 addition & 1 deletion libdispatch/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ endif # NETCDF_ENABLE_BYTERANGE
if NETCDF_ENABLE_S3
if NETCDF_ENABLE_S3_INTERNAL
# Renamed to avoid conflicts with the HDF5 files
libdispatch_la_SOURCES += ncs3sdk_h5.c nch5s3comms.c nch5s3comms.h ncutil.h nccurl_setup.h \
libdispatch_la_SOURCES += ncs3sdk_h5.c nch5s3comms.c nch5s3comms.h nccurl_setup.h \
nccurl_sha256.c nccurl_sha256.h nccurl_hmac.c nccurl_hmac.h
AM_CPPFLAGS += -I$(top_srcdir)/libncxml
libdispatch_la_CPPFLAGS += ${AM_CPPFLAGS}
Expand Down
5 changes: 4 additions & 1 deletion libdispatch/daux.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ See COPYRIGHT for license information.
#include "netcdf_filter.h"
#include "ncpathmgr.h"
#include "nclist.h"
#include "ncutil.h"

struct NCAUX_FIELD {
char* name;
Expand Down Expand Up @@ -953,6 +954,8 @@ This function is just a wrapper around nc_dump__data.
@return error code
*/

EXTERNL int nc_dump_data(int ncid, nc_type xtype, void* memory, size_t count, char** bufp);

EXTERNL int
ncaux_dump_data(int ncid, int xtype, void* memory, size_t count, char** bufp)
{
Expand Down Expand Up @@ -1209,7 +1212,7 @@ ncaux_plugin_path_stringlen(void)
if((stat = nc_plugin_path_get(&npl))) goto done;
/* Convert to a string path separated by ';' */
if((stat = ncaux_plugin_path_tostring(&npl,';',&buf))) goto done;
len = nulllen(buf);
len = (int)nulllen(buf);

done:
if(npl.dirs != NULL) {(void)ncaux_plugin_path_clear(&npl);}
Expand Down
2 changes: 1 addition & 1 deletion libdispatch/dinfermodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "ncbytes.h"
#include "nclist.h"
#include "nclog.h"
#include "ncrc.h"
#include "ncutil.h"
#include "nchttp.h"
#ifdef NETCDF_ENABLE_S3
#include "ncs3sdk.h"
Expand Down
13 changes: 6 additions & 7 deletions libdispatch/drc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ See COPYRIGHT for license information.
#include "nc4internal.h"
#include "ncs3sdk.h"
#include "ncdispatch.h"
#include "ncutil.h"

#undef NOREAD

Expand Down Expand Up @@ -136,15 +137,13 @@ This is set by the environment variable NC_TEST_AWS_DIR.
void
ncrc_initialize(void)
{
int stat = NC_NOERR;
NCglobalstate* ncg = NULL;

if(NCRCinitialized) return;
NCRCinitialized = 1; /* prevent recursion */

ncg = NC_getglobalstate();

#ifndef NOREAD
{
int stat = NC_NOERR;
NCglobalstate* ncg = NC_getglobalstate();
/* Load entrys */
if((stat = NC_rcload())) {
nclog(NCLOGWARN,".rc loading failed");
Expand All @@ -153,6 +152,7 @@ ncrc_initialize(void)
if((stat = NC_aws_load_credentials(ncg))) {
nclog(NCLOGWARN,"AWS config file not loaded");
}
}
#endif
}

Expand Down Expand Up @@ -180,7 +180,6 @@ NC_rcclear(NCRCinfo* info)
nullfree(info->rchome);
rcfreeentries(info->entries);
NC_s3freeprofilelist(info->s3profiles);

}

static void
Expand Down Expand Up @@ -329,7 +328,7 @@ NC_set_rcfile(const char* rcfile)
goto done;
}
fclose(f);
nullfree(globalstate->rcinfo->rcfile);
NC_rcclear(globalstate->rcinfo);
globalstate->rcinfo->rcfile = strdup(rcfile);
/* Clear globalstate->rcinfo */
NC_rcclear(&globalstate->rcinfo);
Expand Down
1 change: 1 addition & 0 deletions libdispatch/ds3util.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "nclist.h"
#include "ncrc.h"
#include "nclog.h"
#include "ncutil.h"
#include "ncs3sdk.h"

#undef AWSDEBUG
Expand Down
Loading

0 comments on commit 01fe8d8

Please sign in to comment.