* Added namespace-context parameter nsc to xpath_first and xpath_vec, (xpath_vec_nsc and xpath_first_nsc` are removed).

This commit is contained in:
Olof hagsand 2019-12-07 20:46:13 +01:00
parent ba2f40728b
commit d68a829862
32 changed files with 186 additions and 318 deletions

View file

@ -228,7 +228,7 @@ xpath_tree_print(FILE *f,
/*! Create an xpath string from an xpath tree, ie "unparsing"
* @param[in] xs XPATH tree
* @param[out] xpath Xpath string as CLIgen buf
* @param[out] xpath XPath string as CLIgen buf
* @see xpath_tree_print
* @note NOT COMPLETE, just simple xpaths eg a/b
*/
@ -329,7 +329,7 @@ xpath_tree_free(xpath_tree *xs)
/*! Given xpath, parse it, and return structured xpath tree
* @param[in] xpath String with XPATH 1.0 syntax
* @param[out] xptree Xpath-tree, parsed, structured XPATH, free:xpath_tree_free
* @param[out] xptree XPath-tree, parsed, structured XPATH, free:xpath_tree_free
* @retval 0 OK
* @retval -1 Error
* @code
@ -428,7 +428,7 @@ xpath_vec_ctx(cxobj *xcur,
return retval;
}
/*! Xpath nodeset function where only the first matching entry is returned
/*! XPath nodeset function where only the first matching entry is returned
*
* @param[in] xcur XML tree where to search
* @param[in] nsc External XML namespace context, or NULL
@ -439,7 +439,7 @@ xpath_vec_ctx(cxobj *xcur,
* @code
* cxobj *x;
* cvec *nsc; // namespace context
* if ((x = xpath_first_nsc(xtop, nsc, "//symbol/foo")) != NULL) {
* if ((x = xpath_first(xtop, nsc, "//symbol/foo")) != NULL) {
* ...
* }
* @endcode
@ -448,65 +448,8 @@ xpath_vec_ctx(cxobj *xcur,
* @see also xpath_vec.
*/
cxobj *
xpath_first_nsc(cxobj *xcur,
cvec *nsc,
char *xpformat,
...)
{
cxobj *cx = NULL;
va_list ap;
size_t len;
char *xpath = NULL;
xp_ctx *xr = NULL;
va_start(ap, xpformat);
len = vsnprintf(NULL, 0, xpformat, ap);
va_end(ap);
/* allocate a message string exactly fitting the message length */
if ((xpath = malloc(len+1)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
/* second round: compute write message from reason and args */
va_start(ap, xpformat);
if (vsnprintf(xpath, len+1, xpformat, ap) < 0){
clicon_err(OE_UNIX, errno, "vsnprintf");
va_end(ap);
goto done;
}
va_end(ap);
if (xpath_vec_ctx(xcur, nsc, xpath, &xr) < 0)
goto done;
if (xr && xr->xc_type == XT_NODESET && xr->xc_size)
cx = xr->xc_nodeset[0];
done:
if (xr)
ctx_free(xr);
if (xpath)
free(xpath);
return cx;
}
/*! Xpath nodeset function where only the first matching entry is returned
*
* @param[in] xcur XML tree where to search
* @param[in] xpformat Format string for XPATH syntax
* @retval xml-tree XML tree of first match
* @retval NULL Error or not found
*
* @code
* cxobj *x;
* if ((x = xpath_first(xtop, "//symbol/foo")) != NULL) {
* ...
* }
* @endcode
* @note the returned pointer points into the original tree so should not be freed after use.
* @note return value does not see difference between error and not found
* @see also xpath_vec.
* @see xpath_first_nsc which is more generic with namespace context
*/
cxobj *
xpath_first(cxobj *xcur,
cvec *nsc,
char *xpformat,
...)
{
@ -532,7 +475,7 @@ xpath_first(cxobj *xcur,
goto done;
}
va_end(ap);
if (xpath_vec_ctx(xcur, NULL, xpath, &xr) < 0)
if (xpath_vec_ctx(xcur, nsc, xpath, &xr) < 0)
goto done;
if (xr && xr->xc_type == XT_NODESET && xr->xc_size)
cx = xr->xc_nodeset[0];
@ -557,7 +500,7 @@ xpath_first(cxobj *xcur,
* cvec *nsc; // namespace context
* cxobj **vec;
* size_t veclen;
* if (xpath_vec_nsc(xcur, nsc, "//symbol/foo", &vec, &veclen) < 0)
* if (xpath_vec(xcur, nsc, "//symbol/foo", &vec, &veclen) < 0)
* goto err;
* for (i=0; i<veclen; i++){
* xn = vec[i];
@ -567,76 +510,8 @@ xpath_first(cxobj *xcur,
* @endcode
*/
int
xpath_vec_nsc(cxobj *xcur,
cvec *nsc,
char *xpformat,
cxobj ***vec,
size_t *veclen,
...)
{
int retval = -1;
va_list ap;
size_t len;
char *xpath = NULL;
xp_ctx *xr = NULL;
va_start(ap, veclen);
len = vsnprintf(NULL, 0, xpformat, ap);
va_end(ap);
/* allocate a message string exactly fitting the message length */
if ((xpath = malloc(len+1)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
/* second round: compute write message from reason and args */
va_start(ap, veclen);
if (vsnprintf(xpath, len+1, xpformat, ap) < 0){
clicon_err(OE_UNIX, errno, "vsnprintf");
va_end(ap);
goto done;
}
va_end(ap);
*vec=NULL;
*veclen = 0;
if (xpath_vec_ctx(xcur, nsc, xpath, &xr) < 0)
goto done;
if (xr && xr->xc_type == XT_NODESET){
*vec = xr->xc_nodeset;
xr->xc_nodeset = NULL;
*veclen = xr->xc_size;
}
retval = 0;
done:
if (xr)
ctx_free(xr);
if (xpath)
free(xpath);
return retval;
}
/*! Given XML tree and xpath, returns nodeset as xml node vector
* If result is not nodeset, return empty nodeset
* @param[in] xcur xml-tree where to search
* @param[in] xpformat Format string for XPATH syntax
* @param[out] vec vector of xml-trees. Vector must be free():d after use
* @param[out] veclen returns length of vector in return value
* @retval 0 OK
* @retval -1 Error
* @code
* cxobj **vec;
* size_t veclen;
* if (xpath_vec(xcur, "//symbol/foo", &vec, &veclen) < 0)
* goto err;
* for (i=0; i<veclen; i++){
* xn = vec[i];
* ...
* }
* free(vec);
* @endcode
* @see xpath_vec_nsc which is more generic with namespace context
*/
int
xpath_vec(cxobj *xcur,
cvec *nsc,
char *xpformat,
cxobj ***vec,
size_t *veclen,
@ -666,7 +541,7 @@ xpath_vec(cxobj *xcur,
va_end(ap);
*vec=NULL;
*veclen = 0;
if (xpath_vec_ctx(xcur, NULL, xpath, &xr) < 0)
if (xpath_vec_ctx(xcur, nsc, xpath, &xr) < 0)
goto done;
if (xr && xr->xc_type == XT_NODESET){
*vec = xr->xc_nodeset;
@ -682,7 +557,7 @@ xpath_vec(cxobj *xcur,
return retval;
}
/* Xpath that returns a vector of matches (only nodes marked with flags)
/* XPath that returns a vector of matches (only nodes marked with flags)
* @param[in] xcur xml-tree where to search
* @param[in] xpformat Format string for XPATH syntax
* @param[in] nsc External XML namespace context, or NULL