| 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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 22 | #include "common/xstr.h"cxx . . $ `<file>/src/common/xstr.h`/ |
| 23 | |
| 24 | #include <cstring> |
| 25 | #include <cassert> |
| 26 | |
| 27 | #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/ |
| 28 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 29 | |
| 30 | /* how many bytes allocate at once on enlarging a xstr */ |
| 31 | #define XSTR_INCREMENTcxx . . $ `src/common/xstr.cpp:31:9`! 256 |
| 32 | |
| 33 | namespace pvpgncxx . . $ pvpgn/ |
| 34 | { |
| 35 | |
| 36 | /* enlarge "dst" enough so it can hold more "size" characters (not including terminator) */ |
| 37 | static void xstr_enlargecxx . . $ pvpgn/xstr_enlarge(4601250c80a97548).(t_xstrcxx . . $ pvpgn/t_xstr#* dst, int size) |
| 38 | { |
| 39 | if (dst->alencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#alen. < dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. + size + 1) { |
| 40 | int nalen = ((dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. + size + 10) / XSTR_INCREMENTcxx . . $ `src/common/xstr.cpp:31:9`! + 1) * XSTR_INCREMENTcxx . . $ `src/common/xstr.cpp:31:9`!; |
| 41 | |
| 42 | dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str. = (char*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(dst->str, nalen); |
| 43 | dst->alencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#alen. = nalen; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | extern t_xstrcxx . . $ pvpgn/t_xstr#* xstr_alloccxx . . $ pvpgn/xstr_alloc(3125114ad3ba0a87).(void) |
| 48 | { |
| 49 | t_xstrcxx . . $ pvpgn/t_xstr#* xstr = (t_xstrcxx . . $ pvpgn/t_xstr#*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(sizeof(t_xstr)); |
| 50 | |
| 51 | xstr_initcxx . . $ pvpgn/xstr_init(dbb2bf3681fb2b00).(xstr); |
| 52 | |
| 53 | return xstr; |
| 54 | } |
| 55 | |
| 56 | extern void xstr_freecxx . . $ pvpgn/xstr_free(dbb2bf3681fb2b00).(t_xstrcxx . . $ pvpgn/t_xstr#* xstr) |
| 57 | { |
| 58 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(xstr); |
| 59 | |
| 60 | if (xstr->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str.) xfreecxx . . $ `src/common/xalloc.h:51:9`!(xstr->str); |
| 61 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(xstr); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | extern t_xstrcxx . . $ pvpgn/t_xstr# * xstr_cpy_strcxx . . $ pvpgn/xstr_cpy_str(5f93d84c9cbedb2c).(t_xstrcxx . . $ pvpgn/t_xstr# * dst, const char * src) |
| 67 | { |
| 68 | int len; |
| 69 | |
| 70 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dst); |
| 71 | |
| 72 | dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#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_enlargecxx . . $ pvpgn/xstr_enlarge(4601250c80a97548).(dst, len); |
| 80 | |
| 81 | std::memcpy(dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str., src, len + 1); |
| 82 | dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. = len; |
| 83 | |
| 84 | return dst; |
| 85 | } |
| 86 | |
| 87 | extern t_xstrcxx . . $ pvpgn/t_xstr#* xstr_cat_xstrcxx . . $ pvpgn/xstr_cat_xstr(489e26548c9f888e).(t_xstrcxx . . $ pvpgn/t_xstr#* dst, const t_xstrcxx . . $ pvpgn/t_xstr#* src) |
| 88 | { |
| 89 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dst); |
| 90 | |
| 91 | if (!src || !src->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen.) return dst; |
| 92 | |
| 93 | /* need to enlarge dst ? */ |
| 94 | xstr_enlargecxx . . $ pvpgn/xstr_enlarge(4601250c80a97548).(dst, src->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen.); |
| 95 | |
| 96 | std::memcpy(dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str. + dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen., src->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str., src->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. + 1); |
| 97 | dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. += src->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen.; |
| 98 | |
| 99 | return dst; |
| 100 | } |
| 101 | |
| 102 | extern t_xstrcxx . . $ pvpgn/t_xstr#* xstr_cat_strcxx . . $ pvpgn/xstr_cat_str(5f93d84c9cbedb2c).(t_xstrcxx . . $ pvpgn/t_xstr#* dst, const char* src) |
| 103 | { |
| 104 | int len; |
| 105 | |
| 106 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dst); |
| 107 | |
| 108 | if (!src) return dst; |
| 109 | |
| 110 | len = std::strlen(src); |
| 111 | |
| 112 | /* need to enlarge dst ? */ |
| 113 | xstr_enlargecxx . . $ pvpgn/xstr_enlarge(4601250c80a97548).(dst, len); |
| 114 | |
| 115 | std::memcpy(dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str. + dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen., src, len + 1); |
| 116 | dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen. += len; |
| 117 | |
| 118 | return dst; |
| 119 | } |
| 120 | |
| 121 | extern t_xstrcxx . . $ pvpgn/t_xstr#* xstr_ncat_strcxx . . $ pvpgn/xstr_ncat_str(8f3e6e1e70863ca).(t_xstrcxx . . $ pvpgn/t_xstr# * dst, const char * src, int len) |
| 122 | { |
| 123 | const char *p; |
| 124 | |
| 125 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dst); |
| 126 | |
| 127 | if (!src || len < 1 || !src[0]) return dst; |
| 128 | |
| 129 | for (p = src; *p && p - src < len; ++p) |
| 130 | xstr_cat_charcxx . . $ pvpgn/xstr_cat_char(b449f1e097feab7).(dst, *p); |
| 131 | |
| 132 | return dst; |
| 133 | } |
| 134 | |
| 135 | extern t_xstrcxx . . $ pvpgn/t_xstr#* xstr_cat_charcxx . . $ pvpgn/xstr_cat_char(b449f1e097feab7).(t_xstrcxx . . $ pvpgn/t_xstr# * dst, const char ch) |
| 136 | { |
| 137 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dst); |
| 138 | |
| 139 | xstr_enlargecxx . . $ pvpgn/xstr_enlarge(4601250c80a97548).(dst, 1); |
| 140 | dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str.[dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen.++] = ch; |
| 141 | dst->strcxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#str.[dst->ulencxx . . $ pvpgn/$anonymous_type_cb3022ae3e20e626_0#ulen.] = '\0'; |
| 142 | |
| 143 | return dst; |
| 144 | } |
| 145 | |
| 146 | } |