[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 08/18] cutils: qemu_strtou32 function added
From: |
Nicolas Eder |
Subject: |
[PATCH v5 08/18] cutils: qemu_strtou32 function added |
Date: |
Wed, 20 Dec 2023 17:25:45 +0100 |
---
include/qemu/cutils.h | 2 ++
util/cutils.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+)
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 5ab1a4ffb0..14f492ba61 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -158,6 +158,8 @@ int qemu_strtoul(const char *nptr, const char **endptr, int
base,
unsigned long *result);
int qemu_strtoi64(const char *nptr, const char **endptr, int base,
int64_t *result);
+int qemu_strtou32(const char *nptr, const char **endptr, int base,
+ uint32_t *result);
int qemu_strtou64(const char *nptr, const char **endptr, int base,
uint64_t *result);
int qemu_strtod(const char *nptr, const char **endptr, double *result);
diff --git a/util/cutils.c b/util/cutils.c
index 42364039a5..5e00a4ec14 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -665,6 +665,36 @@ int qemu_strtoi64(const char *nptr, const char **endptr,
int base,
return check_strtox_error(nptr, ep, endptr, *result == 0, errno);
}
+/**
+ * Convert string @nptr to an uint32_t.
+ *
+ * Works like qemu_strtoul(), except it stores UINT32_MAX on overflow.
+ * (If you want to prohibit negative numbers that wrap around to
+ * positive, use parse_uint()).
+ */
+int qemu_strtou32(const char *nptr, const char **endptr, int base,
+ uint32_t *result)
+{
+ char *ep;
+
+ assert((unsigned) base <= 36 && base != 1);
+ if (!nptr) {
+ *result = 0;
+ if (endptr) {
+ *endptr = nptr;
+ }
+ return -EINVAL;
+ }
+
+ errno = 0;
+ *result = strtoul(nptr, &ep, base);
+ /* Windows returns 1 for negative out-of-range values. */
+ if (errno == ERANGE) {
+ *result = -1;
+ }
+ return check_strtox_error(nptr, ep, endptr, *result == 0, errno);
+}
+
/**
* Convert string @nptr to an uint64_t.
*
--
2.34.1
- [PATCH v5 00/18] first version of mcdstub, Nicolas Eder, 2023/12/20
- [PATCH v5 01/18] gdbstub, mcdstub: file and build structure adapted to accomodate for the mcdstub, Nicolas Eder, 2023/12/20
- [PATCH v5 11/18] mcdstub: system and core queries added, Nicolas Eder, 2023/12/20
- [PATCH v5 02/18] gdbstub: hex conversion functions moved to cutils.h, Nicolas Eder, 2023/12/20
- [PATCH v5 10/18] mcdstub: open and close server functions added, Nicolas Eder, 2023/12/20
- [PATCH v5 03/18] gdbstub: GDBRegisterState moved to gdbstub.h so it can be used outside of the gdbstub, Nicolas Eder, 2023/12/20
- [PATCH v5 05/18] mcdstub: memory helper functions added, Nicolas Eder, 2023/12/20
- [PATCH v5 04/18] gdbstub: DebugClass added to system mode., Nicolas Eder, 2023/12/20
- [PATCH v5 07/18] mcdstub: mcdserver initialization functions added, Nicolas Eder, 2023/12/20
- [PATCH v5 08/18] cutils: qemu_strtou32 function added,
Nicolas Eder <=
- [PATCH v5 12/18] mcdstub: all core specific queries added, Nicolas Eder, 2023/12/20
- [PATCH v5 06/18] mcdstub: -mcd start option added, mcd specific defines added, Nicolas Eder, 2023/12/20
- [PATCH v5 09/18] mcdstub: TCP packet plumbing added, Nicolas Eder, 2023/12/20
- [PATCH v5 13/18] mcdstub: go, step and break added, Nicolas Eder, 2023/12/20
- [PATCH v5 15/18] mcdstub: skeleton for reset handling added, Nicolas Eder, 2023/12/20
- [PATCH v5 14/18] mcdstub: state query added, Nicolas Eder, 2023/12/20
- [PATCH v5 16/18] mcdstub: register access added, Nicolas Eder, 2023/12/20
- [PATCH v5 17/18] mcdstub: memory access added, Nicolas Eder, 2023/12/20
- [PATCH v5 18/18] mcdstub: break/watchpoints added, Nicolas Eder, 2023/12/20