1 /* 2 BStone: A Source port of 3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike 4 5 Copyright (c) 1992-2013 Apogee Entertainment, LLC 6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com) 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the 20 Free Software Foundation, Inc., 21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 */ 23 24 25 #ifndef BSTONE_STRING_HELPER_INCLUDED 26 #define BSTONE_STRING_HELPER_INCLUDED 27 28 29 #include <locale> 30 #include <stdexcept> 31 #include <string> 32 #include <sstream> 33 #include <vector> 34 35 36 namespace bstone { 37 38 39 using StringList = std::vector<std::string>; 40 41 42 class StringHelper { 43 public: 44 StringHelper() = delete; 45 46 StringHelper( 47 const StringHelper& that) = delete; 48 49 StringHelper& operator=( 50 const StringHelper& that) = delete; 51 52 ~StringHelper() = delete; 53 54 55 static char to_lower( 56 char value); 57 58 static std::string to_lower( 59 const std::string& value); 60 61 template<typename T, typename U> lexical_cast(const U & src_value)62 static T lexical_cast( 63 const U& src_value) 64 { 65 std::stringstream oss; 66 oss << src_value; 67 68 T result; 69 oss >> result; 70 71 if (oss) { 72 return result; 73 } 74 75 throw std::runtime_error("lexical_cast"); 76 } 77 78 template<typename T, typename U> lexical_cast(const T & src_value,U & dst_value)79 static bool lexical_cast( 80 const T& src_value, 81 U& dst_value) 82 { 83 std::stringstream oss; 84 oss.unsetf(std::ios_base::skipws); 85 oss << src_value; 86 oss >> dst_value; 87 return !oss.fail(); 88 } 89 90 static bool is_iequal( 91 const std::string& a, 92 const std::string& b); 93 94 static bool is( 95 std::ctype_base::mask mask, 96 char value); 97 98 static const std::string& get_empty(); 99 }; // StringHelper 100 101 102 } // bstone 103 104 105 #endif // BSTONE_STRING_HELPER_INCLUDED 106