diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/mt/mstring.h | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/mstring.h')
| -rw-r--r-- | src/mt/mstring.h | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/src/mt/mstring.h b/src/mt/mstring.h new file mode 100644 index 0000000..be07c1a --- /dev/null +++ b/src/mt/mstring.h | |||
| @@ -0,0 +1,222 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef MSTRINGH | ||
| 21 | #define MSTRINGH | ||
| 22 | |||
| 23 | #include <mt/mstream.h> | ||
| 24 | #include <mt/uniheap.h> | ||
| 25 | |||
| 26 | const int ms_mincaps=32; | ||
| 27 | |||
| 28 | class mstring : public sref { | ||
| 29 | public: | ||
| 30 | mstring(); | ||
| 31 | explicit mstring(int n); | ||
| 32 | mstring(int n,char c); | ||
| 33 | mstring(const char* s); | ||
| 34 | mstring(const char* s,int n); | ||
| 35 | mstring(const sref& s); | ||
| 36 | mstring(const mstring& s); | ||
| 37 | ~mstring(); | ||
| 38 | |||
| 39 | //////////////////////////////////////////////////////////////// | ||
| 40 | |||
| 41 | const char* c_str() const { return map; } | ||
| 42 | int capacity() const { return caps; } | ||
| 43 | |||
| 44 | void clear() { map[dim=0]=0; } | ||
| 45 | |||
| 46 | int own(const sref& s) const { int p=s.data()-map; return p>=0&&p<caps; } | ||
| 47 | |||
| 48 | //////////////////////////////////////////////////////////////// | ||
| 49 | |||
| 50 | void assign(int sdim,char c) { | ||
| 51 | resize(sdim); | ||
| 52 | memset(map,c,sdim); | ||
| 53 | } | ||
| 54 | void assign(const sref& s) { | ||
| 55 | reserve(s.size()); | ||
| 56 | memmove(map,s.data(),s.size()); | ||
| 57 | map[dim=s.size()]=0; | ||
| 58 | } | ||
| 59 | |||
| 60 | void put(char c) { if(dim+1>=caps) reserve(dim+1); map[dim++]=c; map[dim]=0; } | ||
| 61 | void put(const sref& s) { | ||
| 62 | reserve(dim+s.size()); | ||
| 63 | memcpy(map+dim,s.data(),s.size()); | ||
| 64 | map[dim+=s.size()]=0; | ||
| 65 | } | ||
| 66 | |||
| 67 | mstring& append(int sdim,char c) { | ||
| 68 | reserve(dim+sdim); | ||
| 69 | memset(map+dim,c,sdim); | ||
| 70 | map[dim+=sdim]=0; | ||
| 71 | return *this; | ||
| 72 | } | ||
| 73 | |||
| 74 | mstring& append(const sref& s) { | ||
| 75 | reserve(dim+s.size()); | ||
| 76 | memmove(map+dim,s.data(),s.size()); | ||
| 77 | map[dim+=s.size()]=0; | ||
| 78 | return *this; | ||
| 79 | } | ||
| 80 | |||
| 81 | int insert(int pos,int n) { | ||
| 82 | if(pos<0||pos>dim) return 0; | ||
| 83 | if(n) { reserve(dim+n); memmove(map+pos+n,map+pos,dim-pos); map[dim+=n]=0; } | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | mstring& insert(int pos,int sdim,char c) { | ||
| 88 | if(insert(pos,sdim)) memset(map+pos,c,sdim); | ||
| 89 | return *this; | ||
| 90 | } | ||
| 91 | mstring& insert(int pos,const sref& s) { | ||
| 92 | if(insert(pos,s.size())) memcpy(map+pos,s.data(),s.size()); | ||
| 93 | return *this; | ||
| 94 | } | ||
| 95 | |||
| 96 | mstring& replace(int pos,int n,int sdim,char c) { | ||
| 97 | if(insert(pos+n,sdim-n)) memset(map+pos,c,sdim); | ||
| 98 | return *this; | ||
| 99 | } | ||
| 100 | mstring& replace(int pos,int n,const sref& s) { | ||
| 101 | if(insert(pos+n,s.size()-n)) memcpy(map+pos,s.data(),s.size()); | ||
| 102 | return *this; | ||
| 103 | } | ||
| 104 | |||
| 105 | //////////////////////////////////////////////////////////////// | ||
| 106 | |||
| 107 | void erase(int pos,int n=-1); | ||
| 108 | void reserve(int n); | ||
| 109 | void resize(int n) { reserve(n); map[dim=n]=0; } | ||
| 110 | |||
| 111 | //////////////////////////////////////////////////////////////// | ||
| 112 | |||
| 113 | mstring& operator=(const mstring& s) { assign(s); return *this; } | ||
| 114 | mstring& operator=(const sref& s) { assign(s); return *this; } | ||
| 115 | mstring& operator=(char c) { assign(1,c); return *this; } | ||
| 116 | |||
| 117 | mstring& operator+=(const sref& s) { return append(s); } | ||
| 118 | mstring& operator+=(char c) { return append(1,c); } | ||
| 119 | |||
| 120 | //////////////////////////////////////////////////////////////// | ||
| 121 | |||
| 122 | int load(const mstring& path); | ||
| 123 | |||
| 124 | //////////////////////////////////////////////////////////////// | ||
| 125 | |||
| 126 | // NOTE: if you apply convert to %s or any unbound data, | ||
| 127 | // you're in trouble. Internal buffer is sref_t. | ||
| 128 | |||
| 129 | mstring& convert(const char* format,...); | ||
| 130 | |||
| 131 | protected: | ||
| 132 | static uniheap<ms_mincaps> heap; | ||
| 133 | private: | ||
| 134 | int caps; | ||
| 135 | }; | ||
| 136 | |||
| 137 | /////////////////////////////////////////////////////////////////// | ||
| 138 | |||
| 139 | inline void swap(mstring& a,mstring& b) | ||
| 140 | { | ||
| 141 | char buf[sizeof(mstring)]; | ||
| 142 | memcpy(buf,&a,sizeof(mstring)); | ||
| 143 | memcpy(&a,&b,sizeof(mstring)); | ||
| 144 | memcpy(&b,buf,sizeof(mstring)); | ||
| 145 | } | ||
| 146 | |||
| 147 | /////////////////////////////////////////////////////////////////// | ||
| 148 | |||
| 149 | inline mstring operator+(const sref& a,const sref& b) | ||
| 150 | { | ||
| 151 | return mstring(a).append(b); | ||
| 152 | } | ||
| 153 | inline mstring operator+(const sref& a,char b) { | ||
| 154 | return mstring(a).append(1,b); | ||
| 155 | } | ||
| 156 | inline mstring operator+(char a,const sref& b) { | ||
| 157 | return mstring(1,a).append(b); | ||
| 158 | } | ||
| 159 | |||
| 160 | /////////////////////////////////////////////////////////////////// | ||
| 161 | |||
| 162 | inline mstring btoa(int b) { return mstring(1,b?'1':'0'); } | ||
| 163 | inline mstring itoa(int i) { sref_t buf; return mstring(itoa(buf,i)); } | ||
| 164 | inline mstring ftoa(double f) { sref_t buf; return mstring(ftoa(buf,f)); } | ||
| 165 | |||
| 166 | /////////////////////////////////////////////////////////////////// | ||
| 167 | |||
| 168 | const mstring ms_empty; | ||
| 169 | |||
| 170 | /////////////////////////////////////////////////////////////////// | ||
| 171 | |||
| 172 | struct swrite : public cvt_stream { | ||
| 173 | mstring* mp; | ||
| 174 | |||
| 175 | int appc(char c); | ||
| 176 | int apps(const sref& s); | ||
| 177 | |||
| 178 | swrite() : mp(0) {} | ||
| 179 | swrite(mstring& m) : mp(&m) { mp->clear(); } | ||
| 180 | |||
| 181 | swrite& operator()(mstring& m) { (mp=&m)->clear(); return *this; } | ||
| 182 | swrite& hold(mstring& m) { mp=&m; return *this; } | ||
| 183 | }; | ||
| 184 | |||
| 185 | /////////////////////////////////////////////////////////////////// | ||
| 186 | |||
| 187 | class mswrite : public cvt_stream { | ||
| 188 | public: | ||
| 189 | mstring m; | ||
| 190 | |||
| 191 | int appc(char c); | ||
| 192 | int apps(const sref& s); | ||
| 193 | |||
| 194 | const mstring& str() const { return m; } // compatibility | ||
| 195 | void clear() { m.clear(); } | ||
| 196 | |||
| 197 | mswrite() : m() {} | ||
| 198 | private: | ||
| 199 | mswrite(mstring&) {} // make no mistakes due to similarity with swrite | ||
| 200 | }; | ||
| 201 | |||
| 202 | /////////////////////////////////////////////////////////////////// | ||
| 203 | |||
| 204 | struct merror_t : public cvt_stream { | ||
| 205 | int num; | ||
| 206 | mstring desc; | ||
| 207 | |||
| 208 | int appc(char c); | ||
| 209 | int apps(const sref& s); | ||
| 210 | |||
| 211 | merror_t(int n) : num(n),desc() {} | ||
| 212 | }; | ||
| 213 | |||
| 214 | #define THROW(x) { merror_t merr(0); merr<<x; throw merr; } | ||
| 215 | |||
| 216 | /////////////////////////////////////////////////////////////////// | ||
| 217 | |||
| 218 | void getline(mstring& m,FILE* fp,char end='\n'); | ||
| 219 | |||
| 220 | /////////////////////////////////////////////////////////////////// | ||
| 221 | |||
| 222 | #endif | ||
