int NC_urlmodel(const char* path, int cmode, char** newurl, NCmode* model) { int stat = NC_NOERR; int found = 0; struct NCPROTOCOLLIST* protolist; NCURI* url = NULL; const char** fragp = NULL; if(path == NULL) return 0; /* Parse the url */ if(ncuriparse(path,&url) != NCU_OK) return NC_EINVAL; /* Not parseable as url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(url->protocol,protolist->protocol) == 0) { found = 1; break; } } if(found) { model->implementation = protolist->implementation; /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(url,protolist->substitute); } else {stat = NC_EINVAL; goto done;} /* Again, does not look like a url */ /* Iterate over the url fragment parameters */ for(fragp=ncurifragmentparams(url);fragp && *fragp;fragp+=2) { const char* name = fragp[0]; const char* value = fragp[1]; if(strcmp(name,"protocol")==0) name = value; if(strcasecmp(name,"dap2") == 0) { model->format = NC_FORMAT_NC3; model->implementation = NC_FORMATX_DAP2; /* No need to set iosp field */ } else if(strcasecmp(name,"dap4") == 0) { model->format = NC_FORMAT_NETCDF4; model->implementation = NC_FORMATX_DAP4; /* No need to set iosp field */ } else if(strcmp(name,"mode")==0) { if((stat = url_getmodel(value,model))) goto done; } } if(model->implementation == 0) {/* Last resort: use the cmode */ /* If mode specifies netcdf-4, then this is assumed to be dap4 */ if(cmode & NC_NETCDF4) { model->implementation = NC_FORMATX_DAP4; } else {/* Default is DAP2 */ model->implementation = NC_FORMATX_DAP2; } } if(model->implementation == 0) goto done; /* could not interpret */ switch (model->implementation) { case NC_FORMATX_NC3: model->format = NC_FORMAT_NC3; break; case NC_FORMATX_NC4: model->format = NC_FORMAT_NETCDF4; break; case NC_FORMATX_DAP2: model->format = NC_FORMAT_NC3; break; case NC_FORMATX_DAP4: model->format = NC_FORMAT_NETCDF4; break; case NC_FORMATX_ZARR: model->format = NC_FORMAT_NETCDF4; break; default: stat = NC_EINVAL; goto done; } done: if(newurl) *newurl = ncuribuild(url,NULL,NULL,NCURIALL); if(url) ncurifree(url); return stat; }
static int processuri(const char* path, NCURI** urip, char** newpathp, NClist* modeargs) { int stat = NC_NOERR; int found = 0; const char** fragp = NULL; struct NCPROTOCOLLIST* protolist; NCURI* uri = NULL; size_t pathlen = strlen(path); if(path == NULL || pathlen == 0) {stat = NC_EURL; goto done;} /* Defaults */ if(newpathp) *newpathp = NULL; if(urip) *urip = NULL; if(ncuriparse(path,&uri) != NCU_OK) goto done; /* not url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(uri->protocol,protolist->protocol) == 0) { found = 1; break; } } if(!found) {stat = NC_EINVAL; goto done;} /* unrecognized URL form */ /* process the corresponding mode arg */ if(protolist->mode != NULL) nclistpush(modeargs,strdup(protolist->mode)); /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(uri,protolist->substitute); /* Iterate over the url fragment parameters */ for(fragp=ncurifragmentparams(uri);fragp && *fragp;fragp+=2) { const char* name = fragp[0]; const char* value = fragp[1]; if(strcmp(name,"protocol")==0) { nclistpush(modeargs,strdup(value)); } else if(strcmp(name,"mode")==0) { if((stat = parseurlmode(value,modeargs))) goto done; } else if(issingleton(name) && (value == NULL || strlen(value)==0)) { nclistpush(modeargs,strdup(name)); } /*else ignore*/ } /* At this point modeargs should contain all mode args from the URL */ /* Rebuild the path (including fragment)*/ if(newpathp) *newpathp = ncuribuild(uri,NULL,NULL,NCURIALL); if(urip) { *urip = uri; uri = NULL; } done: if(uri != NULL) ncurifree(uri); return check(stat); }
int NC_urlmodel(const char* path, int mode, char** newurl) { int found, model = 0; struct NCPROTOCOLLIST* protolist; NCURI* url = NULL; char* p; if(path == NULL) return 0; /* find leading non-blank */ for(p=(char*)path;*p;p++) {if(*p != ' ') break;} /* Do some initial checking to see if this looks like a file path */ if(*p == '/') return 0; /* probably an absolute file path */ /* Parse the url */ if(ncuriparse(path,&url) != NCU_OK) goto fail; /* Not parseable as url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(url->protocol,protolist->protocol) == 0) { found = 1; break; } } if(found) { model = protolist->model; /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(url,protolist->substitute); } else goto fail; /* Again, does not look like a url */ if(model != NC_FORMATX_DAP2 && model != NC_FORMATX_DAP4) { /* Look for and of the following params: "dap2", "protocol=dap2", "dap4", "protocol=dap4" */ const char* proto = NULL; const char* match = NULL; if((proto=ncurilookup(url,"protocol")) == NULL) proto = NULL; if(proto == NULL) proto = ""; if((match=ncurilookup(url,"dap2")) != NULL || strcmp(proto,"dap2") == 0) model = NC_FORMATX_DAP2; else if((match=ncurilookup(url,"dap4")) != NULL || strcmp(proto,"dap4") == 0) model = NC_FORMATX_DAP4; else model = 0; /* Still don't know */ } if(model == 0) {/* Last resort: use the mode */ /* If mode specifies netcdf-4, then this is assumed to be dap4 */ if(mode & NC_NETCDF4) model = NC_FORMATX_DAP4; else model = NC_FORMATX_DAP2; /* Default */ } if(newurl) *newurl = ncuribuild(url,NULL,NULL,NCURIALL); if(url) ncurifree(url); return model; fail: if(url) ncurifree(url); return 0; }