Also support dumping JMD and SDMP over TCP/IP
[lsnes.git] / src / library / string.cpp
blobb0df9bcc7f7524b9f8b86d5b210c345b7c1fef1e
1 #include "library/string.hpp"
2 #include "library/minmax.hpp"
3 #include <cctype>
4 #include <boost/regex.hpp>
7 std::string strip_CR(const std::string& str)
9 std::string x = str;
10 istrip_CR(x);
11 return x;
14 void istrip_CR(std::string& str)
16 size_t crc = 0;
17 size_t xl = str.length();
18 while(crc < xl) {
19 char y = str[xl - crc - 1];
20 if(y != '\r' && y != '\n')
21 break;
22 crc++;
24 str = str.substr(0, xl - crc);
27 int firstchar(const std::string& str)
29 if(str.length())
30 return static_cast<unsigned char>(str[0]);
31 else
32 return -1;
35 int extract_token(std::string& str, std::string& tok, const char* sep, bool sequence) throw(std::bad_alloc)
37 if(!*sep) {
38 tok = str;
39 str = "";
40 return (tok == "") ? -2 : -1;
42 size_t s = str.find_first_of(sep);
43 if(s < str.length()) {
44 int ech = static_cast<unsigned char>(str[s]);
45 size_t t = sequence ? min(str.find_first_not_of(sep, s), str.length()) : (s + 1);
46 tok = str.substr(0, s);
47 str = str.substr(t);
48 return ech;
49 } else {
50 tok = str;
51 str = "";
52 return -1;
55 int ech = (s < str.length()) ? static_cast<unsigned char>(str[s]) : -1;
56 tok = str.substr(0, s);
57 str = str.substr(s + 1);
58 return ech;
61 int string_to_bool(const std::string& x)
63 std::string y = x;
64 for(size_t i = 0; i < y.length(); i++)
65 y[i] = tolower(y[i]);
66 if(y == "on" || y == "true" || y == "yes" || y == "1" || y == "enable" || y == "enabled")
67 return 1;
68 if(y == "off" || y == "false" || y == "no" || y == "0" || y == "disable" || y == "disabled")
69 return 0;
70 return -1;
73 regex_results::regex_results()
75 matched = false;
78 regex_results::regex_results(std::vector<std::string> res)
80 matched = true;
81 results = res;
84 regex_results::operator bool() const
86 return matched;
89 bool regex_results::operator!() const
91 return !matched;
94 size_t regex_results::size() const
96 return results.size();
98 const std::string& regex_results::operator[](size_t i) const
100 return results[i];
103 regex_results regex(const std::string& regexp, const std::string& str, const char* ex) throw(std::bad_alloc,
104 std::runtime_error)
106 static std::map<std::string, boost::regex*> regexps;
107 if(!regexps.count(regexp)) {
108 boost::regex* y = NULL;
109 try {
110 y = new boost::regex(regexp, boost::regex::extended & ~boost::regex::collate);
111 regexps[regexp] = y;
112 } catch(std::bad_alloc& e) {
113 delete y;
114 throw;
115 } catch(std::exception& e) {
116 throw std::runtime_error(e.what());
120 boost::smatch matches;
121 bool x = boost::regex_match(str.begin(), str.end(), matches, *(regexps[regexp]));
122 if(x) {
123 std::vector<std::string> res;
124 for(size_t i = 0; i < matches.size(); i++)
125 res.push_back(matches.str(i));
126 return regex_results(res);
127 } else if(ex)
128 throw std::runtime_error(ex);
129 else
130 return regex_results();
133 bool regex_match(const std::string& regexp, const std::string& str) throw(std::bad_alloc, std::runtime_error)
135 return regex(regexp, str);