Added xml_template_apply

This commit is contained in:
Olof hagsand 2024-02-19 12:20:20 +01:00
parent 9bc60abe2e
commit b551e9e5c7
4 changed files with 134 additions and 0 deletions

View file

@ -121,6 +121,72 @@ clicon_strsep(char *string,
return vec;
}
/*! Split string using start and stop delimiter strings usable for variable substitution
*
* Example: "foo ${NAME} bar"
* where delim1="${" and delim2="}"
* returns vec: "foo ", "NAME", "bar"
* Both delim1 and delim2 must match
* @param[in] str
* @param[in] delim1 prefix delimiter string
* @param[in] delim2 postfix delimiter string
* @param[out] cvp Created cligen variable vector, deallocate w cvec_free
* @retval 0 OK
* @retval -1 Error
*/
int
clixon_strsep2(char *str,
char *delim1,
char *delim2,
char ***vcp,
int *nvec)
{
int retval = -1;
size_t sz;
char **vec = NULL;
char *s1;
char *s2;
int nr = 0;
char *ptr;
int i;
s1 = str;
while ((s1 = strstr(s1, delim1)) != NULL){
if ((s2 = strstr(s1+strlen(delim1), delim2)) != NULL)
nr += 2;
s1 = s2 + strlen(delim2);
}
/* alloc vector and append copy of string */
sz = (nr+1)* sizeof(char*) + strlen(str)+1;
if ((vec = (char**)malloc(sz)) == NULL){
clixon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(vec, 0, sz);
ptr = (char*)vec + (nr+1)* sizeof(char*); /* this is where ptr starts */
strcpy(ptr, str);
i = 0;
s1 = ptr;
vec[i++] = ptr;
while ((s1 = strstr(s1, delim1)) != NULL){
if ((s2 = strstr(s1+strlen(delim1), delim2)) != NULL){
*s1 = '\0';
*s2 = '\0';
vec[i++] = s1 + strlen(delim1);
vec[i++] = s2 + strlen(delim2);
}
s1 = s2 + strlen(delim2);
}
*vcp = vec;
ptr = NULL;
*nvec = i;
retval = 0;
done:
if (ptr)
free(ptr);
return retval;
}
/*! Concatenate elements of a string array into a string.
*
* An optional delimiter string can be specified which will be inserted between