blocxx

BaseStreamBuffer.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/BaseStreamBuffer.hpp"
00041 #include "blocxx/Exception.hpp"
00042 #include "blocxx/String.hpp"
00043 #include "blocxx/Assertion.hpp"
00044 #include <iostream> // for cerr
00045 #include <cstring> // for memcpy
00046 
00047 namespace BLOCXX_NAMESPACE
00048 {
00049 
00051 BaseStreamBuffer::BaseStreamBuffer(EDirectionFlag direction, size_t bufSize)
00052    : m_bufSize(bufSize), m_inputBuffer(NULL), m_outputBuffer(NULL)
00053 {
00054    if (direction == E_IN || direction == E_IN_OUT)
00055    {
00056       m_inputBuffer = new char[m_bufSize];
00057       initGetBuffer();
00058    }
00059    if (direction == E_OUT || direction == E_IN_OUT)
00060    {
00061       m_outputBuffer = new char[m_bufSize];
00062       initPutBuffer();
00063    }
00064 }
00066 void
00067 BaseStreamBuffer::initBuffers()
00068 {
00069    initPutBuffer();
00070    initGetBuffer();
00071 }
00073 void
00074 BaseStreamBuffer::initPutBuffer()
00075 {
00076    setp(m_outputBuffer, m_outputBuffer + m_bufSize);
00077 }
00079 void
00080 BaseStreamBuffer::initGetBuffer()
00081 {
00082    setg(m_inputBuffer, m_inputBuffer, m_inputBuffer);
00083 }
00085 BaseStreamBuffer::~BaseStreamBuffer()
00086 {
00087    delete [] m_inputBuffer;
00088    delete [] m_outputBuffer;
00089 }
00091 int
00092 BaseStreamBuffer::sync()
00093 {
00094    return buffer_out();
00095 }
00097 int
00098 BaseStreamBuffer::buffer_out()
00099 {
00100     int cnt = pptr() - pbase();
00101     int retval = buffer_to_device(m_outputBuffer, cnt);
00102     pbump(-cnt);
00103     return retval;
00104 }
00106 int
00107 BaseStreamBuffer::overflow(int c)
00108 {
00109    if (buffer_out() < 0)
00110    {
00111       return EOF;
00112    }
00113    else
00114    {
00115       if (c != EOF)
00116       {
00117          return sputc(c);
00118       }
00119       else
00120       {
00121          return c;
00122       }
00123    }
00124 }
00126 std::streamsize
00127 BaseStreamBuffer::xsputn(const char* s, std::streamsize n)
00128 {
00129    if (n < epptr() - pptr())
00130    {
00131       memcpy(pptr(), s, n * sizeof(char));
00132       pbump(n);
00133       return n;
00134    }
00135    else
00136    {
00137       for (std::streamsize i = 0; i < n; i++)
00138       {
00139          if (sputc(s[i]) == EOF)
00140          {
00141             return i;
00142          }
00143       }
00144       return n;
00145    }
00146 }
00148 int
00149 BaseStreamBuffer::underflow()
00150 {
00151     if (gptr() < egptr())
00152     {
00153         return static_cast<unsigned char>(*gptr());   // need a static_cast so a -1 doesn't turn into an EOF
00154     }
00155     if (buffer_in() < 0)
00156     {
00157         return EOF;
00158     }
00159     else
00160     {
00161         return static_cast<unsigned char>(*gptr());   // need a static_cast so a -1 doesn't turn into an EOF
00162     }
00163 }
00165 int
00166 BaseStreamBuffer::buffer_in()
00167 {
00168    int retval = buffer_from_device(m_inputBuffer,
00169          m_bufSize);
00170    if (retval <= 0)
00171    {
00172       setg(0,0,0);
00173       return -1;
00174    }
00175    else
00176    {
00177       setg(m_inputBuffer, m_inputBuffer, m_inputBuffer + retval);
00178       return retval;
00179    }
00180 }
00182 int
00183 BaseStreamBuffer::buffer_to_device(const char* c, int n)
00184 {
00185    BLOCXX_ASSERT("Not implemented, should overwrite" == 0);
00186    return -1; // make the compiler happy
00187 }
00189 int
00190 BaseStreamBuffer::buffer_from_device(char* c, int n)
00191 {
00192    BLOCXX_ASSERT("Not implemented, should overwrite" == 0);
00193    return -1; // make the compiler happy
00194 }
00195 
00196 } // end namespace BLOCXX_NAMESPACE
00197