/************************************************************************* * * 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; }