summaryrefslogtreecommitdiff
path: root/src/libstddjb/fmtscan-internal.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb/fmtscan-internal.h')
-rw-r--r--src/libstddjb/fmtscan-internal.h117
1 files changed, 96 insertions, 21 deletions
diff --git a/src/libstddjb/fmtscan-internal.h b/src/libstddjb/fmtscan-internal.h
index b1cac3e..3c9f214 100644
--- a/src/libstddjb/fmtscan-internal.h
+++ b/src/libstddjb/fmtscan-internal.h
@@ -3,17 +3,19 @@
#ifndef FMTSCAN_INTERNAL_H
#define FMTSCAN_INTERNAL_H
+#include <sys/types.h>
+#include <stdint.h>
#include <errno.h>
#include <limits.h>
#include <skalibs/bytestr.h>
#include <skalibs/fmtscan.h>
#define SCANB(bits) \
-unsigned int uint##bits##_scan_base (char const *s, uint##bits *u, unsigned char base) \
+size_t uint##bits##_scan_base (char const *s, uint##bits##_t *u, unsigned int base) \
{ \
- static uint##bits const max = (uint##bits)(-1) ; \
- uint##bits result = 0 ; \
- unsigned int pos = 0 ; \
+ static uint##bits##_t const max = UINT##bits##_MAX ; \
+ uint##bits##_t result = 0 ; \
+ size_t pos = 0 ; \
for (;; pos++) \
{ \
register unsigned char c = fmtscan_num(s[pos], base) ; \
@@ -25,42 +27,42 @@ unsigned int uint##bits##_scan_base (char const *s, uint##bits *u, unsigned char
} \
#define SCANB0(bits) \
-unsigned int uint##bits##0_scan_base (char const *s, uint##bits *u, unsigned char base) \
+size_t uint##bits##0_scan_base (char const *s, uint##bits##_t *u, unsigned int base) \
{ \
- register unsigned int pos = uint##bits##_scan_base(s, u, base) ; \
+ register size_t pos = uint##bits##_scan_base(s, u, base) ; \
if (!pos) return (errno = EINVAL, 0) ; \
if (!s[pos]) return pos ; \
errno = (fmtscan_num(s[pos], base) < base) ? EDOM : EINVAL ; \
return 0 ; \
} \
-#define SCANS(type, cstname) \
-unsigned int type##_scan (char const *s, type *n) \
+#define SCANS(bits) \
+size_t int##bits##_scan (char const *s, int##bits##_t *n) \
{ \
- unsigned type tmp ; \
- register unsigned int r = 0 ; \
+ uint##bits##_t tmp ; \
+ register size_t r = 0 ; \
register unsigned int sign = 0 ; \
if (*s == '-') \
{ \
- r = 1 + u##type##_scan(s+1, &tmp) ; \
+ r = 1 + uint##bits##_scan(s+1, &tmp) ; \
if (r == 1) return 0 ; \
if (tmp == 0) *n = 0 ; \
else \
{ \
- if (tmp-1 > -(cstname##_MIN+1)) \
+ if (tmp-1 > -(INT##bits##_MIN+1)) \
{ \
tmp /= 10 ; \
r-- ; \
} \
- *n = cstname##_MIN + (-(cstname##_MIN+1) - (tmp-1)) ; \
+ *n = INT##bits##_MIN + (-(INT##bits##_MIN+1) - (tmp-1)) ; \
} \
return r ; \
} \
if (*s == '+') (s++, sign++) ; \
- r = u##type##_scan(s, &tmp) ; \
+ r = uint##bits##_scan(s, &tmp) ; \
if (!r) return 0 ; \
r += sign ; \
- if (tmp > cstname##_MAX) \
+ if (tmp > INT##bits##_MAX) \
{ \
tmp /= 10 ; \
r-- ; \
@@ -69,13 +71,38 @@ unsigned int type##_scan (char const *s, type *n) \
return r ; \
} \
+#define SCANS0(bits) \
+size_t int##bits##0_scan (char const *s, int##bits##_t *u) \
+{ \
+ register size_t pos = int##bits##_scan(s, u) ; \
+ if (!pos) return (errno = EINVAL, 0) ; \
+ if (!s[pos]) return pos ; \
+ errno = (fmtscan_num(s[pos], 10) < 10) ? EDOM : EINVAL ; \
+ return 0 ; \
+} \
+
#define SCANL(bits) \
-unsigned int uint##bits##_scanlist (uint##bits *tab, unsigned int max, char const *s, unsigned int *num) \
+size_t uint##bits##_scanlist (uint##bits##_t *tab, size_t max, char const *s, size_t *num) \
+{ \
+ size_t i = 0, len = 0 ; \
+ for (; s[len] && (i < max) ; i++) \
+ { \
+ register size_t w = uint##bits##_scan(s + len, tab + i) ; \
+ if (!w) break ; \
+ len += w ; \
+ while (byte_chr(",:; \t\r\n", 7, s[len]) < 7) len++ ; \
+ } \
+ *num = i ; \
+ return len ; \
+} \
+
+#define SCANSL(bits) \
+size_t int##bits##_scanlist (int##bits##_t *tab, size_t max, char const *s, size_t *num) \
{ \
- unsigned int i = 0, len = 0 ; \
+ size_t i = 0, len = 0 ; \
for (; s[len] && (i < max) ; i++) \
{ \
- register unsigned int w = uint##bits##_scan(s + len, tab + i) ; \
+ register size_t w = int##bits##_scan(s + len, tab + i) ; \
if (!w) break ; \
len += w ; \
while (byte_chr(",:; \t\r\n", 7, s[len]) < 7) len++ ; \
@@ -85,12 +112,12 @@ unsigned int uint##bits##_scanlist (uint##bits *tab, unsigned int max, char cons
} \
#define FMTL(bits) \
-unsigned int uint##bits##_fmtlist (char *s, uint##bits const *tab, unsigned int n) \
+size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
{ \
- unsigned int i = 0, len = 0 ; \
+ size_t i = 0, len = 0 ; \
for (; i < n ; i++) \
{ \
- register unsigned int w = uint##bits##_fmt(s, tab[i]) ; \
+ register size_t w = uint##bits##_fmt(s, tab[i]) ; \
len += w ; \
if (s) \
{ \
@@ -101,5 +128,53 @@ unsigned int uint##bits##_fmtlist (char *s, uint##bits const *tab, unsigned int
return len ; \
} \
+#define FMTB(bits) \
+size_t uint##bits##_fmt_base (char *s, uint##bits##_t x, unsigned int base) \
+{ \
+ register size_t len = 1 ; \
+ { \
+ register uint##bits##_t q = x ; \
+ while (q >= base) { len++ ; q /= base ; } \
+ } \
+ if (s) \
+ { \
+ s += len ; \
+ do { *--s = fmtscan_asc(x % base) ; x /= base ; } while (x) ; \
+ } \
+ return len ; \
+} \
+
+#define FMTB0(bits) \
+size_t uint##bits##0_fmt_base (char *s, uint##bits##_t x, register size_t n, unsigned int base) \
+{ \
+ register size_t len = uint##bits##_fmt_base(0, x, base) ; \
+ while (n-- > len) *s++ = '0' ; \
+ return uint##bits##_fmt_base(s, x, base) ; \
+} \
+
+#define FMTS(bits) \
+size_t int##bits##_fmt (char *fmt, int##bits##_t n) \
+{ \
+ if (n >= 0) return uint##bits##_fmt(fmt, n) ; \
+ if (fmt) *fmt++ = '-' ; \
+ return 1 + uint##bits##_fmt(fmt, -n) ; \
+} \
+
+#define FMTSL(bits) \
+size_t int##bits##_fmtlist (char *s, int##bits##_t const *tab, size_t n) \
+{ \
+ size_t i = 0, len = 0 ; \
+ for (; i < n ; i++) \
+ { \
+ register size_t w = int##bits##_fmt(s, tab[i]) ; \
+ len += w ; \
+ if (s) \
+ { \
+ s += w ; \
+ if (i < n-1) { *s++ = ',' ; len++ ; } \
+ } \
+ } \
+ return len ; \
+} \
#endif