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
|
/*************************************************************************
*
* 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
*/
#include <mt/mstring.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdarg.h>
uniheap<ms_mincaps> mstring::heap;
///////////////////////////////////////////////////////////////////
mstring::mstring() : sref(heap.push(),0),caps(ms_mincaps) {
map[dim]=0;
}
mstring::mstring(int n) : sref(heap.push(),0),caps(ms_mincaps) {
if(caps<n) { while(caps<n) caps<<=1; heap.pop(map); map=(char*)malloc(caps); }
map[dim=0]=0;
}
mstring::mstring(int n,char c) : sref(heap.push(),0),caps(ms_mincaps) {
resize(n);
memset(map,c,dim);
}
mstring::mstring(const char* s) : sref(heap.push(),0),caps(ms_mincaps) {
resize(strlen(s));
memcpy(map,s,dim);
}
mstring::mstring(const char* s,int n) : sref(heap.push(),0),caps(ms_mincaps) {
resize(n);
memcpy(map,s,dim);
}
mstring::mstring(const sref& s) : sref(heap.push(),0),caps(ms_mincaps) {
resize(s.size());
memcpy(map,s.data(),dim);
}
mstring::mstring(const mstring& s) : sref(heap.push(),0),caps(ms_mincaps) {
resize(s.dim);
memcpy(map,s.map,dim);
}
mstring::~mstring() {
if(caps==ms_mincaps) heap.pop(map);
else free(map);
}
///////////////////////////////////////////////////////////////////
void mstring::erase(int pos,int n)
{
if(pos<0) { pos+=dim; if(pos<0) pos=0; }
if(pos<dim) {
if(n<0) { n+=dim+1; if(n<0) n=0; }
if(n>=dim-pos) map[dim=pos]=0;
else { memmove(map+pos,map+pos+n,dim-pos-n); map[dim-=n]=0; }
}
}
void mstring::reserve(int n) {
if(caps<=n) {
if(caps==ms_mincaps) {
while(caps<=n) caps<<=1;
char* p=(char*)memcpy(malloc(caps),map,dim);
heap.pop(map);
map=p;
}
else {
while(caps<=n) caps<<=1;
map=(char*)realloc(map,caps);
}
}
}
///////////////////////////////////////////////////////////////////
int mstring::load(const mstring& path)
{
int fd=open(path.c_str(),O_RDONLY);
if(fd<0) return -1;
struct stat fs; fstat(fd,&fs);
resize(fs.st_size);
read(fd,map,dim);
close(fd);
return dim;
}
///////////////////////////////////////////////////////////////////
mstring& mstring::convert(const char* format,...)
{
sref_t buf;
va_list ap;
va_start(ap,format);
resize(vsprintf(buf,format,ap));
memcpy(map,buf,dim);
va_end(ap);
return *this;
}
///////////////////////////////////////////////////////////////////
int swrite::appc(char c) { return mp->put(c),1; }
int swrite::apps(const sref& s) { return mp->put(s),s.size(); }
///////////////////////////////////////////////////////////////////
int mswrite::appc(char c) { return m.put(c),1; }
int mswrite::apps(const sref& s) { return m.put(s),s.size(); }
///////////////////////////////////////////////////////////////////
int merror_t::appc(char c) { return desc.put(c),1; }
int merror_t::apps(const sref& s) { return desc.put(s),s.size(); }
///////////////////////////////////////////////////////////////////
void getline(mstring& m,FILE* fp,char end)
{
m.clear();
int c=getc(fp);
while(c!=end&&c!=EOF) { m.append(1,c); c=getc(fp); }
}
|