-
Notifications
You must be signed in to change notification settings - Fork 0
/
eprintf.c
269 lines (225 loc) · 5.04 KB
/
eprintf.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
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
//
// COMP/GENG 422 - Gus Brigantino
//
// eprintf() module
//
#include <stdarg.h>
#include <string.h>
#include "main.h"
#include "eprintf.h"
#include "uart.h"
#define NUM_CHARS_MAX 32 // maximums - %b:32, %d:10, %x:8
static int FieldWidth;
static Bool IsLeftJustified = FALSE;
static Bool IsMinWidth = FALSE;
static Bool IsNegative;
static Bool IsZeroFilled = FALSE;
static int MinWidth;
static int OutputCharCount;
static void LeftFill(void);
static void RightFill(void);
int eprintf(char * FmtStrPtr, ...)
{
va_list ArgPtr;
Bool IsHexLower;
char NumCharArray[NUM_CHARS_MAX + 1]; // allow for null terminator
int NumCharArrayIdx;
int NumValCur;
int NumValNext;
char * StrPtr;
//Variable argument list start-up
va_start(ArgPtr, FmtStrPtr);
OutputCharCount = 0;
NumCharArray[NUM_CHARS_MAX] = '\0'; // array will be filled right-to-left
while(*FmtStrPtr)
{
// Print the format string verbatim until the start of a format specifier
if(*FmtStrPtr != '%')
{
UartCharWrite(*FmtStrPtr++);
OutputCharCount++;
continue;
}
//
// Format specifier handling
//
// Advance to the next char in the format string, reset flags
FmtStrPtr++;
MinWidth = 0;
IsMinWidth = FALSE;
IsLeftJustified = FALSE;
IsNegative = FALSE;
IsZeroFilled = FALSE;
IsHexLower = FALSE;
// Left justification
if(*FmtStrPtr == '-')
{
IsLeftJustified = TRUE;
FmtStrPtr++;
}
// Zero fill
if(*FmtStrPtr == '0')
{
IsZeroFilled = TRUE;
FmtStrPtr++;
}
// Minimum field width, in decimal
while((*FmtStrPtr >= '0') && (*FmtStrPtr <= '9'))
{
IsMinWidth = TRUE;
MinWidth = (MinWidth * 10) + (*FmtStrPtr - '0');
FmtStrPtr++;
}
switch (*FmtStrPtr)
{
case 'b':
NumCharArrayIdx = NUM_CHARS_MAX;
NumValCur = va_arg(ArgPtr, int);
do
{
NumValNext = (NumValCur >> 1) & 0x7FFFFFFF; // mask stops propagation of msb=1 in a signed int
NumValCur &= 0x00000001; // mask off the least significant bit
NumCharArray[--NumCharArrayIdx] = '0' + NumValCur;
NumValCur = NumValNext;
} while (NumValCur);
FieldWidth = NUM_CHARS_MAX - NumCharArrayIdx;
LeftFill();
while (NumCharArray[NumCharArrayIdx])
{
UartCharWrite(NumCharArray[NumCharArrayIdx++]);
OutputCharCount++;
}
RightFill();
break;
case 'c':
FieldWidth = 1;
LeftFill();
UartCharWrite(va_arg(ArgPtr, int));
OutputCharCount++;
RightFill();
break;
case 'd':
NumCharArrayIdx = NUM_CHARS_MAX;
NumValCur = va_arg(ArgPtr, int);
if (NumValCur < 0)
{
NumValCur = -NumValCur;
IsNegative = TRUE;
}
do
{
NumValNext = NumValCur / 10;
NumCharArray[--NumCharArrayIdx] = '0' + (NumValCur - (10 * NumValNext));
NumValCur = NumValNext;
} while (NumValCur);
FieldWidth = (NUM_CHARS_MAX - NumCharArrayIdx) + (IsNegative ? 1 : 0);
LeftFill();
while (NumCharArray[NumCharArrayIdx])
{
UartCharWrite(NumCharArray[NumCharArrayIdx++]);
OutputCharCount++;
}
RightFill();
break;
case 's':
StrPtr = va_arg(ArgPtr, char *);
FieldWidth = strlen(StrPtr);
LeftFill();
while (*StrPtr)
{
UartCharWrite(*StrPtr++);
OutputCharCount++;
}
RightFill();
break;
case 'x':
IsHexLower = TRUE;
// NOTE: lack of break is intentional, flow to next case
case 'X':
NumCharArrayIdx = NUM_CHARS_MAX;
NumValCur = va_arg(ArgPtr, int);
do
{
NumValNext = (NumValCur >> 4) & 0x0FFFFFFF; // mask stops propagation of msb=1 in a signed int
NumValCur &= 0x0000000F; // mask off the least significant hex digit
if (NumValCur < 10)
{
NumCharArray[--NumCharArrayIdx] = '0' + NumValCur;
}
else
{
NumCharArray[--NumCharArrayIdx] = (IsHexLower ? 'a' : 'A') + (NumValCur - 10);
}
NumValCur = NumValNext;
} while (NumValCur);
FieldWidth = NUM_CHARS_MAX - NumCharArrayIdx;
LeftFill();
while (NumCharArray[NumCharArrayIdx])
{
UartCharWrite(NumCharArray[NumCharArrayIdx++]);
OutputCharCount++;
}
RightFill();
break;
case '%':
FieldWidth = 1;
LeftFill();
UartCharWrite('%');
OutputCharCount++;
RightFill();
break;
default:
UartCharWrite('%');
UartCharWrite(*FmtStrPtr++);
OutputCharCount += 2;
break;
}
FmtStrPtr++;
}
// Variable argument list clean-up
va_end(ArgPtr);
return OutputCharCount;
}
static void LeftFill(void)
{
int FillCharCount = MinWidth - FieldWidth;
if (!IsMinWidth || IsLeftJustified || (FillCharCount < 0))
{
if (IsNegative)
{
UartCharWrite('-');
OutputCharCount++;
}
return;
}
if (IsNegative && IsZeroFilled)
{
UartCharWrite('-');
OutputCharCount++;
}
while (FillCharCount > 0)
{
UartCharWrite(IsZeroFilled ? '0' : ' ');
OutputCharCount++;
FillCharCount--;
}
if (IsNegative && !IsZeroFilled)
{
UartCharWrite('-');
OutputCharCount++;
}
}
static void RightFill(void)
{
int FillCharCount = MinWidth - FieldWidth;
if (!IsMinWidth || !IsLeftJustified || (FillCharCount < 0))
{
return;
}
while (FillCharCount > 0)
{
UartCharWrite(' ');
OutputCharCount++;
FillCharCount--;
}
}