forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicfmt.c
More file actions
336 lines (301 loc) · 7.52 KB
/
Copy pathbasicfmt.c
File metadata and controls
336 lines (301 loc) · 7.52 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
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
/*
* BASIC language I/O
*/
#include <libsys/fmt.h>
#ifndef __FLEXC__
// BASIC support routines
extern int _tx(int c);
extern int _rx(void);
#endif
typedef int (*TxFunc)(int c);
typedef int (*RxFunc)(void);
typedef int (*CloseFunc)(void);
typedef int (*VFS_CloseFunc)(vfs_file_t *);
#ifdef _SIMPLE_IO
#define _gettxfunc(h) ((void *)1)
#define _getrxfunc(h) ((void *)1)
#else
// we want the BASIC open function to work correctly with old Spin
// interfaces which don't return sensible values from send, so we
// have to wrap the send function into one which always returns 1
// use this class to do it:
// bytecode also may need wrappers for things as well
typedef struct _bas_wrap_sender {
TxFunc ftx;
RxFunc frx;
CloseFunc fclose;
int tx(int c, void *arg) { ftx(c); return 1; }
int rx(void *arg) { return frx(); }
int close(void *arg) { return fclose(); }
} BasicWrapper;
TxFunc _gettxfunc(unsigned h) {
vfs_file_t *v;
v = __getftab(h);
if (!v || !v->state) return 0;
return (TxFunc)&v->putchar;
}
RxFunc _getrxfunc(unsigned h) {
vfs_file_t *v;
v = __getftab(h);
if (!v || !v->state) return 0;
return (RxFunc)&v->getchar;
}
#endif
//
// basic interfaces
//
int _basic_open(unsigned h, TxFunc sendf, RxFunc recvf, CloseFunc closef)
{
#ifdef _SIMPLE_IO
THROW_RETURN(EIO);
#else
struct _bas_wrap_sender *wrapper = 0;
vfs_file_t *v;
v = __getftab(h);
//__builtin_printf("basic_open(%d) = %x\n", h, (unsigned)v);
if (!v) {
THROW_RETURN(EIO);
}
if (v->state) {
_closeraw(v);
}
if (sendf || recvf || closef) {
wrapper = _gc_alloc_managed(sizeof(BasicWrapper));
if (!wrapper) {
THROW_RETURN(ENOMEM); /* out of memory */
}
wrapper->ftx = 0;
wrapper->frx = 0;
v->vfsdata = wrapper; /* keep the garbage collector from collecting the wrapper */
}
if (sendf) {
wrapper->ftx = sendf;
v->putcf = (putcfunc_t)&wrapper->tx;
} else {
v->putcf = 0;
}
if (recvf) {
wrapper->frx = recvf;
v->getcf = &wrapper->rx;
} else {
v->getcf = 0;
}
if (closef) {
wrapper->fclose = closef;
v->close = (VFS_CloseFunc)&wrapper->close;
} else {
v->close = 0;
}
v->state = _VFS_STATE_INUSE|_VFS_STATE_RDOK|_VFS_STATE_WROK;
return 0;
#endif
}
int _basic_open_string(unsigned h, char *fname, unsigned iomode)
{
#ifdef _SIMPLE_IO
THROW_RETURN(EIO);
#else
vfs_file_t *v;
int r;
v = __getftab(h);
if (!v) {
THROW_RETURN(EIO);
}
if (v->state) {
_closeraw(v);
}
r = _openraw(v, fname, iomode, 0666);
if (r < 0) {
THROW_RETURN(errno);
}
return r;
#endif
}
void _basic_close(unsigned h)
{
close(h);
}
int _basic_print_char(unsigned h, int c, unsigned fmt)
{
TxFunc fn = _gettxfunc(h);
if (!fn) return 0;
PUTC(fn, c);
return 1;
}
int _basic_print_nl(unsigned h)
{
_basic_print_char(h, 10, 0);
return 1;
}
int _basic_print_string(unsigned h, const char *ptr, unsigned fmt)
{
TxFunc tf = _gettxfunc(h);
if (!tf) return 0;
if (!ptr) return 0;
return _fmtstr(tf, fmt, ptr);
}
int _basic_print_boolean(unsigned h, int x, unsigned fmt)
{
const char *ptr = x ? "true" : "false";
return _basic_print_string(h, ptr, fmt);
}
int _basic_print_unsigned(unsigned h, int x, unsigned fmt, int base)
{
TxFunc tf = _gettxfunc(h);
if (!tf) return 0;
fmt |= 3<<SIGNCHAR_BIT;
return _fmtnum(tf, fmt, x, base);
}
int _basic_print_unsigned_2(unsigned h, int x1, int x2, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
fmt |= 3<<SIGNCHAR_BIT;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
return r;
}
int _basic_print_unsigned_3(unsigned h, int x1, int x2, int x3, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
fmt |= 3<<SIGNCHAR_BIT;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x3, base);
return r;
}
int _basic_print_unsigned_4(unsigned h, int x1, int x2, int x3, int x4, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
fmt |= 3<<SIGNCHAR_BIT;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x3, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x4, base);
return r;
}
int _basic_print_integer(unsigned h, int x, unsigned fmt, int base)
{
TxFunc tf;
tf = _gettxfunc(h);
if (!tf) return 0;
return _fmtnum(tf, fmt, x, base);
}
int _basic_print_integer_2(unsigned h, int x1, int x2, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
return r;
}
int _basic_print_integer_3(unsigned h, int x1, int x2, int x3, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x3, base);
return r;
}
int _basic_print_integer_4(unsigned h, int x1, int x2, int x3, int x4, unsigned fmt, int base)
{
TxFunc tf;
int r;
tf = _gettxfunc(h);
if (!tf) return 0;
r = _fmtnum(tf, fmt, x1, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x2, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x3, base);
r += _fmtstr(tf, fmt, ", ");
r += _fmtnum(tf, fmt, x4, base);
return r;
}
int _basic_print_longunsigned(unsigned h, unsigned long long x, unsigned fmt, int base)
{
TxFunc tf = _gettxfunc(h);
if (!tf) return 0;
fmt |= 3<<SIGNCHAR_BIT;
return _fmtnumlong(tf, fmt, x, base);
}
int _basic_print_longinteger(unsigned h, long long int x, unsigned fmt, int base)
{
TxFunc tf;
tf = _gettxfunc(h);
if (!tf) return 0;
return _fmtnumlong(tf, fmt, x, base);
}
int _basic_get_char(unsigned h)
{
#ifdef _SIMPLE_IO
return _rx();
#else
RxFunc rf;
rf = _getrxfunc(h);
if (!rf) return -1;
return (*rf)();
#endif
}
int _basic_print_float(unsigned h, FTYPE x, unsigned fmt, int ch)
{
TxFunc tf;
if (fmt == 0) {
fmt = (ch == '#') ? DEFAULT_BASIC_FLOAT_FMT : DEFAULT_FLOAT_FMT;
}
tf = _gettxfunc(h);
if (!tf) return 0;
return _fmtfloat(tf, fmt, x, ch);
}
// write "elements" items of size "size" bytes from "data"
// returns number of bytes (not elements) successfully written
int _basic_put(int h, unsigned long pos, void *data, unsigned elements, unsigned size)
{
int r;
unsigned bytes = elements * size;
if (pos != 0) {
lseek(h, pos-1, SEEK_SET);
}
r = write(h, data, bytes);
if (r > 0) {
r = r / size;
}
return r;
}
// read "elements" items of size "size" bytes into "data"
// returns number of bytes (not elements) successfully read
int _basic_get(int h, unsigned long pos, void *data, unsigned elements, unsigned size)
{
int r;
unsigned bytes = elements * size;
if (pos != 0) {
lseek(h, pos-1, SEEK_SET);
}
r = read(h, data, bytes);
if (r > 0) {
r = r / size;
}
return r;
}