fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
scan.c
Go to the documentation of this file.
1#include "scan.h"
2
10static long int fromhex(unsigned char c)
11{
12 if (c>='0' && c<='9')
13 return c-'0';
14 else if (c>='A' && c<='F')
15 return c-'A'+10;
16 else if (c>='a' && c<='f')
17 return c-'a'+10;
18 return -1;
19}
20
21unsigned int scan_0x(const char *s,unsigned int *u)
22{
23 unsigned int pos = 0;
24 unsigned long result = 0;
25 long int c;
26
27 while ((c = fromhex((unsigned char) (s[pos]))) >= 0) {
28 result = (result << 4) + c;
29 ++pos;
30 }
31 *u = result;
32 return pos;
33}
34
35unsigned int scan_8long(const char *s,unsigned long *u)
36{
37 unsigned int pos = 0;
38 unsigned long result = 0;
39 unsigned long c;
40
41 while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 8) {
42 result = result * 8 + c;
43 ++pos;
44 }
45 *u = result;
46 return pos;
47}
48
49unsigned int scan_uint(const char *s,unsigned int *u)
50{
51 unsigned int pos;
52 unsigned long result;
53
54 pos = scan_ulong(s,&result);
55 *u = result;
56 return pos;
57}
58
59unsigned int scan_plusminus(const char *s,int *sign)
60{
61 if (*s == '+') { *sign = 1; return 1; }
62 if (*s == '-') { *sign = -1; return 1; }
63 *sign = 1; return 0;
64}
65
66unsigned int scan_long(const char *s,long *i)
67{
68 int sign;
69 unsigned long u;
70 unsigned int len;
71
72 len = scan_plusminus(s,&sign); s += len;
73 len += scan_ulong(s,&u);
74 if (sign < 0) *i = -u; else *i = u;
75 return len;
76}
77
78
79unsigned int scan_ulong(const char *s,unsigned long *u)
80{
81 unsigned int pos = 0;
82 unsigned long result = 0;
83 unsigned long c;
84
85 while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
86 result = result * 10 + c;
87 ++pos;
88 }
89 *u = result;
90 return pos;
91}
92
93unsigned int scan_xlong(const char *s,unsigned long *u)
94{
95 const char *t = s;
96 int l = 0;
97 unsigned char c;
98
99 while ((c = fromhex(*t)) < 16) {
100 l = (l<<4)+c;
101 ++t;
102 }
103 *u=l;
104 return t-s;
105}
106
107unsigned int scan_xint(const char *s,unsigned int *i)
108{
109 const char *t = s;
110 unsigned int l = 0;
111 unsigned char c;
112
113 while ((l >> (sizeof(l)*8 - 4)) == 0
114 && (c = (unsigned char)fromhex((unsigned char)*t))<16) {
115 l= (l << 4) + c;
116 ++t;
117 }
118 *i = l;
119 return (unsigned int)(t-s);
120}
unsigned int scan_xlong(const char *s, unsigned long *u)
Definition: scan.c:93
unsigned int scan_uint(const char *s, unsigned int *u)
Definition: scan.c:49
unsigned int scan_ulong(const char *s, unsigned long *u)
Definition: scan.c:79
unsigned int scan_plusminus(const char *s, int *sign)
Definition: scan.c:59
unsigned int scan_xint(const char *s, unsigned int *i)
Definition: scan.c:107
unsigned int scan_long(const char *s, long *i)
Definition: scan.c:66
unsigned int scan_8long(const char *s, unsigned long *u)
Definition: scan.c:35
unsigned int scan_0x(const char *s, unsigned int *u)
Definition: scan.c:21
int fromhex(unsigned char)
Definition: fmt.c:79