-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfileio.c
1470 lines (1375 loc) · 42.9 KB
/
fileio.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy 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, or (at your option)
** any later version.
**
** Brandy 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 Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file contains the file I/O routines for the interpreter
*/
/*
** The functions here map the Basic VI file I/O facilities on to those
** provided by the underlying operating system.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include "common.h"
#include "miscprocs.h"
#include "target.h"
#include "errors.h"
#include "fileio.h"
#include "strings.h"
#include "screen.h"
#ifndef NONET
#include "net.h"
#endif
#include "keyboard.h"
/* Floating point number format */
enum {XMIXED_ENDIAN, XLITTLE_ENDIAN, XBIG_ENDIAN, XBIG_MIXED_ENDIAN} double_type;
typedef enum {CLOSED, OPENIN, OPENUP, OPENOUT, NETWORK} filestate;
typedef enum {OKAY, PENDING, ATEOF} eofstate;
typedef struct {
FILE *stream; /* 'C' file handle for the file */
filestate filetype; /* Way in which file has been opened */
eofstate eofstatus; /* Current end-of-file status */
boolean lastwaswrite; /* TRUE if the last operation on a file was a write */
int nethandle; /* network handle */
} fileblock;
#ifdef TARGET_RISCOS
#define MAXFILES 4 /* Maximum number of files that can be open simultaneously - only tracks networking on RISC OS */
#define FIRSTHANDLE 4 /* Number of first handle */
#else
#define MAXFILES 256 /* Maximum number of files that can be open simultaneously */
#define FIRSTHANDLE 254 /* Number of first handle */
#endif
static fileblock fileinfo [MAXFILES+1];
/*
** 'isapath' returns TRUE if the file name passed to it is a pathname, that
** is, contains directories as well as a file name, and FALSE if it consists
** of just the name of the file. 'DIRSEPS' is a string containing all the
** characters that can be used in a file name to separate directories or
** the name of the file from the directories.
** This code is meant to be operating system independent
*/
boolean isapath(char *name) {
return strpbrk(name, DIR_SEPS)!=NIL;
}
#ifdef TARGET_RISCOS
/* ================================================================= */
/* ================= RISC OS versions of functions ================= */
/* ================================================================= */
#include "kernel.h"
#include "swis.h"
#ifndef _kernel_ERROR
#define _kernel_ERROR (-2)
#endif
/*
** Under RISC OS, direct operating system calls such as OS_Find are used
** for file I/O so that Basic programs can use a mix of Basic statements
** and SWIs to carry out file I/O. The code does not keep track of what
** files have been opened as it possible to do such things as open a
** file using the SWI OS_Find directly and then read it using the Basic
** function BGET.
*/
/* OS_Find calls */
#define OPEN_INPUT 0x40
#define OPEN_OUTPUT 0x80
#define OPEN_UPDATE 0xC0
/*
** 'report' is called when one of the _kernel_xxx functions
** returns -2 (indicating that the corresponding SWI call
** failed)
*/
static void report(void) {
_kernel_oserror *oserror;
oserror = _kernel_last_oserror();
error(ERR_CMDFAIL, oserror->errmess);
}
/*
** 'read' reads a byte from a file, dealing with any error
** conditions that might arise
*/
static int32 fileio_read(int32 handle) {
int32 value;
value = _kernel_osbget(handle);
if (value==_kernel_ERROR) report(); /* Function returned -2 = SWI call failed */
if (value==-1) error(ERR_CANTREAD);
return value;
}
/*
** 'fileio_openin' opens a file for input
*/
int32 fileio_openin(char *name, int32 namelen) {
int32 handle;
char filename [FNAMESIZE];
memmove(filename, name, namelen);
filename[namelen] = NUL; /* Get a null-terminated version of the file name */
handle = _kernel_osfind(OPEN_INPUT, filename);
if (handle==_kernel_ERROR) {
if (matrixflags.translatefname == 0) {
report();
} else {
char *tfilename = translatefname(filename);
handle = _kernel_osfind(OPEN_INPUT, tfilename);
if (handle==_kernel_ERROR) {
report();
} else {
/* We had to translate so set flags to always translate on save */
matrixflags.translatefname = 1;
}
}
}
return handle;
}
/*
** 'fileio_openout' opens file 'name' for output
*/
int32 fileio_openout(char *name, int32 namelen) {
int32 handle;
char filename [FNAMESIZE];
memmove(filename, name, namelen);
filename[namelen] = NUL;
if(matrixflags.translatefname == 1) {
char *tfilename = translatefname(filename);
handle = _kernel_osfind(OPEN_OUTPUT, tfilename);
} else {
handle = _kernel_osfind(OPEN_OUTPUT, filename);
}
if (handle==_kernel_ERROR) report();
return handle;
}
/*
** 'fileio_openup' opens a file for both input and output
*/
int32 fileio_openup(char *name, int32 namelen) {
int32 handle;
#ifndef NONET
int32 n;
#endif
char filename [FNAMESIZE];
char *tfilename;
memmove(filename, name, namelen);
filename[namelen] = NUL;
#ifndef NONET
/* Check, does it start "ip4:" if so use network handler to open it. */
if (strncmp(filename, "ip0:", 4)==0 || strncmp(filename, "ip4:", 4)==0 || strncmp(filename, "ip6:", 4)==0) {
for (n=FIRSTHANDLE; n>0 && fileinfo[n].stream!=NIL; n--); /* Find an unused handle */
handle=brandynet_connect(filename+4, filename[2], 1);
if (handle == -1) return 0;
fileinfo[n].stream = (void *)42; /* Not used, but != NIL */
fileinfo[n].filetype = NETWORK;
fileinfo[n].eofstatus = OKAY;
fileinfo[n].lastwaswrite = FALSE;
fileinfo[n].nethandle = handle;
return n;
} else {
#endif
handle = _kernel_osfind(OPEN_UPDATE, filename);
if (handle==_kernel_ERROR) {
if (matrixflags.translatefname == 0) {
report();
} else {
tfilename=translatefname(filename);
handle = _kernel_osfind(OPEN_INPUT, tfilename);
if (handle==_kernel_ERROR) {
report();
} else {
/* We had to translate so set flags to always translate on save */
matrixflags.translatefname = 1;
}
}
}
return handle;
#ifndef NONET
}
#endif
}
/*
** 'fileio_close' closes the fie given by 'handle' or all open files
** if 'handle' is zero
*/
static void close_file(int32 handle) {
#ifndef NONET
if ((handle != 0) && (handle <= FIRSTHANDLE) && (fileinfo[handle].filetype == NETWORK)) {
brandynet_close(fileinfo[handle].nethandle);
fileinfo[handle].stream = NIL;
fileinfo[handle].filetype = CLOSED;
fileinfo[handle].lastwaswrite = FALSE;
fileinfo[handle].nethandle = -1;
} else {
#endif
_kernel_oserror *oserror;
_kernel_swi_regs regs;
regs.r[0] = 0;
regs.r[1] = handle;
oserror = _kernel_swi(OS_Find, ®s, ®s);
if (oserror!=NIL) error(ERR_CMDFAIL, oserror->errmess);
#ifndef NONET
}
#endif
}
void fileio_close(int32 handle) {
if (handle==0) { /* Close all open files */
int32 n;
for (n=FIRSTHANDLE; n>0; n--) {
if (fileinfo[n].filetype!=CLOSED) close_file(n);
}
close_file(handle);
}
else { /* close one file */
close_file(handle);
}
}
/*
** 'fileio_bget' returns the next character from file 'handle'.
*/
int32 fileio_bget(int32 handle) {
int32 ch = 0;
if (handle==0) {
error(ERR_BADHANDLE);
return 0;
}
#ifndef NONET
if ((handle <= FIRSTHANDLE) && (fileinfo[handle].filetype == NETWORK)) {
ch=net_bget(fileinfo[handle].nethandle);
if (ch == -2) {
if (fileinfo[handle].eofstatus == PENDING) {
fileinfo[handle].eofstatus = ATEOF;
error(ERR_HITEOF);
return 0;
} else {
fileinfo[handle].eofstatus = PENDING;
}
}
} else {
#endif
_kernel_oserror *oserror;
_kernel_swi_regs regs;
regs.r[0] = 0;
regs.r[1] = handle;
oserror = _kernel_swi(OS_BGet, ®s, ®s);
if (oserror!=NIL) {
error(ERR_CMDFAIL, oserror->errmess);
return 0;
}
ch=regs.r[0];
#ifndef NONET
}
#endif
return ch;
}
/*
** 'fileio_getdol' reads a string from a file. It saves the text read at
** 'buffer'. Note that there is no check on the size of the buffer
** so it is up to the functions that call this one to ensure that the
** buffer is large enough to hold MAXSTRING (65536) characters.
*/
int32 fileio_getdol(int32 handle, char *buffer) {
int32 length = 0;
do {
int32 ch = fileio_bget(handle);
if (ch==_kernel_ERROR) report(); /* Function returned -2 = SWI call failed */
if (ch==-1 || ch==LF) break; /* At end of file or reached end of line */
buffer[length] = ch;
length++;
} while (TRUE);
return length;
}
/*
** 'fileio_getnumber' reads a binary number from the file with
** handle 'handle'. It stores the result at the address given
** by 'ip' if integer or at 'fp' is floating point. 'isint' is
** set to TRUE if the value is an integer.
**
** Note that integers are stored in big endian format in the file
** by PRINT#. Floating point values are written in the byte order
** used in memory but this causes its own problems as the ARM
** format for eight-byte floating point values is different to
** that used on other processors meaning that the RISC OS and
** NetBSD/arm32 versions of the program are not compatible with
** other versions. Also, this code will only read eight-byte
** floating point values produced by this program or basic64.
** It does not handle numbers in Acorn's five byte format.
*/
void fileio_getnumber(int32 handle, boolean *isint, int64 *ip, float64 *fp) {
int32 n, marker;
char temp[sizeof(float64)];
memset(temp,0,sizeof(float64));
marker = fileio_read(handle);
switch (marker) {
case PRINT_INT:
for (n=1; n<=sizeof(int32); n++) temp[sizeof(int32)-n] = fileio_read(handle);
memmove(ip, temp, sizeof(int32));
*isint = TRUE;
break;
case PRINT_UINT8:
for (n=1; n<=sizeof(uint8); n++) temp[sizeof(uint8)-n] = fileio_read(handle);
memmove(ip, temp, sizeof(uint8));
*isint = TRUE;
break;
case PRINT_INT64:
for (n=1; n<=sizeof(int64); n++) temp[sizeof(int64)-n] = fileio_read(handle);
memmove(ip, temp, sizeof(int64));
*isint = TRUE;
break;
case PRINT_FLOAT:
for (n=0; n<sizeof(float64); n++) temp[n] = fileio_read(handle);
memmove(fp, temp, sizeof(float64));
*isint = FALSE;
break;
case PRINT_FLOAT5: { /* Acorn 5-byte floating point format */
/*
** Acorn's five byte format consists of a four byte mantissa and
** a one byte exponent. The mantissa, x, is in little endian format
** and represents a number in the range 0.5<=x<1.0. The value is
** such that x times 2 raised to the power y (where y is the
** exponent) gives the floating point value. The most significant
** bit will always be one, so it is used as the sign bit. The
** exponent has 0x80 added to it.
*/
int32 exponent;
int32 mantissa = fileio_read(handle);
mantissa |= fileio_read(handle) << 8;
mantissa |= fileio_read(handle) << 16;
mantissa |= fileio_read(handle) << 24;
exponent = fileio_read(handle);
if (exponent || mantissa) {
*fp = ((float64)(mantissa & 0x7FFFFFFF) / 4294967296.0 + 0.5)
* pow (2, (float64)(exponent - 0x80))
* (mantissa < 0 ? -1 : 1);
} else {
*fp = 0;
}
*isint = FALSE;
break;
}
default:
error(ERR_TYPENUM);
}
}
/*
** 'fileio_getstring' reads a string from from a file and returns
** the length of the string read. The string is stored at address
** 'p'. It assumes that there is enough room to store the string.
** The function can handle string in both Acorn format and this
** interpreter's. In Acorn's format, strings can be up to 255
** characters long. They are stored in the file in reverse order,
** that is, the last character of the string is first.
*/
int32 fileio_getstring(int32 handle, char *p) {
int32 marker = 0, length = 0, n = 0;
marker = fileio_read(handle);
switch (marker) {
case PRINT_SHORTSTR: /* Reading short string in 'Acorn' format */
length = fileio_read(handle);
for (n=1; n<=length; n++) p[length-n] = fileio_read(handle);
break;
case PRINT_LONGSTR: /* Reading long string */
length = 0; /* Start by reading the string length (four bytes, little endian) */
for (n=0; n<sizeof(int32); n++) length+=fileio_read(handle)<<(n*BYTESHIFT);
for (n=0; n<length; n++) p[n] = fileio_read(handle);
break;
default:
error(ERR_TYPESTR);
return 0;
}
return length;
}
/*
** 'fileio_bput' writes a character to a file
*/
void fileio_bput(int32 handle, int32 value) {
if (handle==0) {
error(ERR_BADHANDLE);
return;
}
#ifndef NONET
if ((handle <= FIRSTHANDLE) && (fileinfo[handle].filetype==NETWORK)) {
if(net_bput(fileinfo[handle].nethandle, value)) error(ERR_CANTWRITE);
} else {
#endif
_kernel_oserror *oserror;
_kernel_swi_regs regs;
regs.r[0] = value;
regs.r[1] = handle;
oserror = _kernel_swi(OS_BPut, ®s, ®s);
if (oserror!=NIL) error(ERR_CMDFAIL, oserror->errmess);
#ifndef NONET
}
#endif
}
/*
** 'fileio_bputstr' writes a string to a file
*/
void fileio_bputstr(int32 handle, char *string, int32 length) {
if (handle==0) {
error(ERR_BADHANDLE);
return;
}
#ifndef NONET
if ((handle <= FIRSTHANDLE) && (fileinfo[handle].filetype==NETWORK)) {
if(net_bputstr(fileinfo[handle].nethandle, string, length)) error(ERR_CANTWRITE);
} else {
#endif
_kernel_oserror *oserror;
_kernel_swi_regs regs;
regs.r[0] = 2; /* OS_GBPB 2 = write to file at current file pointer position */
regs.r[1] = handle;
regs.r[2] = TOINT((int)string);
regs.r[3] = length;
oserror = _kernel_swi(OS_GBPB, ®s, ®s);
if (oserror!=NIL) {
error(ERR_CMDFAIL, oserror->errmess);
return;
}
if (regs.r[3]!=0) error(ERR_CANTWRITE); /* Not all the data was written to the file */
#ifndef NONET
}
#endif
}
/*
** 'fileio_printint' writes a four byte integer to a file in binary
** preceded with 0x40 to mark it as an integer.
** The number has to be written in 'big endian' format for compatibility
** with the Acorn interpreter
*/
void fileio_printint(int32 handle, int32 value) {
int32 n;
char temp[sizeof(int32)];
fileio_bput(handle, PRINT_INT);
memmove(temp, &value, sizeof(int32));
for (n=1; n<=sizeof(int32); n++) fileio_bput(handle, temp[sizeof(int32)-n]);
}
void fileio_printuint8(int32 handle, uint8 value) {
fileio_bput(handle, PRINT_UINT8);
fileio_bput(handle, value);
}
void fileio_printint64(int32 handle, int64 value) {
int32 n;
char temp[sizeof(int64)];
fileio_bput(handle, PRINT_INT64);
memmove(temp, &value, sizeof(int64));
for (n=1; n<=sizeof(int64); n++) fileio_bput(handle, temp[sizeof(int64)-n]);
}
/*
** 'fileio_printfloat' writes an eight byte floating point value to a file
** in binary preceded with 0x88 to mark it as floating point.
*/
void fileio_printfloat(int32 handle, float64 value) {
char temp[sizeof(float64)];
fileio_bput(handle, PRINT_FLOAT);
memmove(temp, &value, sizeof(float64));
fileio_bputstr(handle, temp, sizeof(float64));
}
/*
** 'fileio_printstring' writes a string to a file. If the length of the
** string is less than 256 bytes it is written in 'Acorn' format, that
** is, preceded with 0 and a single byte length with the string in
** reverse order. If longer than 255 bytes, the interpreter uses its own
** format. The string is preceded with 0x01 and its length written in
** four bytes. The string is written in 'true' order
*/
void fileio_printstring(int32 handle, char *string, int32 length) {
int32 n;
if (length<SHORT_STRING) { /* Write string in Acorn format */
fileio_bput(handle, PRINT_SHORTSTR);
fileio_bput(handle, length);
if (length>0) for (n=length-1; n>=0; n--) fileio_bput(handle, string[n]);
}
else { /* Long string - Use interpreter's extended format */
int32 temp = length;
fileio_bput(handle, PRINT_LONGSTR);
for (n=0; n<sizeof(int32); n++) { /* Write four byte length to file */
fileio_bput(handle, temp & BYTEMASK);
temp = temp>>BYTESHIFT;
}
fileio_bputstr(handle, string, length);
}
}
/*
** 'fileio_getptr' returns the current value of the file pointer for
** the file 'handle'
*/
int64 fileio_getptr(int32 handle) {
int32 pointer;
if (handle == 0) return 0;
pointer = _kernel_osargs(0, handle, 0); /* OS_Args 0 = read file pointer */
if (pointer==_kernel_ERROR) report();
return pointer;
}
/*
** 'fileio_setptr' is used to set the current file pointer of
** the file 'handle'
*/
void fileio_setptr(int32 handle, int64 newoffset) {
int32 result;
result = _kernel_osargs(1, handle, newoffset); /* OS_Args 1 = set file pointer */
if (result==_kernel_ERROR) report();
}
/*
** 'fileio_getext' returns the size of the file with handle 'handle'
*/
int64 fileio_getext(int32 handle) {
int32 extent;
extent = _kernel_osargs(2, handle, 0); /* OS_Args 2 = read file's extent (size) */
if (extent==_kernel_ERROR) report();
return extent;
}
/*
** 'fileio_setext' is called to alter the extent (size) of the file
** with handle 'handle'
*/
void fileio_setext(int32 handle, int64 newsize) {
int32 result;
result = _kernel_osargs(3, handle, newsize); /* OS_Args 3 = alter file's extent (size) */
if (result==_kernel_ERROR) report();
}
/*
** 'fileio_eof' returns the current end-of-file state of file 'handle',
** returning 'TRUE' if it is at end-of-file.
*/
int32 fileio_eof(int32 handle) {
int32 result;
result = _kernel_osargs(5, handle, 0); /* OS_Args 5 = Check end-of-file status */
if (result==_kernel_ERROR) report();
return result==0 ? FALSE : TRUE;
}
/*
** 'fileio_shutdown' is called at the end of the run of the
** interpreter. This is not required under RISC OS
*/
void fileio_shutdown(void) {
}
/*
** 'init_fileio' is called to initialise the file handling. This
** step is not required under RISC OS
*/
void init_fileio(void) {
}
#else /* !TARGET_RISCOS */
/* ================================================================== */
/* ========= NetBSD/Linux/DOS/Windows versions of functions ========= */
/* ================================================================== */
/*
** Files are opened in character mode under RISC OS, Linux and NetBSD
** as there is basically no difference between binary and character
** modes with these operating systems. This is what we want as Basic
** does not distinguish between these two types of file. Unfortunately
** DOS has to be different. In character mode the DOS libraries I have
** seen to like to stick 'carriage return' characters in the output
** stream whenever they see a 'linefeed' when writing to a file. Under
** DOS files are always treated as binary.
*/
#if defined(TARGET_DOSWIN)
#define INMODE "rb"
#define OUTMODE "wb+"
#define UPMODE "rb+"
#else
#define INMODE "r"
#define OUTMODE "w+"
#define UPMODE "r+"
#endif
/*
** 'map_handle' maps a Basic-style file handle to the corresponding entry
** in the 'fileinfo' table and checks that the handle is valid
*/
static int32 map_handle(int32 handle) {
handle = FIRSTHANDLE-handle;
if (handle<0 || handle>=MAXFILES || fileinfo[handle].filetype==CLOSED) error(ERR_BADHANDLE);
return handle;
}
/*
** 'fileio_openin' opens a file for input
*/
int32 fileio_openin(char *name, int32 namelen) {
FILE *thefile;
int32 n;
char filename [FNAMESIZE];
if ((namelen < 0) || (namelen > (FNAMESIZE - 1))) {
error(ERR_INVALIDFNAME);
return 0;
}
for (n=0; n<MAXFILES && fileinfo[n].stream!=NIL; n++); /* Find an unused handle */
if (n>=MAXFILES) {
error(ERR_MAXHANDLE);
return 0;
}
memmove(filename, name, namelen);
filename[namelen] = asc_NUL;
thefile = fopen(filename, INMODE);
if (thefile==NIL) {
char filenameb[FNAMESIZE];
STRLCPY(filenameb, filename, FNAMESIZE);
/* Append a .bbc suffix and try again */
STRLCAT(filenameb, ".bbc", FNAMESIZE);
thefile = fopen(filenameb, INMODE);
if (thefile==NIL) {
if (matrixflags.translatefname == 0) {
return 0;
} else {
char *tfilename = translatefname(filename);
thefile = fopen(tfilename, INMODE);
if (thefile==NIL) {
return 0; /* Could not open file - Return null handle */
} else {
/* We had to translate so set flags to always translate on save */
matrixflags.translatefname = 1;
}
}
}
}
fileinfo[n].stream = thefile;
fileinfo[n].filetype = OPENIN;
fileinfo[n].eofstatus = OKAY;
fileinfo[n].lastwaswrite = FALSE;
return FIRSTHANDLE-n;
}
/*
** 'fileio_openout' opens file 'name' for output, creating the
** file (or recreating it if it already exists).
** Note that the file is opened with mode "w+" so that it can be
** both written to and read from. RISC OS allows files opened for
** output to be read from as well
*/
int32 fileio_openout(char *name, int32 namelen) {
FILE *thefile;
int32 n;
char filename [FNAMESIZE];
if ((namelen < 0) || (namelen > (FNAMESIZE - 1))) {
error(ERR_INVALIDFNAME);
return 0;
}
for (n=0; n<MAXFILES && fileinfo[n].stream!=NIL; n++); /* Find an unused handle */
if (n>=MAXFILES) {
error(ERR_MAXHANDLE);
return 0;
}
memmove(filename, name, namelen);
filename[namelen] = asc_NUL;
if(matrixflags.translatefname == 1) {
char *tfilename = translatefname(filename);
thefile = fopen(tfilename, OUTMODE);
} else {
thefile = fopen(filename, OUTMODE);
}
if (thefile==NIL) {
error(ERR_OPENWRITE, filename);
return 0;
}
fileinfo[n].stream = thefile;
fileinfo[n].filetype = OPENOUT;
fileinfo[n].eofstatus = OKAY;
fileinfo[n].lastwaswrite = FALSE;
return FIRSTHANDLE-n;
}
/*
** 'fileio_openup' opens a file for update. The file must exist
** already. The file can both be read from and written to
*/
int32 fileio_openup(char *name, int32 namelen) {
FILE *thefile;
int32 n;
char filename [FNAMESIZE];
char filenameb[FNAMESIZE];
char *tfilename;
if ((namelen < 0) || (namelen > (FNAMESIZE - 1))) {
error(ERR_INVALIDFNAME);
return 0;
}
for (n=0; n<MAXFILES && fileinfo[n].stream!=NIL; n++); /* Find an unused handle */
if (n>=MAXFILES) {
error(ERR_MAXHANDLE);
return 0;
}
memmove(filename, name, namelen);
filename[namelen] = asc_NUL;
#ifndef NONET
/* Check, does it start "ip4:" if so use network handler to open it. */
if (!strncmp(filename, "ip0:", 4) || !strncmp(filename, "ip4:", 4) || !strncmp(filename, "ip6:", 4)) {
int handle;
handle=brandynet_connect(filename+4, filename[2], 1);
if (handle == -1) return 0;
fileinfo[n].stream = (void *)42; /* Not used, but != NIL */
fileinfo[n].filetype = NETWORK;
fileinfo[n].eofstatus = OKAY;
fileinfo[n].lastwaswrite = FALSE;
fileinfo[n].nethandle = handle;
return FIRSTHANDLE-n;
} else {
#endif
thefile = fopen(filename, UPMODE);
if (thefile==NIL) {
STRLCPY(filenameb, filename, FNAMESIZE);
STRLCAT(filenameb, ".bbc", FNAMESIZE); /* Append a .bbc suffix and try again */
thefile = fopen(filenameb, UPMODE);
if (thefile==NIL) {
if (matrixflags.translatefname == 0) {
return 0;
} else {
tfilename=translatefname(filename);
thefile = fopen(tfilename, UPMODE);
if (thefile==NIL) {
return 0; /* Could not open file - Return null handle */
} else {
/* We had to translate so set flags to always translate on save */
matrixflags.translatefname = 1;
}
}
}
}
fileinfo[n].stream = thefile;
fileinfo[n].filetype = OPENUP;
fileinfo[n].eofstatus = OKAY;
fileinfo[n].lastwaswrite = FALSE;
return FIRSTHANDLE-n;
#ifndef NONET
}
#endif
}
/*
** 'close_file' is a function used locally to close a file or network channel
*/
static void close_file(int32 handle) {
#ifndef NONET
if ((handle <= FIRSTHANDLE) && (fileinfo[handle].filetype == NETWORK)) {
brandynet_close(fileinfo[handle].nethandle);
fileinfo[handle].stream = NIL;
fileinfo[handle].filetype = CLOSED;
fileinfo[handle].lastwaswrite = FALSE;
fileinfo[handle].nethandle = -1;
} else {
#endif
fclose(fileinfo[handle].stream);
fileinfo[handle].stream = NIL;
fileinfo[handle].filetype = CLOSED;
fileinfo[handle].lastwaswrite = FALSE;
#ifndef NONET
}
#endif
}
/*
** 'fileio_close' closes the file given by 'handle' or all open files
** if 'handle' is zero
*/
void fileio_close(int32 handle) {
int32 n;
if (handle==0) { /* Close all open files */
for (n=0; n<MAXFILES; n++) {
if (fileinfo[n].filetype!=CLOSED) close_file(n);
}
}
else { /* close one file */
n = map_handle(handle);
if (fileinfo[n].filetype!=CLOSED) close_file(n);
}
}
/*
** 'fileio_bget' returns the next character from file 'handle'.
** Note that RISC OS allows you to read from a file that has been opened for
** writing. One byte can be read. The next attempt to read anything will
** result in an end-of-file error.
*/
int32 fileio_bget(int32 handle) {
int32 ch;
if (handle==0) {
error(ERR_BADHANDLE);
return 0;
}
handle = map_handle(handle);
#ifndef NONET
if (fileinfo[handle].filetype == NETWORK) {
ch=net_bget(fileinfo[handle].nethandle);
if (ch == -2) {
if (fileinfo[handle].eofstatus == PENDING) {
fileinfo[handle].eofstatus = ATEOF;
error(ERR_HITEOF);
return 0;
} else {
fileinfo[handle].eofstatus = PENDING;
}
}
} else {
#endif
if (fileinfo[handle].eofstatus!=OKAY) { /* If EOF is pending, flag an error */
fileinfo[handle].eofstatus = ATEOF;
error(ERR_HITEOF);
return 0;
}
else if (fileinfo[handle].filetype==OPENOUT) { /* If file is open for output, read one char */
fileinfo[handle].eofstatus = PENDING;
}
if (fileinfo[handle].lastwaswrite) { /* Ensure everything has been written to disk first */
fflush(fileinfo[handle].stream);
fileinfo[handle].lastwaswrite = FALSE;
}
ch = fgetc(fileinfo[handle].stream); /* Read a character */
if (ch==EOF) {
fileinfo[handle].eofstatus = PENDING; /* If call returns 'EOF' set 'PENDING EOF' flag */
ch = 0;
}
fileinfo[handle].lastwaswrite = FALSE;
#ifndef NONET
}
#endif
return ch;
}
/*
** 'fileio_getdol' reads a string from a file. It saves the text read at
** 'buffer'. Any terminating line end characters are removed. Both
** 'carriage return-linefeed' and 'linefeed' style line ends are recognised.
** The function returns the number of characters read (minus line end
** characters). Note that there is no check on the size of the buffer
** so it is up to the functions that call this one to ensure that the
** buffer is large enough to hold MAXSTRING (65536) characters.
*/
int32 fileio_getdol(int32 handle, char *buffer) {
char *p;
int32 length;
if (handle==0) {
error(ERR_BADHANDLE);
return 0;
}
handle = map_handle(handle);
if (fileinfo[handle].eofstatus!=OKAY) { /* If EOF is pending or EOF, flag an error */
fileinfo[handle].eofstatus = ATEOF;
error(ERR_HITEOF);
return 0;
}
if (fileinfo[handle].lastwaswrite) { /* Ensure everything has been written to disk first */
fflush(fileinfo[handle].stream);
fileinfo[handle].lastwaswrite = FALSE;
}
p = fgets(buffer, MAXSTRING, fileinfo[handle].stream);
if (p==NIL) {
error(ERR_CANTREAD); /* Read failed utterly */
return 0;
}
length = strlen(buffer);
p = buffer+length-1; /* Point at line end character */
if (*p==asc_LF) { /* Got a 'linefeed' at the end of the line */
length--;
if (length>0 && *(p-1)==asc_CR) length--; /* Got a 'carriage return-linefeed' pair */
}
return length;
}
static int32 fileio_read(FILE *handle) {
int32 ch;
ch = fgetc(handle);
if (ch==EOF) error(ERR_CANTREAD);
return ch;
}
/*
** 'fileio_getnumber' reads a binary number from the file with
** handle 'handle'. It stores the result at the address given
** by 'ip' if integer or at 'fp' is floating point. 'isint' is
** set to TRUE if the value is an integer.
**
** Note that integers are stored in big endian format in the file
** by PRINT#. Floating point values are written in the byte order
** used by the Acorn interpreter under RISC OS. This means that
** some fiddling about is needed to make sure the bytes of the
** value are in the right order under other operating systems and
** other types of hardware. The code to do this was written by
** Darren Salt, along with the support for reading Acorn's five
** byte floating point format
*/
void fileio_getnumber(int32 handle, boolean *isint, int64 *ip, float64 *fp) {
FILE *stream;
int32 n, marker;
char temp[sizeof(float64)];
memset(temp,0,sizeof(float64));
if (handle==0) {
error(ERR_BADHANDLE);
return;
}
handle = map_handle(handle);
if (fileinfo[handle].eofstatus!=OKAY) { /* If EOF is pending, flag an error */
fileinfo[handle].eofstatus = ATEOF;
error(ERR_HITEOF);
return;
}
if (fileinfo[handle].lastwaswrite) { /* Ensure everything has been written to disk first */
fflush(fileinfo[handle].stream);
fileinfo[handle].lastwaswrite = FALSE;
}
stream = fileinfo[handle].stream;
marker = fileio_read(stream);
switch (marker) {
case PRINT_INT:
*ip = 0;
for (n=24; n>=0; n-=8) *ip |= fileio_read(stream) << n;
*isint = TRUE;
break;
case PRINT_UINT8:
*ip = fileio_read(stream);
*isint = TRUE;
break;
case PRINT_INT64:
*ip = 0;
for (n=56; n>=0; n-=8) *ip |= (int64)fileio_read(stream) << n;
*isint = TRUE;
break;
case PRINT_FLOAT:
switch (double_type) {
case XMIXED_ENDIAN:
for (n=0; n<sizeof(float64); n++) temp[n] = fileio_read(stream);
break;
case XLITTLE_ENDIAN:
for (n=0; n<sizeof(float64); n++) temp[n^4] = fileio_read(stream);
break;