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