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.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/mstring.cc')
| -rw-r--r-- | src/mt/mstring.cc | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/mt/mstring.cc b/src/mt/mstring.cc new file mode 100644 index 0000000..7be82e3 --- /dev/null +++ b/src/mt/mstring.cc | |||
| @@ -0,0 +1,139 @@ | |||
| 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 | #include <mt/mstring.h> | ||
| 21 | #include <unistd.h> | ||
| 22 | #include <fcntl.h> | ||
| 23 | #include <sys/stat.h> | ||
| 24 | #include <stdarg.h> | ||
| 25 | |||
| 26 | uniheap<ms_mincaps> mstring::heap; | ||
| 27 | |||
| 28 | /////////////////////////////////////////////////////////////////// | ||
| 29 | |||
| 30 | mstring::mstring() : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 31 | map[dim]=0; | ||
| 32 | } | ||
| 33 | mstring::mstring(int n) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 34 | if(caps<n) { while(caps<n) caps<<=1; heap.pop(map); map=(char*)malloc(caps); } | ||
| 35 | map[dim=0]=0; | ||
| 36 | } | ||
| 37 | mstring::mstring(int n,char c) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 38 | resize(n); | ||
| 39 | memset(map,c,dim); | ||
| 40 | } | ||
| 41 | mstring::mstring(const char* s) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 42 | resize(strlen(s)); | ||
| 43 | memcpy(map,s,dim); | ||
| 44 | } | ||
| 45 | mstring::mstring(const char* s,int n) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 46 | resize(n); | ||
| 47 | memcpy(map,s,dim); | ||
| 48 | } | ||
| 49 | mstring::mstring(const sref& s) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 50 | resize(s.size()); | ||
| 51 | memcpy(map,s.data(),dim); | ||
| 52 | } | ||
| 53 | mstring::mstring(const mstring& s) : sref(heap.push(),0),caps(ms_mincaps) { | ||
| 54 | resize(s.dim); | ||
| 55 | memcpy(map,s.map,dim); | ||
| 56 | } | ||
| 57 | mstring::~mstring() { | ||
| 58 | if(caps==ms_mincaps) heap.pop(map); | ||
| 59 | else free(map); | ||
| 60 | } | ||
| 61 | |||
| 62 | /////////////////////////////////////////////////////////////////// | ||
| 63 | |||
| 64 | void mstring::erase(int pos,int n) | ||
| 65 | { | ||
| 66 | if(pos<0) { pos+=dim; if(pos<0) pos=0; } | ||
| 67 | if(pos<dim) { | ||
| 68 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 69 | if(n>=dim-pos) map[dim=pos]=0; | ||
| 70 | else { memmove(map+pos,map+pos+n,dim-pos-n); map[dim-=n]=0; } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void mstring::reserve(int n) { | ||
| 75 | if(caps<=n) { | ||
| 76 | if(caps==ms_mincaps) { | ||
| 77 | while(caps<=n) caps<<=1; | ||
| 78 | char* p=(char*)memcpy(malloc(caps),map,dim); | ||
| 79 | heap.pop(map); | ||
| 80 | map=p; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | while(caps<=n) caps<<=1; | ||
| 84 | map=(char*)realloc(map,caps); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | /////////////////////////////////////////////////////////////////// | ||
| 90 | |||
| 91 | int mstring::load(const mstring& path) | ||
| 92 | { | ||
| 93 | int fd=open(path.c_str(),O_RDONLY); | ||
| 94 | if(fd<0) return -1; | ||
| 95 | struct stat fs; fstat(fd,&fs); | ||
| 96 | resize(fs.st_size); | ||
| 97 | read(fd,map,dim); | ||
| 98 | close(fd); | ||
| 99 | return dim; | ||
| 100 | } | ||
| 101 | |||
| 102 | /////////////////////////////////////////////////////////////////// | ||
| 103 | |||
| 104 | mstring& mstring::convert(const char* format,...) | ||
| 105 | { | ||
| 106 | sref_t buf; | ||
| 107 | va_list ap; | ||
| 108 | va_start(ap,format); | ||
| 109 | resize(vsprintf(buf,format,ap)); | ||
| 110 | memcpy(map,buf,dim); | ||
| 111 | va_end(ap); | ||
| 112 | return *this; | ||
| 113 | } | ||
| 114 | |||
| 115 | /////////////////////////////////////////////////////////////////// | ||
| 116 | |||
| 117 | int swrite::appc(char c) { return mp->put(c),1; } | ||
| 118 | int swrite::apps(const sref& s) { return mp->put(s),s.size(); } | ||
| 119 | |||
| 120 | /////////////////////////////////////////////////////////////////// | ||
| 121 | |||
| 122 | int mswrite::appc(char c) { return m.put(c),1; } | ||
| 123 | int mswrite::apps(const sref& s) { return m.put(s),s.size(); } | ||
| 124 | |||
| 125 | /////////////////////////////////////////////////////////////////// | ||
| 126 | |||
| 127 | int merror_t::appc(char c) { return desc.put(c),1; } | ||
| 128 | int merror_t::apps(const sref& s) { return desc.put(s),s.size(); } | ||
| 129 | |||
| 130 | /////////////////////////////////////////////////////////////////// | ||
| 131 | |||
| 132 | void getline(mstring& m,FILE* fp,char end) | ||
| 133 | { | ||
| 134 | m.clear(); | ||
| 135 | int c=getc(fp); | ||
| 136 | while(c!=end&&c!=EOF) { m.append(1,c); c=getc(fp); } | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
