blocxx

DataStreams.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/Types.hpp"
00041 #include "blocxx/DataStreams.hpp"
00042 #include <cstring>
00043 
00044 namespace BLOCXX_NAMESPACE
00045 {
00046 
00048 int
00049 DataIStreamBuf::underflow()
00050 {
00051    return (gptr() < egptr()) ? static_cast<unsigned char>(*gptr()) : EOF;  // need a static_cast so a -1 doesn't turn into an EOF
00052 }
00053 
00055 std::streambuf::pos_type
00056 DataIStreamBuf::seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which)
00057 {
00058    pos_type ret = pos_type(off_type(-1));
00059 
00060    char* begin = eback();
00061    char* cur = gptr();
00062    char* end = egptr();
00063 
00064    off_type newOff = 0;
00065 
00066    if (way == std::ios_base::cur)
00067    {
00068       newOff = cur - begin;
00069    }
00070    else if (way == std::ios_base::end)
00071    {
00072       newOff = end - begin;
00073    }
00074 
00075    if (newOff + off >= 0 && end - begin >= newOff + off)
00076    {
00077       setg(begin, begin + newOff + off, end);
00078       ret = pos_type(newOff);
00079    }
00080 
00081    return ret;
00082 }
00083 
00085 std::streambuf::pos_type
00086 DataIStreamBuf::seekpos(pos_type sp, std::ios_base::openmode which)
00087 {
00088    pos_type ret = pos_type(off_type(-1));
00089 
00090    char* begin = eback();
00091    char* end = egptr();
00092 
00093    if (sp <= end - begin)
00094    {
00095       setg(begin, begin + sp, end);
00096       ret = sp;
00097    }
00098 
00099    return ret;
00100 }
00101 
00103 DataOStreamBuf::DataOStreamBuf(size_t initialSize)
00104    : std::streambuf()
00105 {
00106    m_bfr.reserve(initialSize);
00107 }
00109 int
00110 DataOStreamBuf::overflow(int c)
00111 {
00112    m_bfr.push_back(c);
00113    return 0;
00114 }
00116 std::streamsize
00117 DataOStreamBuf::xsputn(const char* s, std::streamsize n)
00118 {
00119    m_bfr.insert(m_bfr.end(), s, s+n);
00120    return n;
00121 }
00122 
00123 } // end namespace BLOCXX_NAMESPACE
00124