-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_qstr.h
250 lines (210 loc) · 7.16 KB
/
m_qstr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2005 James Haley
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//-----------------------------------------------------------------------------
//
// Reallocating string structure
//
// What this class guarantees:
// * The string will always be null-terminated
// * Indexing functions always check array bounds
// * Insertion functions always reallocate when needed
//
// Of course, using getBuffer can negate these, so avoid it
// except for passing a char * to read op functions.
//
// By James Haley
//
//-----------------------------------------------------------------------------
#ifndef M_QSTR_H__
#define M_QSTR_H__
#include "z_zone.h"
class SaveArchive;
class qstring : public ZoneObject
{
private:
char local[16];
char *buffer;
size_t index;
size_t size;
bool isLocal() const { return (buffer == local); }
void unLocalize(size_t pSize);
public:
static const qstring emptyString;
static const size_t npos;
static const size_t basesize;
// Constructors / Destructor
qstring(size_t startSize = 0)
: ZoneObject(), index(0), size(16)
{
buffer = local;
memset(local, 0, sizeof(local));
if(startSize)
initCreateSize(startSize);
}
qstring(const qstring &other)
: ZoneObject(), index(0), size(16)
{
buffer = local;
memset(local, 0, sizeof(local));
copy(other);
}
explicit qstring(const char *cstr)
: ZoneObject(), index(0), size(16)
{
buffer = local;
memset(local, 0, sizeof(local));
copy(cstr);
}
qstring(qstring &&other);
~qstring() { freeBuffer(); }
// Basic Property Getters
//
// qstring::getBuffer
//
// Retrieves a pointer to the internal buffer. This pointer shouldn't be
// cached, and is not meant for writing into (although it is safe to do so, it
// circumvents the encapsulation and security of this structure).
//
char *getBuffer() { return buffer; }
//
// qstring::constPtr
//
// Like qstring::getBuffer, but casts to const to enforce safety.
//
const char *constPtr() const { return buffer; }
//
// qstring::length
//
// Because the validity of "index" is maintained by all insertion and editing
// functions, we can bypass calling strlen.
//
size_t length() const { return index; }
//
// qstring::empty
//
// For C++ std::string compatibility.
//
bool empty() const { return (index == 0); }
//
// qstring::getSize
//
// Returns the amount of size allocated for this qstring (will be >= strlen).
// You are allowed to index into the qstring up to size - 1, although any bytes
// beyond the strlen will be zero.
//
size_t getSize() const { return size; }
// Initialization and Resizing
qstring &initCreate();
qstring &initCreateSize(size_t size);
qstring &createSize(size_t size);
qstring &create();
qstring &grow(size_t len);
qstring &clear();
qstring &clearOrCreate(size_t size);
void freeBuffer();
// Indexing operations
char charAt(size_t idx) const;
char *bufferAt(size_t idx);
unsigned char ucharAt(size_t idx) const { return (unsigned char)charAt(idx); }
// Concatenation and insertion/deletion
qstring &Putc(char ch);
qstring &Delc();
qstring &concat(const char *str);
qstring &concat(const qstring &src);
qstring &insert(const char *insertstr, size_t pos);
// Comparisons: C and C++ style
int strCmp(const char *str) const;
int strNCmp(const char *str, size_t maxcount) const;
int strCaseCmp(const char *str) const;
int strNCaseCmp(const char *str, size_t maxcount) const;
bool compare(const char *str) const;
bool compare(const qstring &other) const;
// Hashing
static unsigned int HashCodeStatic(const char *str);
static unsigned int HashCodeCaseStatic(const char *str);
unsigned int hashCode() const; // case-insensitive
unsigned int hashCodeCase() const; // case-considering
// Copying and Swapping
qstring ©(const char *str);
qstring ©(const char *str, size_t count);
qstring ©(const qstring &src);
char *copyInto(char *dest, size_t size) const;
qstring ©Into(qstring &dest) const;
void swapWith(qstring &str2);
// In-Place Case Conversions
qstring &toUpper();
qstring &toLower();
// Substring Replacements
size_t replace(const char *filter, char repl);
size_t replaceNotOf(const char *filter, char repl);
// File Path Utilities
qstring &normalizeSlashes();
qstring &pathConcatenate(const char *addend);
qstring &addDefaultExtension(const char *ext);
qstring &removeFileSpec();
void extractFileBase(qstring &dest) const;
// Zone strdup wrappers
char *duplicate(int tag = PU_STATIC) const;
char *duplicateAuto() const;
// Numeric Conversions
int toInt() const;
long toLong(char **endptr, int radix);
double toDouble(char **endptr);
// Searching/Substring Finding Routines
const char *strChr(char c) const;
const char *strRChr(char c) const;
size_t findFirstOf(char c) const;
size_t findFirstNotOf(char c) const;
size_t findLastOf(char c) const;
const char *findSubStr(const char *substr) const;
const char *findSubStrNoCase(const char *substr) const;
size_t find(const char *s, size_t pos = 0) const;
// Stripping and Truncation
qstring &lstrip(char c);
qstring &rstrip(char c);
qstring &truncate(size_t pos);
qstring &erase(size_t pos, size_t n = npos);
// Special Formatting
qstring &makeQuoted();
int Printf(size_t maxlen, const char *fmt, ...);
// Operators
bool operator == (const qstring &other) const;
bool operator == (const char *other) const;
bool operator != (const qstring &other) const;
bool operator != (const char *other) const;
qstring &operator = (const qstring &other);
qstring &operator = (const char *other);
qstring &operator += (const qstring &other);
qstring &operator += (const char *other);
qstring &operator += (char ch);
qstring &operator << (const qstring &other);
qstring &operator << (const char *other);
qstring &operator << (char ch);
qstring &operator << (int i);
qstring &operator << (double d);
char &operator [] (size_t idx);
const char &operator [] (size_t idx) const;
// Archiving
#if 0
void archive(SaveArchive &arc);
#endif
};
#endif
// EOF