data.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU 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 library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef CONEXUSDATA_H
00020 #define CONEXUSDATA_H
00021 
00022 #include <string>
00023 #include <boost/shared_ptr.hpp>
00024 
00025 namespace Conexus {
00026 
00036 struct Data {
00041     typedef uint8_t Octet;
00042 
00048     typedef boost::shared_ptr<Octet> Octets;
00049     
00050     typedef boost::shared_ptr<const Octet> ConstOctets;
00051     
00056     Data(): m_size(0) { }
00057 
00061     Data(const void* d, size_t s): m_size(s) {
00062         m_data = Octets(new Octet[m_size], boost::checked_array_deleter<Octet>() );
00063         memcpy(m_data.get(), d, m_size);
00064     }
00065 
00069     Data(Octets d, size_t s): m_data(d), m_size(s) { }
00070 
00074     Data(size_t s): m_size(s) {
00075       m_data = Octets(new Octet[s], boost::checked_array_deleter<Octet>() );
00076     }
00077 
00078     Octets data() { return m_data; }
00079     
00080     ConstOctets data() const { return boost::static_pointer_cast<const Octet>(m_data); }
00081     
00082     void set_data( Octet const* newdata, size_t newsize ) {
00083       m_data = Octets( new Octet[newsize], boost::checked_array_deleter<Octet>() );
00084       memcpy( m_data.get(), newdata, newsize );
00085       m_size = newsize;
00086     }
00087     
00088     size_t size() const { return m_size; }
00089     
00090     void set_size( size_t s ) {
00091       if ( m_size == s )
00092         return;
00093       Octets olddata = m_data;
00094       m_data = Octets( new Octet[s], boost::checked_array_deleter<Octet>() );
00095       memcpy( m_data.get(), olddata.get(), ((m_size<s)?m_size:s) );
00096       m_size = s;
00097     }
00098     
00106     Data clone() const {
00107       Data retval(m_data.get(), m_size);
00108       return retval;
00109     }
00110 
00115     operator Octet*() {
00116         return m_data.get();
00117     }
00118     
00119     operator Octet const*() const {
00120       return static_cast<Octet const*>(m_data.get());
00121     }
00122     
00123     Octet* raw_data() {
00124       return m_data.get();
00125     }
00126     
00127     Octet const* raw_data() const {
00128       return static_cast<Octet const*>(m_data.get() );
00129     }
00130 
00132     std::string hex_string(std::string separator=std::string() ) const
00133     {
00134       std::string s;
00135       size_t i;
00136       uint8_t* p;
00137       char hex[3];
00138 
00139       hex[2] = 0x00;
00140 
00141       p = m_data.get();
00142       for (i=0; i < m_size; i++)
00143       {
00144         if (i != 0)
00145           s += separator;
00146         hex[0] = '0' + (*p >> 4);
00147         hex[1] = '0' + (*p & 0x0F);
00148         if (hex[0] > '9')
00149           hex[0] += 7;
00150         if (hex[1] > '9')
00151           hex[1] += 7;
00152         s += hex;
00153         p++;
00154       }
00155       return s;
00156     }
00157 
00159     void clear() {
00160       m_data.reset();
00161       m_size = 0;
00162     };
00163     
00164   protected:
00165 
00169     Octets m_data;
00170 
00174     size_t m_size;
00175 };
00176 
00177 
00178 }
00179 
00180 #endif

Generated on Sat Aug 26 17:34:53 2006 by  doxygen 1.4.6