--- os2sdk/demos/apps/sse/ssefile.c 2018/08/09 12:25:13 1.1.1.1 +++ os2sdk/demos/apps/sse/ssefile.c 2018/08/09 12:26:04 1.1.1.2 @@ -1,6 +1,10 @@ +/* Created by Microsoft Corp. 1987 */ + +#define INCL_DOSFILEMGR + +#include #include -#include -#include +#include #include #include "ssedefs.h" @@ -9,9 +13,9 @@ /*** readfile - reads file into filebuffer * - * readfile reads a file using DOSREAD into a file buffer - * and then parses out lines and places them into segments - * via 'addline'. + * readfile reads a file using DosRead into a file buffer + * and then parses out lines and places them into segments + * via 'addline'. * * readfile (fhandle) * @@ -20,80 +24,80 @@ * EXIT rc - return code of readfile and addline * * readfile uses line-feeds('\n') as seperators between lines. - * Lines longer than LINESIZE are cut at LINESIZE and continued - * on the next line. All non printable characters execpt TABS are - * ignored in buildling the line. - * - * EFFECTS fbuffer - by reading the file into fbuffer - * bytesread - by setting it to the number of bytes read - * TotalLines - by incrementing it for every line - * LineTable - by adding new lines via 'addline' - * SegTable - by using free space via 'addline' - * and by allocating segments via 'alloseg' - * TotalSegs - by incrementing it via 'alloseg' - * memory - by allocating segments via 'alloseg' + * Lines longer than LINESIZE are cut at LINESIZE and continued + * on the next line. All non printable characters execpt TABS are + * ignored in buildling the line. + * + * EFFECTS fbuffer - by reading the file into fbuffer + * bytesread - by setting it to the number of bytes read + * TotalLines - by incrementing it for every line + * LineTable - by adding new lines via 'addline' + * SegTable - by using free space via 'addline' + * and by allocating segments via 'alloseg' + * TotalSegs - by incrementing it via 'alloseg' + * memory - by allocating segments via 'alloseg' */ short readfile(fhandle) -unsigned short fhandle; +USHORT fhandle; { - register unsigned short i, j; - unsigned char line[LINESIZE +1]; /* temp buffer for building line */ - short rc; /* return code of DOS */ + register USHORT i, j; + UCHAR line[LINESIZE +1]; /* temp buffer for building line */ + short rc; /* return code of DOS */ for (bytesread = 1, j = 0, rc = 0; (bytesread != 0) && (!rc); ) { - rc = DOSREAD(fhandle, (char far *)fbuffer, - FBUFFSIZE, (unsigned far *)&bytesread); + rc = DosRead(fhandle, fbuffer, + FBUFFSIZE, &bytesread); - if (rc == 0) { -/* scan buffer for complete lines */ - for(i =0; (i < bytesread) && (!rc); i++) { - line[j] = fbuffer[i]; - - if (isprint(line[j])) { - if (j < (LINESIZE)) { -/* continued building line */ - j++; - } - else { -/* line length greater than line size */ - rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); - ++TotalLines; - line[0] = line[j]; - j = 1; - } - } - else if (line[j] == '\t') { -/* fill with spaces to next tab */ - if ((j/TABSIZE +1) > MAXTABS) { -/* TAB makes line greater than LINSIZE */ - for ( ; j < LINESIZE; j++) - line[j] = ' '; - rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); - ++TotalLines; - j = 0; - } - do { - line[j] = ' '; - ++j; - } while ((j % TABSIZE) != 0); - } - else if (line[j] == '\n') { -/* a complete line */ - rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); - ++TotalLines; - j = 0; - } - } - if ( (bytesread < FBUFFSIZE) && (j >0) && (!rc) ) { -/* line left in buffer */ - rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); - ++TotalLines; - j = 0; - } - } + if (rc == 0) { +/* scan buffer for complete lines */ + for(i =0; (i < bytesread) && (!rc); i++) { + line[j] = fbuffer[i]; + + if (isprint(line[j])) { + if (j < (LINESIZE)) { +/* continued building line */ + j++; + } + else { +/* line length greater than line size */ + rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); + ++TotalLines; + line[0] = line[j]; + j = 1; + } + } + else if (line[j] == '\t') { +/* fill with spaces to next tab */ + if ((j/TABSIZE +1) > MAXTABS) { +/* TAB makes line greater than LINSIZE */ + for ( ; j < LINESIZE; j++) + line[j] = ' '; + rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); + ++TotalLines; + j = 0; + } + do { + line[j] = ' '; + ++j; + } while ((j % TABSIZE) != 0); + } + else if (line[j] == '\n') { +/* a complete line */ + rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); + ++TotalLines; + j = 0; + } + } + if ( (bytesread < FBUFFSIZE) && (j >0) && (!rc) ) { +/* line left in buffer */ + rc = (addline(TotalLines, j, line) || !(TotalLines < MAXLINES)); + ++TotalLines; + j = 0; + } + } } return(rc); } @@ -109,63 +113,61 @@ unsigned short fhandle; * * ENTRY fhandle - handle of file to be written. * - * EXIT rc - return code of DOSWRITE and - * (byteswritten != bytes in buffer to write) + * EXIT rc - return code of DosWrite and + * (byteswritten != bytes in buffer to write) * * savefile fills a buffer with complete lines until the buffer - * can not accept anoter complete line, then savefile calls - * DOSWRITE with the number of bytes in the buffer. If an - * error occurs durning the write or the number bytes in the + * can not accept another complete line, then savefile calls + * DosWrite with the number of bytes in the buffer. If an + * error occurs during the write or the number bytes in the * buffer does not match the number of bytes written out then * savefile aborts and returns an error code via rc. * * EFFECTS fbuffer - by filling it with 'lines' from the file - * the file - by writing out any changes made to it + * the file - by writing out any changes made to it */ short savefile(fhandle) -unsigned short fhandle; +USHORT fhandle; { - unsigned short byteswritten; /* number written to file - returned by DOS */ - unsigned long npl; /* file pointer - not used */ - unsigned short i, j, k; /* indexes */ - unsigned long totalbytes; /* total bytes written to file */ - short rc; /* return code of DOS call */ + USHORT byteswritten; /* number written to file - returned by OS/2 */ + ULONG npl; /* file pointer - not used */ + USHORT i, j, k; /* indexes */ + ULONG totalbytes; /* total bytes written to file */ + short rc; /* return code of OS/2 call */ - DOSCHGFILEPTR(fhandle, 0l, 0, &npl); /* move file ptr to start of file */ + DosChgFilePtr(fhandle, 0L, 0, &npl); /* move file ptr to start of file */ totalbytes = rc = 0; /* place lines in fbuffer */ for (i = 0, j = 0; (i < TotalLines) && (!rc); i++) { - if ((LineTable[i]->linelength) < (FBUFFSIZE - j)) { - for (k = 0; k < LineTable[i]->linelength; k++, j++) - fbuffer[j] = LineTable[i]->firstchar[k]; - fbuffer[j] = '\r'; - j++; - fbuffer[j] = '\n'; - j++; - } - else { -/* fbuffer is full - write to file */ - rc = DOSWRITE(fhandle, (char far *)fbuffer, j, - (unsigned far *)&byteswritten); - rc = ( !(j == byteswritten) || rc); - totalbytes += (byteswritten); - j = 0; - } + if ((LineTable[i]->linelength) < (FBUFFSIZE - j)) { + for (k = 0; k < LineTable[i]->linelength; k++, j++) + fbuffer[j] = LineTable[i]->firstchar[k]; + fbuffer[j] = '\n'; + j++; + } + else { +/* fbuffer is full - write to file */ + rc = DosWrite(fhandle, fbuffer, j, + &byteswritten); + rc = ( !(j == byteswritten) || rc); + totalbytes += (byteswritten); + j = 0; + } } if ((j > 0) && (!rc)) { -/* lines left in fbuffer - write out to file */ - rc = DOSWRITE(fhandle, (char far *)fbuffer, j, - (unsigned far *)&byteswritten); - rc = ( !(j == byteswritten) || rc); - totalbytes += (byteswritten); +/* lines left in fbuffer - write out to file */ + rc = DosWrite(fhandle, fbuffer, j, + &byteswritten); + rc = ( !(j == byteswritten) || rc); + totalbytes += (byteswritten); } if (!rc) /* no problems writing to the file */ -/* change file size to current file size */ - DOSNEWSIZE(fhandle, totalbytes); +/* change file size to current file size */ + DosNewSize(fhandle, totalbytes); return(rc); } @@ -175,45 +177,45 @@ unsigned short fhandle; /*** backupfile - make of the file * * backupfile creates a backup file for the file being edited - * by sse. + * by sse. * * backupfile (fname, fhandle) * - * ENTRY fhandle - handle to file to backup - * fname - path name of file to backup + * ENTRY fhandle - handle to file to backup + * fname - path name of file to backup * - * EXIT rc - return code of backup + * EXIT rc - return code of backup * * backupfile creates a backup file by replacing the current - * extension with .BAK or adding the extension.BAK if none is - * given. If the file being edited has the extension .BAK then - * backupfile will not create a backup. + * extension with .BAK or adding the extension.BAK if none is + * given. If the file being edited has the extension .BAK then + * backupfile will not create a backup. * - * EFFECTS backup file - by creating it + * EFFECTS backup file - by creating it */ short backupfile(fname, fhandle) - char *fname; - unsigned short fhandle; + char *fname; + USHORT fhandle; { - unsigned short bfhandle; /* handle to backup file */ - unsigned short bytesread; /* number of bytes read from file fname */ - unsigned short byteswriten; /* number of bytes written to file bfname */ - unsigned char i, j; /* indexs */ - unsigned long npl; /* file pointer - not used */ - short rc; /* return code of backupfile */ - char bfname[65]; /* pathname of the backup file */ + USHORT bfhandle; /* handle to backup file */ + USHORT bytesread; /* number of bytes read from file fname */ + USHORT byteswriten; /* number of bytes written to file bfname */ + UCHAR i, j; /* indexes */ + ULONG npl; /* file pointer - not used */ + short rc; /* return code of backupfile */ + char bfname[65]; /* pathname of the backup file */ /* build back up file name - bfname, from fname */ for (i = 0; ((bfname[i] = fname[i]) != '\0'); i++); for (j = 1; - (bfname[i - j] != '\\') && (bfname[i - j] != '.') - && (j < 4) && (j < i); - j++); + (bfname[i - j] != '\\') && (bfname[i - j] != '.') + && (j < 4) && (j < i); + j++); if ((bfname[i - j] == '.') && ((i - j) != 0)) - i -= j; + i -= j; if (bfname[i] != '.') - bfname[i] = '.'; + bfname[i] = '.'; bfname[++i] = 'B'; bfname[++i] = 'A'; bfname[++i] = 'K'; @@ -223,19 +225,19 @@ short backupfile(fname, fhandle) /* create backup file */ rc = openfile(bfname, &bfhandle, WCFLAG); if (!rc) { - DOSCHGFILEPTR(fhandle, 0l, 0, &npl); /* file ptr to beginning of fname */ - for (bytesread = 1, byteswriten = 1; - (bytesread != 0) && !(rc = (bytesread != byteswriten)); ) { -/* copy fname to bfname */ - DOSREAD(fhandle, (char far *)fbuffer, FBUFFSIZE, - (unsigned far *)&bytesread); - DOSWRITE(bfhandle, (char far *)fbuffer, bytesread, - (unsigned far *)&byteswriten); - } - closefile(bfhandle); - if (rc) -/* delete backup file if there was an error creating it */ - DOSDELETE( (char far *)bfname, 0l); + DosChgFilePtr(fhandle, 0L, 0, &npl); /* file ptr to beginning of fname */ + for (bytesread = 1, byteswriten = 1; + (bytesread != 0) && !(rc = (bytesread != byteswriten)); ) { +/* copy fname to bfname */ + DosRead(fhandle, fbuffer, FBUFFSIZE, + &bytesread); + DosWrite(bfhandle, fbuffer, bytesread, + &byteswriten); + } + closefile(bfhandle); + if (rc) +/* delete backup file if there was an error creating it */ + DosDelete(bfname, 0l); } return(rc); } @@ -245,57 +247,57 @@ short backupfile(fname, fhandle) /*** openfile - opens file to be read * - * openfile uses DOSOPEN to open or create a file and - * return a handle to it. + * openfile uses DosOpen to open or create a file and + * return a handle to it. * * openfile (fname, fhandle, openflag) * - * ENTRY fname - name of the file - openflag - is the value of openflag for DOSOPEN + * ENTRY fname - name of the file + openflag - is the value of openflag for DosOpen * - * EXIT fhandle - file handle - * rc - return code of openfile + * EXIT fhandle - file handle + * rc - return code of openfile * - * openfile has the openmode for DOSOPEN set so it may run as - * a bound application. The openflag controls the action - * of the open, example: if the file exist then open it - * or replace it, or if the file does not exist create it - * or fail. + * openfile has the openmode for DosOpen set so it may run as + * a bound application. The openflag controls the action + * of the open, example: if the file exists then open it + * or replace it, or if the file does not exist create it + * or fail. * * EFFECTS fhandle - by returning a handle to the file - * memory - by opening a file + * memory - by opening a file */ short openfile(fname, fhandle, openflag) -char *fname; -unsigned short *fhandle; -unsigned short openflag; +char *fname; +USHORT *fhandle; +USHORT openflag; { - unsigned short actiontaken = 0; /* value return by DOS */ - unsigned long fsize = 0l; /* set to not change file size (0) */ - unsigned short fattrib = 0; /* set to not change file attrib (0) */ + USHORT actiontaken = 0; /* value return by OS/2 */ + ULONG fsize = 0l; /* set to not change file size (0) */ + USHORT fattrib = 0; /* set to not change file attrib (0) */ struct flags { - unsigned access :3 ; /* set to read & write access (2) */ - unsigned reserved1 :1 ; /* must be zero */ - unsigned sharing :3 ; /* set to deny write access (2) */ - unsigned inheritance:1 ; /* set to private (1) */ - unsigned reserved5 :5 ; /* must be zero */ - unsigned failerrors :1 ; /* needes to be zero for API */ - unsigned write :1 ; /* needes to be zero for API */ - unsigned dasdopen :1 ; /* needes to be zero for API */ + unsigned access :3 ; /* set to read & write access (2) */ + unsigned reserved1 :1 ; /* must be zero */ + unsigned sharing :3 ; /* set to deny write access (2) */ + unsigned inheritance:1 ; /* set to private (1) */ + unsigned reserved5 :5 ; /* must be zero */ + unsigned failerrors :1 ; /* needes to be zero for API */ + unsigned write :1 ; /* needes to be zero for API */ + unsigned dasdopen :1 ; /* needes to be zero for API */ }; static union { struct flags modeflags; - unsigned short mode; + USHORT mode; } openmode = { { 2,0,2,1,0,0,0,0 } }; - short rc; /* return code from DOS call */ + short rc; /* return code from DOS call */ /* Open the file */ - rc = DOSOPEN( (char far *)fname, (unsigned far *)fhandle, - (unsigned far *)&actiontaken, - fsize, fattrib, openflag, - openmode.mode, 0l); + rc = DosOpen( fname, (PHFILE)fhandle, + &actiontaken, + fsize, fattrib, openflag, + openmode.mode, 0L); return(rc); } @@ -304,24 +306,24 @@ unsigned short openflag; /*** closefile - closes file * - * closefile uses DOSCLOSE to close the file given by - * fhandle. + * closefile uses DosClose to close the file given by + * fhandle. * * closefile (fhandle). * - * ENTRY fhandle - handle of file to be closed + * ENTRY fhandle - handle of file to be closed * * closefile does not return an error code if one occurs. * - * EFFECTS memory - by closing a file - * the file - by closing it + * EFFECTS memory - by closing a file + * the file - by closing it */ void closefile(fhandle) -unsigned short fhandle; +USHORT fhandle; { - short rc; /* return code of DOS call */ + short rc; /* return code of OS/2 call */ /* Close the file */ - rc = DOSCLOSE(fhandle); + rc = DosClose(fhandle); }