summaryrefslogtreecommitdiff
path: root/apps/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins')
-rw-r--r--apps/plugins/lib/SOURCES1
-rw-r--r--apps/plugins/lib/stdio_compat.c231
-rw-r--r--apps/plugins/lib/stdio_compat.h67
3 files changed, 299 insertions, 0 deletions
diff --git a/apps/plugins/lib/SOURCES b/apps/plugins/lib/SOURCES
index 747355b44b..9a7aef51d0 100644
--- a/apps/plugins/lib/SOURCES
+++ b/apps/plugins/lib/SOURCES
@@ -11,6 +11,7 @@ highscore.c
11simple_viewer.c 11simple_viewer.c
12display_text.c 12display_text.c
13strncpy.c 13strncpy.c
14stdio_compat.c
14 15
15#if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4) 16#if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4)
16grey_core.c 17grey_core.c
diff --git a/apps/plugins/lib/stdio_compat.c b/apps/plugins/lib/stdio_compat.c
new file mode 100644
index 0000000000..d2b8f9bbc7
--- /dev/null
+++ b/apps/plugins/lib/stdio_compat.c
@@ -0,0 +1,231 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2013 by Marcin Bukat
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include "plugin.h"
22#include "stdio_compat.h"
23
24static _FILE_ __file__[MAX_STDIO_FILES] = {
25 {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0},
26 {-1,-1,0},{-1,-1,0},{-1,-1,0},{-1,-1,0}
27};
28
29_FILE_ *_fopen_(const char *path, const char *mode)
30{
31 _FILE_ *f = __file__;
32 int flags = O_RDONLY;
33 int fd = -1;
34 int i;
35
36 /* look for free slot */
37 for (i=0; i<MAX_OPEN_FILES; i++, f++)
38 if (f->fd == -1)
39 break;
40
41 /* no empty slots */
42 if (i == MAX_STDIO_FILES)
43 return NULL;
44
45 if (*mode != 'r')
46 {
47 /* Not read */
48 flags = (O_WRONLY | O_CREAT | O_TRUNC);
49 if (*mode != 'w')
50 {
51 /* Not write (create of append) */
52 flags = (O_WRONLY | O_CREAT | O_APPEND);
53 if (*mode != 'a')
54 {
55 /* Illegal */
56 return NULL;
57 }
58 }
59 }
60
61 if (mode[1] == 'b')
62 {
63 /* Ignore */
64 mode++;
65 }
66
67 if (mode[1] == '+')
68 {
69 /* Read and Write. */
70 flags |= (O_RDONLY | O_WRONLY);
71 flags += (O_RDWR - (O_RDONLY | O_WRONLY));
72 }
73
74
75 fd = rb->open(path, flags);
76
77 if (fd == -1)
78 return NULL;
79
80 /* initialize */
81 f->fd = fd;
82 f->unget_char = -1;
83
84 return f;
85}
86
87int _fflush_(_FILE_ *stream)
88{
89 (void)stream;
90 /* no buffering so we are always in sync */
91 return 0;
92}
93
94size_t _fread_(void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
95{
96 ssize_t ret = rb->read(stream->fd, (char *)ptr, size*nmemb);
97
98 if (ret < (ssize_t)(size*nmemb))
99 stream->error = -1;
100
101 return ret;
102}
103
104size_t _fwrite_(const void *ptr, size_t size, size_t nmemb, _FILE_ *stream)
105{
106 ssize_t ret = rb->write(stream->fd, ptr, size*nmemb);
107
108 if (ret < (ssize_t)(size*nmemb))
109 stream->error = -1;
110
111 return ret;
112}
113
114int _fseek_(_FILE_ *stream, long offset, int whence)
115{
116 return rb->lseek(stream->fd, offset, whence);
117}
118
119long _ftell_(_FILE_ *stream)
120{
121 return rb->lseek(stream->fd, 0, SEEK_CUR);
122}
123
124int _fclose_(_FILE_ *stream)
125{
126 if (stream->fd == -1)
127 return EOF;
128
129 if (rb->close(stream->fd) == -1)
130 return EOF;
131
132 /* free the resource */
133 stream->fd = -1;
134
135 return 0;
136}
137
138int _fgetc_(_FILE_ *stream)
139{
140 unsigned char c;
141
142 if (stream->unget_char != -1)
143 {
144 c = stream->unget_char;
145 stream->unget_char = -1;
146 return c;
147 }
148
149 if ( rb->read(stream->fd, &c, 1) != 1)
150 return EOF;
151
152 return c;
153}
154
155int _ungetc_(int c, _FILE_ *stream)
156{
157 stream->unget_char = c;
158
159 return c;
160}
161
162int _fputc_(int c, _FILE_ *stream)
163{
164 unsigned char cb = c;
165
166 if (rb->write(stream->fd, &cb, 1) != 1)
167 return EOF;
168
169 return cb;
170}
171
172char *_fgets_(char *s, int size, _FILE_ *stream)
173{
174 char *p = s;
175 char c = '\0';
176
177 for (int i=0; i<size; i++)
178 {
179 if ( rb->read(stream->fd, &c, 1) != 1)
180 break;
181
182 *p++ = c;
183
184 if (c == '\n')
185 break;
186 }
187
188 if (p == s)
189 return NULL;
190
191 *p = '\0';
192
193 return s;
194}
195
196void _clearerr_(_FILE_ *stream)
197{
198 if (stream)
199 stream->error = 0;
200}
201
202int _ferror_(_FILE_ *stream)
203{
204 return (stream) ? stream->error : -1;
205}
206
207int _feof_(_FILE_ *stream)
208{
209 int c = _fgetc_(stream);
210
211 if (c != EOF)
212 {
213 _ungetc_(c, stream);
214 return 0;
215 }
216
217 return 0;
218}
219
220int _fprintf_(_FILE_ *stream, const char *format, ...)
221{
222 int len;
223 va_list ap;
224 char buf[256];
225
226 va_start(ap, format);
227 len = rb->vsnprintf(buf, sizeof(buf), format, ap);
228 va_end(ap);
229
230 return _fwrite_(buf, 1, len, stream);
231}
diff --git a/apps/plugins/lib/stdio_compat.h b/apps/plugins/lib/stdio_compat.h
new file mode 100644
index 0000000000..bb37940296
--- /dev/null
+++ b/apps/plugins/lib/stdio_compat.h
@@ -0,0 +1,67 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2013 by Marcin Bukat
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include <stddef.h>
22
23#define MAX_STDIO_FILES 8
24
25#undef FILE
26#define FILE _FILE_
27
28#define fopen _fopen_
29#define fclose _fclose_
30#define fflush _fflush_
31#define fread _fread_
32#define fwrite _fwrite_
33#define fseek _fseek_
34#define fseeko _fseek_
35#define ftell _ftell_
36#define ftello _ftell_
37#define fgetc _fgetc_
38#define ungetc _ungetc_
39#define fputc _fputc_
40#define fgets _fgets_
41#define clearerr _clearerr_
42#define ferror _ferror_
43#define feof _feof_
44#define fprintf _fprintf_
45
46typedef struct {
47 int fd;
48 int unget_char;
49 int error;
50} _FILE_;
51
52_FILE_ *_fopen_(const char *path, const char *mode);
53int _fclose_(_FILE_ *stream);
54int _fflush_(_FILE_ *stream);
55size_t _fread_(void *ptr, size_t size, size_t nmemb, _FILE_ *stream);
56size_t _fwrite_(const void *ptr, size_t size, size_t nmemb, _FILE_ *stream);
57int _fseek_(_FILE_ *stream, long offset, int whence);
58long _ftell_(_FILE_ *stream);
59int _fgetc_(_FILE_ *stream);
60int _ungetc_(int c, _FILE_ *stream);
61int _fputc_(int c, _FILE_ *stream);
62char *_fgets_(char *s, int size, _FILE_ *stream);
63int _unlink_(const char *pathname);
64void _clearerr_(_FILE_ *stream);
65int _ferror_(_FILE_ *stream);
66int _feof_(_FILE_ *stream);
67int _fprintf_(_FILE_ *stream, const char *format, ...);