summaryrefslogtreecommitdiff
path: root/src/libstddjb/fmtscan-internal.h
blob: f6c33917f35fbd1b5e6124d03e316ccc4b8919b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ISC license. */

#ifndef FMTSCAN_INTERNAL_H
#define FMTSCAN_INTERNAL_H

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>

#define SCANB0(bits) \
size_t uint##bits##0_scan_base (char const *s, uint##bits##_t *u, uint8_t base) \
{ \
  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 ? ERANGE : EINVAL ; \
  return 0 ; \
} \

#define SCANS0(bits) \
size_t int##bits##0_scan_base (char const *s, int##bits##_t *d, uint8_t base) \
{ \
  size_t pos = int##bits##_scan(s, d) ; \
  if (!pos) return (errno = EINVAL, 0) ; \
  if (!s[pos]) return pos ; \
  errno = (fmtscan_num(s[pos], base) < base) ? ERANGE : EINVAL ; \
  return 0 ; \
} \

#define SCANL(bits) \
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++) \
  { \
    size_t w = uint##bits##_scan(s + len, tab + i) ; \
    if (!w) break ; \
    len += w ; \
    while (memchr(",:; \t\r\n", 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) \
{ \
  size_t i = 0, len = 0 ; \
  for (; s[len] && (i < max) ; i++) \
  { \
    size_t w = int##bits##_scan(s + len, tab + i) ; \
    if (!w) break ; \
    len += w ; \
    while (memchr(",:; \t\r\n", s[len], 7)) len++ ; \
  } \
  *num = i ; \
  return len ; \
} \

#define FMTL(bits) \
static uint64_t get (void const *tab, size_t i) \
{ \
  return ((uint##bits##_t const *)tab)[i] ; \
} \
size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
{ \
  return uint64_fmtlist_generic(s, tab, n, 10, &get) ; \
} \

#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++) \
  { \
    size_t w = int##bits##_fmt(s, tab[i]) ; \
    len += w ; \
    if (s) s += w ; \
    if (i < n-1) { len++ ; if (s) *s++ = ',' ; } \
  } \
  return len ; \
} \

#endif