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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/*
* File : filerw.c
* This file is part of RT-Thread GUI Engine
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2009-10-16 Bernard first version
*/
#include <rtgui/rtgui_system.h>
#include <rtgui/filerw.h>
#ifdef RTGUI_USING_DFS_FILERW
/* standard file read/write */
struct rtgui_filerw_stdio
{
/* inherit from rtgui_filerw */
struct rtgui_filerw parent;
int fd;
rt_bool_t eof;
};
static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};
if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
{
return -1;
}
return lseek(stdio_filerw->fd, offset, stdio_whence[whence]);
}
static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
{
int result;
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
/* end of file */
if (stdio_filerw->eof == RT_TRUE) return -1;
result = read(stdio_filerw->fd, ptr, size * maxnum);
if (result == 0) stdio_filerw->eof = RT_TRUE;
return result;
}
static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
{
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
return write(stdio_filerw->fd, (char *)ptr, size * num);
}
static int stdio_tell(struct rtgui_filerw *context)
{
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
return lseek(stdio_filerw->fd, 0, SEEK_CUR);
}
static int stdio_eof(struct rtgui_filerw *context)
{
int result;
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
if (stdio_filerw->eof == RT_TRUE) result = 1;
else result = -1;
return result;
}
static int stdio_close(struct rtgui_filerw *context)
{
struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
if (stdio_filerw)
{
close(stdio_filerw->fd);
rtgui_free(stdio_filerw);
return 0;
}
return -1;
}
#endif
/* memory file read/write */
struct rtgui_filerw_mem
{
/* inherit from rtgui_filerw */
struct rtgui_filerw parent;
const rt_uint8_t *mem_base, *mem_position, *mem_end;
};
static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
const rt_uint8_t *newpos;
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
RT_ASSERT(mem != RT_NULL);
switch (whence)
{
case RTGUI_FILE_SEEK_SET:
newpos = mem->mem_base + offset;
break;
case RTGUI_FILE_SEEK_CUR:
newpos = mem->mem_position + offset;
break;
case RTGUI_FILE_SEEK_END:
newpos = mem->mem_end + offset;
break;
default:
return -1;
}
if (newpos < mem->mem_base)
newpos = mem->mem_base;
if (newpos > mem->mem_end)
newpos = mem->mem_end;
mem->mem_position = newpos;
return mem->mem_position - mem->mem_base;
}
static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
{
int total_bytes;
int mem_available;
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
total_bytes = (maxnum * size);
if ((maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size))
{
return -1;
}
mem_available = mem->mem_end - mem->mem_position;
if (total_bytes > mem_available)
total_bytes = mem_available;
memcpy(ptr, mem->mem_position, total_bytes);
mem->mem_position += total_bytes;
return (total_bytes / size);
}
static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
{
return 0; /* not support memory write */
}
static int mem_tell(struct rtgui_filerw *context)
{
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
return mem->mem_position - mem->mem_base;
}
static int mem_eof(struct rtgui_filerw *context)
{
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
return mem->mem_position >= mem->mem_end;
}
static int mem_close(struct rtgui_filerw *context)
{
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
if (mem != RT_NULL)
{
rtgui_free(mem);
return 0;
}
return -1;
}
const rt_uint8_t *rtgui_filerw_mem_getdata(struct rtgui_filerw *context)
{
struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
/* check whether it's a memory filerw */
if (mem->parent.read != mem_read) return RT_NULL;
return mem->mem_base;
}
/* file read/write public interface */
#ifdef RTGUI_USING_DFS_FILERW
static int parse_mode(const char *mode)
{
int f = 0;
for (;;)
{
switch (*mode)
{
case 0:
return f;
case 'b':
f |= O_BINARY;
break;
case 'r':
f = O_RDONLY;
break;
case 'w':
f = O_WRONLY | O_CREAT | O_TRUNC;
break;
case 'a':
f = O_WRONLY | O_CREAT | O_APPEND;
break;
case '+':
f = (f & (~O_WRONLY)) | O_RDWR;
break;
}
++mode;
}
}
struct rtgui_filerw *rtgui_filerw_create_file(const char *filename, const char *mode)
{
int fd;
struct rtgui_filerw_stdio *rw;
RT_ASSERT(filename != RT_NULL);
rw = RT_NULL;
#ifdef _WIN32_NATIVE
fd = _open(filename, parse_mode(mode), 0);
#else
fd = open(filename, parse_mode(mode), 0);
#endif
if (fd >= 0)
{
rw = (struct rtgui_filerw_stdio *) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
if (rw != RT_NULL)
{
rw->parent.seek = stdio_seek;
rw->parent.read = stdio_read;
rw->parent.write = stdio_write;
rw->parent.tell = stdio_tell;
rw->parent.close = stdio_close;
rw->parent.eof = stdio_eof;
rw->fd = fd;
rw->eof = RT_FALSE;
return &(rw->parent);
}
close(fd);
}
else
{
rt_kprintf("open:%s failed! errno=%d\n", filename, errno);
}
return RT_NULL;
}
int rtgui_filerw_unlink(const char *filename)
{
#ifdef _WIN32_NATIVE
return _unlink(filename);
#else
#ifndef RT_USING_DFS
/* no unlink function */
return -1;
#else
return unlink(filename);
#endif
#endif
}
#endif
struct rtgui_filerw *rtgui_filerw_create_mem(const rt_uint8_t *mem, rt_size_t size)
{
struct rtgui_filerw_mem *rw;
RT_ASSERT(mem != RT_NULL);
rw = (struct rtgui_filerw_mem *) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
if (rw != RT_NULL)
{
rw->parent.seek = mem_seek;
rw->parent.read = mem_read;
rw->parent.write = mem_write;
rw->parent.tell = mem_tell;
rw->parent.eof = mem_eof;
rw->parent.close = mem_close;
rw->mem_base = mem;
rw->mem_position = mem;
rw->mem_end = mem + size;
}
return &(rw->parent);
}
int rtgui_filerw_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
RT_ASSERT(context != RT_NULL);
return context->seek(context, offset, whence);
}
int rtgui_filerw_read(struct rtgui_filerw *context, void *buffer, rt_size_t size, rt_size_t count)
{
RT_ASSERT(context != RT_NULL);
return context->read(context, buffer, size, count);
}
int rtgui_filerw_write(struct rtgui_filerw *context, const void *buffer, rt_size_t size, rt_size_t count)
{
RT_ASSERT(context != RT_NULL);
return context->write(context, buffer, size, count);
}
int rtgui_filerw_eof(struct rtgui_filerw *context)
{
RT_ASSERT(context != RT_NULL);
return context->eof(context);
}
int rtgui_filerw_tell(struct rtgui_filerw *context)
{
RT_ASSERT(context != RT_NULL);
return context->tell(context);
}
int rtgui_filerw_close(struct rtgui_filerw *context)
{
int result;
RT_ASSERT(context != RT_NULL);
/* close context */
result = context->close(context);
if (result != 0)
{
/* close file failed */
return -1;
}
return 0;
}
|