PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Simple Libraries
Social
libbadgetools
sscan_ct.c
1
/*
2
* Super-simple text I/O for PropGCC, stripped of all stdio overhead.
3
* Copyright (c) 2012, Ted Stefanik. Concept inspired by:
4
*
5
* very simple printf, adapted from one written by me [Eric Smith]
6
* for the MiNT OS long ago
7
* placed in the public domain
8
* - Eric Smith
9
* Propeller specific adaptations
10
* Copyright (c) 2011 Parallax, Inc.
11
* Written by Eric R. Smith, Total Spectrum Software Inc.
12
*
13
* MIT licensed (see terms at end of file)
14
*/
15
16
#include "
badgetools.h
"
17
#include <ctype.h>
18
#include <limits.h>
19
#include <stdarg.h>
20
#include <stdlib.h>
21
#include "
simpletext.h
"
22
23
static
const
char
* trim_ct(
const
char
* str);
24
static
const
char
* _scanf_gets_ct(
const
char
*str,
char
* dst,
unsigned
width,
int
gettingChars);
25
int
_doscanf_ct(
const
char
* str,
const
char
*fmt, va_list args);
26
27
static
const
char
* trim_ct(
const
char
* str)
28
{
30
while
(!
isprint
(*str))
31
str++;
32
33
return
str;
34
}
35
36
static
const
char
* _scanf_gets_ct(
const
char
*str,
char
* dst,
unsigned
width,
int
gettingChars)
37
{
39
while
(width-- && *str && (gettingChars ||
isprint
(*str)))
40
*dst++ = *str++;
41
42
if
(!gettingChars)
43
*dst = 0;
44
45
return
str;
46
}
47
48
int
_doscanf_ct(
const
char
* str,
const
char
*fmt, va_list args)
49
{
50
int
blocks = 0;
51
52
int
fch;
53
while
(str && *str && (fch = *fmt++))
54
{
55
int
width = -1;
56
int
base = 16;
57
int
isWhiteSpaceOK = 0;
58
int
done = 0;
59
60
if
(fch !=
'%'
)
61
{
62
if
(
isspace
(fch))
63
str = trim_ct(str);
64
else
if
(*str++ != fch)
65
break
;
66
continue
;
67
}
68
69
if
(!
isdigit
(*fmt))
70
width = ULONG_MAX;
71
else
72
fmt = _scanf_getl(fmt, &width, 10, 11, 0);
73
74
fch = *fmt++;
75
if
(fch !=
'c'
&& fch !=
'%'
)
76
{
77
str = trim_ct(str);
78
if
(!*str)
79
break
;
80
}
81
82
switch
(fch)
83
{
84
case
'%'
:
85
if
(*str++ !=
'%'
)
86
done = 1;
87
break
;
88
89
case
'c'
:
90
isWhiteSpaceOK = 1;
91
if
(width == ULONG_MAX)
92
width = 1;
93
/* Fall Through */
94
case
's'
: {
95
char
* arg = va_arg(args,
char
*);
96
if
((str = _scanf_gets_ct(str, arg, width, isWhiteSpaceOK)))
97
blocks++;
98
break
;
99
}
100
case
'b'
: {
101
int
* arg = va_arg(args,
int
*);
102
if
((str = _scanf_getl(str, arg, 2, width, (fch ==
'b'
))))
103
blocks++;
104
break
;
105
}
106
107
case
'u'
:
108
case
'd'
:
109
base = 10;
/* Fall Through */
110
case
'x'
: {
111
int
* arg = va_arg(args,
int
*);
112
if
((str = _scanf_getl(str, arg, base, width, (fch ==
'd'
))))
113
blocks++;
114
break
;
115
}
116
117
case
'e'
:
118
case
'f'
:
119
case
'g'
: {
120
float
* arg = va_arg(args,
float
*);
121
if
((str = _scanf_getf(str, arg)))
122
blocks++;
123
break
;
124
}
125
126
default
: {
127
done = 1;
128
}
129
}
130
131
if
(done)
132
break
;
133
}
134
135
/* Sometimes the last arg can have data, but should not.
136
* This can happen if a buffer is recycled without clearing.
137
* Just zero the last arg.
138
{ int* endarg = va_arg(args, int*);
139
*endarg = 0;
140
}
141
*/
142
143
return
blocks;
144
}
145
146
147
/* +--------------------------------------------------------------------
148
* | TERMS OF USE: MIT License
149
* +--------------------------------------------------------------------
150
* Permission is hereby granted, free of charge, to any person obtaining
151
* a copy of this software and associated documentation files
152
* (the "Software"), to deal in the Software without restriction,
153
* including without limitation the rights to use, copy, modify, merge,
154
* publish, distribute, sublicense, and/or sell copies of the Software,
155
* and to permit persons to whom the Software is furnished to do so,
156
* subject to the following conditions:
157
*
158
* The above copyright notice and this permission notice shall be
159
* included in all copies or substantial portions of the Software.
160
*
161
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
162
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
163
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
164
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
165
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
166
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
167
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
168
* +--------------------------------------------------------------------
169
*/
170
171
172
/*
173
* Super-simple text I/O for PropGCC, stripped of all stdio overhead.
174
* Copyright (c) 2012, Ted Stefanik. Concept inspired by:
175
*
176
* very simple printf, adapted from one written by me [Eric Smith]
177
* for the MiNT OS long ago
178
* placed in the public domain
179
* - Eric Smith
180
* Propeller specific adaptations
181
* Copyright (c) 2011 Parallax, Inc.
182
* Written by Eric R. Smith, Total Spectrum Software Inc.
183
*
184
* MIT licensed (see terms at end of file)
185
*/
186
#include <stdlib.h>
187
#include "
simpletext.h
"
188
189
int
sscan_ct(
const
char
*str,
const
char
*fmt, ...)
190
{
191
va_list args;
192
193
va_start(args, fmt);
194
int
blocks = _doscanf_ct(str, fmt, args);
195
va_end(args);
196
197
return
blocks;
198
}
199
200
201
/* +--------------------------------------------------------------------
202
* | TERMS OF USE: MIT License
203
* +--------------------------------------------------------------------
204
* Permission is hereby granted, free of charge, to any person obtaining
205
* a copy of this software and associated documentation files
206
* (the "Software"), to deal in the Software without restriction,
207
* including without limitation the rights to use, copy, modify, merge,
208
* publish, distribute, sublicense, and/or sell copies of the Software,
209
* and to permit persons to whom the Software is furnished to do so,
210
* subject to the following conditions:
211
*
212
* The above copyright notice and this permission notice shall be
213
* included in all copies or substantial portions of the Software.
214
*
215
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
216
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
217
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
218
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
219
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
220
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
221
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
222
* +--------------------------------------------------------------------
223
*/
isspace
#define isspace(c)
Definition:
ctype.h:119
isdigit
#define isdigit(c)
Definition:
ctype.h:83
isprint
#define isprint(c)
Definition:
ctype.h:107
simpletext.h
This library provides a collection of functions for communicating with text devices such as SimpleIDE...
badgetools.h
This library provides convenient functions for a variety of Parallax eBadge operations.
Generated on Fri May 20 2022 16:48:05 for PropWare by
1.8.17