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 BIT_DATA_H
00020 #define BIT_DATA_H
00021 
00022 #include <string>
00023 #include <boost/shared_array.hpp>
00024 
00025 #include <bit/utility.h>
00026 
00027 namespace bit {
00028 
00038 struct Data {
00043     typedef uint8_t Octet;
00044 
00050     typedef boost::shared_array<Octet> Octets;
00051 
00056     Data(): data(NULL), size(0) { }
00057 
00061     Data(const void* d, size_t s): data(NULL), size(s) {
00062       data = Octets(new Octet[size]);
00063       memcpy(data.get(), d, size);
00064     }
00065 
00069     Data(Octets d, size_t s): data(d), size(s) { }
00070 
00074     Data(size_t s): data(NULL), size(s) {
00075       data = Octets(new Octet[s]);
00076     }
00077 
00085     Data clone() {
00086       Data retval(data.get(), size);
00087       return retval;
00088     }
00089 
00094     operator Octet*()
00095     {
00096       return data.get();
00097     }
00098 
00100     std::string hex_string( bool uppercase=true,
00101                             std::string separator="",
00102                             std::string prefix="0x",
00103                             std::string postfix=""
00104                           )
00105                             {
00106                               return bit::hex_string(data.get(), size, uppercase, separator, prefix, postfix);
00107                             }
00108 
00109     std::string binary_string( std::string separator="",
00110                                size_t separator_digits=8,
00111                                std::string prefix="",
00112                                std::string postfix=""
00113                              )
00114                                {
00115                                  return bit::binary_string(data.get(), size, separator, separator_digits, prefix, postfix);
00116                                }
00117 
00119     void clear() {
00120       data.reset();
00121       size = 0;
00122     };
00123 
00127     Octets data;
00128 
00132     size_t size;
00133   };
00134 
00144 struct CData {
00149     typedef uint8_t Octet;
00150 
00156     typedef boost::shared_array<const Octet> Octets;
00157 
00162     CData(): data(NULL), size(0) { }
00163 
00167     CData(const Octet* d, size_t s): data(d), size(s)
00168     { }
00169 
00174     operator const Octet*()
00175     {
00176       return data.get();
00177     }
00178 
00182     Octets data;
00183 
00187     const size_t size;
00188   };
00189 
00190 }
00191 
00192 #endif

Generated on Thu Jul 6 14:38:08 2006 by  doxygen 1.4.6