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_IO_H
00034 #define _MOOT_IO_H
00035
00036 #include <stdio.h>
00037 #include <stdarg.h>
00038 #include <string.h>
00039 #include <errno.h>
00040 #include <assert.h>
00041
00042 #include <string>
00043
00045 namespace mootio {
00046
00047
00048
00049
00051 typedef int ByteCount;
00052
00053
00054
00055
00076 class mstream {
00077 public:
00078 std::string name;
00079
00080 public:
00081
00083
00084
00085 mstream(const std::string &myname="") : name(myname) {};
00086
00088 virtual ~mstream(void) {};
00090
00091
00093
00094
00095 virtual bool valid(void) { return false; };
00096
00098 inline operator bool(void) { return valid(); };
00099
00101 virtual bool eof(void) { return true; };
00102
00104 virtual std::string errmsg(void) {
00105 return std::string(valid() ? "" : "Invalid stream");
00106 };
00108
00109
00111
00112
00113 virtual bool reopen(void) { return true; };
00114
00116 virtual bool close(void) { return true; };
00118 };
00119
00120
00121
00122
00123
00125 class mistream : virtual public mstream {
00126 public:
00127
00128
00129
00131
00132
00133 mistream(void) {};
00134
00136 virtual ~mistream(void) {};
00138
00139
00140
00141
00143
00144
00146 virtual int getc(void) { return EOF; }
00147
00150 virtual ByteCount read(char *buf, size_t n) {
00151 size_t nread = 0;
00152 int c;
00153 for (c = this->getc(); nread < n && c != EOF; nread++, c = this->getc()) {
00154 *buf++ = c;
00155 }
00156 return (ByteCount)nread;
00157 };
00158
00164 virtual ByteCount getline(std::string &s, const std::string &delim="\n\r") {
00165 ByteCount nread = 0;
00166 int c;
00167 s.clear();
00168 for (c = this->getc(); c != EOF; nread++, c = this->getc()) {
00169 s.push_back((char)c);
00170 if (delim.find(c) != delim.npos) break;
00171 }
00172 return nread ? nread : EOF;
00173 };
00175 };
00176
00177
00178
00179
00180
00182 class mostream : virtual public mstream {
00183 public:
00184
00185
00186
00188
00189
00190 mostream(void) {};
00191
00193 virtual ~mostream(void) {};
00195
00196
00197
00198
00200
00201
00203 virtual bool flush(void) { return false; };
00204
00206 virtual bool write(const char *buf, size_t n) { return false; };
00207
00209 virtual bool putc(unsigned char c) { return false; };
00210
00212 virtual bool puts(const char *s) { return false; };
00214 virtual bool puts(const std::string &s) { return false; };
00215
00220 virtual bool vprintf(const char *fmt, va_list &ap) {
00221 char *obuf = NULL;
00222 size_t len = 0;
00223 len = vasprintf(&obuf, fmt, ap);
00224 bool rc = len >= 0 && write(obuf, len);
00225 if (obuf) free(obuf);
00226 return rc;
00227 };
00232 inline bool printf(const char *fmt, ...) {
00233 va_list ap;
00234 va_start(ap,fmt);
00235 bool rc = this->vprintf(fmt,ap);
00236 va_end(ap);
00237 return rc;
00238 };
00240 };
00241
00242 };
00243
00244
00245 #endif //_MOOT_IO_H