home *** CD-ROM | disk | FTP | other *** search
- /*____________________________________________________________________________*\
- *
-
- Copyright (c) 1997 John Roy. 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. Redistributions of any form whatsoever and all advertising materials
- mentioning features must contain the following
- acknowledgment:
- "This product includes software developed by John Roy
- (http://www.johnroy.com/pi3/)."
-
- 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: PIIOBuf.h$
- * $Date: Sun Aug 10 06:36:33 1997$
- *
- Description:
- \*____________________________________________________________________________*/
- /* $SourceTop:$ */
-
- #ifndef PIIOBUF_H_
- #define PIIOBUF_H_
-
- #include "Pi2API.h"
-
- /*____________________________________________________________________________*\
- *
- Description:
- Flags which effect behaviour
- \*____________________________________________________________________________*/
- #define PIIOBUF_NONE (0)
- #define PIIOBUF_NOBUFFER (1)
-
- /* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
-
- C++ Interface
-
- \* -------------- +++++++++++++++++++++++++++++++ ------------------ */
- #if defined(__cplusplus) && defined(PI3_INTERNAL)
-
- #include <assert.h>
-
- /*____________________________________________________________________________*\
- *
- Description:
- \*____________________________________________________________________________*/
- class PIIOBuffer
- {
- private:
- PIObject *pIOObject;
- enum { IN_BUF_SIZE=2047, OUT_BUFFER_SIZE=8192 };
- char szInBuf[ IN_BUF_SIZE ];
- char *pOutBuffer;
- int iBufferLen;
- int iBytesSent;
- int iOutBufferSize;
- int iOutIOError;
-
- /* --- internal methods --- */
- int GrowData();
- int FlushOutput();
- int Internal_Write( const char *pData, int iLen, int iFlags );
-
- public:
- PIIOBuffer( PIObject *pTheIOObject )
- : pIOObject( pTheIOObject ),
- pOutBuffer( new char[OUT_BUFFER_SIZE] ),
- iBufferLen( 0 ),
- iBytesSent( 0 ),
- iOutBufferSize( 0 ),
- iOutIOError( 0 )
- {
- assert( pOutBuffer );
- };
-
- ~PIIOBuffer()
- {
- FlushOutput();
- delete [] pOutBuffer;
- };
-
- inline PIObject *GetIOObject() { return pIOObject; };
- int GetLine( char *pszBuf, int iBufLen );
- int PollBeforeRead();
- const char *Read( int *piRead );
- inline int Flush() { return FlushOutput(); };
- int Read( char *pszBuffer, int iMaxLen );
- int Write( const char *pData, int iItsLength, int iFlags=PIIOBUF_NONE );
- int WriteLn( const char *pData, int iItsLength, int iFlags=PIIOBUF_NONE );
- inline int GetOutputBuffer( char **ppBuffer )
- {
- assert( pOutBuffer );
- assert( ppBuffer );
- *ppBuffer = &( pOutBuffer[iOutBufferSize] );
- return OUT_BUFFER_SIZE-iOutBufferSize;
- };
- inline int AdvanceBufferPointer( int iCount )
- {
- assert( iCount<=(OUT_BUFFER_SIZE-iOutBufferSize) );
- iOutBufferSize += iCount;
- return 1;
- };
- inline int GetBytesSent() const { return iBytesSent; };
- inline void ResetBytesSent() { iBytesSent = 0; };
- };
-
- #else
- /* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
-
- External C Interface
-
- \* -------------- +++++++++++++++++++++++++++++++ ------------------ */
- /*____________________________________________________________________________*\
- *
- Description:
- Structure is opaque in C
- \*____________________________________________________________________________*/
- typedef int PIIOBuffer;
-
-
- /* -------------- +++++++++++++++++++++++++++++++ ------------------ *\
-
- C Functions
-
- \* -------------- +++++++++++++++++++++++++++++++ ------------------ */
- #endif
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_new
-
- Synopsis:
- PIIOBuffer *PIIOBuffer_new( PIObject *pIOObject )
-
- Description:
- Create a new IO buffer object which uses IO channel object pIOObject for
- input and output.
-
- Notes:
- Return Values:
- Errors:
- See Also:
- PIIOBuffer_delete().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI PIIOBuffer *PIIOBuffer_new( PIObject *pIOObject );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_delete
-
- Synopsis:
- int PIIOBuffer_delete( PIIOBuffer *pPIIOBuffer )
-
- Description:
- Deallocate memory associated with IO buffer object pIOBuffer after
- flushing output.
-
- Notes:
- Destroying the buffer object does not destroy the IO channel it
- encapsulates.
-
- Return Values:
- Errors:
-
- See Also:
- PIIOBuffer_new().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_delete( PIIOBuffer *pPIIOBuffer );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_getLine
-
- Synopsis:
- int PIIOBuffer_getLine( PIIOBuffer *pIOBuf, char *pszBuf, int iBufLen )
-
- Description:
- Read a line from the IO buffer into buffer pszBuf. Up to iBufLen
- characters are written.
-
- Notes:
- Return Values:
- Returns number of characters read or -1 on error.
-
- Errors:
- Returns -1 on error.
-
- See Also:
- PIIOBuffer_read().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_getLine( PIIOBuffer *pIOBuf, char *pszBuf,
- int iBufLen );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_pollBeforeRead
-
- Synopsis:
- int PIIOBuffer_pollBeforeRead( PIIOBuffer *pIOBuf )
-
- Description:
- Causes the current thread to yield until data has been read from the
- IO channel into the internal buffer.
-
- Notes:
- Return Values:
- Returns -1 on IO error, 0 for channel closed and >0 for success.
-
- Errors:
- Returns -1 on error.
-
- See Also:
- PIIOBuffer_read().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_pollBeforeRead( PIIOBuffer *pIOBuf );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_read
-
- Synopsis:
- const char *PIIOBuffer_read( PIIOBuffer *pIOBuf, int *piRead )
-
- Description:
- Reads data from the IO channel into the internal buffer and returns
- a pointer to the data read. The pointer piRead should point at
- an integer which will be set to the number of bytes read on success.
-
- Notes:
- Return Values:
- Return a non-NULL pointer on success.
-
- Errors:
- Returns NULL on channel closed or error.
-
- See Also:
- PIIOBuffer_readToBuffer().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI const char *PIIOBuffer_read( PIIOBuffer *pIOBuf, int *piRead );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_readToBuffer
-
- Synopsis:
- int PIIOBuffer_readToBuffer( PIIOBuffer *pIOBuf, char *pszBuf,
- int iMaxLen )
-
- Description:
- Reads data from the IO channel into the buffer pIOBuf. Up to iMaxLen
- bytes will be read. A positive return value gives the number of bytes
- written into the buffer.
-
- Notes:
- Return Values:
- Returns values are: 0 on channel closed, -1 on error and >0 on success.
-
- Errors:
- Returns -1 on error, 0 on channel closed.
-
- See Also:
- PIIOBuffer_read().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_readToBuffer( PIIOBuffer *pIOBuf, char *pszBuf,
- int iMaxLen );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_write
-
- Synopsis:
- int PIIOBuffer_write( PIIOBuffer *pIOBuf, const char *pszBuf, int iLen,
- int iFlags )
-
- Description:
- Write the contents of the buffer pszBuf of length iLen into the
- the internal output buffer optionally flushing the output buffer.
-
- The value iFlags is formed by OR'ing the following flags
-
- PIIOBUF_NONE No flags (0).
- PIIOBUF_NOBUFFER Don't buffer, output data with directly after
- flushing existing output buffer.
- Notes:
- If iLen is -1, PIIOBuffer_write() uses strlen() to determine the
- length of the string pszBuf.
-
- Return Values:
- Returns number of bytes written or -1 on error.
-
- Errors:
- Returns -1 on error.
-
- See Also:
- PIIOBuffer_writeLn().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_write( PIIOBuffer *pIOBuf, const char *pszBuf,
- int iLen, int iFlags );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_writeLn
-
- Synopsis:
- int PIIOBuffer_writeLn( PIIOBuffer *pIOBuf, const char *pszBuf, int iLen,
- int iFlags )
-
- Description:
- Write the contents of the buffer pszBuf of length iLen into the
- the internal output buffer followed by CRLF to terminate the line.
-
- See PIIOBuffer_write() for a description of flags.
-
- Notes:
- If iLen is -1, PIIOBuffer_write() uses strlen() to determine the
- length of the string pszBuf.
-
- Return Values:
- Returns number of bytes written or -1 on error.
-
- Errors:
- Returns -1 on error.
-
- See Also:
- PIIOBuffer_write().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_writeLn( PIIOBuffer *pIOBuf, const char *pszBuf,
- int iLen, int iFlags );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_flush
-
- Synopsis:
- int PIIOBuffer_flush( PIIOBuffer *pIOBuf )
-
- Description:
- Flush contents of output buffer to the IO channel.
-
- Notes:
- Return Values:
- Returns an integer which should be interpreted as a boolean.
-
- Errors:
- Returns non-zero on success, zero on failure.
-
- See Also:
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_flush( PIIOBuffer *pIOBuf );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_getOutputBuffer
-
- Synopsis:
- int PIIOBuffer_getOutputBuffer( PIIOBuffer *pIOBuf, char **ppBuffer )
-
- Description:
- Returns the output buffer and its length so data can be written into
- it directly rather than using intermediate buffering. If the buffer
- length is not sufficient PIIOBuffer_flush() may be invoked followed by
- PIIOBuffer_getOutputBuffer() to get the flushed buffer.
-
- The pointer ppBuffer will be set to point at the internal buffer.
-
- After data has been written to the buffer and before any other output
- buffer functions are invoked the function PIIOBuffer_advanceBufferPointer()
- should be called to set the internal length of the output buffer.
-
- Notes:
- Return Values:
- Returns the length of the output buffer.
-
- Errors:
- See Also:
- PIIOBuffer_advanceBufferPointer().
-
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_getOutputBuffer( PIIOBuffer *pIOBuf,
- char **ppBuffer );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_advanceBufferPointer
-
- Synopsis:
- int PIIOBuffer_advanceBufferPointer( PIIOBuffer *pIOBuf, int iCount )
-
- Description:
- Advances the internal buffer pointer by iCount bytes. See
- PIIOBuffer_getBufferPointer() for more information.
-
- Notes:
- Return Values:
- Returns a positive integer on success.
-
- Errors:
- Returns a negative or zero value on failure.
-
- See Also:
- PIIOBuffer_getBufferPointer().
-
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_advanceBufferPointer( PIIOBuffer *pIOBuf,
- int iCount );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_getBytesSent
-
- Synopsis:
- int PIIOBuffer_getBytesSent( PIIOBuffer *pIOBuf )
-
- Description:
- Returns the number of byes which have been place into this buffer
- object since it was created or PIIOBuffer_resetBytesSent() was
- last invoked. This value is the sum of the data sent down the IO channel
- and data currently in the output buffer.
-
- Notes:
- Return Values:
- Returns number of bytes sent.
-
- Errors:
- This function does not raise an error.
-
- See Also:
- PIIOBuffer_resetBytesSent().
-
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI int PIIOBuffer_getBytesSent( PIIOBuffer *pIOBuf );
-
- /*____________________________________________________________________________*\
- *
- Name:
- PIIOBuffer_resetBytesSent
-
- Synopsis:
- int PIIOBuffer_resetBytesSent( PIIOBuffer *pIOBuf )
-
- Description:
- Resets the count of bytes sent through this buffer object to 0.
- Refer to PIIOBuffer_getBytesSent() for more information.
-
- Notes:
- Return Values:
- This function does not return a value.
- Returns number of bytes sent.
-
- Errors:
- This function does not raise an error.
-
- See Also:
- PIIOBuffer_getBytesSent().
- \*____________________________________________________________________________*/
- PUBLIC_PIAPI void PIIOBuffer_resetBytesSent( PIIOBuffer *pIOBuf );
-
- #endif /* PIIOBUF_H_ */
-
-