X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/20d884ddac6b8cacedb2701e282efe3ff9785cbf..be3d58dbca4e5aeb16c15983ca59a1db56b9fd7a:/stglibs/srvconf.lib/parsers/property.h?ds=sidebyside diff --git a/stglibs/srvconf.lib/parsers/property.h b/stglibs/srvconf.lib/parsers/property.h index 59d47817..7aa98aeb 100644 --- a/stglibs/srvconf.lib/parsers/property.h +++ b/stglibs/srvconf.lib/parsers/property.h @@ -33,7 +33,7 @@ class BASE_PROPERTY_PARSER { public: virtual ~BASE_PROPERTY_PARSER() {} - virtual bool Parse(const char ** attr, const std::string & attrName) = 0; + virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & fromEncoding) = 0; }; template @@ -42,12 +42,30 @@ class PROPERTY_PARSER : public BASE_PROPERTY_PARSER public: typedef bool (* FUNC)(const char **, T &, const std::string &); PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} - virtual bool Parse(const char ** attr, const std::string & attrName) { return func(attr, value, attrName); } + PROPERTY_PARSER(T & v, FUNC f, const std::string & e) : value(v), func(f), encoding(e) {} + virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & /*fromEncoding*/) { return func(attr, value, attrName); } private: T & value; FUNC func; + std::string encoding; }; +template <> +inline +bool PROPERTY_PARSER::Parse(const char ** attr, const std::string & attrName, const std::string & toEncoding) +{ +if (!encoding.empty() && !toEncoding.empty()) + { + std::string tmp; + if (!func(attr, tmp, attrName)) + return false; + value = IconvString(tmp, encoding, toEncoding); + return true; + } +else + return func(attr, value, attrName); +} + typedef std::map PROPERTY_PARSERS; bool CheckValue(const char ** attr, const std::string & attrName); @@ -87,16 +105,20 @@ bool GetEncodedValue(const char ** attr, std::string & value, const std::string bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName); template -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); +inline +void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue) +{ + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); +} template inline -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func) +void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const std::string & toEncoding, const typename PROPERTY_PARSER::FUNC & func = GetValue) { - parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func, toEncoding))); } -bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName = "value"); +bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & fromEncoding, const std::string & attrName = "value"); } // namespace STG