Add three aliases virBoolYesNo, virBoolOnOff and virBoolTrueFalse for
type 'bool'. The tool xmlgen depends on them to determine their ture
values in XML. Also, add corresponding functions to parse them.
Add virStrToTime and virTimeFormatBuf to convert 'time_t' and 'string'.
Add virStrToLong_u8p to convert 'string' to 'uint_8'.
Signed-off-by: Shi Lei <shi_lei(a)massclouds.com>
---
src/internal.h | 8 +++
src/libvirt_private.syms | 5 ++
src/util/virstring.c | 102 +++++++++++++++++++++++++++++++++++++++
src/util/virstring.h | 15 ++++++
4 files changed, 130 insertions(+)
diff --git a/src/internal.h b/src/internal.h
index 0a03dfc4..3342934f 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -529,3 +529,11 @@ enum {
# define fprintf(fh, ...) g_fprintf(fh, __VA_ARGS__)
#endif /* VIR_NO_GLIB_STDIO */
+
+/* These are aliases for bool.
+ * The xmlgen depends on them to determine their true values
+ * in XML.
+ */
+typedef bool virBoolYesNo; /* True values: 'yes', 'no' */
+typedef bool virBoolOnOff; /* True values: 'on', 'off' */
+typedef bool virBoolTrueFalse; /* True values: 'true', 'false' */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index fad0ff71..e78491dc 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3275,6 +3275,9 @@ virStringStripIPv6Brackets;
virStringStripSuffix;
virStringToUpper;
virStringTrimOptionalNewline;
+virStrToBoolOnOff;
+virStrToBoolTrueFalse;
+virStrToBoolYesNo;
virStrToDouble;
virStrToLong_i;
virStrToLong_l;
@@ -3285,6 +3288,8 @@ virStrToLong_ul;
virStrToLong_ull;
virStrToLong_ullp;
virStrToLong_ulp;
+virStrToTime;
+virTimeFormatBuf;
virTrimSpaces;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index a2c07e5c..ac6c0aae 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -233,6 +233,21 @@ virStrToLong_ulp(char const *s, char **end_ptr, int base,
return 0;
}
+/* Just like virStrToLong_uip, above, but produce an "uint8_t" value.
+ * This version rejects any negative signs. */
+int
+virStrToLong_u8p(char const *s, char **end_ptr, int base, uint8_t *result)
+{
+ unsigned int val;
+ int ret;
+ ret = virStrToLong_uip(s, end_ptr, base, &val);
+ if (ret < 0 || val > 0xff)
+ return -1;
+
+ *result = (uint8_t) val;
+ return 0;
+}
+
/* Just like virStrToLong_i, above, but produce a "long long" value. */
int
virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result)
@@ -1077,3 +1092,90 @@ int virStringParseYesNo(const char *str, bool *result)
return 0;
}
+
+
+int
+virStrToBoolYesNo(const char *str, bool *result)
+{
+ if (STREQ(str, "yes"))
+ *result = true;
+ else if (STREQ(str, "no"))
+ *result = false;
+ else
+ return -1;
+
+ return 0;
+}
+
+
+int
+virStrToBoolOnOff(const char *str, bool *result)
+{
+ if (STREQ(str, "on"))
+ *result = true;
+ else if (STREQ(str, "off"))
+ *result = false;
+ else
+ return -1;
+
+ return 0;
+}
+
+
+int
+virStrToBoolTrueFalse(const char *str, bool *result)
+{
+ if (STREQ(str, "true"))
+ *result = true;
+ else if (STREQ(str, "false"))
+ *result = false;
+ else
+ return -1;
+
+ return 0;
+}
+
+
+int virStrToTime(const char *str, time_t *result)
+{
+ g_autoptr(GDateTime) then = NULL;
+ g_autoptr(GTimeZone) tz = g_time_zone_new_utc();
+ char *tmp;
+ int year, mon, mday, hour, min, sec;
+
+ /* Expect: YYYY-MM-DDTHH:MM:SS (%d-%d-%dT%d:%d:%d) eg 2010-11-28T14:29:01 */
+ if (/* year */
+ virStrToLong_i(str, &tmp, 10, &year) < 0 || *tmp != '-' ||
+ /* month */
+ virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp != '-' ||
+ /* day */
+ virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp != 'T' ||
+ /* hour */
+ virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp != ':' ||
+ /* minute */
+ virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp != ':' ||
+ /* second */
+ virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0 || *tmp != '\0') {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("invalid time '%s', expect
YYYY-MM-DDTHH:MM:SS"),
+ str);
+ return -1;
+ }
+
+ then = g_date_time_new(tz, year, mon, mday, hour, min, sec);
+ *result = (time_t)g_date_time_to_unix(then);
+ return 0;
+}
+
+
+int
+virTimeFormatBuf(virBuffer *buf, const char *layout, const time_t time)
+{
+ g_autoptr(GDateTime) then = NULL;
+ g_autofree char *thenstr = NULL;
+
+ then = g_date_time_new_from_unix_utc(time);
+ thenstr = g_date_time_format(then, "%Y-%m-%dT%H:%M:%S");
+ virBufferAsprintf(buf, layout, thenstr);
+ return 0;
+}
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 7cc1d8c5..e16da824 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -19,6 +19,7 @@
#pragma once
#include "internal.h"
+#include "virbuffer.h"
#define VIR_INT64_STR_BUFLEN 21
@@ -59,6 +60,11 @@ int virStrToLong_ulp(char const *s,
int base,
unsigned long *result)
G_GNUC_WARN_UNUSED_RESULT;
+int virStrToLong_u8p(char const *s,
+ char **end_ptr,
+ int base,
+ uint8_t *result)
+ G_GNUC_WARN_UNUSED_RESULT;
int virStrToLong_ll(char const *s,
char **end_ptr,
int base,
@@ -141,3 +147,12 @@ int virStringParsePort(const char *str,
int virStringParseYesNo(const char *str,
bool *result)
G_GNUC_WARN_UNUSED_RESULT;
+
+int virStrToBoolYesNo(const char *str, bool *result);
+int virStrToBoolOnOff(const char *str, bool *result);
+int virStrToBoolTrueFalse(const char *str, bool *result);
+
+int virStrToTime(const char *str, time_t *result)
+ G_GNUC_WARN_UNUSED_RESULT;
+
+int virTimeFormatBuf(virBuffer *buf, const char *layout, const time_t time);
--
2.25.1