Category : Word Processors
Archive   : HP22D5.ZIP
Filename : LOWIO.C

 
Output of file : LOWIO.C contained in archive : HP22D5.ZIP

/*
** This file implements the following DOS level I/O routines:
**
** open (lopen)
** close (lcreate)
** read (lread)
** write (lwrite)
** filelength (lfilelength)
** tell (ltell)
** seek (lseek)
**
** The routines use MS-DOS file numbers to refer to files. These are the same
** number that HyperPAD uses internally to refer to files; so these routines
** can be inter-mixed with HyperPAD file routines.
*/
#include "extern.h"
#include "io.h"

/*
** This routine opens a file and returns its file number to HyperPAD. -1 will
** be returned if the open wasn't successful.
**
** The function takes two parameters:
**
** filename DOS file name, like "C:\DATA.DAT" or "data.dat"
** mode "w" open for write mode
** "r" open for read-only mode
** "wr" open for read and write
**
** This parameter is case sensitive
**
** To call this function from HyperPAD:
**
** get lopen("data.dat","r");
** if it is -1 then
** answer "Couldn't open the file." with "Ok";
*/
lopen(NumArgs,hFileName,hMode)

int NumArgs;
HANDLE hFileName;
HANDLE hMode;

{
PTR p;
int mode = READ;

if (NumArgs != 2) return(STOP);

p = deref(hMode);
if (*p == 'r') {
if (*(p + 1) == 'w') mode = READ_WRITE;
}
else if (*p == 'w') {
if (*(p + 1) == 'r') mode = READ_WRITE;
else mode = WRITE;
}

ReturnValue(itoh(open(htos(hFileName),mode)));

return(STOP);
}

/*
** This routine creates a file and returns its file number to HyperPAD.
**
** To call this function from HyperPAD:
**
** get lcreate("data.dat");
** if it is -1 then
** answer "Couldn't create the file." with "Ok";
*/
lcreate(NumArgs,hFileName)

int NumArgs;
HANDLE hFileName;

{
if (NumArgs != 1) return(STOP);
ReturnValue(itoh(create(deref(hFileName))));
return(STOP);
}

/*
** This routine closes a file.
**
** To call this handler from HyperPAD:
**
** lclose fileNum;
*/
lclose(NumArgs,hFileHandle)

int NumArgs;
HANDLE hFileHandle;

{
if (NumArgs != 1) return(STOP);
close(htoi(hFileHandle));
return(STOP);
}

/*
** This routine reads a specified number of bytes from a file
** and returns the text to HyperPAD. Be careful that the number of bytes
** is less than 32000 and that the data will not contain NULs.
**
** put lread(fileNum,40) into page field 1; -- read 40 bytes
*/
lread(NumArgs,hFileHandle,hNumBytes)

int NumArgs;
HANDLE hFileHandle;
HANDLE hNumBytes;

{
WORD numbytes;
HANDLE hdl;

if (NumArgs != 2) return(STOP);
numbytes = htoi(hNumBytes);
if (hdl = NewHandle(numbytes)) {
numbytes = read(htoi(hFileHandle),deref(hdl),numbytes);
ReAllocHandle(hdl,numbytes);
ReturnValue(hdl);
}
else ReturnValue(stoh(""));
return(STOP);
}

/*
** This routine writes text to a file.
**
** To call this handler from HyperPAD:
**
** lwrite fileNum,"hello there";
*/
lwrite(NumArgs,hFileHandle,hText)

int NumArgs;
HANDLE hFileHandle,hText;

{
if (NumArgs != 2) return(STOP);
write(htoi(hFileHandle),deref(hText),strlen(deref(hText)));
return(STOP);
}

/*
** This routine returns the number of bytes in an opened file.
**
** To call this function from HyperPAD:
**
** put lfilelength(fileNum) into page field 1;
*/
lfilelength(NumArgs,hFileHandle)

int NumArgs;
HANDLE hFileHandle;

{
DWORD d,c;
int handle;

handle = htoi(hFileHandle);

c = lseek(handle,0L,FROM_CURRENT);
d = lseek(handle,0L,FROM_EOF);
lseek(handle,d,FROM_BEGINNING);
ReturnValue(ltoh(d));
return(STOP);
}

/*
** This routine returns the position of the file pointer within an opened file.
**
** To call this function from HyperPAD:
**
** put ltell(fileNum) into savedPosition;
*/
ltell(NumArgs,hFileHandle)

int NumArgs;
HANDLE hFileHandle;

{
if (NumArgs != 1) return(STOP);
ReturnValue(ltoh(lseek(htoi(hFileHandle),0L,FROM_CURRENT)));
return(STOP);
}

/*
** This routine moves the file pointer anywhere within an opened file.
**
** The handler takes three arguments:
**
** fileNum file number (returned from lopen())
** numBytes number of bytes from the specified position
** fromWhere "c" -> from current position
** "e" -> from end-of-file
** "b" -> from beginning of file
**
** This parameter is case sensitive
**
** To call this routine from HyperPAD:
**
** lseek fileNum,200,"b"; -- seek 200 bytes from the start of the file
*/
llseek(NumArgs,hFileHandle,hNumBytes,hFromWhere)

int NumArgs;
HANDLE hFileHandle,hNumBytes,hFromWhere;

{
int from = FROM_BEGINNING;
PTR p;
DWORD d;

if (NumArgs != 3) return(STOP);

p = deref(hFromWhere);
if (*p == 'c') from = FROM_CURRENT;
else if (*p == 'e') from = FROM_EOF;

ReturnValue(ltoh(lseek(htoi(hFileHandle),htol(hNumBytes),from)));
return(STOP);
}

POOL pascal Pool[] = {
{ "lopen", lopen, 0, FUNCTION},
{ "lcreate", lcreate, 0, FUNCTION},
{ "lclose", lclose, 0, HANDLER},
{ "lread", lread, 0, FUNCTION},
{ "lwrite", lwrite, 0, HANDLER},
{ "lfilelength", lfilelength, 0, FUNCTION},
{ "ltell", ltell, 0, FUNCTION},

{ "lseek", llseek, 0, FUNCTION},
{ NULL, NULL, 0, 0} };