From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mt/mstring.cc | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/mt/mstring.cc (limited to 'src/mt/mstring.cc') 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 @@ +/************************************************************************* + * + * 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 +#include +#include +#include +#include + +uniheap 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=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); } +} + + -- cgit v1.2.3