-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
extrafat.c
209 lines (186 loc) · 5.35 KB
/
extrafat.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
#include "fatfs/ff.h"
#include <stdarg.h> // va_list
// bringing in some fat functions (from ff.c)
/*----------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.10a (C)ChaN, 2014
/-----------------------------------------------------------------------------/
/ FatFs module is a generic FAT file system module for small embedded systems.
/ This is a free software that opened for education, research and commercial
/ developments under license policy of following terms.
/
/ Copyright (C) 2014, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/-----------------------------------------------------------------------------*/
/* Character code support macros */
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
#define IsLower(c) (((c)>='a')&&((c)<='z'))
#define IsDigit(c) (((c)>='0')&&((c)<='9'))
typedef struct {
FIL* fp;
int idx, nchr;
BYTE buf[64];
} putbuff;
static
void putc_bfd (
putbuff* pb,
TCHAR c
)
{
UINT bw;
int i;
if (_USE_STRFUNC == 2 && c == '\n') /* LF -> CRLF conversion */
putc_bfd(pb, '\r');
i = pb->idx; /* Buffer write index (-1:error) */
if (i < 0) return;
#if _USE_LFN && _LFN_UNICODE
#if _STRF_ENCODE == 3 /* Write a character in UTF-8 */
if (c < 0x80) { /* 7-bit */
pb->buf[i++] = (BYTE)c;
} else {
if (c < 0x800) { /* 11-bit */
pb->buf[i++] = (BYTE)(0xC0 | c >> 6);
} else { /* 16-bit */
pb->buf[i++] = (BYTE)(0xE0 | c >> 12);
pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));
}
pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));
}
#elif _STRF_ENCODE == 2 /* Write a character in UTF-16BE */
pb->buf[i++] = (BYTE)(c >> 8);
pb->buf[i++] = (BYTE)c;
#elif _STRF_ENCODE == 1 /* Write a character in UTF-16LE */
pb->buf[i++] = (BYTE)c;
pb->buf[i++] = (BYTE)(c >> 8);
#else /* Write a character in ANSI/OEM */
c = ff_convert(c, 0); /* Unicode -> OEM */
if (!c) c = '?';
if (c >= 0x100)
pb->buf[i++] = (BYTE)(c >> 8);
pb->buf[i++] = (BYTE)c;
#endif
#else /* Write a character without conversion */
pb->buf[i++] = (BYTE)c;
#endif
if (i >= (int)(sizeof pb->buf) - 3) { /* Write buffered characters to the file */
f_write(pb->fp, pb->buf, (UINT)i, &bw);
i = (bw == (UINT)i) ? 0 : -1;
}
pb->idx = i;
pb->nchr++;
}
int f_printf (
FIL* fp, /* Pointer to the file object */
const TCHAR* fmt, /* Pointer to the format string */
... /* Optional arguments... */
)
{
va_list arp;
BYTE f, r;
UINT nw, i, j, w;
DWORD v;
TCHAR c, d, s[16], *p;
putbuff pb;
pb.fp = fp; /* Initialize output buffer */
pb.nchr = pb.idx = 0;
va_start(arp, fmt);
for (;;) {
c = *fmt++;
if (c == 0) break; /* End of string */
if (c != '%') { /* Non escape character */
putc_bfd(&pb, c);
continue;
}
w = f = 0;
c = *fmt++;
if (c == '0') { /* Flag: '0' padding */
f = 1; c = *fmt++;
} else {
if (c == '-') { /* Flag: left justified */
f = 2; c = *fmt++;
}
}
while (IsDigit(c)) { /* Precision */
w = w * 10 + c - '0';
c = *fmt++;
}
if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
f |= 4; c = *fmt++;
}
if (!c) break;
d = c;
if (IsLower(d)) d -= 0x20;
switch (d) { /* Type is... */
case 'S' : /* String */
p = va_arg(arp, TCHAR*);
for (j = 0; p[j]; j++) ;
if (!(f & 2)) {
while (j++ < w) putc_bfd(&pb, ' ');
}
while (*p) putc_bfd(&pb, *p++);
while (j++ < w) putc_bfd(&pb, ' ');
continue;
case 'C' : /* Character */
putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;
case 'B' : /* Binary */
r = 2; break;
case 'O' : /* Octal */
r = 8; break;
case 'D' : /* Signed decimal */
case 'U' : /* Unsigned decimal */
r = 10; break;
case 'X' : /* Hexdecimal */
r = 16; break;
default: /* Unknown type (pass-through) */
putc_bfd(&pb, c); continue;
}
/* Get an argument and put it in numeral */
v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));
if (d == 'D' && (v & 0x80000000)) {
v = 0 - v;
f |= 8;
}
i = 0;
do {
d = (TCHAR)(v % r); v /= r;
if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
s[i++] = d + '0';
} while (v && i < sizeof s / sizeof s[0]);
if (f & 8) s[i++] = '-';
j = i; d = (f & 1) ? '0' : ' ';
while (!(f & 2) && j++ < w) putc_bfd(&pb, d);
do putc_bfd(&pb, s[--i]); while (i);
while (j++ < w) putc_bfd(&pb, d);
}
va_end(arp);
if ( pb.idx >= 0 /* Flush buffered characters to the file */
&& f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
&& (UINT)pb.idx == nw) return pb.nchr;
return EOF;
}
TCHAR* f_gets (
TCHAR* buff, /* Pointer to the string buffer to read */
int len, /* Size of string buffer (characters) */
FIL* fp /* Pointer to the file object */
)
{
int n = 0;
TCHAR c, *p = buff;
BYTE s[2];
UINT rc;
// chopped down a bit (ignoring LFN stuff)
while (n < len - 1) { /* Read characters until buffer gets filled */
f_read(fp, s, 1, &rc);
if (rc != 1) break;
c = s[0];
*p++ = c;
n++;
if (c == '\n') break; /* Break on EOL */
}
*p = 0;
return n ? buff : 0; /* When no data read (eof or error), return with error. */
}