|
|
Microsoft OS/2 SDK 03-01-1988
/* Created by Microsoft Corp. 1987 */
#define INCL_DOSFILEMGR
#include <os2def.h>
#include <ctype.h>
#include <bsedos.h>
#include <stdlib.h>
#include "ssedefs.h"
/*** 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 (fhandle)
*
* ENTRY fhandle - handle to file being read
*
* 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'
*/
short readfile(fhandle)
USHORT fhandle;
{
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, 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;
}
}
}
return(rc);
}
/*** savefile - saves file to disk
*
* savefile attempts to write the file to disk.
*
* savefile (fhandle)
*
* ENTRY fhandle - handle of file to be written.
*
* 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 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
*/
short savefile(fhandle)
USHORT fhandle;
{
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 */
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] = '\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, 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);
return(rc);
}
/*** backupfile - make of the file
*
* backupfile creates a backup file for the file being edited
* by sse.
*
* backupfile (fname, fhandle)
*
* ENTRY fhandle - handle to file to backup
* fname - path name of file to 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.
*
* EFFECTS backup file - by creating it
*/
short backupfile(fname, fhandle)
char *fname;
USHORT fhandle;
{
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++);
if ((bfname[i - j] == '.') && ((i - j) != 0))
i -= j;
if (bfname[i] != '.')
bfname[i] = '.';
bfname[++i] = 'B';
bfname[++i] = 'A';
bfname[++i] = 'K';
bfname[++i] = '\0';
/* 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, 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);
}
/*** openfile - opens file to be read
*
* 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
*
* 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 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
*/
short openfile(fname, fhandle, openflag)
char *fname;
USHORT *fhandle;
USHORT openflag;
{
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 */
};
static union {
struct flags modeflags;
USHORT mode;
} openmode = { { 2,0,2,1,0,0,0,0 } };
short rc; /* return code from DOS call */
/* Open the file */
rc = DosOpen( fname, (PHFILE)fhandle,
&actiontaken,
fsize, fattrib, openflag,
openmode.mode, 0L);
return(rc);
}
/*** closefile - closes file
*
* closefile uses DosClose to close the file given by
* fhandle.
*
* closefile (fhandle).
*
* 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
*/
void closefile(fhandle)
USHORT fhandle;
{
short rc; /* return code of OS/2 call */
/* Close the file */
rc = DosClose(fhandle);
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.