📄 pistream.h
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Pi3API/PIStream.h,v $
* $Date: 2003/05/13 18:42:10 $
*
Description:
Use C++ IOStreams with PIIOBuffer for C++ stream operations up/down
the PIIOBuffer object.
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#ifndef PISTREAM_H_
#define PISTREAM_H_
/*
** Must be using C++ for IOStreams
*/
#if defined(__cplusplus)
#include <iostream.h>
#include "PIIOBuf.h"
#define PISTREAM_BUF_SIZE (1024)
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
class PIStreamBuf : public streambuf
{
public:
enum Direction { IN, OUT };
ios::open_mode tMode;
private:
char szBuffer[PISTREAM_BUF_SIZE];
PIIOBuffer *pBuffer;
Direction eDirection;
void FlushOutput()
{
assert( eDirection==OUT );
PIIOBuffer_write( pBuffer, pbase(), out_waiting(), PIIOBUF_NONE );
setp( szBuffer, &(szBuffer[PISTREAM_BUF_SIZE]) );
};
int ReadInput()
{
assert( eDirection==IN );
if ( gptr() < egptr() )
{
return *gptr();
};
int iRead = PIIOBuffer_readToBuffer( pBuffer, szBuffer,
PISTREAM_BUF_SIZE );
switch( iRead )
{
case 0:
case -1:
return EOF;
};
#if WIN32
/*
** On DOSish systems remove \r if this is a text file
*/
if ( !(tMode & ios::binary ) )
{
int i=0;
int j=0;
for( ; i<iRead; )
{
if ( szBuffer[i]=='\r' )
{ i++; }
else
{ szBuffer[j++] = szBuffer[i++]; };
};
iRead = j;
};
#endif
setg( szBuffer, szBuffer, &(szBuffer[iRead]) );
return *szBuffer;
};
public:
PIStreamBuf::PIStreamBuf( PIIOBuffer *pTheBuffer, Direction eTheDirection,
ios::open_mode tTheMode )
:
streambuf( szBuffer, PISTREAM_BUF_SIZE ),
pBuffer( pTheBuffer ),
eDirection( eTheDirection ),
tMode( tTheMode )
{
if ( eDirection==OUT )
{
setp( szBuffer, &(szBuffer[PISTREAM_BUF_SIZE]) );
}
else
{
setg( szBuffer, szBuffer, szBuffer );
};
};
~PIStreamBuf()
{
if ( eDirection==OUT )
{ FlushOutput(); }
else
{ setg( szBuffer, szBuffer, szBuffer ); };
};
int sync()
{
if ( eDirection==OUT )
{ FlushOutput(); }
else
{ setg( szBuffer, szBuffer, szBuffer ); };
return 0;
};
int overflow( int ch )
{
if ( eDirection==OUT )
{
FlushOutput();
return sputc( ch );
};
return 0;
};
int underflow()
{
if ( eDirection==IN )
{
return ReadInput();
}
else
{
return EOF;
};
};
};
/*____________________________________________________________________________*\
*
Description:
Define the input and output stream classes based on this streambuf
\*____________________________________________________________________________*/
class PIIStream : public istream
{
private:
PIStreamBuf tPIStreamBuf;
public:
PIIStream( PIIOBuffer *pBuffer, ios::open_mode tMode = ios::in )
: istream( &tPIStreamBuf ),
tPIStreamBuf( pBuffer, PIStreamBuf::IN, tMode )
{
};
~PIIStream()
{
};
};
class PIOStream : public ostream
{
private:
PIStreamBuf tPIStreamBuf;
public:
PIOStream( PIIOBuffer *pBuffer, ios::open_mode tMode = ios::out )
: ostream( &tPIStreamBuf ),
tPIStreamBuf( pBuffer, PIStreamBuf::OUT, tMode )
{
};
~PIOStream()
{
};
};
#endif /* defined(__cplusplus) */
#endif /* PISTREAM_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -