commit 843f68a

zzo38  ·  2023-06-20 01:01:51 +0000 UTC
parent 2ae3e9c
Change the nonstandard file seeking so that it automatically opens the file for both reading and writing if it is not already open.
2 files changed,  +16, -5
M main.c
+4, -4
 1@@ -238,10 +238,10 @@ emulator to the program), ? for parameters that may be input and/or output,
 2 
 3 a0 offset%
 4   Given a 32-bit offset, seek the currently opened file to the specified
 5-  absolute offset. (You must open the file first, by attempting to read
 6-  from or write to it; you can read/write zero bytes to open it without
 7-  reading/writing anything.) (The effect of seeking (absolute or relative)
 8-  when a file is opened in append mode is unspecified.)
 9+  absolute offset. (The effect of seeking (absolute or relative) when a
10+  file is opened in append mode is unspecified.) If the file is not already
11+  open, it is opened for reading and writing (the append flag is ignored);
12+  if writing is not allowed, then it is opened for reading only.
13 
14 a1 offset%
15   Given a signed 32-bit offset, seek the currently opened file to the
M main.c
+12, -1
 1@@ -439,7 +439,7 @@ static Uint32 files_seek(UxnFile*uf,Uint8 mode,Uint32 len) {
 2 }
 3 
 4 static int files_set(Device*dev,int rw) {
 5-  // rw: 0(close) 1(read) 2(write)
 6+  // rw: 0(close) 1(read) 2(write) 3(both)
 7   // return: 1 if file opened, 0 if not opened
 8   Uint8 m=dev->d[7];
 9   UxnFile*uf=dev->aux;
10@@ -468,6 +468,16 @@ static int files_set(Device*dev,int rw) {
11     } else {
12       warn("Cannot open file '%s' for writing",uf->name);
13     }
14+  } else if(rw==3) {
15+    if(allow_write && (uf->file=fopen(uf->name,"r+"))) {
16+      uf->mode=4;
17+      return 1;
18+    } else if(uf->file=fopen(uf->name,"r")) {
19+      uf->mode=1;
20+      return 1;
21+    } else {
22+      warn("Cannot open file '%s' for reading/writing",uf->name);
23+    }
24   }
25   return 0;
26 }
27@@ -586,6 +596,7 @@ static void files_exp(Device*dev,Uint16 addr) {
28     case 0 ... 2:
29       x=GET16(mem+addr+1)<<16;
30       x|=GET16(mem+addr+3);
31+      if(!uf->mode) files_set(dev,3);
32       files_seek(uf,mem[addr]&15,x);
33       break;
34     case 3: