commit 1e02ae1

zzo38  ·  2022-10-31 03:56:36 +0000 UTC
parent 4587b1d
Implement file system writing, and a few other improvements.
1 files changed,  +43, -16
M main.c
M main.c
+43, -16
  1@@ -263,6 +263,7 @@ static void run(Uint16 pc) {
  2   if(bicycle && (device[0].d[0] || device[0].d[1])) {
  3     if(device[0].d[2]) mem[(device[0].d[2]<<8)+0xFE]=v; else fprintf(stderr,"Uxn error %d at 0x%04X\n",v,pc);
  4     pc=GET16(device[0].d);
  5+    PUT16(device[0].d,0);
  6     ds.p=rs.p=0;
  7     goto start;
  8   } else {
  9@@ -347,15 +348,17 @@ static void extension_out(Device*dev,Uint8 id) {
 10   Uint16 len;
 11   switch(id) {
 12     case 8:
 13-      if((dev->d[8]>>4)==0) {
 14-        x=GET16(dev->d+2);
 15-        y=GET16(dev->d+4);
 16-        len=GET16(dev->d+6);
 17-        if(dev->d[8]&4) len=256;
 18-        if(x+len>0x10000 || y+len>0x10000) uxnerr(5,x);
 19-        *(dev->d[8]&1?&p:&q)=mem+x;
 20-        *(dev->d[8]&1?&q:&p)=mem+y+(dev->d[8]&2?0x20000:0x10000);
 21-        memcpy(q,p,len);
 22+      switch(dev->d[8]>>4) {
 23+        case 0:
 24+          x=GET16(dev->d+2);
 25+          y=GET16(dev->d+4);
 26+          len=GET16(dev->d+6);
 27+          if(dev->d[8]&4) len=256;
 28+          if(x+len>0x10000 || y+len>0x10000) uxnerr(5,x);
 29+          *(dev->d[8]&1?&p:&q)=mem+x;
 30+          *(dev->d[8]&1?&q:&p)=mem+y+(dev->d[8]&2?0x20000:0x10000);
 31+          memcpy(q,p,len);
 32+          break;
 33       }
 34       dev->d[8]=0;
 35       break;
 36@@ -418,7 +421,7 @@ static void files_out(Device*dev,Uint8 id) {
 37         x=fread(mem+addr,1,len,uf->file);
 38         PUT16(dev->d+2,x);
 39         if(ferror(uf->file)) {
 40-          warn("Error reading file");
 41+          warn("Error reading file '%s'",uf->name);
 42           clearerr(uf->file);
 43         }
 44       } else if(uf->mode==3) {
 45@@ -430,23 +433,47 @@ static void files_out(Device*dev,Uint8 id) {
 46         uf->dir=0;
 47         uf->de=0;
 48         if(!uf->name[0]) break;
 49-        if(uf->name[0]=='.' && !uf->name[1] && (uf->dir=opendir(uf->name))) {
 50+        if(uf->dir=opendir(uf->name)) {
 51           uf->mode=3;
 52           goto read;
 53         } else if(uf->file=fopen(uf->name,"r")) {
 54           uf->mode=1;
 55           goto read;
 56         } else {
 57-          warn("Cannot open file for reading");
 58+          warn("Cannot open file '%s' for reading",uf->name);
 59         }
 60       }
 61       break;
 62     case 15:
 63       addr=GET16(dev->d+14);
 64       dev->d[2]=dev->d[3]=0;
 65-      if(!allow_write) break;
 66+      if(!allow_write) {
 67+        warnx("Not allowed to write to file '%s'.",uf->name);
 68+        break;
 69+      }
 70       write:
 71-      //TODO
 72+      if(uf->mode==2) {
 73+        if(!len) break;
 74+        x=fwrite(mem+addr,1,len,uf->file);
 75+        PUT16(dev->d+2,x);
 76+        if(ferror(uf->file)) {
 77+          warn("Error writing file '%s'",uf->name);
 78+          clearerr(uf->file);
 79+        }
 80+      } else {
 81+        if(uf->file) fclose(uf->file);
 82+        if(uf->dir) closedir(uf->dir);
 83+        uf->file=0;
 84+        uf->dir=0;
 85+        uf->de=0;
 86+        if(!uf->name[0]) break;
 87+        if(uf->file=fopen(uf->name,dev->d[7]?"a":"w")) {
 88+          uf->mode=2;
 89+          goto read;
 90+        } else {
 91+          warn("Cannot open file '%s' for %sing",uf->name,dev->d[7]?"append":"writ");
 92+        }
 93+      }
 94       break;
 95   }
 96 }
 97@@ -462,7 +489,7 @@ static Uint8 files_in(Device*dev,Uint8 id) {
 98         c=fgetc(uf->file);
 99         if(c==EOF) {
100           if(ferror(uf->file)) {
101-            warn("Error reading file");
102+            warn("Error reading file '%s'",uf->name);
103             clearerr(uf->file);
104           }
105           c=0;
106@@ -486,7 +513,7 @@ static Uint8 files_in(Device*dev,Uint8 id) {
107           uf->mode=1;
108           goto read;
109         } else {
110-          warn("Cannot open file for reading");
111+          warn("Cannot open file '%s' for reading",uf->name);
112         }
113       }
114       return 0;