/*! @file @author Albert Semenov @date 11/2007 */ /* This file is part of MyGUI. MyGUI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. MyGUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with MyGUI. If not, see . */ #ifndef __MYGUI_STRING_UTILITY_H__ #define __MYGUI_STRING_UTILITY_H__ #include "MyGUI_Prerequest.h" #include #include namespace MyGUI { namespace utility { inline void trim(std::string& _str, bool _left = true, bool _right = true) { if (_right) _str.erase(_str.find_last_not_of(" \t\r") + 1); if (_left) _str.erase(0, _str.find_first_not_of(" \t\r")); } // конвертирование в строку template inline std::string toString (T p) { std::ostringstream stream; stream << p; return stream.str(); } inline const std::string& toString (const std::string& _value) { return _value; } template inline std::string toString (T1 p1, T2 p2) { std::ostringstream stream; stream << p1 << p2; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3) { std::ostringstream stream; stream << p1 << p2 << p3; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4) { std::ostringstream stream; stream << p1 << p2 << p3 << p4; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { std::ostringstream stream; stream << p1 << p2 << p3 << p4 << p5; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6) { std::ostringstream stream; stream << p1 << p2 << p3 << p4 << p5 << p6; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7) { std::ostringstream stream; stream << p1 << p2 << p3 << p4 << p5 << p6 << p7; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8) { std::ostringstream stream; stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8; return stream.str(); } template inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9) { std::ostringstream stream; stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9; return stream.str(); } template<> inline std::string toString (bool _value) { return _value ? "true" : "false"; } // утилиты для парсинга template inline T parseValue( const std::string& _value ) { std::istringstream stream(_value); T result; stream >> result; if (stream.fail()) return T(); else { int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return T(); item = stream.get(); } } return result; } // отдельная имплементация под bool template<> inline bool parseValue(const std::string& _value) { if (_value == "True" || _value == "true" || _value == "1") return true; return false; } // отдельная имплементация под char template<> inline char parseValue(const std::string& _value) { return (char)parseValue(_value); } // отдельная имплементация под unsigned char template<> inline unsigned char parseValue(const std::string& _value) { return (unsigned char)parseValue(_value); } inline short parseShort(const std::string& _value) { return parseValue(_value); } inline unsigned short parseUShort(const std::string& _value) { return parseValue(_value); } inline int parseInt(const std::string& _value) { return parseValue(_value); } inline unsigned int parseUInt(const std::string& _value) { return parseValue(_value); } inline size_t parseSizeT(const std::string& _value) { return parseValue(_value); } inline float parseFloat(const std::string& _value) { return parseValue(_value); } inline double parseDouble(const std::string& _value) { return parseValue(_value); } inline bool parseBool(const std::string& _value) { return parseValue(_value); } inline char parseChar(const std::string& _value) { return parseValue(_value); } inline unsigned char parseUChar(const std::string& _value) { return parseValue(_value); } // для парсинга сложных типов, состоящих из простых template inline T1 parseValueEx2(const std::string& _value) { T2 p1, p2; std::istringstream stream(_value); stream >> p1 >> p2; if (stream.fail()) return T1(); else { int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return T1(); item = stream.get(); } } return T1(p1, p2); } template inline T1 parseValueEx3(const std::string& _value) { T2 p1, p2, p3; std::istringstream stream(_value); stream >> p1 >> p2 >> p3; if (stream.fail()) return T1(); else { int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return T1(); item = stream.get(); } } return T1(p1, p2, p3); } template inline T1 parseValueEx4(const std::string& _value) { T2 p1, p2, p3, p4; std::istringstream stream(_value); stream >> p1 >> p2 >> p3 >> p4; if (stream.fail()) return T1(); else { int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return T1(); item = stream.get(); } } return T1(p1, p2, p3, p4); } namespace templates { template inline void split(std::vector& _ret, const Type& _source, const Type& _delims) { size_t start = _source.find_first_not_of(_delims); while (start != _source.npos) { size_t end = _source.find_first_of(_delims, start); if (end != _source.npos) _ret.push_back(_source.substr(start, end - start)); else { _ret.push_back(_source.substr(start)); break; } start = _source.find_first_not_of(_delims, end + 1); } } } // namespace templates inline std::vector split(const std::string& _source, const std::string& _delims = "\t\n ") { std::vector result; templates::split(result, _source, _delims); return result; } template inline bool parseComplex(const std::string& _value, T1& _p1, T2& _p2, T3& _p3, T4& _p4) { std::istringstream stream(_value); stream >> _p1 >> _p2 >> _p3 >> _p4; if (stream.fail()) return false; int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return false; item = stream.get(); } return true; } template inline bool parseComplex(const std::string& _value, T1& _p1, T2& _p2, T3& _p3) { std::istringstream stream(_value); stream >> _p1 >> _p2 >> _p3; if (stream.fail()) return false; int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return false; item = stream.get(); } return true; } template inline bool parseComplex(const std::string& _value, T1& _p1, T2& _p2) { std::istringstream stream(_value); stream >> _p1 >> _p2; if (stream.fail()) return false; int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return false; item = stream.get(); } return true; } template inline bool parseComplex(const std::string& _value, T1& _p1) { std::istringstream stream(_value); stream >> _p1; if (stream.fail()) return false; int item = stream.get(); while (item != -1) { if (item != ' ' && item != '\t') return false; item = stream.get(); } return true; } template<> inline bool parseComplex(const std::string& _value, bool& _p1) { std::string value(_value); trim(value); if ((value == "True") || (value == "true") || (value == "1")) { _p1 = true; return true; } else if ((value == "False") || (value == "false") || (value == "0")) { _p1 = false; return true; } return false; } inline bool startWith(const std::string& _source, const std::string& _value) { size_t count = _value.size(); if (_source.size() < count) return false; for (size_t index = 0; index < count; ++ index) { if (_source[index] != _value[index]) return false; } return true; } inline bool endWith(const std::string& _source, const std::string& _value) { size_t count = _value.size(); if (_source.size() < count) return false; size_t offset = _source.size() - count; for (size_t index = 0; index < count; ++ index) { if (_source[index + offset] != _value[index]) return false; } return true; } } // namespace utility } // namespace MyGUI #endif // __MYGUI_STRING_UTILITY_H__