Changeset 10425 for trunk/FACT++/src
- Timestamp:
- 04/20/11 15:24:14 (14 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/Converter.cc
r10387 r10425 211 211 T Converter::Get(std::stringstream &line) const 212 212 { 213 char c; 214 line >> c; 215 216 if (c=='0') 217 { 218 if (line.peek()==-1) 219 return 0; 220 221 if (line.peek()=='x') 222 line >> hex; 223 else 224 line >> oct; 225 226 line.unget(); 227 } 228 229 line.unget(); 230 213 231 T val; 214 232 line >> val; … … 331 349 return stream.str(); 332 350 } 333 334 // --------------------------------------------------------------------------335 //336 //! Converts from a binary block into a hex representation.337 //!338 //! @param dat339 //! Pointer to the data block340 //!341 //! @param size342 //! Size of the data block343 //!344 //! @param chunk345 //! Size of the size of hex chunks seperted by a ':' in bytes346 //!347 //! @returns348 //! The string349 //!350 string Converter::GetHex(const void *dat, size_t size, size_t chunk)351 {352 const unsigned char *ptr = reinterpret_cast<const unsigned char *>(dat);353 354 ostringstream text;355 356 text << hex;357 358 for (size_t i=0; i<size; i++)359 {360 text << setw(2) << setfill('0') << (unsigned int)ptr[i];361 if ((i%chunk)==chunk-1)362 text << ":";363 }364 365 return text.str();366 }367 368 351 369 352 // -------------------------------------------------------------------------- -
trunk/FACT++/src/Converter.h
r10364 r10425 2 2 #define FACT_Converter 3 3 4 #include <math.h> 5 4 6 #include <vector> 5 #include <ostream> 7 #include <iomanip> 8 #include <sstream> 6 9 7 10 #include <boost/any.hpp> 11 12 #include <stdexcept> 8 13 9 14 class Converter … … 89 94 std::vector<char> GetVector(const std::string &str) const; 90 95 91 static std::string GetHex(const void *d, size_t size, size_t chunk=1);92 93 96 void ToFits(void* dest, const void* src, size_t size) const; 94 97 95 98 std::vector<char> ToFits(const void* src, size_t size) const; 96 99 97 /* 98 // Returns the number of entries in Converter:vec 99 int N() const { return vec.size(); } 100 template<typename T> 101 static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true) 102 { 103 if (size%sizeof(T)!=0) 104 throw std::runtime_error("Total not dividable by typesize."); 100 105 101 /// Checks if an entry with the given class type is available at index i in Converter::vec 102 template <class T> 103 bool Has(unsigned int i) const { return i<vec.size() && vec[i].type()==typeid(T); } 106 const T *ptr = reinterpret_cast<const T*>(dat); 104 107 105 /// return the entry with the given class type at index i from Converter::vec (exception safe) 106 template <class T> 107 T Get(int i) const { return !Has<T>(i) ? T() : boost::any_cast<T>(vec[i]); } 108 std::ostringstream text; 109 text << std::hex; 108 110 109 /// return the entry with the given class type at index i from Converter::vec (throws exceptions) 110 template <class T> 111 T At(int i) const { return boost::any_cast<T>(vec[i]); } 112 */ 111 const size_t w = log2l(size+1)/4+1; 112 113 for (size_t i=0; i<size/sizeof(T); i++) 114 { 115 if (prefix && i%col==0) 116 text << std::setfill('0') << std::setw(w) << i << "| "; 117 118 text << std::setfill('0') << std::setw(2*sizeof(T)) << (unsigned int)ptr[i]; 119 text << ':'; 120 121 if (i%col==col-1) 122 text << '\n'; 123 } 124 125 return text.str(); 126 } 113 127 114 128 static std::vector<std::string> Regex(const std::string &expr, const std::string &line); … … 116 130 117 131 #endif 132 133 // *************************************************************************** 134 /** @fn GetHex(const void *dat, size_t size) 135 136 Converts from a binary block into a hex representation. 137 138 @param dat 139 Pointer to the data block 140 141 @param size 142 Size of the data block (in bytes) 143 144 @param col 145 Number of columns before new line 146 147 @tparam T 148 type to which the data should be converted. Most usefull types are 149 unsigned byte, unsigned short, unsigned int, uint8_t, uint16_t, ... 150 151 @returns 152 The string 153 154 155 **/ 156 // ***************************************************************************
Note:
See TracChangeset
for help on using the changeset viewer.