PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Simple Libraries
TextDevices
libsimpletext
doscanf.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
#include <ctype.h>
16
#include <limits.h>
17
#include <stdarg.h>
18
#include <stdlib.h>
19
#include "
simpletext.h
"
20
21
static
const
char
* trim(
const
char
* str)
22
{
23
while
(
isspace
(*str))
24
str++;
25
26
return
str;
27
}
28
29
static
const
char
* _scanf_gets(
const
char
*str,
char
* dst,
unsigned
width,
int
gettingChars)
30
{
31
while
(width-- && *str && (gettingChars || !
isspace
(*str)))
32
*dst++ = *str++;
33
34
if
(!gettingChars)
35
*dst = 0;
36
37
return
str;
38
}
39
40
int
_doscanf(
const
char
* str,
const
char
*fmt, va_list args)
41
{
42
int
blocks = 0;
43
44
int
fch;
45
while
(str && *str && (fch = *fmt++))
46
{
47
int
width = -1;
48
int
base = 16;
49
int
isWhiteSpaceOK = 0;
50
int
done = 0;
51
52
if
(fch !=
'%'
)
53
{
54
if
(
isspace
(fch))
55
str = trim(str);
56
else
if
(*str++ != fch)
57
break
;
58
continue
;
59
}
60
61
if
(!
isdigit
(*fmt))
62
width = ULONG_MAX;
63
else
64
fmt = _scanf_getl(fmt, &width, 10, 11, 0);
65
66
fch = *fmt++;
67
if
(fch !=
'c'
&& fch !=
'%'
)
68
{
69
str = trim(str);
70
if
(!*str)
71
break
;
72
}
73
74
switch
(fch)
75
{
76
case
'%'
:
77
if
(*str++ !=
'%'
)
78
done = 1;
79
break
;
80
81
case
'c'
:
82
isWhiteSpaceOK = 1;
83
if
(width == ULONG_MAX)
84
width = 1;
85
/* Fall Through */
86
case
's'
: {
87
char
* arg = va_arg(args,
char
*);
88
if
((str = _scanf_gets(str, arg, width, isWhiteSpaceOK)))
89
blocks++;
90
break
;
91
}
92
case
'b'
: {
93
int
* arg = va_arg(args,
int
*);
94
if
((str = _scanf_getl(str, arg, 2, width, (fch ==
'b'
))))
95
blocks++;
96
break
;
97
}
98
99
case
'u'
:
100
case
'd'
:
101
base = 10;
/* Fall Through */
102
case
'x'
: {
103
int
* arg = va_arg(args,
int
*);
104
if
((str = _scanf_getl(str, arg, base, width, (fch ==
'd'
))))
105
blocks++;
106
break
;
107
}
108
109
case
'e'
:
110
case
'f'
:
111
case
'g'
: {
112
float
* arg = va_arg(args,
float
*);
113
if
((str = _scanf_getf(str, arg)))
114
blocks++;
115
break
;
116
}
117
118
default
: {
119
done = 1;
120
}
121
}
122
123
if
(done)
124
break
;
125
}
126
127
/* Sometimes the last arg can have data, but should not.
128
* This can happen if a buffer is recycled without clearing.
129
* Just zero the last arg.
130
{ int* endarg = va_arg(args, int*);
131
*endarg = 0;
132
}
133
*/
134
135
return
blocks;
136
}
137
138
139
/* +--------------------------------------------------------------------
140
* | TERMS OF USE: MIT License
141
* +--------------------------------------------------------------------
142
* Permission is hereby granted, free of charge, to any person obtaining
143
* a copy of this software and associated documentation files
144
* (the "Software"), to deal in the Software without restriction,
145
* including without limitation the rights to use, copy, modify, merge,
146
* publish, distribute, sublicense, and/or sell copies of the Software,
147
* and to permit persons to whom the Software is furnished to do so,
148
* subject to the following conditions:
149
*
150
* The above copyright notice and this permission notice shall be
151
* included in all copies or substantial portions of the Software.
152
*
153
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
154
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
155
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
156
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
157
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
158
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
159
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
160
* +--------------------------------------------------------------------
161
*/
isspace
#define isspace(c)
Definition:
ctype.h:119
isdigit
#define isdigit(c)
Definition:
ctype.h:83
simpletext.h
This library provides a collection of functions for communicating with text devices such as SimpleIDE...
Generated on Fri May 20 2022 16:48:05 for PropWare by
1.8.17