diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0fab8a65c238bfc7ae77c85f6b96cb66b3a6e785..28a34045f7ed6727fbc18f81374a87267dc23992 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1032,6 +1032,7 @@ virSetNonBlock; virSetUIDGID; virSkipSpaces; virSkipSpacesAndBackslash; +virSkipSpacesBackwards; virStrToDouble; virStrToLong_i; virStrToLong_l; @@ -1043,6 +1044,7 @@ virStrcpy; virStrncpy; virTimeMs; virTimestamp; +virTrimSpaces; virVasprintf; diff --git a/src/util/util.c b/src/util/util.c index c0d8f57ec0dd477167e2620f3dca606e745c693e..55425577d344b8fe4a8a51946df35159be917100 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -1561,6 +1561,62 @@ virSkipSpacesAndBackslash(const char **str) *str = cur; } +/** + * virTrimSpaces: + * @str: string to modify to remove all trailing spaces + * @endp: track the end of the string + * + * If @endp is NULL on entry, then all spaces prior to the trailing + * NUL in @str are removed, by writing NUL into the appropriate + * location. If @endp is non-NULL but points to a NULL pointer, + * then all spaces prior to the trailing NUL in @str are removed, + * NUL is written to the new string end, and endp is set to the + * location of the (new) string end. If @endp is non-NULL and + * points to a non-NULL pointer, then that pointer is used as + * the end of the string, endp is set to the (new) location, but + * no NUL pointer is written into the string. + */ +void +virTrimSpaces(char *str, char **endp) +{ + char *end; + + if (!endp || !*endp) + end = str + strlen(str); + else + end = *endp; + while (end > str && c_isspace(end[-1])) + end--; + if (endp) { + if (!*endp) + *end = '\0'; + *endp = end; + } else { + *end = '\0'; + } +} + +/** + * virSkipSpacesBackwards: + * @str: start of string + * @endp: on entry, *endp must be NULL or a location within @str, on exit, + * will be adjusted to skip trailing spaces, or to NULL if @str had nothing + * but spaces. + */ +void +virSkipSpacesBackwards(const char *str, char **endp) +{ + /* Casting away const is safe, since virTrimSpaces does not + * modify string with this particular usage. */ + char *s = (char*) str; + + if (!*endp) + *endp = s + strlen(s); + virTrimSpaces(s, endp); + if (s == *endp) + *endp = NULL; +} + /** * virParseNumber: * @str: pointer to the char pointer used diff --git a/src/util/util.h b/src/util/util.h index ccc32fed686d9373a16e802725e4bf95a7016ea7..7a1eb11523672c20400ff5d2766e52a626f04c35 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -166,8 +166,12 @@ int virHexToBin(unsigned char c); int virMacAddrCompare (const char *mac1, const char *mac2); -void virSkipSpaces(const char **str); -void virSkipSpacesAndBackslash(const char **str); +void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1); +void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1); +void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1); +void virSkipSpacesBackwards(const char *str, char **endp) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + int virParseNumber(const char **str); int virParseVersionString(const char *str, unsigned long *version, bool allowMissing);