Repository navigation
Expand file tree
/
Copy pathrprintf.c
More file actions
284 lines (241 loc) · 7.31 KB
/
Copy pathrprintf.c
File metadata and controls
284 lines (241 loc) · 7.31 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*---------------------------------------------------*/
/* Public Domain version of printf */
/* */
/* Rud Merriam, Compsult, Inc. Houston, Tx. */
/* */
/* For Embedded Systems Programming, 1991 */
/* */
/*---------------------------------------------------*/
#include "rprintf.h"
/*---------------------------------------------------*/
/* The purpose of this routine is to output data the */
/* same as the standard printf function without the */
/* overhead most run-time libraries involve. Usually */
/* the printf brings in many kiolbytes of code and */
/* that is unacceptable in most embedded systems. */
/*---------------------------------------------------*/
static func_ptr out_char;
static int do_padding;
static int left_flag;
static int len;
static int num1;
static int num2;
static char pad_character;
size_t strlen(const char *str) {
unsigned int len = 0;
while(str[len] != '\0') {
len++;
}
return len;
}
int tolower(int c) {
if(c < 'a') { // Check if c is uppercase
c -= 'a' - 'A';
}
return c;
}
int isdig(int c) {
if((c >= '0') && (c <= '9')){
return 1;
} else {
return 0;
}
}
/*---------------------------------------------------*/
/* */
/* This routine puts pad characters into the output */
/* buffer. */
/* */
static void padding( const int l_flag)
{
int i;
if (do_padding && l_flag && (len < num1))
for (i=len; i<num1; i++)
out_char( pad_character);
}
/*---------------------------------------------------*/
/* */
/* This routine moves a string to the output buffer */
/* as directed by the padding and positioning flags. */
/* */
static void outs( charptr lp)
{
if(lp == NULL)
lp = "(null)";
/* pad on left if needed */
len = strlen( lp);
padding( !left_flag);
/* Move string to the buffer */
while (*lp && num2--)
out_char( *lp++);
/* Pad on right if needed */
len = strlen( lp);
padding( left_flag);
}
/*---------------------------------------------------*/
/* */
/* This routine moves a number to the output buffer */
/* as directed by the padding and positioning flags. */
/* */
static void outnum(unsigned int num, const int base)
{
charptr cp;
int negative;
char outbuf[32];
const char digits[] = "0123456789ABCDEF";
/* Check if number is negative */
/* NAK 2009-07-29 Negate the number only if it is not a hex value. */
if (num < 0L && base != 16L) {
negative = 1;
num = -num;
}
else
negative = 0;
/* Build number (backwards) in outbuf */
cp = outbuf;
do {
*cp++ = digits[num % base];
} while ((num /= base) > 0);
if (negative)
*cp++ = '-';
*cp-- = 0;
/* Move the converted number to the buffer and */
/* add in the padding where needed. */
len = strlen(outbuf);
padding( !left_flag);
while (cp >= outbuf)
out_char( *cp--);
padding( left_flag);
}
/*---------------------------------------------------*/
/* */
/* This routine gets a number from the format */
/* string. */
/* */
static int getnum( charptr* linep)
{
int n;
charptr cp;
n = 0;
cp = *linep;
while (isdig((int)*cp))
n = n*10 + ((*cp++) - '0');
*linep = cp;
return(n);
}
/*---------------------------------------------------*/
/* */
/* This routine operates just like a printf/sprintf */
/* routine. It outputs a set of data under the */
/* control of a formatting string. Not all of the */
/* standard C format control are supported. The ones */
/* provided are primarily those needed for embedded */
/* systems work. Primarily the floaing point */
/* routines are omitted. Other formats could be */
/* added easily by following the examples shown for */
/* the supported formats. */
/* */
void esp_printf( const func_ptr f_ptr, charptr ctrl, ...)
{
va_list args;
va_start(args, *ctrl);
esp_vprintf(f_ptr, ctrl, args);
va_end( args );
}
void esp_vprintf( const func_ptr f_ptr, charptr ctrl, va_list argp)
{
int long_flag;
int dot_flag;
char ch;
//va_list argp;
//va_start( argp, ctrl);
out_char = f_ptr;
for ( ; *ctrl; ctrl++) {
/* move format string chars to buffer until a */
/* format control is found. */
if (*ctrl != '%') {
out_char(*ctrl);
continue;
}
/* initialize all the flags for this format. */
dot_flag =
long_flag =
left_flag =
do_padding = 0;
pad_character = ' ';
num2=32767;
try_next:
ch = *(++ctrl);
if (isdig((int)ch)) {
if (dot_flag)
num2 = getnum(&ctrl);
else {
if (ch == '0')
pad_character = '0';
num1 = getnum(&ctrl);
do_padding = 1;
}
ctrl--;
goto try_next;
}
switch (tolower((int)ch)) {
case '%':
out_char( '%');
continue;
case '-':
left_flag = 1;
break;
case '.':
dot_flag = 1;
break;
case 'l':
long_flag = 1;
break;
case 'i':
case 'd':
if (long_flag || ch == 'D') {
outnum( va_arg(argp, long), 10L);
continue;
}
else {
outnum( va_arg(argp, int), 10L);
continue;
}
case 'x':
outnum( (long)va_arg(argp, int), 16L);
continue;
case 's':
outs( va_arg( argp, charptr));
continue;
case 'c':
out_char( va_arg( argp, int));
continue;
case '\\':
switch (*ctrl) {
case 'a':
out_char( 0x07);
break;
case 'h':
out_char( 0x08);
break;
case 'r':
out_char( 0x0D);
break;
case 'n':
out_char( 0x0D);
out_char( 0x0A);
break;
default:
out_char( *ctrl);
break;
}
ctrl++;
break;
default:
continue;
}
goto try_next;
}
va_end( argp);
}
/*---------------------------------------------------*/