2017-09-01 04:36:54 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:29:57 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2017-09-01 04:36:54 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2021-09-28 08:36:33 +08:00
|
|
|
#include <assert.h>
|
2017-09-01 04:36:54 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include "netcdf.h"
|
|
|
|
#include "ncuri.h"
|
|
|
|
#include "ncbytes.h"
|
2019-09-30 02:59:28 +08:00
|
|
|
#include "nclist.h"
|
2017-09-01 04:36:54 +08:00
|
|
|
#include "nclog.h"
|
2021-09-28 08:36:33 +08:00
|
|
|
#include "ncrc.h"
|
2020-10-14 09:12:15 +08:00
|
|
|
#include "ncpathmgr.h"
|
2017-09-01 04:36:54 +08:00
|
|
|
|
|
|
|
#define NC_MAX_PATH 4096
|
2022-01-25 04:48:41 +08:00
|
|
|
#ifndef nulldup
|
|
|
|
#define nulldup(x) ((x)?strdup(x):(x))
|
|
|
|
#endif
|
2017-09-01 04:36:54 +08:00
|
|
|
/**************************************************/
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2017-09-01 04:36:54 +08:00
|
|
|
* Provide a hidden interface to allow utilities
|
|
|
|
* to check if a given path name is really an ncdap4 url.
|
|
|
|
* If no, return null, else return basename of the url
|
|
|
|
* minus any extension.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
NC__testurl(const char* path, char** basenamep)
|
|
|
|
{
|
|
|
|
NCURI* uri;
|
|
|
|
int ok = NC_NOERR;
|
2019-09-30 02:59:28 +08:00
|
|
|
if(ncuriparse(path,&uri))
|
2017-09-01 04:36:54 +08:00
|
|
|
ok = NC_EURL;
|
|
|
|
else {
|
|
|
|
char* slash = (uri->path == NULL ? NULL : strrchr(uri->path, '/'));
|
|
|
|
char* dot;
|
|
|
|
if(slash == NULL) slash = (char*)path; else slash++;
|
|
|
|
slash = nulldup(slash);
|
|
|
|
if(slash == NULL)
|
|
|
|
dot = NULL;
|
|
|
|
else
|
|
|
|
dot = strrchr(slash, '.');
|
|
|
|
if(dot != NULL && dot != slash) *dot = '\0';
|
|
|
|
if(basenamep)
|
|
|
|
*basenamep=slash;
|
|
|
|
else if(slash)
|
|
|
|
free(slash);
|
|
|
|
}
|
|
|
|
ncurifree(uri);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal Return 1 if this machine is little endian */
|
2017-09-01 04:36:54 +08:00
|
|
|
int
|
|
|
|
NC_isLittleEndian(void)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
unsigned char bytes[SIZEOF_INT];
|
|
|
|
int i;
|
|
|
|
} u;
|
|
|
|
u.i = 1;
|
|
|
|
return (u.bytes[0] == 1 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2017-09-01 04:36:54 +08:00
|
|
|
char*
|
|
|
|
NC_backslashEscape(const char* s)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
size_t len;
|
|
|
|
char* escaped = NULL;
|
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
escaped = (char*)malloc(1+(2*len)); /* max is everychar is escaped */
|
|
|
|
if(escaped == NULL) return NULL;
|
|
|
|
for(p=s,q=escaped;*p;p++) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
case '.':
|
|
|
|
case '@':
|
|
|
|
*q++ = '\\'; *q++ = '\\';
|
|
|
|
break;
|
|
|
|
default: *q++ = c; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2017-09-01 04:36:54 +08:00
|
|
|
char*
|
|
|
|
NC_backslashUnescape(const char* esc)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char* s;
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
|
|
|
|
if(esc == NULL) return NULL;
|
|
|
|
len = strlen(esc);
|
|
|
|
s = (char*)malloc(len+1);
|
|
|
|
if(s == NULL) return NULL;
|
|
|
|
for(p=esc,q=s;*p;) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
p++;
|
|
|
|
/* fall thru */
|
|
|
|
default: *q++ = *p++; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2017-09-01 04:36:54 +08:00
|
|
|
char*
|
|
|
|
NC_entityescape(const char* s)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
size_t len;
|
|
|
|
char* escaped = NULL;
|
|
|
|
const char* entity;
|
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
escaped = (char*)malloc(1+(6*len)); /* 6 = |'| */
|
|
|
|
if(escaped == NULL) return NULL;
|
|
|
|
for(p=s,q=escaped;*p;p++) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '&': entity = "&"; break;
|
|
|
|
case '<': entity = "<"; break;
|
|
|
|
case '>': entity = ">"; break;
|
|
|
|
case '"': entity = """; break;
|
|
|
|
case '\'': entity = "'"; break;
|
|
|
|
default : entity = NULL; break;
|
|
|
|
}
|
|
|
|
if(entity == NULL)
|
|
|
|
*q++ = c;
|
|
|
|
else {
|
|
|
|
len = strlen(entity);
|
|
|
|
memcpy(q,entity,len);
|
|
|
|
q+=len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2021-04-22 04:59:15 +08:00
|
|
|
Depending on the platform, the shell will sometimes
|
|
|
|
pass an escaped octotherpe character without removing
|
|
|
|
the backslash. So this function is appropriate to be called
|
|
|
|
on possible url paths to unescape such cases. See e.g. ncgen.
|
|
|
|
*/
|
2022-01-30 06:27:52 +08:00
|
|
|
char*
|
2021-04-22 04:59:15 +08:00
|
|
|
NC_shellUnescape(const char* esc)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char* s;
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
|
|
|
|
if(esc == NULL) return NULL;
|
|
|
|
len = strlen(esc);
|
|
|
|
s = (char*)malloc(len+1);
|
|
|
|
if(s == NULL) return NULL;
|
|
|
|
for(p=esc,q=s;*p;) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
if(p[1] == '#')
|
|
|
|
p++;
|
|
|
|
/* fall thru */
|
|
|
|
default: *q++ = *p++; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2017-09-03 08:09:36 +08:00
|
|
|
Wrap mktmp and return the generated path,
|
|
|
|
or null if failed.
|
|
|
|
Base is the base file path. XXXXX is appended
|
|
|
|
to allow mktmp add its unique id.
|
|
|
|
Return the generated path.
|
2017-09-01 04:36:54 +08:00
|
|
|
*/
|
|
|
|
|
2017-09-03 08:09:36 +08:00
|
|
|
char*
|
|
|
|
NC_mktmp(const char* base)
|
2017-09-01 04:36:54 +08:00
|
|
|
{
|
2021-05-15 01:33:03 +08:00
|
|
|
int fd = -1;
|
|
|
|
char* tmp = NULL;
|
|
|
|
size_t len;
|
Support installation of filters into user-specified location
re: https://github.com/Unidata/netcdf-c/issues/2294
Ed Hartnett suggested that the netcdf library installation process
be extended to install the standard filters into a user specified
location. The user can then set HDF5_PLUGIN_PATH to that location.
This PR provides that capability using:
````
configure option: --with-plugin-dir=<absolute directory path>
cmake option: -DPLUGIN_INSTALL_DIR=<absolute directory path>
````
Currently, the following plugins are always installed, if
available: bzip2, zstd, blosc.
If NCZarr is enabled, then additional plugins are installed:
fletcher32, shuffle, deflate, szip.
Additionally, the necessary codec support is installed
for each of the above filters that is installed.
## Changes:
1. Cleanup handling of built-in bzip2.
2. Add documentation to docs/filters.md
3. Re-factor the NCZarr codec libraries
4. Add a test, although it can only be exercised after
the library is installed, so it cannot be used during
normal testing.
5. Cleanup use of HDF5_PLUGIN_PATH in the filter test cases.
2022-04-30 04:31:55 +08:00
|
|
|
#ifndef HAVE_MKSTEMP
|
|
|
|
int tries;
|
2022-02-09 11:53:30 +08:00
|
|
|
#define MAXTRIES 4
|
2022-04-30 02:36:33 +08:00
|
|
|
#else
|
Support installation of filters into user-specified location
re: https://github.com/Unidata/netcdf-c/issues/2294
Ed Hartnett suggested that the netcdf library installation process
be extended to install the standard filters into a user specified
location. The user can then set HDF5_PLUGIN_PATH to that location.
This PR provides that capability using:
````
configure option: --with-plugin-dir=<absolute directory path>
cmake option: -DPLUGIN_INSTALL_DIR=<absolute directory path>
````
Currently, the following plugins are always installed, if
available: bzip2, zstd, blosc.
If NCZarr is enabled, then additional plugins are installed:
fletcher32, shuffle, deflate, szip.
Additionally, the necessary codec support is installed
for each of the above filters that is installed.
## Changes:
1. Cleanup handling of built-in bzip2.
2. Add documentation to docs/filters.md
3. Re-factor the NCZarr codec libraries
4. Add a test, although it can only be exercised after
the library is installed, so it cannot be used during
normal testing.
5. Cleanup use of HDF5_PLUGIN_PATH in the filter test cases.
2022-04-30 04:31:55 +08:00
|
|
|
mode_t mask;
|
2022-02-09 11:53:30 +08:00
|
|
|
#endif
|
2017-09-03 08:09:36 +08:00
|
|
|
|
2021-05-15 01:33:03 +08:00
|
|
|
len = strlen(base)+6+1;
|
2022-02-09 11:53:30 +08:00
|
|
|
if((tmp = (char*)calloc(1,len))==NULL)
|
2021-05-15 01:33:03 +08:00
|
|
|
goto done;
|
2022-02-09 11:53:30 +08:00
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
strlcat(tmp,base,len);
|
2021-05-15 01:33:03 +08:00
|
|
|
strlcat(tmp, "XXXXXX", len);
|
2022-02-09 11:53:30 +08:00
|
|
|
mask=umask(0077);
|
2021-05-15 01:33:03 +08:00
|
|
|
fd = NCmkstemp(tmp);
|
2022-02-09 11:53:30 +08:00
|
|
|
(void)umask(mask);
|
|
|
|
#else /* !HAVE_MKSTEMP */
|
|
|
|
/* Need to simulate by using some kind of pseudo-random number */
|
|
|
|
for(tries=0;tries<MAXTRIES;tries++) {
|
|
|
|
int rno = rand();
|
|
|
|
char spid[7];
|
|
|
|
if(rno < 0) rno = -rno;
|
|
|
|
tmp[0] = '\0';
|
|
|
|
strlcat(tmp,base,len);
|
|
|
|
snprintf(spid,sizeof(spid),"%06d",rno);
|
|
|
|
strlcat(tmp,spid,len);
|
|
|
|
fd=NCopen3(tmp,O_RDWR|O_CREAT, _S_IREAD|_S_IWRITE);
|
|
|
|
if(fd >= 0) break; /* sucess */
|
|
|
|
fd = -1; /* try again */
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_MKSTEMP */
|
2017-09-01 04:36:54 +08:00
|
|
|
if(fd < 0) {
|
2022-02-09 11:53:30 +08:00
|
|
|
nclog(NCLOGERR, "Could not create temp file: %s",tmp);
|
|
|
|
nullfree(tmp);
|
|
|
|
tmp = NULL;
|
|
|
|
goto done;
|
2021-05-15 01:33:03 +08:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(fd >= 0) close(fd);
|
|
|
|
return tmp;
|
2017-09-01 04:36:54 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2018-10-11 03:32:17 +08:00
|
|
|
int
|
|
|
|
NC_readfile(const char* filename, NCbytes* content)
|
2022-02-09 11:53:30 +08:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
stat = NC_readfilen(filename, content, -1);
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NC_readfilen(const char* filename, NCbytes* content, long long amount)
|
2018-10-11 03:32:17 +08:00
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
FILE* stream = NULL;
|
|
|
|
|
|
|
|
stream = NCfopen(filename,"r");
|
|
|
|
if(stream == NULL) {ret=errno; goto done;}
|
2022-02-09 11:53:30 +08:00
|
|
|
ret = NC_readfileF(stream,content,amount);
|
|
|
|
if (stream) fclose(stream);
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NC_readfileF(FILE* stream, NCbytes* content, long long amount)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
long long red = 0;
|
|
|
|
char part[1024];
|
|
|
|
|
|
|
|
while(amount < 0 || red < amount) {
|
2018-10-11 03:32:17 +08:00
|
|
|
size_t count = fread(part, 1, sizeof(part), stream);
|
|
|
|
if(ferror(stream)) {ret = NC_EIO; goto done;}
|
2022-02-09 11:53:30 +08:00
|
|
|
if(count > 0) ncbytesappendn(content,part,(unsigned long)count);
|
|
|
|
red += count;
|
|
|
|
if (feof(stream)) break;
|
|
|
|
}
|
|
|
|
/* Keep only amount */
|
|
|
|
if(amount >= 0) {
|
|
|
|
if(red > amount) ncbytessetlength(content,amount); /* read too much */
|
|
|
|
if(red < amount) ret = NC_ETRUNC; /* |file| < amount */
|
2018-10-11 03:32:17 +08:00
|
|
|
}
|
|
|
|
ncbytesnull(content);
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2018-10-11 03:32:17 +08:00
|
|
|
int
|
|
|
|
NC_writefile(const char* filename, size_t size, void* content)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
FILE* stream = NULL;
|
|
|
|
void* p;
|
|
|
|
size_t remain;
|
|
|
|
|
2021-11-04 02:49:54 +08:00
|
|
|
if(content == NULL) {content = ""; size = 0;}
|
|
|
|
|
2018-10-11 03:32:17 +08:00
|
|
|
stream = NCfopen(filename,"w");
|
|
|
|
if(stream == NULL) {ret=errno; goto done;}
|
|
|
|
p = content;
|
|
|
|
remain = size;
|
|
|
|
while(remain > 0) {
|
|
|
|
size_t written = fwrite(p, 1, remain, stream);
|
|
|
|
if(ferror(stream)) {ret = NC_EIO; goto done;}
|
|
|
|
remain -= written;
|
2022-02-09 11:53:30 +08:00
|
|
|
if (feof(stream)) break;
|
2018-10-11 03:32:17 +08:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(stream) fclose(stream);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2019-09-30 02:59:28 +08:00
|
|
|
Parse a path as a url and extract the modelist.
|
|
|
|
If the path is not a URL, then return a NULL list.
|
|
|
|
If a URL, but modelist is empty or does not exist,
|
|
|
|
then return empty list.
|
|
|
|
*/
|
|
|
|
int
|
2021-10-30 10:06:37 +08:00
|
|
|
NC_getmodelist(const char* modestr, NClist** modelistp)
|
2019-09-30 02:59:28 +08:00
|
|
|
{
|
|
|
|
int stat=NC_NOERR;
|
|
|
|
NClist* modelist = NULL;
|
|
|
|
|
2022-01-25 04:48:41 +08:00
|
|
|
modelist = nclistnew();
|
2019-09-30 02:59:28 +08:00
|
|
|
if(modestr == NULL || strlen(modestr) == 0) goto done;
|
2021-10-30 10:06:37 +08:00
|
|
|
|
2019-09-30 02:59:28 +08:00
|
|
|
/* Parse the mode string at the commas or EOL */
|
2021-10-30 10:06:37 +08:00
|
|
|
if((stat = NC_split_delim(modestr,',',modelist))) goto done;
|
2019-09-30 02:59:28 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
if(stat == NC_NOERR) {
|
|
|
|
if(modelistp) {*modelistp = modelist; modelist = NULL;}
|
2021-10-30 10:06:37 +08:00
|
|
|
} else
|
|
|
|
nclistfree(modelist);
|
2019-09-30 02:59:28 +08:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2019-09-30 02:59:28 +08:00
|
|
|
Check "mode=" list for a path and return 1 if present, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
2021-10-30 10:06:37 +08:00
|
|
|
NC_testpathmode(const char* path, const char* tag)
|
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
NCURI* uri = NULL;
|
|
|
|
ncuriparse(path,&uri);
|
|
|
|
if(uri != NULL) {
|
|
|
|
found = NC_testmode(uri,tag);
|
|
|
|
ncurifree(uri);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal
|
2021-10-30 10:06:37 +08:00
|
|
|
Check "mode=" list for a url and return 1 if present, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC_testmode(NCURI* uri, const char* tag)
|
2019-09-30 02:59:28 +08:00
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
2021-10-30 10:06:37 +08:00
|
|
|
const char* modestr = NULL;
|
2019-09-30 02:59:28 +08:00
|
|
|
NClist* modelist = NULL;
|
|
|
|
|
2022-01-25 04:48:41 +08:00
|
|
|
modestr = ncurifragmentlookup(uri,"mode");
|
2021-10-30 10:06:37 +08:00
|
|
|
if(modestr == NULL) goto done;
|
|
|
|
/* Parse mode str */
|
|
|
|
if((stat = NC_getmodelist(modestr,&modelist))) goto done;
|
|
|
|
/* Search for tag */
|
2019-09-30 02:59:28 +08:00
|
|
|
for(i=0;i<nclistlength(modelist);i++) {
|
2021-10-30 10:06:37 +08:00
|
|
|
const char* mode = (const char*)nclistget(modelist,i);
|
|
|
|
if(strcasecmp(mode,tag)==0) {found = 1; break;}
|
2022-01-25 04:48:41 +08:00
|
|
|
}
|
2019-09-30 02:59:28 +08:00
|
|
|
done:
|
2020-02-17 03:59:33 +08:00
|
|
|
nclistfreeall(modelist);
|
2019-09-30 02:59:28 +08:00
|
|
|
return found;
|
|
|
|
}
|
2020-08-18 09:15:47 +08:00
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
#if ! defined __INTEL_COMPILER
|
|
|
|
#if defined __APPLE__
|
|
|
|
/** \internal */
|
|
|
|
|
2020-08-18 09:15:47 +08:00
|
|
|
int isinf(double x)
|
|
|
|
{
|
|
|
|
union { unsigned long long u; double f; } ieee754;
|
|
|
|
ieee754.f = x;
|
|
|
|
return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) == 0x7ff00000 &&
|
|
|
|
( (unsigned)ieee754.u == 0 );
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2020-08-18 09:15:47 +08:00
|
|
|
int isnan(double x)
|
|
|
|
{
|
|
|
|
union { unsigned long long u; double f; } ieee754;
|
|
|
|
ieee754.f = x;
|
|
|
|
return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) +
|
|
|
|
( (unsigned)ieee754.u != 0 ) > 0x7ff00000;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*APPLE*/
|
2021-11-04 02:49:54 +08:00
|
|
|
#endif /*!_INTEL_COMPILER*/
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal */
|
2021-09-28 08:36:33 +08:00
|
|
|
int
|
2021-10-30 10:06:37 +08:00
|
|
|
NC_split_delim(const char* arg, char delim, NClist* segments)
|
2021-09-28 08:36:33 +08:00
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
const char* p = NULL;
|
|
|
|
const char* q = NULL;
|
|
|
|
ptrdiff_t len = 0;
|
|
|
|
char* seg = NULL;
|
|
|
|
|
2021-10-30 10:06:37 +08:00
|
|
|
if(arg == NULL || strlen(arg)==0 || segments == NULL)
|
2021-09-28 08:36:33 +08:00
|
|
|
goto done;
|
2021-10-30 10:06:37 +08:00
|
|
|
p = arg;
|
2021-09-28 08:36:33 +08:00
|
|
|
if(p[0] == delim) p++;
|
|
|
|
for(;*p;) {
|
|
|
|
q = strchr(p,delim);
|
|
|
|
if(q==NULL)
|
|
|
|
q = p + strlen(p); /* point to trailing nul */
|
|
|
|
len = (q - p);
|
|
|
|
if(len == 0)
|
|
|
|
{stat = NC_EURL; goto done;}
|
|
|
|
if((seg = malloc(len+1)) == NULL)
|
|
|
|
{stat = NC_ENOMEM; goto done;}
|
|
|
|
memcpy(seg,p,len);
|
|
|
|
seg[len] = '\0';
|
|
|
|
nclistpush(segments,seg);
|
|
|
|
seg = NULL; /* avoid mem errors */
|
|
|
|
if(*q) p = q+1; else p = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
nullfree(seg);
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:27:52 +08:00
|
|
|
/** \internal concat the the segments with each segment preceded by '/' */
|
2021-10-30 10:06:37 +08:00
|
|
|
int
|
|
|
|
NC_join(NClist* segments, char** pathp)
|
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
int i;
|
|
|
|
NCbytes* buf = NULL;
|
|
|
|
|
|
|
|
if(segments == NULL)
|
|
|
|
{stat = NC_EINVAL; goto done;}
|
|
|
|
if((buf = ncbytesnew())==NULL)
|
|
|
|
{stat = NC_ENOMEM; goto done;}
|
|
|
|
if(nclistlength(segments) == 0)
|
|
|
|
ncbytescat(buf,"/");
|
|
|
|
else for(i=0;i<nclistlength(segments);i++) {
|
|
|
|
const char* seg = nclistget(segments,i);
|
|
|
|
if(seg[0] != '/')
|
|
|
|
ncbytescat(buf,"/");
|
2022-01-25 04:48:41 +08:00
|
|
|
ncbytescat(buf,seg);
|
2021-10-30 10:06:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if(!stat) {
|
|
|
|
if(pathp) *pathp = ncbytesextract(buf);
|
|
|
|
}
|
|
|
|
ncbytesfree(buf);
|
|
|
|
return stat;
|
|
|
|
}
|