On 05/04/2018 04:21 PM, Stefan Berger wrote:
> Implement virStringFilterLines() that takes as an input a buffer with text
> and extracts each line that contains a given needle. The size of each re-
> turned line can be restricted and if it is restricted '...' will automa-
> tically be appended.
>
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> ---
> src/util/virstring.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> src/util/virstring.h | 3 +++
> 2 files changed, 65 insertions(+)
>
? No src/libvirt_private.syms change to add the new function?
> diff --git a/src/util/virstring.c b/src/util/virstring.c
> index 15f367a..f1d91c7 100644
> --- a/src/util/virstring.c
> +++ b/src/util/virstring.c
> @@ -1499,3 +1499,65 @@ virStringParsePort(const char *str,
>
> return 0;
> }
Similar to 1/14 - 2 blank lines...
> +
> +/**
> + * virStringFilterLines:
I think this is more ExtractLines
as compared to virStringFilterChars which filters out certain chars from
a buffer and returns a buffer with those filtered out.
> + * @input: input buffer with text
> + * @needle: the needle to search in each line
> + * @maxlinelen: maximum line length of each line in output buffer;
> + * 0 to not restrict
> + *
> + * Search for a given needle in each line of the input buffer and create
> + * an output buffer that contains only these line.
Seems to be an incomplete thought, but I can adjust. So given Extract
vs. Filter:
* Search for a given @needle in each line of the @input buffer and
* create a return buffer that contains only those lines with the
* @needle. Each output buffer line can be further restricted by
* providing @maxlinelen in which case the output line would be
* truncated @maxlinelen with "..." appended to the line to indicate
* the truncation.
*
* Returns NULL on failure or a buffer with the extracted lines. It
* is up to the caller to free the returned buffer.
> + */
> +char *
> +virStringFilterLines(char *input, const char *needle, size_t maxlinelen)
... and 1 line per argument.
> +{
> + char *sol = input;
> + char *eol;
> + char *buf = NULL;
> + size_t buflen = 1, llen;
> + const char *dots = "...";
> +
See below [1]
+ if (!input || !needle) {
+ virReportError(VIR_ERR_INALID_ARG,
+ _("neither input=%s nor needle=%s can be NULL"),
+ NULLSTR(input), NULLSTR(needle));
+ return NULL;
+ }
+
> + while (*sol) {
> + eol = strchr(sol, '\n');
> + if (eol)
> + *eol = 0;
> +
> + if (strstr(sol, needle)) {
> + size_t additional = 0;
> +
> + llen = strlen(sol);
> + if (maxlinelen && llen > maxlinelen) {
> + llen = maxlinelen;
> + additional = strlen(dots);
> + }
> +
> + if (VIR_REALLOC_N(buf, buflen + llen + additional + 1) < 0) {
> + VIR_FREE(buf);
> + if (*eol)
Ran the patch through Coverity and it complained right here... I think
you meant "if (eol)", right?
> + *eol = '\n';
> + return NULL;
> + }
> + strncpy(&buf[buflen - 1], sol, llen);
> + buflen += llen;
> +
> + if (additional) {
> + strncpy(&buf[buflen - 1], dots, additional);
> + buflen += additional;
> + }
> +
> + strcpy(&buf[buflen - 1], "\n");
> + buflen += 1;
> + }
> +
> + if (eol)
> + *eol = '\n';
> + else
> + break;
> +
> + sol = eol + 1;
> + }
> +
> + return buf;
> +}
> diff --git a/src/util/virstring.h b/src/util/virstring.h
> index fa2ec1d..1fb9851 100644
> --- a/src/util/virstring.h
> +++ b/src/util/virstring.h
> @@ -309,4 +309,7 @@ int virStringParsePort(const char *str,
> unsigned int *port)
> ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>
> +char *virStringFilterLines(char *input, const char *needle, size_t maxlinelen)
> + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
[1]
Unfortunately all this does is ensure no one calls this with (NULL,
NULL,...)... In practice, if @input or @needle is assigned to NULL, the
compiler won't catch it.
With the adjustments,
Reviewed-by: John Ferlan <jferlan@redhat.com>
But like patch 1, I do have some other concerns later in patch 7 when
this is used.
John
> +
> #endif /* __VIR_STRING_H__ */
>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list