schema compilation CHANGE partial support for types
- resolving types
- support for binary type
- support for range/length restriction of the type, but missing checking
that the derived type strictly narrows the defined range/length
diff --git a/src/common.c b/src/common.c
index dab8288..f21c97c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -303,3 +303,59 @@
}
return LY_SUCCESS;
}
+
+LY_ERR
+ly_parse_int(const char *val_str, int64_t min, int64_t max, int base, int64_t *ret)
+{
+ char *strptr;
+
+ LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL);
+
+ /* convert to 64-bit integer, all the redundant characters are handled */
+ errno = 0;
+ strptr = NULL;
+
+ /* parse the value */
+ *ret = strtoll(val_str, &strptr, base);
+ if (errno) {
+ return LY_EVALID;
+ } else if ((*ret < min) || (*ret > max)) {
+ return LY_EDENIED;
+ } else if (strptr && *strptr) {
+ while (isspace(*strptr)) {
+ ++strptr;
+ }
+ if (*strptr) {
+ return LY_EVALID;
+ }
+ }
+ return LY_SUCCESS;
+}
+
+LY_ERR
+ly_parse_uint(const char *val_str, uint64_t max, int base, uint64_t *ret)
+{
+ char *strptr;
+ uint64_t u;
+
+ LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL);
+
+ errno = 0;
+ strptr = NULL;
+ u = strtoull(val_str, &strptr, base);
+ if (errno) {
+ return LY_EVALID;
+ } else if ((u > max) || (u && val_str[0] == '-')) {
+ return LY_EDENIED;
+ } else if (strptr && *strptr) {
+ while (isspace(*strptr)) {
+ ++strptr;
+ }
+ if (*strptr) {
+ return LY_EVALID;
+ }
+ }
+
+ *ret = u;
+ return LY_SUCCESS;
+}