skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

fmtscan-internal.h (2203B)


      1 /* ISC license. */
      2 
      3 #ifndef FMTSCAN_INTERNAL_H
      4 #define FMTSCAN_INTERNAL_H
      5 
      6 #include <stdint.h>
      7 #include <string.h>
      8 #include <errno.h>
      9 #include <limits.h>
     10 
     11 #include <skalibs/uint64.h>
     12 #include <skalibs/fmtscan.h>
     13 
     14 #define SCANB0(bits) \
     15 size_t uint##bits##0_scan_base (char const *s, uint##bits##_t *u, uint8_t base) \
     16 { \
     17   size_t pos = uint##bits##_scan_base(s, u, base) ; \
     18   if (!pos) return (errno = EINVAL, 0) ; \
     19   if (!s[pos]) return pos ; \
     20   errno = fmtscan_num(s[pos], base) < base ? ERANGE : EINVAL ; \
     21   return 0 ; \
     22 } \
     23 
     24 #define SCANS0(bits) \
     25 size_t int##bits##0_scan_base (char const *s, int##bits##_t *d, uint8_t base) \
     26 { \
     27   size_t pos = int##bits##_scan(s, d) ; \
     28   if (!pos) return (errno = EINVAL, 0) ; \
     29   if (!s[pos]) return pos ; \
     30   errno = (fmtscan_num(s[pos], base) < base) ? ERANGE : EINVAL ; \
     31   return 0 ; \
     32 } \
     33 
     34 #define SCANL(bits) \
     35 size_t uint##bits##_scanlist (uint##bits##_t *tab, size_t max, char const *s, size_t *num) \
     36 { \
     37   size_t i = 0, len = 0 ; \
     38   for (; s[len] && (i < max) ; i++) \
     39   { \
     40     size_t w = uint##bits##_scan(s + len, tab + i) ; \
     41     if (!w) break ; \
     42     len += w ; \
     43     while (memchr(",:; \t\r\n", s[len], 7)) len++ ; \
     44   } \
     45   *num = i ; \
     46   return len ; \
     47 } \
     48 
     49 #define SCANSL(bits) \
     50 size_t int##bits##_scanlist (int##bits##_t *tab, size_t max, char const *s, size_t *num) \
     51 { \
     52   size_t i = 0, len = 0 ; \
     53   for (; s[len] && (i < max) ; i++) \
     54   { \
     55     size_t w = int##bits##_scan(s + len, tab + i) ; \
     56     if (!w) break ; \
     57     len += w ; \
     58     while (memchr(",:; \t\r\n", s[len], 7)) len++ ; \
     59   } \
     60   *num = i ; \
     61   return len ; \
     62 } \
     63 
     64 #define FMTL(bits) \
     65 static uint64_t get (void const *tab, size_t i) \
     66 { \
     67   return ((uint##bits##_t const *)tab)[i] ; \
     68 } \
     69 size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
     70 { \
     71   return uint64_fmtlist_generic(s, tab, n, 10, &get) ; \
     72 } \
     73 
     74 #define FMTSL(bits) \
     75 size_t int##bits##_fmtlist (char *s, int##bits##_t const *tab, size_t n) \
     76 { \
     77   size_t i = 0, len = 0 ; \
     78   for (; i < n ; i++) \
     79   { \
     80     size_t w = int##bits##_fmt(s, tab[i]) ; \
     81     len += w ; \
     82     if (s) s += w ; \
     83     if (i < n-1) { len++ ; if (s) *s++ = ',' ; } \
     84   } \
     85   return len ; \
     86 } \
     87 
     88 #endif