#ifndef FACT_Converter #define FACT_Converter #include #include #include #include #include #include class Converter { public: typedef std::pair Type; typedef std::pair Offset; typedef std::pair Format; typedef std::vector FormatList; struct O { }; struct W { }; static std::string Clean(std::string s); private: std::ostream &wout; /// ostream to which output is redirected const std::string fFormat; /// Original format string const FormatList fList; /// Compiled format description template T Get(std::stringstream &line) const; bool GetBool(std::stringstream &line) const; std::string GetString(std::stringstream &line) const; std::string GetStringEol(std::stringstream &line) const; template void GetBinImp(std::vector &v, const T &val) const; template void GetBinImp(std::vector &v, const T &val) const; void GetBinString(std::vector &v, const std::string &val) const; void GetBinString(std::vector &v, const std::string &val) const; template std::string GetString(const char *&data) const; template static Type GetType(); template static Type GetVoid(); template std::vector Get(const std::string &str) const; template T Get(const void *d, size_t size) const; template void Add(std::string &str, const char* &ptr) const; void AddString(std::string &str, const char* &ptr) const; template void Add(std::vector &vec, const char* &ptr) const; void AddString(std::vector &vec, const char* &ptr) const; public: Converter(std::ostream &out, const std::string &fmt, bool strict=true); Converter(const std::string &fmt, bool strict=true); /// @returns whether the interpreted format was valid but empty ("") bool empty() const { return fList.size()==1 && fList.back().first.second==0; } /// @returns whether the compilation was successfull bool valid() const { return !fList.empty() && fList.back().first.second==0; } /// @returns true if the compilation failed bool operator!() const { return !valid(); } const FormatList &GetList() const { return fList; } static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false); static FormatList Compile(const std::string &fmt, bool strict=false); std::string GetString(const void *d, size_t size) const; std::vector GetVector(const void *d, size_t size) const; std::vector GetAny(const void *d, size_t size) const; std::vector GetAny(const std::string &str) const; std::vector GetVector(const std::string &str) const; void ToFits(void* dest, const void* src, size_t size) const; std::vector ToFits(const void* src, size_t size) const; template static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true) { if (size%sizeof(T)!=0) throw std::runtime_error("Total not dividable by typesize."); const T *ptr = reinterpret_cast(dat); std::ostringstream text; text << std::hex; const size_t w = log2l(size+1)/4+1; for (size_t i=0; i static std::string GetHex(const S &s, size_t col=0, bool prefix=true) { return GetHex(&s, sizeof(S), col, prefix); } void Print(std::ostream &out) const; void Print() const; static std::vector Regex(const std::string &expr, const std::string &line); }; #endif // *************************************************************************** /** @fn GetHex(const void *dat, size_t size) Converts from a binary block into a hex representation. @param dat Pointer to the data block @param size Size of the data block (in bytes) @param col Number of columns before new line @tparam T type to which the data should be converted. Most usefull types are unsigned byte, unsigned short, unsigned int, uint8_t, uint16_t, ... @returns The string **/ // ***************************************************************************