diff options
Diffstat (limited to 'src/mt/mstream.h')
| -rw-r--r-- | src/mt/mstream.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/mt/mstream.h b/src/mt/mstream.h new file mode 100644 index 0000000..4f5aeaf --- /dev/null +++ b/src/mt/mstream.h | |||
| @@ -0,0 +1,103 @@ | |||
| 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 | #ifndef MSTREAMH | ||
| 21 | #define MSTREAMH | ||
| 22 | |||
| 23 | #include <mt/stringref.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <stdio.h> | ||
| 26 | |||
| 27 | /////////////////////////////////////////////////////////// | ||
| 28 | |||
| 29 | const char cvt_form_i[]="%d"; | ||
| 30 | |||
| 31 | /////////////////////////////////////////////////////////// | ||
| 32 | |||
| 33 | inline sref itoa(sref_t& buf,int i) { return sref(buf,sprintf(buf,cvt_form_i,i)); } | ||
| 34 | sref ftoa(sref_t& buf,double f); | ||
| 35 | |||
| 36 | /////////////////////////////////////////////////////////// | ||
| 37 | |||
| 38 | struct mstream { | ||
| 39 | virtual int appc(char c); | ||
| 40 | virtual int appi(int i); | ||
| 41 | virtual int appf(double f); | ||
| 42 | virtual int apps(const sref& s); | ||
| 43 | |||
| 44 | mstream& put(char c) { return appc(c),*this; } | ||
| 45 | int write(const char* s,int n) { return apps(sref(s,n)); } | ||
| 46 | }; | ||
| 47 | |||
| 48 | inline mstream& operator<<(mstream& out,int i) { return out.appi(i),out; } | ||
| 49 | inline mstream& operator<<(mstream& out,long l) { return out.appi(l),out; } | ||
| 50 | inline mstream& operator<<(mstream& out,unsigned u) { return out.appi(u),out; } | ||
| 51 | inline mstream& operator<<(mstream& out,unsigned long ul) { return out.appi(ul),out; } | ||
| 52 | inline mstream& operator<<(mstream& out,double f) { return out.appf(f),out; } | ||
| 53 | |||
| 54 | inline mstream& operator<<(mstream& out,const sref& s) { return out.apps(s),out; } | ||
| 55 | inline mstream& operator<<(mstream& out,const char* s) { return out.apps(sref(s)),out; } | ||
| 56 | |||
| 57 | /////////////////////////////////////////////////////////// | ||
| 58 | |||
| 59 | struct cvt_stream : public mstream { | ||
| 60 | int appi(int i); | ||
| 61 | int appf(double f); | ||
| 62 | }; | ||
| 63 | |||
| 64 | /////////////////////////////////////////////////////////// | ||
| 65 | |||
| 66 | struct fp_stream : public cvt_stream { | ||
| 67 | FILE* fp; | ||
| 68 | |||
| 69 | int appc(char c); | ||
| 70 | int apps(const sref& s); | ||
| 71 | |||
| 72 | fp_stream(FILE* f) : fp(f) {} | ||
| 73 | |||
| 74 | int flush() { return fflush(fp); } | ||
| 75 | }; | ||
| 76 | |||
| 77 | /////////////////////////////////////////////////////////// | ||
| 78 | |||
| 79 | struct fd_stream : public cvt_stream { | ||
| 80 | int fd; | ||
| 81 | |||
| 82 | int appc(char c); | ||
| 83 | int apps(const sref& s); | ||
| 84 | |||
| 85 | fd_stream(int f) : fd(f) {} | ||
| 86 | }; | ||
| 87 | |||
| 88 | /////////////////////////////////////////////////////////// | ||
| 89 | |||
| 90 | struct pipe_stream : public cvt_stream { | ||
| 91 | int fd,hup; | ||
| 92 | |||
| 93 | int appc(char c); | ||
| 94 | int apps(const sref& s); | ||
| 95 | |||
| 96 | pipe_stream(int f) : fd(f),hup(0) {} | ||
| 97 | |||
| 98 | int mwrite(const char* s,int n); | ||
| 99 | }; | ||
| 100 | |||
| 101 | /////////////////////////////////////////////////////////// | ||
| 102 | |||
| 103 | #endif | ||
