| 1 | #ifndef FACT_Converter
|
|---|
| 2 | #define FACT_Converter
|
|---|
| 3 |
|
|---|
| 4 | #include <math.h>
|
|---|
| 5 |
|
|---|
| 6 | #include <vector>
|
|---|
| 7 | #include <iomanip>
|
|---|
| 8 | #include <sstream>
|
|---|
| 9 |
|
|---|
| 10 | #include <boost/any.hpp>
|
|---|
| 11 |
|
|---|
| 12 | #include <stdexcept>
|
|---|
| 13 |
|
|---|
| 14 | #include <iostream>
|
|---|
| 15 |
|
|---|
| 16 | class Converter
|
|---|
| 17 | {
|
|---|
| 18 | public:
|
|---|
| 19 | typedef std::pair<const std::type_info *, int> Type;
|
|---|
| 20 | typedef std::pair<int, int> Offset;
|
|---|
| 21 | typedef std::pair<Type, Offset> Format;
|
|---|
| 22 | typedef std::vector<Format> FormatList;
|
|---|
| 23 |
|
|---|
| 24 | struct O { };
|
|---|
| 25 | struct W { };
|
|---|
| 26 |
|
|---|
| 27 | static std::string Clean(std::string s);
|
|---|
| 28 |
|
|---|
| 29 | private:
|
|---|
| 30 | std::ostream &wout; /// ostream to which output is redirected
|
|---|
| 31 |
|
|---|
| 32 | const std::string fFormat; /// Original format string
|
|---|
| 33 | const FormatList fList; /// Compiled format description
|
|---|
| 34 |
|
|---|
| 35 | template <class T>
|
|---|
| 36 | T Get(std::stringstream &line) const;
|
|---|
| 37 |
|
|---|
| 38 | bool GetBool(std::stringstream &line) const;
|
|---|
| 39 | std::string GetString(std::stringstream &line) const;
|
|---|
| 40 | std::string GetStringEol(std::stringstream &line) const;
|
|---|
| 41 |
|
|---|
| 42 | template<class T>
|
|---|
| 43 | void GetBinImp(std::vector<char> &v, const T &val) const;
|
|---|
| 44 | template<class T>
|
|---|
| 45 | void GetBinImp(std::vector<boost::any> &v, const T &val) const;
|
|---|
| 46 |
|
|---|
| 47 | void GetBinString(std::vector<char> &v, const std::string &val) const;
|
|---|
| 48 | void GetBinString(std::vector<boost::any> &v, const std::string &val) const;
|
|---|
| 49 |
|
|---|
| 50 | template<class T>
|
|---|
| 51 | std::string GetString(const char *&data) const;
|
|---|
| 52 |
|
|---|
| 53 | template<class T>
|
|---|
| 54 | static Type GetType();
|
|---|
| 55 | template<class T>
|
|---|
| 56 | static Type GetVoid();
|
|---|
| 57 |
|
|---|
| 58 | template <class T>
|
|---|
| 59 | std::vector<T> Get(const std::string &str) const;
|
|---|
| 60 | template <class T>
|
|---|
| 61 | T Get(const void *d, size_t size) const;
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | template<class T>
|
|---|
| 66 | void Add(std::string &str, const char* &ptr) const;
|
|---|
| 67 | void AddString(std::string &str, const char* &ptr) const;
|
|---|
| 68 | template<class T>
|
|---|
| 69 | void Add(std::vector<boost::any> &vec, const char* &ptr) const;
|
|---|
| 70 | void AddString(std::vector<boost::any> &vec, const char* &ptr) const;
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | public:
|
|---|
| 74 | Converter(std::ostream &out, const std::string &fmt, bool strict=true);
|
|---|
| 75 | Converter(const std::string &fmt, bool strict=true);
|
|---|
| 76 |
|
|---|
| 77 | /// @returns whether the interpreted format was valid but empty ("")
|
|---|
| 78 | bool empty() const { return fList.size()==1 && fList.back().first.second==0; }
|
|---|
| 79 |
|
|---|
| 80 | /// @returns whether the compilation was successfull
|
|---|
| 81 | bool valid() const { return !fList.empty() && fList.back().first.second==0; }
|
|---|
| 82 |
|
|---|
| 83 | /// @returns true if the compilation failed
|
|---|
| 84 | bool operator!() const { return !valid(); }
|
|---|
| 85 |
|
|---|
| 86 | const FormatList &GetList() const { return fList; }
|
|---|
| 87 |
|
|---|
| 88 | static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false);
|
|---|
| 89 | static FormatList Compile(const std::string &fmt, bool strict=false);
|
|---|
| 90 |
|
|---|
| 91 | std::string GetString(const void *d, size_t size) const;
|
|---|
| 92 | std::vector<char> GetVector(const void *d, size_t size) const;
|
|---|
| 93 | std::vector<boost::any> GetAny(const void *d, size_t size) const;
|
|---|
| 94 |
|
|---|
| 95 | std::vector<boost::any> GetAny(const std::string &str) const;
|
|---|
| 96 | std::vector<char> GetVector(const std::string &str) const;
|
|---|
| 97 |
|
|---|
| 98 | std::vector<std::string> ToStrings(const void *src/*, size_t size*/) const;
|
|---|
| 99 | void ToFits(void* dest, const void* src, size_t size) const;
|
|---|
| 100 |
|
|---|
| 101 | std::vector<char> ToFits(const void* src, size_t size) const;
|
|---|
| 102 |
|
|---|
| 103 | static std::string ToFormat(const std::vector<std::string> &fits);
|
|---|
| 104 |
|
|---|
| 105 | template<typename T>
|
|---|
| 106 | static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true)
|
|---|
| 107 | {
|
|---|
| 108 | if (size%sizeof(T)!=0)
|
|---|
| 109 | throw std::runtime_error("GetHex: Total not dividable by typesize.");
|
|---|
| 110 |
|
|---|
| 111 | const T *ptr = reinterpret_cast<const T*>(dat);
|
|---|
| 112 |
|
|---|
| 113 | std::ostringstream text;
|
|---|
| 114 | text << std::hex;
|
|---|
| 115 |
|
|---|
| 116 | const size_t w = nearbyint(ceil(log2(size+1)))/4+1;
|
|---|
| 117 |
|
|---|
| 118 | for (size_t i=0; i<size/sizeof(T); i++)
|
|---|
| 119 | {
|
|---|
| 120 | if (prefix && col!=0 && i%col==0)
|
|---|
| 121 | text << std::setfill('0') << std::setw(w) << i << "| ";
|
|---|
| 122 |
|
|---|
| 123 | text << std::setfill('0') << std::setw(2*sizeof(T));
|
|---|
| 124 | text << (unsigned int)ptr[i] << ':';
|
|---|
| 125 |
|
|---|
| 126 | if (col!=0 && i%col==col-1)
|
|---|
| 127 | text << '\n';
|
|---|
| 128 |
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | return text.str();
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | template<typename T, typename S>
|
|---|
| 135 | static std::string GetHex(const S &s, size_t col=0, bool prefix=true)
|
|---|
| 136 | {
|
|---|
| 137 | return GetHex<T>(&s, sizeof(S), col, prefix);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void Print(std::ostream &out) const;
|
|---|
| 141 | void Print() const;
|
|---|
| 142 |
|
|---|
| 143 | static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
|
|---|
| 144 | };
|
|---|
| 145 |
|
|---|
| 146 | #endif
|
|---|
| 147 |
|
|---|
| 148 | // ***************************************************************************
|
|---|
| 149 | /** @template GetHex(const void *dat, size_t size, size_t col, bool prefix)
|
|---|
| 150 |
|
|---|
| 151 | Converts from a binary block into a hex representation.
|
|---|
| 152 |
|
|---|
| 153 | @param dat
|
|---|
| 154 | Pointer to the data block
|
|---|
| 155 |
|
|---|
| 156 | @param size
|
|---|
| 157 | Size of the data block (in bytes)
|
|---|
| 158 |
|
|---|
| 159 | @param col
|
|---|
| 160 | Number of columns before new line (zero <default> to write a
|
|---|
| 161 | continous stream
|
|---|
| 162 |
|
|---|
| 163 | @param prefix
|
|---|
| 164 | Boolean which defines whether each line should be prefixed with a counter,
|
|---|
| 165 | the default is true. It is ignored if col==0
|
|---|
| 166 |
|
|---|
| 167 | @tparam T
|
|---|
| 168 | type to which the data should be converted. Most usefull types are
|
|---|
| 169 | unsigned byte, unsigned short, unsigned int, uint8_t, uint16_t, ...
|
|---|
| 170 |
|
|---|
| 171 | @returns
|
|---|
| 172 | The string
|
|---|
| 173 |
|
|---|
| 174 | **/
|
|---|
| 175 | // ***************************************************************************
|
|---|