Debian Codegraph @

contrib/epee/src/string_tools.cpp

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27 #include "string_tools.h"
28 #include "string_tools_lexical.h"
29
30
31 // Previously pulled in by ASIO, further cleanup still required ...
32 #ifdef _WIN32
33 # include <winsock2.h>
34 # include <windows.h>
35 #endif
36
37 #include <locale>
38 #include <cstdlib>
39 #include <string>
40 #include <type_traits>
41 #include <system_error>
42 #include <boost/lexical_cast.hpp>
43 #include <boost/algorithm/string.hpp>
44 #include <boost/algorithm/string/predicate.hpp>
45 #include <boost/utility/string_ref.hpp>
46 #include <boost/filesystem.hpp>
47 #include "misc_log_ex.h"
48 #include "storages/parserse_base_utils.h"
49 #include "hex.h"
50 #include "memwipe.h"
51 #include "mlocker.h"
52 #include "span.h"
53 #include "warnings.h"
54
55 #include <ctype.h>
56
57 #ifdef _WIN32
58 # include <winsock2.h>
59 #else
60 # include <arpa/inet.h>
61 # include <netinet/in.h>
62 #endif
63
64 namespace epee
65 {
66 namespace string_tools
67 {
68 std::string get_ip_string_from_int32(uint32_t ip)
69 {
70 in_addr adr;
71 adr.s_addr = ip;
72 const char* pbuf = inet_ntoa(adr);
73 if(pbuf)
74 return pbuf;
75 else
76 return "[failed]";
77 }
78 //----------------------------------------------------------------------------
79 bool get_ip_int32_from_string(uint32_t& ip, const std::string& ip_str)
80 {
81 ip = inet_addr(ip_str.c_str());
82 if(INADDR_NONE == ip)
83 return false;
84
85 return true;
86 }
87 //----------------------------------------------------------------------------
88 bool validate_hex(uint64_t length, const std::string& str)
89 {
90 if (str.size() != length)
91 return false;
92 for (char c: str)
93 if (!isxdigit(c))
94 return false;
95 return true;
96 }
97 //----------------------------------------------------------------------------
98 bool parse_peer_from_string(uint32_t& ip, uint16_t& port, const std::string& addres)
99 {
100 //parse ip and address
101 std::string::size_type p = addres.find(':');
102 std::string ip_str, port_str;
103 if(p == std::string::npos)
104 {
105 port = 0;
106 ip_str = addres;
107 }
108 else
109 {
110 ip_str = addres.substr(0, p);
111 port_str = addres.substr(p+1, addres.size());
112 }
113
114 if(!get_ip_int32_from_string(ip, ip_str))
115 {
116 return false;
117 }
118
119 if(p != std::string::npos && !get_xtype_from_string(port, port_str))
120 {
121 return false;
122 }
123 return true;
124 }
125
126 std::string num_to_string_fast(int64_t val)
127 {
128 /*
129 char buff[30] = {0};
130 i64toa_s(val, buff, sizeof(buff)-1, 10);
131 return buff;*/
132 return boost::lexical_cast<std::string>(val);
133 }
134
135
136 bool compare_no_case(const std::string& str1, const std::string& str2)
137 {
138
139 return !boost::iequals(str1, str2);
140 }
141 //----------------------------------------------------------------------------
142 std::string& get_current_module_name()
143 {
144 static std::string module_name;
145 return module_name;
146 }
147 //----------------------------------------------------------------------------
148 std::string& get_current_module_folder()
149 {
150 static std::string module_folder;
151 return module_folder;
152 }
153
154 #ifdef _WIN32
155 std::string get_current_module_path()
156 {
157 char pname [5000] = {0};
158 GetModuleFileNameA( NULL, pname, sizeof(pname));
159 pname[sizeof(pname)-1] = 0; //be happy ;)
160 return pname;
161 }
162 #endif
163
164 void set_module_name_and_folder(const std::string& path_to_process_)
165 {
166 boost::filesystem::path path_to_process;
167
168 #ifdef _WIN32
169 // Convert to wide string to avoid codecvt errors with Unicode paths
170 std::wstring wpath = epee::string_tools::utf8_to_utf16(get_current_module_path());
171 path_to_process = boost::filesystem::path(wpath);
172 #else
173 path_to_process = boost::filesystem::path(path_to_process_);
174 #endif
175
176 get_current_module_name() = path_to_process.filename().string();
177 get_current_module_folder() = path_to_process.parent_path().string();
178 }
179
180 //----------------------------------------------------------------------------
181 std::string pad_string(std::string s, size_t n, char c, bool prepend)
182 {
183 if (s.size() < n)
184 {
185 if (prepend)
186 s = std::string(n - s.size(), c) + s;
187 else
188 s.append(n - s.size(), c);
189 }
190 return s;
191 }
192
193 std::string get_extension(const std::string& str)
194 {
195 std::string ext_with_dot = boost::filesystem::path(str).extension().string();
196
197 if (ext_with_dot.empty())
198 return {};
199
200 return ext_with_dot.erase(0, 1);
201 }
202
203 //----------------------------------------------------------------------------
204 std::string cut_off_extension(const std::string& str)
205 {
206 return boost::filesystem::path(str).replace_extension("").string();
207 }
208
209 #ifdef _WIN32
210 std::wstring utf8_to_utf16(const std::string& str)
211 {
212 if (str.empty())
213 return {};
214 int wstr_size = MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), NULL, 0);
215 if (wstr_size == 0)
216 {
217 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
218 }
219 std::wstring wstr(wstr_size, wchar_t{});
220 if (!MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), &wstr[0], wstr_size))
221 {
222 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
223 }
224 return wstr;
225 }
226 std::string utf16_to_utf8(const std::wstring& wstr)
227 {
228 if (wstr.empty())
229 return {};
230 int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
231 if (str_size == 0)
232 {
233 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
234 }
235 std::string str(str_size, char{});
236 if (!WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &str[0], str_size, NULL, NULL))
237 {
238 throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
239 }
240 return str;
241 }
242 #endif
243 }
244 }
245