2012-11-30 15:21:02 +00:00
|
|
|
/*
|
2014-05-01 02:11:09 +00:00
|
|
|
* Copyright (C) 2007-2014 Red Hat, Inc.
|
2012-11-30 15:21:02 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-18 16:13:02 +00:00
|
|
|
#pragma once
|
2012-11-30 15:21:02 +00:00
|
|
|
|
2019-06-18 16:13:02 +00:00
|
|
|
#include <stdarg.h>
|
2013-04-03 10:36:23 +00:00
|
|
|
|
2019-06-18 16:13:02 +00:00
|
|
|
#include "internal.h"
|
2018-07-13 17:54:41 +00:00
|
|
|
|
2014-05-12 07:46:37 +00:00
|
|
|
char **virStringSplitCount(const char *string,
|
|
|
|
const char *delim,
|
|
|
|
size_t max_tokens,
|
|
|
|
size_t *tokcount)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2012-11-30 15:21:02 +00:00
|
|
|
char **virStringSplit(const char *string,
|
|
|
|
const char *delim,
|
|
|
|
size_t max_tokens)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2016-11-25 08:18:35 +00:00
|
|
|
char *virStringListJoin(const char **strings,
|
|
|
|
const char *delim)
|
2012-11-30 15:21:02 +00:00
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2018-07-26 14:10:10 +00:00
|
|
|
int virStringListAdd(char ***strings,
|
|
|
|
const char *item);
|
2016-11-28 13:38:58 +00:00
|
|
|
void virStringListRemove(char ***strings,
|
|
|
|
const char *item);
|
2016-11-25 14:43:53 +00:00
|
|
|
|
2017-11-25 08:29:54 +00:00
|
|
|
int virStringListMerge(char ***dst,
|
|
|
|
char ***src);
|
|
|
|
|
2017-09-20 08:41:35 +00:00
|
|
|
int virStringListCopy(char ***dst,
|
|
|
|
const char **src);
|
|
|
|
|
2016-11-25 08:18:35 +00:00
|
|
|
void virStringListFree(char **strings);
|
2019-02-26 15:10:17 +00:00
|
|
|
void virStringListAutoFree(char ***strings);
|
2016-11-25 08:18:35 +00:00
|
|
|
void virStringListFreeCount(char **strings,
|
|
|
|
size_t count);
|
2012-11-30 15:21:02 +00:00
|
|
|
|
2016-11-25 08:18:35 +00:00
|
|
|
bool virStringListHasString(const char **strings,
|
|
|
|
const char *needle);
|
|
|
|
char *virStringListGetFirstWithPrefix(char **strings,
|
|
|
|
const char *prefix)
|
2015-08-03 12:03:23 +00:00
|
|
|
ATTRIBUTE_NONNULL(2);
|
2013-04-12 20:55:45 +00:00
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToLong_i(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
int *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
|
|
|
|
int virStrToLong_ui(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned int *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2014-05-01 02:11:09 +00:00
|
|
|
int virStrToLong_uip(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned int *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToLong_l(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToLong_ul(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2014-05-01 02:11:09 +00:00
|
|
|
int virStrToLong_ulp(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToLong_ll(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
long long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToLong_ull(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned long long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2014-05-01 02:11:09 +00:00
|
|
|
int virStrToLong_ullp(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
2014-07-21 08:17:02 +00:00
|
|
|
unsigned long long *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
int virStrToDouble(char const *s,
|
|
|
|
char **end_ptr,
|
2014-07-21 08:17:02 +00:00
|
|
|
double *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 10:36:23 +00:00
|
|
|
|
2017-06-21 17:08:28 +00:00
|
|
|
int virDoubleToStr(char **strp, double number)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
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);
|
|
|
|
|
2014-10-22 08:26:42 +00:00
|
|
|
bool virStringIsEmpty(const char *str);
|
|
|
|
|
2018-07-20 07:50:37 +00:00
|
|
|
int virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
|
2013-04-03 10:36:23 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2018-07-20 07:50:37 +00:00
|
|
|
int virStrcpy(char *dest, const char *src, size_t destbytes)
|
2013-04-03 10:36:23 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2019-06-18 16:13:02 +00:00
|
|
|
#define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))
|
2013-04-03 12:51:20 +00:00
|
|
|
|
|
|
|
/* Don't call these directly - use the macros below */
|
2019-08-29 14:23:31 +00:00
|
|
|
int virStrdup(char **dest, const char *src)
|
2013-05-07 02:42:44 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
2013-04-03 12:51:20 +00:00
|
|
|
|
2019-08-29 14:23:31 +00:00
|
|
|
int virStrndup(char **dest, const char *src, ssize_t n)
|
2013-05-07 02:42:44 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
2019-08-29 14:23:31 +00:00
|
|
|
int virAsprintfInternal(char **strp, const char *fmt, ...)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3)
|
2013-06-07 15:10:28 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2019-08-29 14:23:31 +00:00
|
|
|
int virVasprintfInternal(char **strp, const char *fmt, va_list list)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0)
|
2013-06-07 15:10:28 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2013-04-03 12:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_STRDUP:
|
|
|
|
* @dst: variable to hold result (char*, not char**)
|
|
|
|
* @src: string to duplicate
|
|
|
|
*
|
2019-09-06 12:10:24 +00:00
|
|
|
* DEPRECATED: use g_strdup instead
|
|
|
|
*
|
2013-04-03 12:51:20 +00:00
|
|
|
* Duplicate @src string and store it into @dst.
|
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* This macro is safe to use on arguments with side effects.
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define VIR_STRDUP(dst, src) virStrdup(&(dst), src)
|
2013-04-03 12:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_STRDUP_QUIET:
|
|
|
|
* @dst: variable to hold result (char*, not char**)
|
|
|
|
* @src: string to duplicate
|
|
|
|
*
|
2019-09-06 12:10:24 +00:00
|
|
|
* DEPRECATED: use g_strdup instead
|
|
|
|
*
|
2013-04-03 12:51:20 +00:00
|
|
|
* Duplicate @src string and store it into @dst.
|
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* This macro is safe to use on arguments with side effects.
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define VIR_STRDUP_QUIET(dst, src) VIR_STRDUP(dst, src)
|
2013-04-03 12:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_STRNDUP:
|
|
|
|
* @dst: variable to hold result (char*, not char**)
|
|
|
|
* @src: string to duplicate
|
|
|
|
* @n: the maximum number of bytes to copy
|
|
|
|
*
|
2019-09-06 12:10:24 +00:00
|
|
|
* DEPRECATED: use g_strndup instead
|
|
|
|
*
|
2013-04-03 12:51:20 +00:00
|
|
|
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
2013-05-24 08:45:57 +00:00
|
|
|
* only @n bytes are copied and terminating null byte '\0' is added. If @n
|
|
|
|
* is a negative number, then the whole @src string is copied. That is,
|
|
|
|
* VIR_STRDUP(dst, src) and VIR_STRNDUP(dst, src, -1) are equal.
|
2013-04-03 12:51:20 +00:00
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* This macro is safe to use on arguments with side effects.
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define VIR_STRNDUP(dst, src, n) virStrndup(&(dst), src, n)
|
2013-04-03 12:51:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_STRNDUP_QUIET:
|
|
|
|
* @dst: variable to hold result (char*, not char**)
|
|
|
|
* @src: string to duplicate
|
|
|
|
* @n: the maximum number of bytes to copy
|
|
|
|
*
|
2019-09-06 12:10:24 +00:00
|
|
|
* DEPRECATED: use g_strndup instead
|
|
|
|
*
|
2013-04-03 12:51:20 +00:00
|
|
|
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
2013-05-24 08:45:57 +00:00
|
|
|
* only @n bytes are copied and terminating null byte '\0' is added. If @n
|
|
|
|
* is a negative number, then the whole @src string is copied. That is,
|
|
|
|
* VIR_STRDUP_QUIET(dst, src) and VIR_STRNDUP_QUIET(dst, src, -1) are
|
|
|
|
* equal.
|
2013-04-03 12:51:20 +00:00
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* This macro is safe to use on arguments with side effects.
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n)
|
2013-04-25 16:05:00 +00:00
|
|
|
|
2016-02-09 17:18:49 +00:00
|
|
|
size_t virStringListLength(const char * const *strings);
|
2013-04-25 16:05:00 +00:00
|
|
|
|
2013-06-07 15:10:28 +00:00
|
|
|
/**
|
|
|
|
* virVasprintf
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Like glibc's vasprintf but aborts on OOM
|
2013-06-07 15:10:28 +00:00
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns number of bytes printed on success, aborts on OOM
|
2013-06-07 15:10:28 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define virVasprintf(strp, fmt, list) virVasprintfInternal(strp, fmt, list)
|
2013-06-07 15:10:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virVasprintfQuiet
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Like glibc's vasprintf but aborts on OOM.
|
2013-06-07 15:10:28 +00:00
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns number of bytes printed on success, aborts on OOM
|
2013-06-07 15:10:28 +00:00
|
|
|
*/
|
2019-08-29 14:23:31 +00:00
|
|
|
#define virVasprintfQuiet(strp, fmt, list) virVasprintf(strp, fmt, list)
|
2013-06-07 15:10:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virAsprintf:
|
|
|
|
* @strp: variable to hold result (char **)
|
|
|
|
* @fmt: printf format
|
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Like glibc's asprintf but aborts on OOM.
|
2013-06-07 15:10:28 +00:00
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns number of bytes printed on success, aborts on OOM
|
2013-06-07 15:10:28 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-29 14:23:31 +00:00
|
|
|
#define virAsprintf(strp, ...) virAsprintfInternal(strp, __VA_ARGS__)
|
2013-06-07 15:10:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virAsprintfQuiet:
|
|
|
|
* @strp: variable to hold result (char **)
|
|
|
|
* @fmt: printf format
|
|
|
|
*
|
2018-06-11 10:46:21 +00:00
|
|
|
* Like glibc's asprintf but makes sure *strp == NULL on failure.
|
2013-06-07 15:10:28 +00:00
|
|
|
*
|
2019-08-29 14:23:31 +00:00
|
|
|
* Returns number of bytes printed on success, aborts on OOM
|
2013-06-07 15:10:28 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-29 14:23:31 +00:00
|
|
|
#define virAsprintfQuiet(strp, ...) virAsprintf(strp, __VA_ARGS__)
|
2013-06-07 15:10:28 +00:00
|
|
|
|
2013-11-28 11:14:59 +00:00
|
|
|
int virStringSortCompare(const void *a, const void *b);
|
|
|
|
int virStringSortRevCompare(const void *a, const void *b);
|
2015-06-15 16:53:58 +00:00
|
|
|
int virStringToUpper(char **dst, const char *src);
|
2013-11-28 11:14:59 +00:00
|
|
|
|
2014-01-23 09:28:29 +00:00
|
|
|
ssize_t virStringSearch(const char *str,
|
|
|
|
const char *regexp,
|
|
|
|
size_t max_results,
|
|
|
|
char ***matches)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
|
|
|
|
|
2017-05-11 15:16:13 +00:00
|
|
|
bool virStringMatch(const char *str,
|
|
|
|
const char *regexp);
|
|
|
|
|
2014-02-19 20:30:46 +00:00
|
|
|
char *virStringReplace(const char *haystack,
|
|
|
|
const char *oldneedle,
|
|
|
|
const char *newneedle)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
|
|
|
|
|
2019-03-06 16:05:03 +00:00
|
|
|
bool virStringHasSuffix(const char *str,
|
|
|
|
const char *suffix);
|
2019-03-07 09:17:19 +00:00
|
|
|
bool virStringHasCaseSuffix(const char *str,
|
|
|
|
const char *suffix);
|
2019-03-07 09:18:38 +00:00
|
|
|
bool virStringStripSuffix(char *str,
|
|
|
|
const char *suffix) ATTRIBUTE_RETURN_CHECK;
|
2019-03-07 09:18:58 +00:00
|
|
|
bool virStringMatchesNameSuffix(const char *file,
|
|
|
|
const char *name,
|
|
|
|
const char *suffix);
|
2019-03-06 14:07:26 +00:00
|
|
|
|
2014-10-07 15:27:40 +00:00
|
|
|
void virStringStripIPv6Brackets(char *str);
|
2017-04-26 14:26:53 +00:00
|
|
|
bool virStringHasChars(const char *str,
|
|
|
|
const char *chars);
|
2015-04-14 10:30:16 +00:00
|
|
|
bool virStringHasControlChars(const char *str);
|
|
|
|
void virStringStripControlChars(char *str);
|
2017-12-18 14:46:53 +00:00
|
|
|
void virStringFilterChars(char *str, const char *valid);
|
2014-10-07 15:27:40 +00:00
|
|
|
|
2016-04-11 11:35:25 +00:00
|
|
|
bool virStringIsPrintable(const char *str);
|
2016-05-12 15:43:39 +00:00
|
|
|
bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen);
|
2016-04-11 11:35:25 +00:00
|
|
|
|
2017-05-10 08:16:11 +00:00
|
|
|
void virStringTrimOptionalNewline(char *str);
|
2017-03-29 11:48:22 +00:00
|
|
|
|
2017-07-13 13:31:50 +00:00
|
|
|
int virStringParsePort(const char *str,
|
2017-07-20 10:42:53 +00:00
|
|
|
unsigned int *port)
|
2017-07-13 13:31:50 +00:00
|
|
|
ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
2019-03-13 06:30:10 +00:00
|
|
|
int virStringParseYesNo(const char *str,
|
|
|
|
bool *result)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2019-02-26 15:10:17 +00:00
|
|
|
/**
|
|
|
|
* VIR_AUTOSTRINGLIST:
|
|
|
|
*
|
|
|
|
* Declares a NULL-terminated list of strings which will be automatically freed
|
|
|
|
* when the pointer goes out of scope.
|
|
|
|
*/
|
2019-06-18 16:13:02 +00:00
|
|
|
#define VIR_AUTOSTRINGLIST \
|
2019-02-26 15:10:17 +00:00
|
|
|
__attribute__((cleanup(virStringListAutoFree))) char **
|