string_conversion.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "string_conversion.h"
  17. #include <ctime>
  18. using namespace std;
  19. int a2ip(const string& s)
  20. {
  21. int r = 0;
  22. int l = 0;
  23. for (int i = 0; i < 3; i++)
  24. {
  25. int c = s.find('.', l);
  26. r = r << 8 | atoi(s.substr(l, c - l).c_str());
  27. l = c + 1;
  28. }
  29. r = r << 8 | atoi(s.substr(l).c_str());
  30. return r;
  31. }
  32. string ip2a(int v)
  33. {
  34. return n(v >> 24 & 0xff) + '.' + n(v >> 16 & 0xff) + '.' + n(v >> 8 & 0xff) + '.' + n(v & 0xff);
  35. }
  36. int get_net_mask(int v)
  37. {
  38. if (v & 0x80000000)
  39. return 0xffffff00;
  40. if (v & 0x40000000)
  41. return 0xffff0000;
  42. return 0xff000000;
  43. }
  44. int get_net_part(int v)
  45. {
  46. return v & get_net_mask(v);
  47. }
  48. bool atob(string s)
  49. {
  50. return s == "true" || s == "yes";
  51. }
  52. string btoa(bool v)
  53. {
  54. return v ? "yes" : "no";
  55. }
  56. string n(unsigned int v)
  57. {
  58. return to_string(v);
  59. }
  60. string n(int v)
  61. {
  62. return to_string(v);
  63. }
  64. string n(long long v)
  65. {
  66. return to_string(v);
  67. }
  68. string n(unsigned long long v)
  69. {
  70. return to_string(v);
  71. }
  72. string swsl(int l, string s)
  73. {
  74. while (s.size() < l)
  75. s = ' ' + s;
  76. return s;
  77. }
  78. string swsr(int l, string s)
  79. {
  80. while (s.size() < l)
  81. s += ' ';
  82. return s;
  83. }
  84. string nwzl(int l, unsigned int v)
  85. {
  86. string s = n(v);
  87. while (s.size() < l)
  88. s = '0' + s;
  89. return s;
  90. }
  91. string nwsl(int l, unsigned int v)
  92. {
  93. return swsl(l, n(v));
  94. }
  95. string nh(int l, __int64 v)
  96. {
  97. string s;
  98. int w;
  99. while (l)
  100. {
  101. w = v & 0xf;
  102. if (w > 9)
  103. w += 7;
  104. s = char(w + 48) + s;
  105. v >>= 4;
  106. l--;
  107. }
  108. return s;
  109. }
  110. string nwp(int l, unsigned int v)
  111. {
  112. string r;
  113. string s = n(v);
  114. while (1)
  115. {
  116. int l = s.size();
  117. if (l > 3)
  118. {
  119. r = '.' + s.substr(l - 3, 3) + r;
  120. s.erase(l - 3, 3);
  121. }
  122. else
  123. {
  124. r = s + r;
  125. break;
  126. }
  127. }
  128. return swsl(l, r);
  129. }
  130. void split_key(const string& key, string& name, string& value)
  131. {
  132. size_t i = key.find('=');
  133. if (i == string::npos)
  134. {
  135. name = key;
  136. value.erase();
  137. }
  138. else
  139. {
  140. name = key.substr(0, i);
  141. value = key.substr(i + 1);
  142. }
  143. }
  144. string tabs2spaces(const string& v)
  145. {
  146. string r;
  147. for (size_t i = 0; i < v.size(); i++)
  148. {
  149. char c = v[i];
  150. if (c == '\t')
  151. {
  152. do
  153. r += ' ';
  154. while (r.size() & 3);
  155. }
  156. else
  157. r += c;
  158. }
  159. return r;
  160. }
  161. string time2a(time_t v)
  162. {
  163. char b[20];
  164. const tm* date = gmtime(&v);
  165. if (date)
  166. sprintf(b, "%04d-%02d-%02d %02d:%02d:%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec);
  167. else
  168. *b = 0;
  169. return b;
  170. }
  171. string js_encode(const string& v)
  172. {
  173. string r;
  174. for (size_t i = 0; i < v.size(); i++)
  175. {
  176. switch (v[i])
  177. {
  178. case '<':
  179. r += "&lt;";
  180. break;
  181. case '\"':
  182. case '\'':
  183. case '\\':
  184. r += '\\';
  185. default:
  186. r += v[i];
  187. }
  188. }
  189. return r;
  190. }
  191. string trim_field(const string& v)
  192. {
  193. string r;
  194. bool copy_white = false;
  195. for (size_t i = 0; i < v.size(); i++)
  196. {
  197. if (isspace(v[i]))
  198. copy_white = true;
  199. else
  200. {
  201. if (copy_white)
  202. {
  203. if (!r.empty())
  204. r += ' ';
  205. copy_white = false;
  206. }
  207. r += v[i];
  208. }
  209. }
  210. return r;
  211. }
  212. string trim_text(const string& v)
  213. {
  214. string r;
  215. bool copy_white = false;
  216. for (size_t i = 0; i < v.size(); )
  217. {
  218. int p = v.find('\n', i);
  219. if (p == string::npos)
  220. p = v.size();
  221. string line = trim_field(v.substr(i, p - i));
  222. if (line.empty())
  223. copy_white = true;
  224. else
  225. {
  226. if (copy_white)
  227. {
  228. if (!r.empty())
  229. r += '\n';
  230. copy_white = false;
  231. }
  232. r += line + '\n';
  233. }
  234. i = p + 1;
  235. }
  236. return r;
  237. }