diff --git a/common_src/file_io.h b/common_src/file_io.h new file mode 100644 index 0000000..cd76d71 --- /dev/null +++ b/common_src/file_io.h @@ -0,0 +1,129 @@ +#ifndef __FILE_IO_H__ +#define __FILE_IO_H__ + + +#define FIO_SEEK_START 0 +#define FIO_SEEK_CUR 1 +#define FIO_SEEK_END 2 + +#define FIO_OPEN_RD 0 +#define FIO_OPEN_WR 1 +#define FIO_OPEN_RW 2 + +#ifdef USE_CUSTOM_FILE_IO /* In case the C stdlib file handling is buggy, like in DOS/OpenWatcom */ + + #define FIO_CACHE 32 + + struct file_t { + unsigned short fh; /* file handle */ + unsigned long flen; /* file length */ + unsigned long curpos; /* current offset position (ftell) */ + unsigned char buff[FIO_CACHE]; /* buffer storage */ + unsigned long bufoffs; /* offset of buffer */ + unsigned char flags; /* flags */ + }; + + /* open file fname and set fhandle with the associated file handle. returns 0 on success, non-zero otherwise */ + int fio_open(struct file_t *f, const char *fname, int mode); + + /* reads count bytes from file pointed at by fhandle, and writes the data into buff. returns the number of bytes actually read */ + int fio_read(struct file_t *f, void *buff, int count); + + /* seek to offset position of file pointed at by fhandle. returns current file position on success, a negative error otherwise */ + signed long fio_seek(struct file_t *f, unsigned short origin, signed long offset); + + /* close file handle. returns 0 on success, non-zero otherwise */ + int fio_close(struct file_t *f); + +#else /* Just use the C standard library */ + #include + + struct file_t { + FILE *fh; /* file handle */ + long flen; /* file length */ + long curpos; /* current offset position (ftell) */ + }; + + /* open file fname and set fhandle with the associated file handle. returns 0 on success, non-zero otherwise */ + static int fio_open(struct file_t *f, const char *fname, int mode) { + switch(mode) { + case FIO_OPEN_RD: + f->fh = fopen(fname, "r"); + break; + case FIO_OPEN_WR: + f->fh = fopen(fname, "w"); + break; + case FIO_OPEN_RW: + f->fh = fopen(fname, "r+"); + break; + default: + return -1; + } + + if(f->fh==NULL) { + return -1; + } + + if(fseek(f->fh, 0, SEEK_END) != 0) { + return -1; + } + + if( (f->flen = ftell(f->fh)) < 0 ) { + return -1; + } + + if(fseek(f->fh, 0, SEEK_SET) != 0) { + return -1; + } + + f->curpos = 0; + return 0; + } + + /* reads count bytes from file pointed at by fhandle, and writes the data into buff. returns the number of bytes actually read */ + static int fio_read(struct file_t *f, void *buff, int count) { + int out = fread(buff, 1, count, f->fh); + if( (f->curpos = ftell(f->fh)) < 0 ) { + return -1; + } + return out; + } + + /* seek to offset position of file pointed at by fhandle. returns current file position on success, a negative error otherwise */ + static signed long fio_seek(struct file_t *f, unsigned short origin, signed long offset) { + int o = SEEK_SET; + switch(origin) { + case FIO_SEEK_START: + o = SEEK_SET; + break; + case FIO_SEEK_CUR: + o = SEEK_CUR; + break; + case FIO_SEEK_END: + o = SEEK_END; + break; + default: + return -1; + } + + if(fseek(f->fh, offset, o) != 0) { + return -1; + } + + if( (f->curpos = ftell(f->fh)) < 0 ) { + return -1; + } + + return 0; + } + + /* close file handle. returns 0 on success, non-zero otherwise */ + static int fio_close(struct file_t *f) { + if(fclose(f->fh) == EOF) { + return -1; + } + return 0; + } +#endif /* USE_CUSTOM_FILE_IO */ + +#endif /* __FILE_IO_H__ */ diff --git a/common_src/mt32-pi.c b/common_src/mt32-pi.c index efb2ce4..874d0b9 100644 --- a/common_src/mt32-pi.c +++ b/common_src/mt32-pi.c @@ -34,10 +34,7 @@ #include "midi_dev.h" #include "getopt.h" #include "delay.h" - -#ifdef USE_CUSTOM_FILE_IO -#include "fio.h" -#endif +#include "file_io.h" #ifndef PROGRAM_NAME #define PROGRAM_NAME "MT32-PI.EXE" @@ -351,40 +348,25 @@ int main(int argc, char *argv[]) { // -Y/--syx FILE.SYX if(syx_fname[0] != '\0') { unsigned char fbuf[512]; - #ifdef USE_CUSTOM_FILE_IO - struct fiofile_t fh; + struct file_t fh; int ret; - #else - FILE *fh; - #endif if(verbose) fprintf(stderr, "Sending %s.\n", syx_fname); - #ifdef USE_CUSTOM_FILE_IO - ret = fio_open(syx_fname, FIO_OPEN_RD, &fh); + + ret = fio_open(&fh, syx_fname, FIO_OPEN_RD); if(ret!=0) { fprintf(stderr, "ERROR: Can't open %s.\n", syx_fname); return EXIT_FAILURE; } - #else - fh = fopen(syx_fname, "r"); - if(fh==NULL) { - fprintf(stderr, "ERROR: Can't open %s.\n", syx_fname); - return EXIT_FAILURE; - } - #endif + // go through file while(1) { int i, found_start, found_end, start_index, end_index; int n; - #ifdef USE_CUSTOM_FILE_IO long int read_start = fh.curpos; n = fio_read(&fh, fbuf, 512); - #else - long int read_start = ftell(fh); - n = fread(fbuf, 1, 512, fh); - #endif found_start = 0; found_end = 0; @@ -423,21 +405,11 @@ int main(int argc, char *argv[]) { mididev_send_bytes(fbuf+start_index, end_index - start_index+1); // Wait grace period delay_ms(55); - #ifdef USE_CUSTOM_FILE_IO if(read_start + end_index+1 == fh.curpos) break; fio_seek(&fh, FIO_SEEK_START, read_start + end_index+1); - #else - if(read_start + end_index+1 == ftell(fh)) - break; - fseek(fh, read_start + end_index+1, SEEK_SET); - #endif } - #ifdef USE_CUSTOM_FILE_IO fio_close(&fh); - #else - fclose(fh); - #endif } mididev_deinit(); @@ -487,46 +459,25 @@ static void bmp_to_sysex_disp_sc55(unsigned char *sysexbuf, const char *fname, i const unsigned char prefix[7] = { 0xF0, 0x41, 0x10, 0x45, 0x12, 0x10, 0x01 }; int i; unsigned char fbuf[64]; - #ifdef USE_CUSTOM_FILE_IO - struct fiofile_t fh; + struct file_t fh; int ret; - #else - FILE *fh; - #endif unsigned char pix_offset; memset(sysexbuf, '\0', 74); /* Copy SysEx start and display cmd into msg */ memcpy(sysexbuf, prefix, 7); - #ifdef USE_CUSTOM_FILE_IO - ret = fio_open(fname, FIO_OPEN_RD, &fh); + ret = fio_open(&fh, fname, FIO_OPEN_RD); if(ret!=0) { fprintf(stderr, "ERROR: Can't open %s.\n", fname); abort(); } - #else - fh = fopen(fname, "r"); - if(fh==NULL) { - fprintf(stderr, "ERROR: Can't open %s.\n", sc55_bmp_fname); - abort(); - } - #endif // check if this is a BMP - #ifdef USE_CUSTOM_FILE_IO fio_read(&fh, fbuf, 2); - #else - fread(fbuf, 1, 2, fh); - #endif if(!(fbuf[0]=='B' && fbuf[1]=='M')) { fprintf(stderr, "ERROR: %s is not a valid BMP file.\n", sc55_bmp_fname); - #ifdef USE_CUSTOM_FILE_IO fio_close(&fh); - #else - fclose(fh); - #endif abort(); } - #ifdef USE_CUSTOM_FILE_IO // check offset of pixel array fio_seek(&fh, FIO_SEEK_START, 0xA); fio_read(&fh, &pix_offset, 1); @@ -534,15 +485,6 @@ static void bmp_to_sysex_disp_sc55(unsigned char *sysexbuf, const char *fname, i fio_seek(&fh, FIO_SEEK_START, pix_offset); fio_read(&fh, fbuf, 64); fio_close(&fh); - #else - // check offset of pixel array - fseek(fh, 0xA, SEEK_SET); - fread(&pix_offset, 1, 1, fh); - // read pixel array - fseek(fh, pix_offset, SEEK_SET); - fread(fbuf, 1, 64, fh); - fclose(fh); - #endif // Perform bit-manipulation magic to // build the SysEx diff --git a/dos_src/FIO.C b/dos_src/FIO.C index af3b46b..ac05fbc 100644 --- a/dos_src/FIO.C +++ b/dos_src/FIO.C @@ -1,17 +1,20 @@ /* - * File I/O, based on DOS int 21h calls - * This file is part of the DOSMid project + * File I/O, based on the DOSMID source code * http://dosmid.sourceforge.net * * Copyright (C) 2018 Mateusz Viste + * with modifications + * Copyright (C) 2021 Andreas Zdziarstek */ #include /* REGS */ #include /* _fmemcpy() */ -#include "fio.h" /* include self for control */ +#include "file_io.h" -static void fio_seek_sync(struct fiofile_t *f) { +#define FIO_FLAG_SEEKSYNC 1 + +static void fio_seek_sync(struct file_t *f) { /* DOS 2+ - LSEEK - SET CURRENT FILE POSITION AH = 42h AL = origin of move @@ -43,7 +46,7 @@ static void fio_seek_sync(struct fiofile_t *f) { } /* seek to offset position of file pointed at by fhandle. returns current file position on success, a negative error otherwise */ -signed long fio_seek(struct fiofile_t *f, unsigned short origin, signed long offset) { +signed long fio_seek(struct file_t *f, unsigned short origin, signed long offset) { switch (origin) { case FIO_SEEK_START: if (offset < 1) { @@ -62,30 +65,7 @@ signed long fio_seek(struct fiofile_t *f, unsigned short origin, signed long off return(f->curpos); } -/* reads a line from file pointed at by fhandle, fill buff up to buflen bytes. returns the line length (possibly longer than buflen), or -1 on EOF */ -int fio_getline(struct fiofile_t *f, void far *buff, short buflen) { - unsigned char bytebuf; - short linelen = 0; - buflen--; /* leave space for the zero terminator */ - for (;;) { - if (fio_read(f, &bytebuf, 1) == 0) { /* EOF */ - if (linelen == 0) linelen = -1; - break; - } - if (bytebuf == '\n') break; - if (bytebuf == '\r') continue; - linelen++; - if (buflen > 0) { - buflen--; - *((unsigned char far *)buff) = bytebuf; - buff = ((unsigned char far *)buff) + 1; - } - } - *((unsigned char far *)buff) = 0; - return(linelen); -} - -static void loadcache(struct fiofile_t *f) { +static void loadcache(struct file_t *f) { union REGS regs; struct SREGS sregs; fio_seek_sync(f); @@ -100,7 +80,7 @@ static void loadcache(struct fiofile_t *f) { } /* open file fname and set fhandle with the associated file handle. returns 0 on success, non-zero otherwise */ -int fio_open(char far *fname, int mode, struct fiofile_t *f) { +int fio_open(struct file_t *f, const char *fname, int mode) { /* DOS 2+ - OPEN - OPEN EXISTING FILE AH = 3Dh AL = access and sharing modes @@ -142,7 +122,7 @@ int fio_open(char far *fname, int mode, struct fiofile_t *f) { } /* reads count bytes from file pointed at by fhandle, and writes the data into buff. returns the number of bytes actually read, or a negative number on error */ -int fio_read(struct fiofile_t *f, void far *buff, int count) { +int fio_read(struct file_t *f, void *buff, int count) { /* DOS 2+ - READ - READ FROM FILE OR DEVICE * AH = 3Fh * BX = file handle @@ -178,7 +158,7 @@ int fio_read(struct fiofile_t *f, void far *buff, int count) { } /* close file handle. returns 0 on success, non-zero otherwise */ -int fio_close(struct fiofile_t *f) { +int fio_close(struct file_t *f) { /* DOS 2+ - CLOSE - CLOSE FILE AH = 3Eh BX = file handle