1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +00:00
C02/test/filetest.c02
2020-10-08 16:46:15 -04:00

179 lines
4.1 KiB
Plaintext

/************************************
* 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 $%x 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 $%x%n");
goto exit;
}
}
void flshfl(aa) {
printf(aa, "FFLUSH($%x): ");
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=$%x%n");
return zz;
}
void prtpos(aa) {
printf("FTELL($%x): ");
err, filpos = ftell(aa); chkerr();
setdst(filpos); printf("POSITION=$%w%n");
return aa;
}
void posfil(aa) {
setdst(filpos); printf(aa, "FSEEK($%x,$%w): ");
err = fseek(aa, filpos); chkerr();
putln("POSITIONED");
}
void rwdfil(aa) {
printf(aa, "REWIND($%x): ");
err = rewind(aa, filpos); chkerr();
putln("REWOUND");
}
void wbnry(aa) {
setdst(&bnry); printf("FWRITE($%x,\"%s\"): ");
fsaddr(&bnry);
n, err = fwrite(@bnry, aa); chkerr();
printf(n, "WROTE $%x 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($%x,\"%s\"): ");
n, err = fputs(aa, str1); chkerr();
printf(n, "WROTE $%x CHARACTERS%n");
setdst(str2); printf(aa, "FPUTLN(%s,\"%s\"): ");
n, err = fputln(aa, str2); chkerr();
printf(n, "WROTE $%x 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($%x,$%w): ");
err, addr = fsave(aa, &bnry); chkerr();
putln("FILE SAVED");
}
void rbnry(aa) {
setdst(&r); printf("FSADDR($%w); ");
fsaddr(&r);
printf(@bnry, "FREAD($%x,"); printf(aa,"$%x): ");
n, err = fread(@bnry, aa); chkerr();
puts(&r); printf(n, ", READ $%x 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($%x): ");
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($%x, &S): ");
n, err = fgets(aa, &s); chkerr();
puts(&s); printf(n, " READ $%x CHARACTERS%n");
}
void clsfil(aa) {
printf(aa, "FCLOSE($%x): ");
err = fclose(aa); chkerr();
putln("CLOSED");
}