00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _MOOT_CXXIO_H
00034 #define _MOOT_CXXIO_H
00035
00036 #include <iostream>
00037 #include <fstream>
00038
00039 #include <mootIO.h>
00040
00041 #include <stdarg.h>
00042 #include <string.h>
00043 #include <assert.h>
00044
00045 namespace mootio {
00046 using namespace std;
00047
00048
00049
00050
00051 template <class _StreamClass>
00052 class mcxxstream : virtual public mstream {
00053 public:
00054 _StreamClass *streamp;
00055 public:
00056
00057
00058
00060
00061
00062 mcxxstream(_StreamClass *streamptr=NULL) : streamp(streamptr) {};
00063
00065 mcxxstream(_StreamClass &stream) : streamp(&stream) {};
00066
00068 virtual ~mcxxstream(void) {};
00070
00071
00072
00073
00075
00076
00077 virtual bool valid(void) { return streamp && streamp->good(); };
00078
00080 virtual bool eof(void) { return !streamp || streamp->eof(); };
00082 };
00083
00084
00085
00086
00087
00089 class micxxstream
00090 : public mcxxstream<std::istream>,
00091 public mistream
00092 {
00093 public:
00094
00095
00096
00098
00099
00100 micxxstream(std::istream *streamptr=NULL)
00101 : mcxxstream<std::istream>(streamptr)
00102 {};
00103
00105 micxxstream(std::istream &istream)
00106 : mcxxstream<std::istream>(istream)
00107 {};
00108
00110 ~micxxstream(void) {};
00112
00113
00114
00115
00117
00118
00120 virtual ByteCount read(char *buf, size_t n) {
00121 return streamp ? streamp->read(buf,n).gcount() : 0;
00122 };
00123
00125 virtual int getc(void) {
00126 return streamp ? streamp->get() : EOF;
00127 };
00129 };
00130
00131
00132
00133
00134
00136 class mocxxstream
00137 : public mcxxstream<std::ostream>,
00138 public mostream
00139 {
00140 public:
00141 char *_printf_buffer;
00142 ByteCount _printf_buflen;
00143
00144 public:
00145
00146
00147
00149
00150
00151 mocxxstream(std::ostream *ostreamptr)
00152 : mcxxstream<std::ostream>(ostreamptr),
00153 _printf_buffer(NULL),
00154 _printf_buflen(0)
00155 {};
00156
00158 mocxxstream(std::ostream &ostream)
00159 : mcxxstream<std::ostream>(ostream),
00160 _printf_buffer(NULL),
00161 _printf_buflen(0)
00162 {};
00163
00165 virtual ~mocxxstream(void)
00166 {
00167 if (_printf_buffer) free(_printf_buffer);
00168 };
00170
00171
00172
00173
00175
00176
00177 virtual bool mflush(void) { return streamp->flush().good(); };
00178
00180 virtual bool write(const char *buf, size_t n) {
00181 return streamp ? streamp->write(buf,n).good() : false;
00182 };
00183
00185 virtual bool putc(unsigned char c) {
00186 return streamp ? streamp->put(c).good() : false;
00187 };
00188
00190 virtual bool puts(const char *s) {
00191 return streamp ? ((*streamp) << s).good() : false;
00192 };
00194 virtual bool puts(const std::string &s) {
00195 return streamp ? ((*streamp) << s).good() : false;
00196 };
00197
00200 virtual bool vprintf(const char *fmt, va_list &ap) {
00201 if (!streamp) return false;
00202 ByteCount nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
00203 if (nchars >= _printf_buflen) {
00204
00205 _printf_buffer = (char *)realloc(_printf_buffer, nchars+1);
00206 assert(_printf_buffer != NULL);
00207 _printf_buflen = nchars+1;
00208
00209 nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
00210 }
00211
00212 return streamp->write(_printf_buffer, nchars).good();
00213 };
00215 };
00216
00217 };
00218
00219
00220 #endif //_MOOT_CXXIO_H