| 1 | /* |
| 2 | * Copyright (C) 2005 Dizzy |
| 3 | * |
| 4 | * lstr is a structure to be used for cached string lengths for speed |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (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 | |
| 21 | #include "common/setup_before.h" |
| 22 | #include "common/xstr.h" |
| 23 | |
| 24 | #include <cstring> |
| 25 | #include <cassert> |
| 26 | |
| 27 | #include "common/xalloc.h" |
| 28 | #include "common/setup_after.h" |
| 29 | |
| 30 | /* how many bytes allocate at once on enlarging a xstr */ |
| 31 | #define XSTR_INCREMENT 256 |
| 32 | |
| 33 | namespace pvpgn |
| 34 | { |
| 35 | |
| 36 | /* enlarge "dst" enough so it can hold more "size" characters (not including terminator) */ |
| 37 | static void xstr_enlarge(t_xstr* dst, int size) |
| 38 | { |
| 39 | if (dst->alen < dst->ulen + size + 1) { |
| 40 | int nalen = ((dst->ulen + size + 10) / XSTR_INCREMENT + 1) * XSTR_INCREMENT; |
| 41 | |
| 42 | dst->str = (char*)xrealloc(dst->str, nalen); |
| 43 | dst->alen = nalen; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | extern t_xstr* xstr_alloc(void) |
| 48 | { |
| 49 | t_xstr* xstr = (t_xstr*)xmalloc(sizeof(t_xstr)); |
| 50 | |
| 51 | xstr_init(xstr); |
| 52 | |
| 53 | return xstr; |
| 54 | } |
| 55 | |
| 56 | extern void xstr_free(t_xstr* xstr) |
| 57 | { |
| 58 | assert(xstr); |
| 59 | |
| 60 | if (xstr->str) xfree(xstr->str); |
| 61 | xfree(xstr); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | extern t_xstr * xstr_cpy_str(t_xstr * dst, const char * src) |
| 67 | { |
| 68 | int len; |
| 69 | |
| 70 | assert(dst); |
| 71 | |
| 72 | dst->ulen = 0; |
| 73 | /* so if we cpy a NULL string we delete the old one :) */ |
| 74 | if (!src) return dst; |
| 75 | |
| 76 | len = std::strlen(src); |
| 77 | |
| 78 | /* need to enlarge dst ? */ |
| 79 | xstr_enlarge(dst, len); |
| 80 | |
| 81 | std::memcpy(dst->str, src, len + 1); |
| 82 | dst->ulen = len; |
| 83 | |
| 84 | return dst; |
| 85 | } |
| 86 | |
| 87 | extern t_xstr* xstr_cat_xstr(t_xstr* dst, const t_xstr* src) |
| 88 | { |
| 89 | assert(dst); |
| 90 | |
| 91 | if (!src || !src->ulen) return dst; |
| 92 | |
| 93 | /* need to enlarge dst ? */ |
| 94 | xstr_enlarge(dst, src->ulen); |
| 95 | |
| 96 | std::memcpy(dst->str + dst->ulen, src->str, src->ulen + 1); |
| 97 | dst->ulen += src->ulen; |
| 98 | |
| 99 | return dst; |
| 100 | } |
| 101 | |
| 102 | extern t_xstr* xstr_cat_str(t_xstr* dst, const char* src) |
| 103 | { |
| 104 | int len; |
| 105 | |
| 106 | assert(dst); |
| 107 | |
| 108 | if (!src) return dst; |
| 109 | |
| 110 | len = std::strlen(src); |
| 111 | |
| 112 | /* need to enlarge dst ? */ |
| 113 | xstr_enlarge(dst, len); |
| 114 | |
| 115 | std::memcpy(dst->str + dst->ulen, src, len + 1); |
| 116 | dst->ulen += len; |
| 117 | |
| 118 | return dst; |
| 119 | } |
| 120 | |
| 121 | extern t_xstr* xstr_ncat_str(t_xstr * dst, const char * src, int len) |
| 122 | { |
| 123 | const char *p; |
| 124 | |
| 125 | assert(dst); |
| 126 | |
| 127 | if (!src || len < 1 || !src[0]) return dst; |
| 128 | |
| 129 | for (p = src; *p && p - src < len; ++p) |
| 130 | xstr_cat_char(dst, *p); |
| 131 | |
| 132 | return dst; |
| 133 | } |
| 134 | |
| 135 | extern t_xstr* xstr_cat_char(t_xstr * dst, const char ch) |
| 136 | { |
| 137 | assert(dst); |
| 138 | |
| 139 | xstr_enlarge(dst, 1); |
| 140 | dst->str[dst->ulen++] = ch; |
| 141 | dst->str[dst->ulen] = '\0'; |
| 142 | |
| 143 | return dst; |
| 144 | } |
| 145 | |
| 146 | } |