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/mread.cc | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/mt/mread.cc (limited to 'src/mt/mread.cc') diff --git a/src/mt/mread.cc b/src/mt/mread.cc new file mode 100644 index 0000000..da5a966 --- /dev/null +++ b/src/mt/mread.cc @@ -0,0 +1,135 @@ +/************************************************************************* + * + * 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 + +const int MSEC=4000; +const int RETRY=3; +const int TIMEOUT=2; + +////////////////////////////////////////////////////////////////////////// + +mread::mread() : fd(-1),buf() +{ +} + +mread::mread(int f) : fd(f),buf() +{ +} + +////////////////////////////////////////////////////////////////////////// + +void mread::clear() +{ + buf.clear(); +} + +int mread::poll() +{ + pollfd pfd={fd,POLLIN,0}; + return ::poll(&pfd,1,MSEC); +} + +int mread::read(char* s,int n) +{ + if(buf.empty()) { + int m=::read(fd,s,n); + for(int i=0;i=0;i++) m=::read(fd,s,n); + if(m<0&&n) *s=0; + return m; + } + else { + if(n>buf.size()) n=buf.size(); + memcpy(s,buf.data(),n); + buf.erase(0,n); + return n; + } +} + +void mread::putback(const sref& s) +{ + if(s.nempty()) buf.insert(0,s); +} + +////////////////////////////////////////////////////////////////////////// + +int mread::readx(char* s,int n) +{ + int tries=0,acc=0; + while(acc=TIMEOUT) return acc; + } + else { + acc+=m; + tries=0; + } + } + return acc; +} + +////////////////////////////////////////////////////////////////////////// + +int mread::readln(char* s,int n) +{ + int tries=0,acc=0; + while(acc=TIMEOUT) return acc?-1:0; + } + else { + int p=sref(s+acc,m).find('\n')+1; + if(p>0) { acc+=p; putback(sref(s+acc,m-p)); return acc; } + else { acc+=m; tries=0; } + } + } + return acc; +} + +int mread::readpara(char* s,int n) +{ + int tries=0,acc=0; + while(acc=TIMEOUT) return acc?-1:0; + } + else { + int found=0; + sref first,rest(s+acc,m); + while(Splitln(first,rest)) if(first.empty()) { found=1; break; } + if(found) { acc=rest.begin()-s; putback(rest); return acc; } + else { acc+=m; tries=0; } + } + } + return acc; +} + -- cgit v1.2.3