From 1696178edf8121ab680baa121fa2b851d1a78cd8 Mon Sep 17 00:00:00 2001 From: Michal Vasko Date: Wed, 1 Nov 2023 10:55:36 +0100 Subject: [PATCH] xpath UPDATE do not use memstream Not supported on Windows. --- src/xpath.c | 64 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/src/xpath.c b/src/xpath.c index da1107d43..309150beb 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -219,17 +219,53 @@ str2axis(const char *str, uint32_t str_len) } /** - * @brief Print the whole expression \p exp to debug output. + * @brief Append a string to a dynamic string variable. + * + * @param[in,out] str String to use. + * @param[in,out] size String size. + * @param[in,out] used String used size excluding terminating zero. + * @param[in] format Message format. + * @param[in] ... Message format arguments. + */ +static void +print_expr_str(char **str, size_t *size, size_t *used, const char *format, ...) +{ + int p; + va_list ap; + + va_start(ap, format); + + /* try to append the string */ + p = vsnprintf(*str + *used, *size - *used, format, ap); + + if ((unsigned)p >= *size - *used) { + /* realloc */ + *str = ly_realloc(*str, *size + p + 1); + *size += p + 1; + + /* restart ap */ + va_end(ap); + va_start(ap, format); + + /* print */ + p = vsnprintf(*str + *used, *size - *used, format, ap); + } + + *used += p; + va_end(ap); +} + +/** + * @brief Print the whole expression @p exp to debug output. * * @param[in] exp Expression to use. */ static void print_expr_struct_debug(const struct lyxp_expr *exp) { - char *tmp; + char *buf = NULL; uint32_t i, j; - FILE *fp; - size_t s; + size_t size = 0, used = 0; if (!exp || (ly_ll < LY_LLDBG)) { return; @@ -237,21 +273,21 @@ print_expr_struct_debug(const struct lyxp_expr *exp) LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); for (i = 0; i < exp->used; ++i) { - fp = open_memstream(&tmp, &s); - fprintf(fp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i], - &exp->expr[exp->tok_pos[i]]); + print_expr_str(&buf, &size, &used, "\ttoken %s, in expression \"%.*s\"", + lyxp_token2str(exp->tokens[i]), exp->tok_len[i], &exp->expr[exp->tok_pos[i]]); + if (exp->repeat && exp->repeat[i]) { - fprintf(fp, " (repeat %d", exp->repeat[i][0]); + print_expr_str(&buf, &size, &used, " (repeat %d", exp->repeat[i][0]); for (j = 1; exp->repeat[i][j]; ++j) { - fprintf(fp, ", %d", exp->repeat[i][j]); + print_expr_str(&buf, &size, &used, ", %d", exp->repeat[i][j]); } - fprintf(fp, ")"); + print_expr_str(&buf, &size, &used, ")"); } - fflush(fp); - LOGDBG(LY_LDGXPATH, tmp); - fclose(fp); - free(tmp); + LOGDBG(LY_LDGXPATH, buf); + used = 0; } + + free(buf); } #ifndef NDEBUG