[Pdbg] [PATCH v2 1/2] optcmd: Add parser for 8-bit power of 2 integers
Alistair Popple
alistair at popple.id.au
Tue Dec 11 16:05:25 AEDT 2018
Signed-off-by: Alistair Popple <alistair at popple.id.au>
---
src/parsers.c | 26 ++++++++++++++++++++++++++
src/parsers.h | 1 +
2 files changed, 27 insertions(+)
diff --git a/src/parsers.c b/src/parsers.c
index 868c383..8575784 100644
--- a/src/parsers.c
+++ b/src/parsers.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
@@ -55,6 +56,31 @@ uint16_t *parse_number16(const char *argv)
return n;
}
+/* Parse an 8-bit number that is a power of 2 */
+uint8_t *parse_number8_pow2(const char *argv)
+{
+ uint8_t *n;
+ unsigned long int tmp, i;
+ char *endptr;
+
+ if (!argv)
+ return NULL;
+
+ errno = 0;
+ tmp = strtoul(argv, &endptr, 0);
+ if (tmp >= 256)
+ return NULL;
+
+ for (i = tmp; i; i >>= 1)
+ if ((i & 1) && (i >> 1))
+ return NULL;
+
+ n = malloc(sizeof(*n));
+ *n = tmp;
+
+ return n;
+}
+
/* Parse a GPR number, returning an error if it's greater than 32 */
int *parse_gpr(const char *argv)
{
diff --git a/src/parsers.h b/src/parsers.h
index 935c242..ba52178 100644
--- a/src/parsers.h
+++ b/src/parsers.h
@@ -16,6 +16,7 @@
uint64_t *parse_number64(const char *argv);
uint32_t *parse_number32(const char *argv);
uint16_t *parse_number16(const char *argv);
+uint8_t *parse_number8_pow2(const char *argv);
int *parse_gpr(const char *argv);
int *parse_spr(const char *argv);
bool *parse_flag_noarg(const char *argv);
--
2.11.0
More information about the Pdbg
mailing list