Changeset 10425 for trunk/FACT++


Ignore:
Timestamp:
04/20/11 15:24:14 (14 years ago)
Author:
tbretz
Message:
Changed the GetHex function; added support for hex and octal numbers.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Converter.cc

    r10387 r10425  
    211211T Converter::Get(std::stringstream &line) const
    212212{
     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
    213231    T val;
    214232    line >> val;
     
    331349    return stream.str();
    332350}
    333 
    334 // --------------------------------------------------------------------------
    335 //
    336 //! Converts from a binary block into a hex representation.
    337 //!
    338 //! @param dat
    339 //!     Pointer to the data block
    340 //!
    341 //! @param size
    342 //!     Size of the data block
    343 //!
    344 //! @param chunk
    345 //!     Size of the size of hex chunks seperted by a ':' in bytes
    346 //!
    347 //! @returns
    348 //!     The string
    349 //!
    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 
    368351
    369352// --------------------------------------------------------------------------
  • trunk/FACT++/src/Converter.h

    r10364 r10425  
    22#define FACT_Converter
    33
     4#include <math.h>
     5
    46#include <vector>
    5 #include <ostream>
     7#include <iomanip>
     8#include <sstream>
    69
    710#include <boost/any.hpp>
     11
     12#include <stdexcept>
    813
    914class Converter
     
    8994    std::vector<char>       GetVector(const std::string &str) const;
    9095
    91     static std::string GetHex(const void *d, size_t size, size_t chunk=1);
    92    
    9396    void ToFits(void* dest, const void* src, size_t size) const;
    94    
     97
    9598    std::vector<char> ToFits(const void* src, size_t size) const;
    9699
    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.");
    100105
    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);
    104107
    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;
    108110
    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    }
    113127
    114128    static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
     
    116130
    117131#endif
     132
     133// ***************************************************************************
     134/** @fn GetHex(const void *dat, size_t size)
     135
     136Converts 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.