/************************************
 * FILETEST - Test/Demo Module fileio *
 **************************************/
 
//Specify System Header using -H option
#include <screen.h02>
#include <stddef.h02>
#include <stdlib.h02>
#include <intlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <memory.h02>
#include <fileio.h02>

const char txtfil = "TESTFILE.TXT";
const char savfil = "SAVEFILE.TXT";
char fp;        //Text File Pointer
char err;       //Error Code
char c;         //Character Read from File
char i;         //Loop Index Variable
char n;         //Number of Characters to Write
int addr;       //End Address for load()
int filpos;     //Position in File
char l[255];    //Array for load()
char r[255];    //Array for fread()
char s[128];    //String for fgets()
const char str1 = "THE QUICK BROWN FOX ";
const char str2 = "JUMPED OVER THE LAZY DOG";
const char save = "ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210";
const char bnry = "ZYW1XVU2TS3RQ4PO5NM6LKJI7HG8FED9CBA0";
char aa,xx,yy,zz;  //Function Arguments

main:
  clrscr();
  fsinit(); //Initialize File System
  fp = opnfil(#MWRITE, &txtfil);
  if (fp) {
    wstrng(fp); //Test fputs()
    flshfl(fp); //Test fflush()
    wbnry(fp);  //Test fwrite()
    prtpos(fp); //Print Position in File 
  }
  clsfil(fp);
  fp = opnfil(#MAPPND, &txtfil);
  if (fp) wchars(fp); //Test fputc()
  clsfil(fp);
  sfile(0, &savfil);

  fp = opnfil(#MREAD, &txtfil);
  else printf(n, "  READ %d CHARACTERS%n");
  if (fp) {
    rstrng(fp);
    rbnry(fp);
  }
  clsfil(fp);
  fp = opnfil(#MREAD, &txtfil);
  if (fp) {
    posfil(fp);
    rchars(fp);
    rwdfil(fp);
    rstrng(fp);
  }
  clsfil(fp);
  lfile(#MABSLT, &savfil); //Test fload()

  goto exit;

void chkerr() {
  if (err) {
    printf(err, " ERROR %d%n");
    goto exit;
  }
}

void flshfl(aa) {
  printf(aa, "FFLUSH(%d): ");
  zz, err = fflush(aa); chkerr();
  puts("FLUSHED, "); anykey(0);
}

void opnfil(aa,yy,xx) {
  setdst();printf("FOPEN($%x,\"%s\"): "); 
  zz, err = fopen(aa,yy,xx); chkerr();
  printf(zz,"CHANNEL=%d%n");
  return zz;
}

void prtpos(aa) {
  printf("FTELL(%d): ");
  err, filpos = ftell(aa); chkerr();
  setdst(filpos); printf("POSITION=$%w%n");
  return aa;
}

void posfil(aa) {
  setdst(filpos); printf(aa, "FSEEK(%d,$%w): ");
  err = fseek(aa, filpos); chkerr();
  putln("POSITIONED");
}

void rwdfil(aa) {
  printf(aa, "REWIND(%d): ");
  err = rewind(aa, filpos); chkerr();
  putln("REWOUND");
}

void wbnry(aa) {
  setdst(&bnry); printf("FWRITE(%d,\"%s\"): ");
  fsaddr(&bnry);
  n, err = fwrite(@bnry, aa); chkerr();
  printf(n, "WROTE %d BYTES%n");
}

void wchars(aa) {
  puts("FPUTC '");
  for (i='0'; i<='Z'; i++) {zz, err = fputc(i, aa); chkerr(); putc(zz);}
  putln("'");
}

void wstrng(aa) {
  setdst(str1); printf(aa, "FPUTS(%d,\"%s\"): ");
  n, err = fputs(aa, str1); chkerr();
  printf(n, "WROTE %d CHARACTERS%n");

  setdst(str2); printf(aa, "FPUTLN(%s,\"%s\"): ");
  n, err = fputln(aa, str2); chkerr();
  printf(n, "WROTE %d CHARACTERS%n");
}
void sfile(aa, yy, xx) {
  setdst(); printf("FSNAME(\"%s\"); ");
  fsname(.,yy,xx); //Set Filename
  setdst(&save); printf("FSADDR($%w); ");
  fsaddr(&save);   //Set Start Address
  setdst(&bnry); printf(aa, "FSAVE(%d,$%w): ");
  err, addr = fsave(aa, &bnry); chkerr();
  putln("FILE SAVED");
}

void rbnry(aa) {
  setdst(&r); printf("FSADDR($%w); ");
  fsaddr(&r); 
  printf(@bnry, "FREAD(%d,"); printf(aa,"%d): ");
  n, err = fread(@bnry, aa); chkerr();
  puts(&r); printf(n, ", READ %d BYTES%n");
}

void lfile(aa, yy, xx) {
  setdst(); printf("FSNAME(\"%s\"); ");
  fsname(.,yy,xx); //Set Filename
  memclr(255, &l); printf("FSADDR($%w); ");
  fsaddr(&l);      //Set Load Address  
  printf(aa, "FLOAD(%d): ");
  err, addr = fload(aa); chkerr();
  setdst(addr); printf("END=$%w, "); putln(&l);
}

void rchars(aa) {
  printf("FGETC(A): ");
  while(!feof(aa)) {
    c, err = fgetc(aa); 
    if (err == $ff) break; else chkerr();
    putc(c);
  }
  newlin();
}

void rstrng(aa) {
  printf("FGETS(%d, &S): ");
  n, err = fgets(aa, &s); chkerr();
  puts(&s); printf(n, "  READ %d CHARACTERS%n");
}

void clsfil(aa) {
  printf(aa, "FCLOSE(%d): ");
  err = fclose(aa); chkerr();
  putln("CLOSED");
}
