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/mstream.cc | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/mt/mstream.cc (limited to 'src/mt/mstream.cc') diff --git a/src/mt/mstream.cc b/src/mt/mstream.cc new file mode 100644 index 0000000..3d94936 --- /dev/null +++ b/src/mt/mstream.cc @@ -0,0 +1,72 @@ +/************************************************************************* + * + * 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 + +const char cvt_form_f[]="%#.12f"; +const int MSEC=4000; + +sref ftoa(sref_t& buf,double f) +{ + char* e=buf+sprintf(buf,cvt_form_f,f)-1; + while(*e=='0') e--; if(*e=='.') e--; + return sref(buf,e-buf+1); +} + +/////////////////////////////////////////////////////////// + +int mstream::appc(char c) { return -1; } +int mstream::appi(int i) { return -1; } +int mstream::appf(double f) { return -1; } +int mstream::apps(const sref& s) { return -1; } + +/////////////////////////////////////////////////////////// + +int cvt_stream::appi(int i) { sref_t buf; return apps(itoa(buf,i)); } +int cvt_stream::appf(double f) { sref_t buf; return apps(ftoa(buf,f)); } + +/////////////////////////////////////////////////////////// + +int fp_stream::appc(char c) { return putc(c,fp)<0?-1:1; } +int fp_stream::apps(const sref& s) { return fwrite(s.data(),s.size(),1,fp); } + +/////////////////////////////////////////////////////////// + +int fd_stream::appc(char c) { return ::write(fd,&c,1); } +int fd_stream::apps(const sref& s) { return ::write(fd,s.data(),s.size()); } + +/////////////////////////////////////////////////////////// + +int pipe_stream::appc(char c) { return mwrite(&c,1); } +int pipe_stream::apps(const sref& s) { return mwrite(s.data(),s.size()); } + +int pipe_stream::mwrite(const char* s,int n) +{ + int old=n; + while(n>0&&!hup) { + pollfd pfd={fd,POLLOUT,0}; + if(::poll(&pfd,1,MSEC)<0) return hup=-1; + int m=::write(fd,s,n); + if(m<0) return hup=-1; + s+=m; n-=m; + } + return hup?-1:old; +} -- cgit v1.2.3