| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef VMSTRREF_H |
| 16 |
#define VMSTRREF_H |
| 17 |
|
| 18 |
#include "osifcnet.h" |
| 19 |
|
| 20 |
|
| 21 |
class StringRef: public CVmRefCntObj |
| 22 |
{ |
| 23 |
public: |
| 24 |
|
| 25 |
StringRef() { init(0, 0, 256); } |
| 26 |
StringRef(long init_alo) { init(0, 0, init_alo); } |
| 27 |
StringRef(const char *s) |
| 28 |
{ |
| 29 |
size_t l = strlen(s); |
| 30 |
init(s, l, l); |
| 31 |
} |
| 32 |
StringRef(const char *s, long l) |
| 33 |
{ init(s, l, l); } |
| 34 |
StringRef(const char *s, long l, long init_alo) |
| 35 |
{ init(s, l, init_alo); } |
| 36 |
|
| 37 |
|
| 38 |
char *get() const { return str; } |
| 39 |
char *getend() const { return str + len; } |
| 40 |
long getlen() const { return len; } |
| 41 |
|
| 42 |
void setlen(long len) { this->len = len; } |
| 43 |
void addlen(long len) { this->len += len; } |
| 44 |
|
| 45 |
|
| 46 |
static StringRef *format(const char *fmt, ...) |
| 47 |
{ |
| 48 |
|
| 49 |
StringRef *s = new StringRef(); |
| 50 |
|
| 51 |
|
| 52 |
va_list argp; |
| 53 |
va_start(argp, fmt); |
| 54 |
s->str = t3sprintf_alloc(fmt, argp); |
| 55 |
va_end(argp); |
| 56 |
|
| 57 |
|
| 58 |
s->alo = s->len = strlen(s->str); |
| 59 |
|
| 60 |
|
| 61 |
return s; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
void append(const char *s) { append(s, strlen(s)); } |
| 66 |
void append(const char *s, long l) |
| 67 |
{ |
| 68 |
|
| 69 |
if (len + l > alo) |
| 70 |
{ |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
alo = len + l + (len+l > 512 ? (len+l)/2 : 256); |
| 77 |
|
| 78 |
|
| 79 |
str = (char *)t3realloc(str, alo + 1); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
memcpy(str + len, s, l); |
| 84 |
|
| 85 |
|
| 86 |
str[len + l] = '\0'; |
| 87 |
|
| 88 |
|
| 89 |
len += l; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
void appendf(const char *fmt, ...) |
| 94 |
{ |
| 95 |
|
| 96 |
va_list argp; |
| 97 |
va_start(argp, fmt); |
| 98 |
char *s = t3sprintf_alloc(fmt, argp); |
| 99 |
va_end(argp); |
| 100 |
|
| 101 |
|
| 102 |
append(s); |
| 103 |
|
| 104 |
|
| 105 |
t3free(s); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void append_utf8(wchar_t ch) |
| 110 |
{ |
| 111 |
char buf[3]; |
| 112 |
|
| 113 |
if (ch <= 0x7f) |
| 114 |
{ |
| 115 |
|
| 116 |
buf[0] = (char)ch; |
| 117 |
append(buf, 1); |
| 118 |
} |
| 119 |
else if (ch <= 0x7ff) |
| 120 |
{ |
| 121 |
|
| 122 |
buf[0] = (char)(0xC0 | ((ch >> 6) & 0x1F)); |
| 123 |
buf[1] = (char)(0x80 | (ch & 0x3F)); |
| 124 |
append(buf, 2); |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
|
| 129 |
buf[0] = (char)(0xE0 | ((ch >> 12) & 0x0F)); |
| 130 |
buf[1] = (char)(0x80 | ((ch >> 6) & 0x3F)); |
| 131 |
buf[2] = (char)(0x80 | (ch & 0x3F)); |
| 132 |
append(buf, 3); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void ensure(long n) |
| 140 |
{ |
| 141 |
if (n + len > alo) |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
alo = len + n + (len+n > 512 ? (len+n)/2 : 256); |
| 149 |
|
| 150 |
|
| 151 |
str = (char *)t3realloc(str, alo + 1); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
void truncate(long l) |
| 157 |
{ |
| 158 |
|
| 159 |
if (l < len) |
| 160 |
{ |
| 161 |
|
| 162 |
len = l; |
| 163 |
str[l] = '\0'; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
protected: |
| 168 |
~StringRef() |
| 169 |
{ |
| 170 |
if (str != 0) |
| 171 |
t3free(str); |
| 172 |
} |
| 173 |
|
| 174 |
void init(const char *s, size_t l, size_t init_alo) |
| 175 |
{ |
| 176 |
|
| 177 |
if (init_alo < l) |
| 178 |
init_alo = l; |
| 179 |
|
| 180 |
|
| 181 |
alo = init_alo; |
| 182 |
str = (char *)t3malloc(alo + 1); |
| 183 |
|
| 184 |
|
| 185 |
if (str != 0) |
| 186 |
memcpy(str, s, l); |
| 187 |
|
| 188 |
|
| 189 |
str[l] = '\0'; |
| 190 |
|
| 191 |
|
| 192 |
len = l; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
char *str; |
| 197 |
|
| 198 |
|
| 199 |
long len; |
| 200 |
|
| 201 |
|
| 202 |
long alo; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
#endif |