home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
NOTEPAD2.ZIP
/
MBUF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-08
|
6KB
|
236 lines
/*
* mbuf.c -- Buffer Manager
*
* Created by Microsoft Corporation, 1989
*/
#define INCL_WIN
#include <os2.h>
#include "mle.h"
#include "mtypes.h"
#include "mfuncs.h"
/***********************************************************************
* Forward declarations
***********************************************************************/
/*
* VOID BufInit(PED ped)
*
* Initializes the buffer manager
*/
public VOID BufInit(PED ped)
{
ped->pchBlockBuf = NULL;
ped->usBufLength = 0;
ped->usBlockType = MLE_CFTEXT;
}
/*
* BOOL BufSetBuf(PED ped,PCHAR pch,USHORT usLen)
*
* Sets the transfer buffer to a particular address
*/
public BOOL BufSetBuf(PED ped,PCHAR pch,USHORT usLen)
{
ped->pchBlockBuf = pch;
ped->usBufLength = usLen;
return(TRUE);
}
/*
* PCHAR BufQueryBuf(PED ped)
*
* Returns the address of the current transfer buffer
*/
public PCHAR BufQueryBuf(PED ped)
{
return(ped->pchBlockBuf);
}
/*
* USHORT BufQueryBufSize(PED ped)
*
* Returns the size of the current transfer buffer
*/
public USHORT BufQueryBufSize(PED ped)
{
return(ped->usBufLength);
}
/*
* BOOL BufSetFormat(PED ped,USHORT usFmt)
*
* Sets the transfer format
*/
public BOOL BufSetFormat(PED ped,USHORT usFmt)
{
ped->usBlockType = usFmt;
return(TRUE);
}
/*
* USHORT BufQueryFormat(PED ped)
*
* Returns the current format
*/
public USHORT BufQueryFormat(PED ped)
{
return(ped->usBlockType);
}
/*
* IPT BufFormattedSize(PED ped, IPT iptFrom, IPT iptTo, USHORT usFormat)
*
* Determines the size, in bytes, of a range of ipts in a particular format
* Returns a value < 0 if there is an error.
*/
public IPT BufFormattedSize(PED ped, IPT iptFrom, IPT iptTo, USHORT usFormat)
{
PPR ppr;
IPT ipt;
OFFSET off;
ULONG cBytes;
IPT cch = iptTo - iptFrom;
cBytes = 0;
ipt = iptFrom;
ppr = TxtPPROfIpt(ped, &ipt);
off = (USHORT)ipt;
while (cch > 0) {
if (fIsMarker(ppr)) {
if (((PMR)ppr)->fFlags & MARKER_LINE) {
switch (usFormat) {
case MLE_CFTEXT:
cBytes++;
break;
case MLE_CSTD:
break;
case MLE_WINFMT:
cBytes++;
if (((PMR)ppr)->fFlags&
(MARKER_SLB||MARKER_SPILL)) {
cBytes++;
}
break;
default:
return(-1);
break;
}
}
} else {
cBytes++;
}
AdvancePointer(&ppr,&off);
cch--;
}
return(cBytes);
}
/*
* OFFSET BufCopyOut(PED ped, IPT iptFrom, USHORT usFormat,
* PSZ pszBuff, OFFSET cch)
*
* Copies from the MLE text to a buffer. If the buffer is null, returns
* zero and does nothing. The number of bytes copied into the buffer is
* limited by cch. Note that NULL's ('\0') are not given special treatment
* unless so defined by the copy format. Function returns the number of
* bytes copied into the buffer.
*/
public OFFSET BufCopyOut(PED ped, IPT iptFrom, USHORT usFormat,
PSZ pszBuff, OFFSET cch)
{
PPR ppr;
IPT ipt;
OFFSET off;
OFFSET cBytes;
CHAR ch;
if (pszBuff == NULL)
return(0);
cch = min(cch, ped->usBufLength);
cBytes = 0;
ipt = iptFrom;
ppr = TxtPPROfIpt(ped, &ipt);
off = (USHORT)ipt;
ch = CharOfPointer(ppr, off);
while (cch > cBytes) {
if (fIsMarker(ppr)) {
if (((PMR)ppr)->fFlags & MARKER_LINE) {
switch (usFormat) {
case MLE_CFTEXT:
case MLE_CSTD:
break;
case MLE_WINFMT:
if (fIsMarkerType(ppr, MARKER_SOFTLINE)) {
if (cBytes+3 <= cch) {
cBytes += 3;
*(pszBuff++) = '\r';
*(pszBuff++) = '\r';
*(pszBuff++) = '\n';
}
}
break;
default:
return(-1);
break;
}
}
} else {
if (ch == '\n') {
switch (usFormat) {
case MLE_CFTEXT:
case MLE_WINFMT:
if (cBytes+2 >= cch) {
return(cBytes);
} else {
*(pszBuff++) = '\r';
*(pszBuff++) = '\n';
cBytes += 2;
}
break;
case MLE_CSTD:
cBytes ++;
*(pszBuff++) = '\n';
break;
default:
return(-1);
break;
}
} else {
*(pszBuff++) = ch;
cBytes++;
}
}
ch = AdvancePointer(&ppr,&off);
}
return(cBytes);
}
/*
* BOOL BufCopyIn(PED ped, IPT iptFrom, IPT iptTo,
* PSZ pszBuff, OFFSET cch)
*
* Copies from an external buffer to the MLE text. Replaces the text in the
* range iptFrom..iptTo with the contents of the buffer. Returns TRUE iff
* all the text was properly inserted into the MLE.
*/
public BOOL BufCopyIn(PED ped, IPT iptFrom, IPT iptTo,
PSZ pszBuff, OFFSET cch)
{
if (pszBuff == NULL)
return(FALSE);
return(TxtChange(ped, iptFrom, iptTo, pszBuff, cch));
}