#ifndef FACT_Converter #define FACT_Converter #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; static std::string GetHex(const void *d, size_t size, size_t chunk=1); void ToFits(void* dest, const void* src, size_t size) const; std::vector ToFits(const void* src, size_t size) const; /* // Returns the number of entries in Converter:vec int N() const { return vec.size(); } /// Checks if an entry with the given class type is available at index i in Converter::vec template bool Has(unsigned int i) const { return i T Get(int i) const { return !Has(i) ? T() : boost::any_cast(vec[i]); } /// return the entry with the given class type at index i from Converter::vec (throws exceptions) template T At(int i) const { return boost::any_cast(vec[i]); } */ static std::vector Regex(const std::string &expr, const std::string &line); }; #endif