char* ncuribuild(NCURI* duri, const char* prefix, const char* suffix, int flags) { size_t len = 0; char* newuri; char* tmpfile; char* tmpsuffix; char* tmpquery; int nparams = 0; int paramslen = 0; /* if both are specified, prefix has priority */ int withsuffixparams = ((flags&NCURISUFFIXPARAMS)!=0 && duri->params != NULL); int withprefixparams = ((flags&NCURIPREFIXPARAMS)!=0 && duri->params != NULL); int withuserpwd = ((flags&NCURIUSERPWD)!=0 && duri->user != NULL && duri->password != NULL); int withconstraints = ((flags&NCURICONSTRAINTS)!=0 && duri->constraint != NULL); #ifdef NEWESCAPE int encode = (flags&NCURIENCODE); #else int encode = 0; #endif if(prefix != NULL) len += NILLEN(prefix); len += (NILLEN(duri->protocol)+NILLEN("://")); if(withuserpwd) { len += (NILLEN(duri->user)+NILLEN(duri->password)+NILLEN(":@")); } len += (NILLEN(duri->host)); if(duri->port != NULL) { len += (NILLEN(":")+NILLEN(duri->port)); } tmpfile = duri->file; if(encode) tmpfile = ncuriencode(tmpfile,fileallow); len += (NILLEN(tmpfile)); if(suffix != NULL) { tmpsuffix = (char*)suffix; if(encode) tmpsuffix = ncuriencode(tmpsuffix,fileallow); len += (NILLEN(tmpsuffix)); } if(withconstraints) { tmpquery = duri->constraint; if(encode) tmpquery = ncuriencode(tmpquery,queryallow); len += (NILLEN("?")+NILLEN(tmpquery)); } if(withprefixparams || withsuffixparams) { char** p; if(duri->paramlist == NULL) if(!ncuridecodeparams(duri)) return NULL; for(paramslen=0,nparams=0,p=duri->paramlist;*p;p++) { nparams++; paramslen += NILLEN(*p); } if(nparams % 2 == 1) return NULL; /* malformed */ nparams = (nparams / 2); len += paramslen; len += 3*nparams; /* for brackets for every param plus possible = */ if(withsuffixparams) len += strlen("#"); } len += 1; /* null terminator */ newuri = (char*)malloc(len); if(newuri == NULL) return NULL; newuri[0] = EOFCHAR; if(prefix != NULL) strcat(newuri,prefix); if(withprefixparams) { ncappendparams(newuri,duri->paramlist); } if(duri->protocol != NULL) strcat(newuri,duri->protocol); strcat(newuri,"://"); if(withuserpwd) { strcat(newuri,duri->user); strcat(newuri,":"); strcat(newuri,duri->password); strcat(newuri,"@"); } if(duri->host != NULL) { /* may be null if using file: protocol */ strcat(newuri,duri->host); } if(duri->port != NULL) { strcat(newuri,":"); strcat(newuri,duri->port); } if(tmpfile != NULL) { strcat(newuri,tmpfile); if(suffix != NULL) strcat(newuri,tmpsuffix); } if(withconstraints) { strcat(newuri,"?"); strcat(newuri,tmpquery); } if(withsuffixparams & !withprefixparams) { strcat(newuri,"#"); ncappendparams(newuri,duri->paramlist); } return newuri; }
char* nc_uribuild(NC_URI* duri, const char* prefix, const char* suffix, int pieces) { size_t len = 0; char* newuri; int withparams = ((pieces&NC_URIPARAMS) && duri->params != NULL); int withuserpwd = ((pieces&NC_URIUSERPWD) && duri->user != NULL && duri->password != NULL); int withconstraints = ((pieces&NC_URICONSTRAINTS) && duri->constraint != NULL); if(prefix != NULL) len += NILLEN(prefix); if(withparams) { len += NILLEN("[]"); len += NILLEN(duri->params); } len += (NILLEN(duri->protocol)+NILLEN("://")); if(withuserpwd) { len += (NILLEN(duri->user)+NILLEN(duri->password)+NILLEN(":@")); } len += (NILLEN(duri->host)); if(duri->port != NULL) { len += (NILLEN(":")+NILLEN(duri->port)); } len += (NILLEN(duri->file)); if(suffix != NULL) len += NILLEN(suffix); if(withconstraints) { len += (NILLEN("?")+NILLEN(duri->constraint)); } len += 1; /* null terminator */ newuri = (char*)malloc(len); if(!newuri) return NULL; newuri[0] = '\0'; if(prefix != NULL) strcat(newuri,prefix); if(withparams) { strcat(newuri,"["); strcat(newuri,duri->params); strcat(newuri,"]"); } strcat(newuri,duri->protocol); strcat(newuri,"://"); if(withuserpwd) { strcat(newuri,duri->user); strcat(newuri,":"); strcat(newuri,duri->password); strcat(newuri,"@"); } if(duri->host != NULL) { /* may be null if using file: protocol */ strcat(newuri,duri->host); } if(duri->port != NULL) { strcat(newuri,":"); strcat(newuri,duri->port); } strcat(newuri,duri->file); if(suffix != NULL) strcat(newuri,suffix); if(withconstraints) { strcat(newuri,"?"); strcat(newuri,duri->constraint); } return newuri; }