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_array.hpp>
00024 
00025 namespace Conexus {
00026 
00036 struct Data {
00041     typedef uint8_t Octet;
00042 
00048     typedef boost::shared_array<Octet> Octets;
00049 
00054     Data(): size(0) { }
00055 
00059     Data(const void* d, size_t s): size(s) {
00060         data = Octets(new Octet[size]);
00061         memcpy(data.get(), d, size);
00062     }
00063 
00067     Data(Octets d, size_t s): data(d), size(s) { }
00068 
00072     Data(size_t s): size(s) {
00073         data = Octets(new Octet[s]);
00074     }
00075 
00083     Data clone() {
00084       Data retval(data.get(), size);
00085       return retval;
00086     }
00087 
00092     operator Octet*() {
00093         return data.get();
00094     }
00095 
00097     std::string hex_string(std::string separator=std::string() )
00098     {
00099       std::string s;
00100       size_t i;
00101       uint8_t* p;
00102       char hex[3];
00103 
00104       hex[2] = 0x00;
00105 
00106       p = data.get();
00107       for (i=0; i < size; i++)
00108       {
00109         if (i != 0)
00110           s += separator;
00111         hex[0] = '0' + (*p >> 4);
00112         hex[1] = '0' + (*p & 0x0F);
00113         if (hex[0] > '9')
00114           hex[0] += 7;
00115         if (hex[1] > '9')
00116           hex[1] += 7;
00117         s += hex;
00118         p++;
00119       }
00120       return s;
00121     }
00122 
00124     void clear() {
00125       data.reset();
00126       size = 0;
00127     };
00128 
00132     Octets data;
00133 
00137     size_t size;
00138 };
00139 
00149 struct CData {
00154     typedef uint8_t Octet;
00155 
00161     typedef boost::shared_array<Octet> Octets;
00162 
00167     CData(): data(m_data), size(0) { }
00168 
00172     CData(const void* d, size_t s): data(m_data), size(s) {
00173       m_data = Octets(new Octet[size]);
00174       memcpy(m_data.get(), d, size);
00175     }
00176 
00181     operator const Octet*() {
00182         return data.get();
00183     }
00184 
00185     operator bool() {
00186       return size != 0;
00187     }
00188 
00192     const Octets& data;
00193 
00197     const size_t size;
00198 
00199   protected:
00200     boost::shared_array<Octet> m_data;
00201 };
00202 
00203 
00204 
00205 }
00206 
00207 #endif

Generated on Sun Aug 6 12:16:57 2006 by  doxygen 1.4.6