-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc_scan.c
216 lines (202 loc) · 3.78 KB
/
c_scan.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* mini-scanf - Minimal scanf() implementation for embedded projects.
* Copyright (c) 2023 Aleksej Muratov
*/
#include "c_scan.h"
// implementation of basic dependencies
// std
int c_isspace(const int c)
{
switch (c)
{ /* in the "C" locale: */
case ' ': /* space */
case '\f': /* form feed */
case '\n': /* new-line */
case '\r': /* carriage return */
case '\t': /* horizontal tab */
case '\v': /* vertical tab */
return 1;
default:
return 0;
}
}
// std
int c_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
else
return (0);
}
// parodies the standard
#ifdef C_SSCANF
#define NEXTCHAR (PointBuf++)
#define CURCHAR (buff[PointBuf])
int c_sscanf(const char* buff, char* format, ...)
#else
#define NEXTCHAR (charBuf=c_getch())
#define CURCHAR (charBuf)
int c_scanf(char* format, ...)
#endif
{
int count = 0;
#ifdef C_SSCANF
int PointBuf = 0;
#else
char charBuf = c_getch();
#endif
int PointFt = 0;
va_list ap;
va_start(ap, format);
while (format && format[PointFt]) // Read format
{
if (format[PointFt] == '%')
{
PointFt++;
// for %*
bool save = true;
if (format[PointFt] == '*')
{
save = false;
PointFt++;
}
// for %1234567890
unsigned len = 0;
bool lenEn = false;
while (c_isdigit(format[PointFt]))
{
lenEn = true;
len *= 10;
len += (format[PointFt] - '0');
PointFt++;
}
// for %[]
char stop[LENSCANS];
unsigned stopN = 0;
if (format[PointFt] == '[')
{
while (format[PointFt] != ']')
{
if (format[PointFt] != '[')
{
stop[stopN] = format[PointFt];
stopN++;
}
PointFt++;
}
}
// %?
switch (format[PointFt])
{
case 'c':
while (c_isspace(CURCHAR)) // ignore isspace (std)
NEXTCHAR; //
if (save)
*(char*)va_arg(ap, char*) = CURCHAR;
NEXTCHAR;
//if (save) // ignore %* (std)
count++;
break;
case 'u':
case 'd':
int sign = 1;
while (!c_isdigit(CURCHAR))
{
if (CURCHAR == '+' || CURCHAR == '-')
if (CURCHAR == '-')
//if(format[PointFt] != 'u') // ignore sign (no std)
sign = -1;
NEXTCHAR;
}
long value = 0;
while(c_isdigit(CURCHAR) && (lenEn != true || len > 0))
{
value *= 10;
value += (int)(CURCHAR - '0');
NEXTCHAR;
len--;
}
if (save)
*(int*)va_arg(ap, int*) = value * sign;
//if (save) // ignore %* (std)
count++;
break;
case ']':
case 's':
char* t = save ? va_arg(ap, char*) : NULL;
while (c_isspace(CURCHAR)) // ignor isspace (std)
NEXTCHAR; //
while (true)
{
bool con = false;
if (stopN != 0)
{
bool invert = (stop[0] == '^');
con = !invert;
for (unsigned i = (invert ? 1 : 0); i < stopN; i++)
if (stop[i] == CURCHAR)
{
con = invert;
break;
}
if (con == true)
break;
}
if (!c_isspace(CURCHAR) || (!con && stopN != 0) && (lenEn != true || len > 0))
{
if (save)
*t = CURCHAR;
NEXTCHAR;
t++;
len--;
}
else
break;
}
// add \0
{
if (save)
*t = '\0';
t++;
}
//if (save) // ignore %* (std)
count++;
break;
}
#ifndef C_SSCANF
if(format[PointFt] != 'c'
&& format[PointFt] != 'u'
&& format[PointFt] != 'd')
c_getbackch(CURCHAR);
#endif
}
//else // drop char in buff (no std)
// NEXTCHAR; //
PointFt++;
}
va_end(ap);
return count;
}
// custom
char backch = 0;
char c_getch()
{
if(backch == 0)
return (char)getch();
else
{
char tmp = backch;
backch = 0;
return tmp;
}
}
// new
bool c_getbackch(char b)
{
char tmp = backch;
backch = b;
if (tmp != 0)
return 1;
else
return 0;
}