Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

InSerializer.cxx

00001 /*
00002  * Copyright (C) 1991-9 Eric M. Hopper <hopper@omnifarious.mn.org>
00003  * 
00004  *     This program is free software; you can redistribute it and/or modify it
00005  *     under the terms of the GNU Lesser General Public License as published
00006  *     by the Free Software Foundation; either version 2 of the License, or
00007  *     (at your option) any later version.
00008  * 
00009  *     This program is distributed in the hope that it will be useful, but
00010  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *     Lesser General Public License for more details.
00013  * 
00014  *     You should have received a copy of the GNU Lesser General Public
00015  *     License along with this program; if not, write to the Free Software
00016  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 /* $Header: /home/hopper/src/cvs/C++/StrMod/InSerializer.cxx,v 1.7 2002/08/29 00:58:04 hopper Exp $ */
00020 
00021 // For a log, see ChangeLog
00022 // Revision 1.2  1998/08/24 02:07:43  hopper
00023 // Changed order of members in initialization list to correspond to actual
00024 // initialization order.
00025 //
00026 // Revision 1.1  1997/04/14 23:55:06  hopper
00027 // Added class to aid in serialization of simple data tyes.
00028 //
00029 
00030 #ifdef __GNUG__
00031 #  pragma implementation "InSerializer.h"
00032 #endif
00033 
00034 #include <StrMod/InSerializer.h>
00035 #include <StrMod/StrChunk.h>
00036 #include <StrMod/StrChunkPtr.h>
00037 #include <StrMod/ChunkIterator.h>
00038 #include <StrMod/StaticBuffer.h>
00039 
00040 namespace strmod {
00041 namespace strmod {
00042 
00043 struct InSerializer::Impl {
00044    inline Impl(const void *buf, size_t size);
00045    inline Impl(const StrChunkPtr &ptr);
00046    inline ~Impl();
00047    inline size_t BytesLeft() const;
00048 
00049    StrChunk::const_iterator i_;
00050    StrChunk::const_iterator end_;
00051    StrChunkPtr chnkptr_;
00052 };
00053 
00054 inline InSerializer::Impl::Impl(const void *buf, size_t size)
00055 {
00056    StaticBuffer *sbuf = new StaticBuffer(buf, size);
00057    chnkptr_ = sbuf;
00058    i_ = sbuf->begin();
00059    end_ = sbuf->end();
00060 }
00061 
00062 inline InSerializer::Impl::Impl(const StrChunkPtr &ptr)
00063      : i_(ptr->begin()), end_(ptr->end()), chnkptr_(ptr)
00064 {
00065 }
00066 
00067 inline InSerializer::Impl::~Impl()
00068 {
00069 }
00070 
00071 inline size_t InSerializer::Impl::BytesLeft() const
00072 {
00073    return(end_ - i_);
00074 }
00075 
00076 InSerializer::InSerializer(const StrChunkPtr &ptr)
00077      : impl_(*(new Impl(ptr))), had_error_(false)
00078 {
00079 }
00080 
00081 InSerializer::InSerializer(const void *buf, size_t len)
00082      : impl_(*(new Impl(buf, len))), had_error_(false)
00083 {
00084 }
00085 
00086 InSerializer::~InSerializer()
00087 {
00088    delete &impl_;
00089 }
00090 
00091 lcore::S1Byte InSerializer::GetS1Byte()
00092 {
00093    if (HadError()) {
00094       return(0);
00095    } else if (impl_.BytesLeft() < 1) {
00096       had_error_ = true;
00097       return(0);
00098    } else {
00099       lcore::U1Byte ch = *(impl_.i_);
00100       ++impl_.i_;
00101 
00102       lcore::S1Byte val;
00103 
00104       lcore::ntoh(&ch, val);
00105       return(val);
00106    }
00107 }
00108 
00109 lcore::U1Byte InSerializer::GetU1Byte()
00110 {
00111    if (HadError()) {
00112       return(0);
00113    } else if (impl_.BytesLeft() < 1) {
00114       had_error_ = true;
00115       return(0);
00116    } else {
00117       lcore::U1Byte ch = *(impl_.i_);
00118       ++impl_.i_;
00119 
00120       lcore::U1Byte val;
00121 
00122       lcore::ntoh(&ch, val);
00123       return(val);
00124    }
00125 }
00126 
00127 lcore::S2Byte InSerializer::GetS2Byte()
00128 {
00129    if (HadError()) {
00130       return(0);
00131    } else if (impl_.BytesLeft() < 2) {
00132       had_error_ = true;
00133       return(0);
00134    } else {
00135       lcore::U1Byte ch[2];
00136       lcore::S2Byte val;
00137 
00138       ch[0] = *(impl_.i_);
00139       ++impl_.i_;
00140       ch[1] = *(impl_.i_);
00141       ++impl_.i_;
00142       lcore::ntoh(ch, val);
00143       return(val);
00144    }
00145 }
00146 
00147 lcore::U2Byte InSerializer::GetU2Byte()
00148 {
00149    if (HadError()) {
00150       return(0);
00151    } else if (impl_.BytesLeft() < 2) {
00152       had_error_ = true;
00153       return(0);
00154    } else {
00155       lcore::U1Byte ch[2];
00156       lcore::U2Byte val;
00157 
00158       ch[0] = *(impl_.i_);
00159       ++impl_.i_;
00160       ch[1] = *(impl_.i_);
00161       ++impl_.i_;
00162       lcore::ntoh(ch, val);
00163       return(val);
00164    }
00165 }
00166 
00167 lcore::S4Byte InSerializer::GetS4Byte()
00168 {
00169    if (HadError()) {
00170       return(0);
00171    } else if (impl_.BytesLeft() < 4) {
00172       had_error_ = true;
00173       return(0);
00174    } else {
00175       lcore::U1Byte ch[4];
00176       lcore::S4Byte val;
00177 
00178       ch[0] = *(impl_.i_);
00179       ++impl_.i_;
00180       ch[1] = *(impl_.i_);
00181       ++impl_.i_;
00182       ch[2] = *(impl_.i_);
00183       ++impl_.i_;
00184       ch[3] = *(impl_.i_);
00185       ++impl_.i_;
00186       lcore::ntoh(ch, val);
00187       return(val);
00188    }
00189 }
00190 
00191 lcore::U4Byte InSerializer::GetU4Byte()
00192 {
00193    if (HadError()) {
00194       return(0);
00195    } else if (impl_.BytesLeft() < 4) {
00196       had_error_ = true;
00197       return(0);
00198    } else {
00199       lcore::U1Byte ch[4];
00200       lcore::U4Byte val;
00201 
00202       ch[0] = *(impl_.i_);
00203       ++impl_.i_;
00204       ch[1] = *(impl_.i_);
00205       ++impl_.i_;
00206       ch[2] = *(impl_.i_);
00207       ++impl_.i_;
00208       ch[3] = *(impl_.i_);
00209       ++impl_.i_;
00210       lcore::ntoh(ch, val);
00211       return(val);
00212    }
00213 }
00214 
00215 const std::string InSerializer::GetString()
00216 {
00217    if (HadError()) {
00218       return("");
00219    } else if (impl_.BytesLeft() < 3) {
00220       had_error_ = true;
00221       return("");
00222    } else {
00223       lcore::U1Byte ch[2];
00224       lcore::U2Byte len;
00225 
00226       ch[0] = *(impl_.i_);
00227       ++impl_.i_;
00228       ch[1] = *(impl_.i_);
00229       ++impl_.i_;
00230 
00231       lcore::ntoh(ch, len);
00232       if ((len < 1) || (len > impl_.BytesLeft())) {
00233          had_error_ = true;
00234          return("");
00235       } else {
00236          std::string result;
00237          lcore::U2Byte templen = len - 1;  // Skip trailing '\0'
00238 
00239          result.reserve(templen);  // Weak attempt to optimize performance.
00240          while (templen--) {
00241             result += *(impl_.i_);
00242             ++impl_.i_;
00243          }
00244          ++impl_.i_;  // Move iterator past trailing '\0'
00245          return(result);
00246       }
00247    }
00248 }
00249 
00250 void InSerializer::GetRaw(void *destbuf, size_t len)
00251 {
00252    if (!HadError()) {
00253       if (impl_.BytesLeft() >= len) {
00254          lcore::U1Byte *dest = static_cast<lcore::U1Byte *>(destbuf);
00255          size_t templen = len;
00256 
00257          while (templen--) {
00258             *dest++ = *(impl_.i_);
00259             ++impl_.i_;
00260          }
00261       } else {
00262          had_error_ = true;
00263       }
00264    }
00265 }
00266 
00267 size_t InSerializer::BytesLeft()
00268 {
00269    return(impl_.BytesLeft());
00270 }
00271 
00272 };  // End namespace strmod
00273 };  // End namespace strmod

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1