string.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* $Id$ */
  2. /*
  3. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef __PJPP_STRING_HPP__
  21. #define __PJPP_STRING_HPP__
  22. #include "pjsua/pj/string.h"
  23. #include "pjsua/pj++/pool.hpp"
  24. #include "pjsua/pj/assert.h"
  25. //
  26. // String wrapper class for pj_str_t.
  27. //
  28. class Pj_String : public pj_str_t
  29. {
  30. public:
  31. //
  32. // Default constructor.
  33. //
  34. Pj_String()
  35. {
  36. pj_assert(sizeof(Pj_String) == sizeof(pj_str_t));
  37. ptr=NULL;
  38. slen=0;
  39. }
  40. //
  41. // Construct the buffer from a char* (use with care)
  42. //
  43. Pj_String(char *str)
  44. {
  45. set(str);
  46. }
  47. //
  48. // Construct from a const char*.
  49. //
  50. Pj_String(Pj_Pool &pool, const char *src)
  51. {
  52. set(pool, src);
  53. }
  54. //
  55. // Construct from pj_str_t&.
  56. //
  57. explicit Pj_String(pj_str_t &s)
  58. {
  59. ptr = s.ptr;
  60. slen = s.slen;
  61. }
  62. //
  63. // Construct from const pj_str_t& (use with care!).
  64. //
  65. explicit Pj_String(const pj_str_t &s)
  66. {
  67. ptr = (char*)s.ptr;
  68. slen = s.slen;
  69. }
  70. //
  71. // Construct by copying from const pj_str_t*.
  72. //
  73. Pj_String(Pj_Pool &pool, const pj_str_t *s)
  74. {
  75. set(pool, s);
  76. }
  77. //
  78. // Construct by copying from Pj_String
  79. //
  80. Pj_String(Pj_Pool &pool, const Pj_String &rhs)
  81. {
  82. set(pool, rhs);
  83. }
  84. //
  85. // Construct from another Pj_String, use with care!
  86. //
  87. explicit Pj_String(const Pj_String &rhs)
  88. {
  89. ptr = rhs.ptr;
  90. slen = rhs.slen;
  91. }
  92. //
  93. // Construct from a char* and a length.
  94. //
  95. Pj_String(char *str, pj_size_t len)
  96. {
  97. set(str, len);
  98. }
  99. //
  100. // Construct from pair of pointer.
  101. //
  102. Pj_String(char *begin, char *end)
  103. {
  104. pj_strset3(this, begin, end);
  105. }
  106. //
  107. // You can cast Pj_String to pj_str_t*
  108. //
  109. operator pj_str_t*()
  110. {
  111. return this;
  112. }
  113. //
  114. // You can cast const Pj_String to const pj_str_t*
  115. //
  116. operator const pj_str_t*() const
  117. {
  118. return this;
  119. }
  120. //
  121. // Get the length of the string.
  122. //
  123. pj_size_t length() const
  124. {
  125. return pj_strlen(this);
  126. }
  127. //
  128. // Get the length of the string.
  129. //
  130. pj_size_t size() const
  131. {
  132. return length();
  133. }
  134. //
  135. // Get the string buffer.
  136. //
  137. const char *buf() const
  138. {
  139. return ptr;
  140. }
  141. //
  142. // Initialize buffer from char*.
  143. //
  144. void set(char *str)
  145. {
  146. pj_strset2(this, str);
  147. }
  148. //
  149. // Initialize by copying from a const char*.
  150. //
  151. void set(Pj_Pool &pool, const char *s)
  152. {
  153. pj_strdup2(pool, this, s);
  154. }
  155. //
  156. // Initialize from pj_str_t*.
  157. //
  158. void set(pj_str_t *s)
  159. {
  160. pj_strassign(this, s);
  161. }
  162. //
  163. // Initialize by copying from const pj_str_t*.
  164. //
  165. void set(Pj_Pool &pool, const pj_str_t *s)
  166. {
  167. pj_strdup(pool, this, s);
  168. }
  169. //
  170. // Initialize from char* and length.
  171. //
  172. void set(char *str, pj_size_t len)
  173. {
  174. pj_strset(this, str, len);
  175. }
  176. //
  177. // Initialize from pair of pointers.
  178. //
  179. void set(char *begin, char *end)
  180. {
  181. pj_strset3(this, begin, end);
  182. }
  183. //
  184. // Initialize from other Pj_String.
  185. //
  186. void set(Pj_String &rhs)
  187. {
  188. pj_strassign(this, &rhs);
  189. }
  190. //
  191. // Initialize by copying from a Pj_String*.
  192. //
  193. void set(Pj_Pool &pool, const Pj_String *s)
  194. {
  195. pj_strdup(pool, this, s);
  196. }
  197. //
  198. // Initialize by copying from other Pj_String.
  199. //
  200. void set(Pj_Pool &pool, const Pj_String &s)
  201. {
  202. pj_strdup(pool, this, &s);
  203. }
  204. //
  205. // Copy the contents of other string.
  206. //
  207. void strcpy(const pj_str_t *s)
  208. {
  209. pj_strcpy(this, s);
  210. }
  211. //
  212. // Copy the contents of other string.
  213. //
  214. void strcpy(const Pj_String &rhs)
  215. {
  216. pj_strcpy(this, &rhs);
  217. }
  218. //
  219. // Copy the contents of other string.
  220. //
  221. void strcpy(const char *s)
  222. {
  223. pj_strcpy2(this, s);
  224. }
  225. //
  226. // Compare string.
  227. //
  228. int strcmp(const char *s) const
  229. {
  230. return pj_strcmp2(this, s);
  231. }
  232. //
  233. // Compare string.
  234. //
  235. int strcmp(const pj_str_t *s) const
  236. {
  237. return pj_strcmp(this, s);
  238. }
  239. //
  240. // Compare string.
  241. //
  242. int strcmp(const Pj_String &rhs) const
  243. {
  244. return pj_strcmp(this, &rhs);
  245. }
  246. //
  247. // Compare string.
  248. //
  249. int strncmp(const char *s, pj_size_t len) const
  250. {
  251. return pj_strncmp2(this, s, len);
  252. }
  253. //
  254. // Compare string.
  255. //
  256. int strncmp(const pj_str_t *s, pj_size_t len) const
  257. {
  258. return pj_strncmp(this, s, len);
  259. }
  260. //
  261. // Compare string.
  262. //
  263. int strncmp(const Pj_String &rhs, pj_size_t len) const
  264. {
  265. return pj_strncmp(this, &rhs, len);
  266. }
  267. //
  268. // Compare string.
  269. //
  270. int stricmp(const char *s) const
  271. {
  272. return pj_stricmp2(this, s);
  273. }
  274. //
  275. // Compare string.
  276. //
  277. int stricmp(const pj_str_t *s) const
  278. {
  279. return pj_stricmp(this, s);
  280. }
  281. //
  282. // Compare string.
  283. //
  284. int stricmp(const Pj_String &rhs) const
  285. {
  286. return stricmp(&rhs);
  287. }
  288. //
  289. // Compare string.
  290. //
  291. int strnicmp(const char *s, pj_size_t len) const
  292. {
  293. return pj_strnicmp2(this, s, len);
  294. }
  295. //
  296. // Compare string.
  297. //
  298. int strnicmp(const pj_str_t *s, pj_size_t len) const
  299. {
  300. return pj_strnicmp(this, s, len);
  301. }
  302. //
  303. // Compare string.
  304. //
  305. int strnicmp(const Pj_String &rhs, pj_size_t len) const
  306. {
  307. return strnicmp(&rhs, len);
  308. }
  309. //
  310. // Compare contents for equality.
  311. //
  312. bool operator==(const char *s) const
  313. {
  314. return strcmp(s) == 0;
  315. }
  316. //
  317. // Compare contents for equality.
  318. //
  319. bool operator==(const pj_str_t *s) const
  320. {
  321. return strcmp(s) == 0;
  322. }
  323. //
  324. // Compare contents for equality.
  325. //
  326. bool operator==(const Pj_String &rhs) const
  327. {
  328. return pj_strcmp(this, &rhs) == 0;
  329. }
  330. //
  331. // Assign from char*
  332. //
  333. Pj_String& operator=(char *s)
  334. {
  335. set(s);
  336. return *this;
  337. }
  338. ///
  339. // Assign from another Pj_String, use with care!
  340. //
  341. Pj_String& operator=(const Pj_String &rhs)
  342. {
  343. ptr = rhs.ptr;
  344. slen = rhs.slen;
  345. return *this;
  346. }
  347. //
  348. // Find a character in the string.
  349. //
  350. char *strchr(int chr)
  351. {
  352. return pj_strchr(this, chr);
  353. }
  354. //
  355. // Find a character in the string.
  356. //
  357. char *find(int chr)
  358. {
  359. return strchr(chr);
  360. }
  361. //
  362. // Concatenate string.
  363. //
  364. void strcat(const Pj_String &rhs)
  365. {
  366. pj_strcat(this, &rhs);
  367. }
  368. //
  369. // Left trim.
  370. //
  371. void ltrim()
  372. {
  373. pj_strltrim(this);
  374. }
  375. //
  376. // Right trim.
  377. //
  378. void rtrim()
  379. {
  380. pj_strrtrim(this);
  381. }
  382. //
  383. // Left and right trim.
  384. //
  385. void trim()
  386. {
  387. pj_strtrim(this);
  388. }
  389. //
  390. // Convert to unsigned long.
  391. //
  392. unsigned long to_ulong() const
  393. {
  394. return pj_strtoul(this);
  395. }
  396. //
  397. // Convert from unsigned long.
  398. //
  399. void from_ulong(unsigned long value)
  400. {
  401. slen = pj_utoa(value, ptr);
  402. }
  403. //
  404. // Convert from unsigned long with padding.
  405. //
  406. void from_ulong_with_pad(unsigned long value, int min_dig=0, int pad=' ')
  407. {
  408. slen = pj_utoa_pad(value, ptr, min_dig, pad);
  409. }
  410. };
  411. #endif /* __PJPP_STRING_HPP__ */