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/mstream.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/mstream.cc')
| -rw-r--r-- | src/mt/mstream.cc | 72 |
1 files changed, 72 insertions, 0 deletions
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 @@ | |||
| 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/mstream.h> | ||
| 21 | #include <poll.h> | ||
| 22 | #include <signal.h> | ||
| 23 | |||
| 24 | const char cvt_form_f[]="%#.12f"; | ||
| 25 | const int MSEC=4000; | ||
| 26 | |||
| 27 | sref ftoa(sref_t& buf,double f) | ||
| 28 | { | ||
| 29 | char* e=buf+sprintf(buf,cvt_form_f,f)-1; | ||
| 30 | while(*e=='0') e--; if(*e=='.') e--; | ||
| 31 | return sref(buf,e-buf+1); | ||
| 32 | } | ||
| 33 | |||
| 34 | /////////////////////////////////////////////////////////// | ||
| 35 | |||
| 36 | int mstream::appc(char c) { return -1; } | ||
| 37 | int mstream::appi(int i) { return -1; } | ||
| 38 | int mstream::appf(double f) { return -1; } | ||
| 39 | int mstream::apps(const sref& s) { return -1; } | ||
| 40 | |||
| 41 | /////////////////////////////////////////////////////////// | ||
| 42 | |||
| 43 | int cvt_stream::appi(int i) { sref_t buf; return apps(itoa(buf,i)); } | ||
| 44 | int cvt_stream::appf(double f) { sref_t buf; return apps(ftoa(buf,f)); } | ||
| 45 | |||
| 46 | /////////////////////////////////////////////////////////// | ||
| 47 | |||
| 48 | int fp_stream::appc(char c) { return putc(c,fp)<0?-1:1; } | ||
| 49 | int fp_stream::apps(const sref& s) { return fwrite(s.data(),s.size(),1,fp); } | ||
| 50 | |||
| 51 | /////////////////////////////////////////////////////////// | ||
| 52 | |||
| 53 | int fd_stream::appc(char c) { return ::write(fd,&c,1); } | ||
| 54 | int fd_stream::apps(const sref& s) { return ::write(fd,s.data(),s.size()); } | ||
| 55 | |||
| 56 | /////////////////////////////////////////////////////////// | ||
| 57 | |||
| 58 | int pipe_stream::appc(char c) { return mwrite(&c,1); } | ||
| 59 | int pipe_stream::apps(const sref& s) { return mwrite(s.data(),s.size()); } | ||
| 60 | |||
| 61 | int pipe_stream::mwrite(const char* s,int n) | ||
| 62 | { | ||
| 63 | int old=n; | ||
| 64 | while(n>0&&!hup) { | ||
| 65 | pollfd pfd={fd,POLLOUT,0}; | ||
| 66 | if(::poll(&pfd,1,MSEC)<0) return hup=-1; | ||
| 67 | int m=::write(fd,s,n); | ||
| 68 | if(m<0) return hup=-1; | ||
| 69 | s+=m; n-=m; | ||
| 70 | } | ||
| 71 | return hup?-1:old; | ||
| 72 | } | ||
