blocxx

IOIFCStreamBuffer.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/IOIFCStreamBuffer.hpp"
00041 #include <cstring> // for strcmp
00042 
00043 namespace BLOCXX_NAMESPACE
00044 {
00045 
00046 namespace
00047 {
00048    IOIFCStreamBuffer::EDirectionFlag directionToEnum(const char* direction)
00049    {
00050       if (strcmp(direction, "in") == 0)
00051       {
00052          return IOIFCStreamBuffer::E_IN;
00053       }
00054       if (strcmp(direction, "out") == 0)
00055       {
00056          return IOIFCStreamBuffer::E_OUT;
00057       }
00058       return IOIFCStreamBuffer::E_IN_OUT;
00059    }
00060 }
00061 
00063 IOIFCStreamBuffer::IOIFCStreamBuffer(IOIFC* dev, int bufSize,
00064    const char* direction)
00065    : BaseStreamBuffer(directionToEnum(direction), bufSize)
00066    , m_dev(dev)
00067    , m_tied_buf(0)
00068    , m_error_action(IOIFC::E_RETURN_ON_ERROR)
00069 {
00070 }
00072 IOIFCStreamBuffer::IOIFCStreamBuffer(IOIFC* dev, EDirectionFlag direction, int bufSize)
00073    : BaseStreamBuffer(direction, bufSize)
00074    , m_dev(dev)
00075    , m_tied_buf(0)
00076    , m_error_action(IOIFC::E_RETURN_ON_ERROR)
00077 {
00078 }
00080 IOIFCStreamBuffer::~IOIFCStreamBuffer()
00081 {
00082    try
00083    {
00084       sync();
00085    }
00086    catch (...)
00087    {
00088    }
00089 }
00091 std::streambuf * IOIFCStreamBuffer::tie(std::streambuf * tied_buf)
00092 {
00093    std::streambuf * retval = m_tied_buf;
00094    m_tied_buf = tied_buf;
00095    return retval;
00096 }
00098 void IOIFCStreamBuffer::setErrorAction(IOIFC::ErrorAction error_action)
00099 {
00100    m_error_action = error_action;
00101 }
00102 
00104 int
00105 IOIFCStreamBuffer::buffer_from_device(char* c, int n)
00106 {
00107    if (m_tied_buf)
00108    {
00109       m_tied_buf->pubsync();
00110    }
00111    return m_dev->read(c, n, m_error_action);
00112 }
00114 int
00115 IOIFCStreamBuffer::buffer_to_device(const char* c, int n)
00116 {
00117    while (n > 0)
00118    {
00119       int cnt = m_dev->write(c, n, m_error_action);
00120       if (cnt == -1) // failure
00121       {
00122          return -1;
00123       }
00124       c += cnt;
00125       n -= cnt;
00126    }
00127    return 0;
00128 }
00129 
00131 void
00132 IOIFCStreamBuffer::reset()
00133 { 
00134    initBuffers(); 
00135 }
00136 
00137 } // end namespace BLOCXX_NAMESPACE
00138