# builder.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       builder.cc
    00003 ///             Virtual protocol packet builder wrapper
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include "builder.h"
    00023 #include <stdexcept>
    00024 #include <string.h>
    00025
    00026 namespace Barry {
    00027
    00028 //////////////////////////////////////////////////////////////////////////////
    00029 // DBDataBuilder class
    00030
    00031
    00032 DBDataBuilder::DBDataBuilder(const DBData &orig)
    00033         : m_orig(orig)
    00034 {
    00035 }
    00036
    00037 DBDataBuilder::~DBDataBuilder()
    00038 {
    00039 }
    00040
    00041 bool DBDataBuilder::BuildRecord(DBData &data, size_t &offset,
    00042                                 const IConverter *ic)
    00043 {
    00044         if( offset == m_orig.GetOffset() ) {
    00045                 data = m_orig;
    00046         }
    00047         else {
    00048                 // copy the metadata
    00049                 data.CopyMeta(m_orig);
    00050
    00051                 // copy the buffer, to the new offset
    00052                 if( m_orig.GetOffset() > m_orig.GetData().GetSize() )
    00053                         throw std::logic_error("DBDataBuilder: offset greater than size");
    00054                 size_t actual = m_orig.GetData().GetSize() - m_orig.GetOffset();
    00055                 size_t total = offset + actual;
    00056                 unsigned char *buf = data.UseData().GetBuffer(total);
    00057                 memcpy(buf + offset,
    00058                         m_orig.GetData().GetData() + m_orig.GetOffset(),
    00059                         actual);
    00060                 data.UseData().ReleaseBuffer(total);
    00061
    00062                 // set the new offset
    00063                 data.SetOffset(offset);
    00064         }
    00065         return true;
    00066 }
    00067
    00068 bool DBDataBuilder::FetchRecord(DBData &data, const IConverter *ic)
    00069 {
    00070         data = m_orig;
    00071         return true;
    00072 }
    00073
    00074 bool DBDataBuilder::EndOfFile() const
    00075 {
    00076         return true;
    00077 }
    00078
    00079 } // namespace Barry
    00080
    00081