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
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MSTRINGH
#define MSTRINGH
#include <mt/mstream.h>
#include <mt/uniheap.h>
const int ms_mincaps=32;
class mstring : public sref {
public:
mstring();
explicit mstring(int n);
mstring(int n,char c);
mstring(const char* s);
mstring(const char* s,int n);
mstring(const sref& s);
mstring(const mstring& s);
~mstring();
////////////////////////////////////////////////////////////////
const char* c_str() const { return map; }
int capacity() const { return caps; }
void clear() { map[dim=0]=0; }
int own(const sref& s) const { int p=s.data()-map; return p>=0&&p<caps; }
////////////////////////////////////////////////////////////////
void assign(int sdim,char c) {
resize(sdim);
memset(map,c,sdim);
}
void assign(const sref& s) {
reserve(s.size());
memmove(map,s.data(),s.size());
map[dim=s.size()]=0;
}
void put(char c) { if(dim+1>=caps) reserve(dim+1); map[dim++]=c; map[dim]=0; }
void put(const sref& s) {
reserve(dim+s.size());
memcpy(map+dim,s.data(),s.size());
map[dim+=s.size()]=0;
}
mstring& append(int sdim,char c) {
reserve(dim+sdim);
memset(map+dim,c,sdim);
map[dim+=sdim]=0;
return *this;
}
mstring& append(const sref& s) {
reserve(dim+s.size());
memmove(map+dim,s.data(),s.size());
map[dim+=s.size()]=0;
return *this;
}
int insert(int pos,int n) {
if(pos<0||pos>dim) return 0;
if(n) { reserve(dim+n); memmove(map+pos+n,map+pos,dim-pos); map[dim+=n]=0; }
return 1;
}
mstring& insert(int pos,int sdim,char c) {
if(insert(pos,sdim)) memset(map+pos,c,sdim);
return *this;
}
mstring& insert(int pos,const sref& s) {
if(insert(pos,s.size())) memcpy(map+pos,s.data(),s.size());
return *this;
}
mstring& replace(int pos,int n,int sdim,char c) {
if(insert(pos+n,sdim-n)) memset(map+pos,c,sdim);
return *this;
}
mstring& replace(int pos,int n,const sref& s) {
if(insert(pos+n,s.size()-n)) memcpy(map+pos,s.data(),s.size());
return *this;
}
////////////////////////////////////////////////////////////////
void erase(int pos,int n=-1);
void reserve(int n);
void resize(int n) { reserve(n); map[dim=n]=0; }
////////////////////////////////////////////////////////////////
mstring& operator=(const mstring& s) { assign(s); return *this; }
mstring& operator=(const sref& s) { assign(s); return *this; }
mstring& operator=(char c) { assign(1,c); return *this; }
mstring& operator+=(const sref& s) { return append(s); }
mstring& operator+=(char c) { return append(1,c); }
////////////////////////////////////////////////////////////////
int load(const mstring& path);
////////////////////////////////////////////////////////////////
// NOTE: if you apply convert to %s or any unbound data,
// you're in trouble. Internal buffer is sref_t.
mstring& convert(const char* format,...);
protected:
static uniheap<ms_mincaps> heap;
private:
int caps;
};
///////////////////////////////////////////////////////////////////
inline void swap(mstring& a,mstring& b)
{
char buf[sizeof(mstring)];
memcpy(buf,&a,sizeof(mstring));
memcpy(&a,&b,sizeof(mstring));
memcpy(&b,buf,sizeof(mstring));
}
///////////////////////////////////////////////////////////////////
inline mstring operator+(const sref& a,const sref& b)
{
return mstring(a).append(b);
}
inline mstring operator+(const sref& a,char b) {
return mstring(a).append(1,b);
}
inline mstring operator+(char a,const sref& b) {
return mstring(1,a).append(b);
}
///////////////////////////////////////////////////////////////////
inline mstring btoa(int b) { return mstring(1,b?'1':'0'); }
inline mstring itoa(int i) { sref_t buf; return mstring(itoa(buf,i)); }
inline mstring ftoa(double f) { sref_t buf; return mstring(ftoa(buf,f)); }
///////////////////////////////////////////////////////////////////
const mstring ms_empty;
///////////////////////////////////////////////////////////////////
struct swrite : public cvt_stream {
mstring* mp;
int appc(char c);
int apps(const sref& s);
swrite() : mp(0) {}
swrite(mstring& m) : mp(&m) { mp->clear(); }
swrite& operator()(mstring& m) { (mp=&m)->clear(); return *this; }
swrite& hold(mstring& m) { mp=&m; return *this; }
};
///////////////////////////////////////////////////////////////////
class mswrite : public cvt_stream {
public:
mstring m;
int appc(char c);
int apps(const sref& s);
const mstring& str() const { return m; } // compatibility
void clear() { m.clear(); }
mswrite() : m() {}
private:
mswrite(mstring&) {} // make no mistakes due to similarity with swrite
};
///////////////////////////////////////////////////////////////////
struct merror_t : public cvt_stream {
int num;
mstring desc;
int appc(char c);
int apps(const sref& s);
merror_t(int n) : num(n),desc() {}
};
#define THROW(x) { merror_t merr(0); merr<<x; throw merr; }
///////////////////////////////////////////////////////////////////
void getline(mstring& m,FILE* fp,char end='\n');
///////////////////////////////////////////////////////////////////
#endif
|