OW_BaseStreamBuffer.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_BaseStreamBuffer.hpp"
00038 #include "OW_Exception.hpp"
00039 #include "OW_String.hpp"
00040 #include "OW_Assertion.hpp"
00041 #include <iostream> // for cerr
00042 #include <cstring> // for memcpy
00043 
00044 namespace OW_NAMESPACE
00045 {
00046 
00047 BaseStreamBuffer::BaseStreamBuffer(size_t bufSize,
00048       const char* direction_)
00049    : m_bufSize(bufSize), m_inputBuffer(NULL), m_outputBuffer(NULL)
00050 {
00051    String direction(direction_);
00052    if (direction.equals("in") || direction.equals("io"))
00053    {
00054       m_inputBuffer = new char[m_bufSize];
00055       initGetBuffer();
00056    }
00057    if (direction.equals("out") || direction.equals("io"))
00058    {
00059       m_outputBuffer = new char[m_bufSize];
00060       initPutBuffer();
00061    }
00062 }
00064 void
00065 BaseStreamBuffer::initBuffers()
00066 {
00067    initPutBuffer();
00068    initGetBuffer();
00069 }
00071 void
00072 BaseStreamBuffer::initPutBuffer()
00073 {
00074    setp(m_outputBuffer, m_outputBuffer + m_bufSize);
00075 }
00077 void
00078 BaseStreamBuffer::initGetBuffer()
00079 {
00080    setg(m_inputBuffer, m_inputBuffer, m_inputBuffer);
00081 }
00083 BaseStreamBuffer::~BaseStreamBuffer()
00084 {
00085    delete [] m_inputBuffer;
00086    delete [] m_outputBuffer;
00087 }
00089 int
00090 BaseStreamBuffer::sync()
00091 {
00092    return buffer_out();
00093 }
00095 int
00096 BaseStreamBuffer::buffer_out()
00097 {
00098    // NOTE: If an exception escapes this function, __terminate will be called
00099    // for gcc 2.95.2
00100 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00101    try
00102    {
00103 #endif
00104       int cnt = pptr() - pbase();
00105       int retval = buffer_to_device(m_outputBuffer, cnt);
00106       pbump(-cnt);
00107       return retval;
00108 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00109    }
00110    catch (const Exception& e)
00111    {
00112       std::cerr << "Caught Exception in BaseStreamBuffer::buffer_out(): " << e << std::endl;
00113       return EOF;
00114    }
00115    catch (const std::exception& e)
00116    {
00117       std::cerr << "Caught std::exception in BaseStreamBuffer::buffer_out(): " << e.what() << std::endl;
00118       return EOF;
00119    }
00120    catch (...)
00121    {
00122       std::cerr << "Caught unknown exception in BaseStreamBuffer::buffer_out()" << std::endl;
00123       return EOF;
00124    }
00125 #endif
00126 }
00128 int
00129 BaseStreamBuffer::overflow(int c)
00130 {
00131    if (buffer_out() < 0)
00132    {
00133       return EOF;
00134    }
00135    else
00136    {
00137       if (c != EOF)
00138       {
00139          return sputc(c);
00140       }
00141       else
00142       {
00143          return c;
00144       }
00145    }
00146 }
00148 std::streamsize
00149 BaseStreamBuffer::xsputn(const char* s, std::streamsize n)
00150 {
00151    if (n < epptr() - pptr())
00152    {
00153       memcpy(pptr(), s, n * sizeof(char));
00154       pbump(n);
00155       return n;
00156    }
00157    else
00158    {
00159       for (std::streamsize i = 0; i < n; i++)
00160       {
00161          if (sputc(s[i]) == EOF)
00162          {
00163             return i;
00164          }
00165       }
00166       return n;
00167    }
00168 }
00170 int
00171 BaseStreamBuffer::underflow()
00172 {
00173    // NOTE: If an exception escapes this function, __terminate will be called
00174    // for gcc 2.95.2
00175 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00176    try
00177    {
00178 #endif
00179       if (gptr() < egptr())
00180       {
00181          return static_cast<unsigned char>(*gptr());  // need a static_cast so a -1 doesn't turn into an EOF
00182       }
00183       if (buffer_in() < 0)
00184       {
00185          return EOF;
00186       }
00187       else
00188       {
00189          return static_cast<unsigned char>(*gptr());  // need a static_cast so a -1 doesn't turn into an EOF
00190       }
00191 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00192    }
00193    catch (const Exception& e)
00194    {
00195       std::cerr << "Caught Exception in BaseStreamBuffer::underflow(): " << e << std::endl;
00196       return EOF;
00197    }
00198    catch (const std::exception& e)
00199    {
00200       std::cerr << "Caught std::exception in BaseStreamBuffer::underflow(): " << e.what() << std::endl;
00201       return EOF;
00202    }
00203    catch (...)
00204    {
00205       std::cerr << "Caught unknown exception in BaseStreamBuffer::underflow()" << std::endl;
00206       return EOF;
00207    }
00208 #endif
00209 }
00211 int
00212 BaseStreamBuffer::buffer_in()
00213 {
00214    int retval = buffer_from_device(m_inputBuffer,
00215          m_bufSize);
00216    if (retval <= 0)
00217    {
00218       setg(0,0,0);
00219       return -1;
00220    }
00221    else
00222    {
00223       setg(m_inputBuffer, m_inputBuffer, m_inputBuffer + retval);
00224       return retval;
00225    }
00226 }
00228 int
00229 BaseStreamBuffer::buffer_to_device(const char* c, int n)
00230 {
00231    OW_ASSERT("Not implemented, should overwrite" == 0);
00232    return -1; // make the compiler happy
00233 }
00235 int
00236 BaseStreamBuffer::buffer_from_device(char* c, int n)
00237 {
00238    OW_ASSERT("Not implemented, should overwrite" == 0);
00239    return -1; // make the compiler happy
00240 }
00241 
00242 } // end namespace OW_NAMESPACE
00243 

Generated on Thu Feb 9 08:47:52 2006 for openwbem by  doxygen 1.4.6