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_CIO_H
00034 #define _MOOT_CIO_H
00035
00036 #include <mootIO.h>
00037 #include <stdio.h>
00038
00039 namespace mootio {
00040
00041
00042
00043
00045 class mcstream
00046 : public mistream,
00047 public mostream
00048 {
00049 public:
00050 FILE *file;
00051 public:
00052
00053
00054
00056
00057
00058 mcstream(FILE *f=NULL) : file(f) {};
00059
00061 ~mcstream(void) {};
00063
00064
00065
00066
00068
00069
00070 virtual bool valid(void) { return file && !ferror(file); };
00071
00073 virtual bool eof(void) { return !file || feof(file); };
00074
00076 virtual std::string errmsg(void) {
00077 return std::string(file && !ferror(file) ? "" : strerror(errno));
00078 };
00080
00081
00082
00083
00085
00086
00087 virtual bool reopen(void) { return true; };
00088
00090 virtual bool close(void) { file=NULL; return true; };
00092
00093
00094
00095
00097
00098
00100 virtual ByteCount read(char *buf, size_t n) {
00101 return file ? fread(buf, 1, n, file) : 0;
00102 };
00103
00105 virtual int getbyte(void) {
00106 return file ? fgetc(file) : EOF;
00107 };
00109
00110
00111
00112
00114
00115
00116 virtual bool flush(void) { return file && fflush(file) != EOF; };
00117
00119 virtual bool write(const char *buf, size_t n) {
00120 return file ? (fwrite(buf,1,n,file) == n) : false;
00121 };
00122
00124 virtual bool putc(unsigned char c) {
00125 return file ? (fputc(c,file) != EOF) : false;
00126 };
00127
00129 virtual bool puts(const char *s) {
00130 return file ? (fputs(s,file) >= 0) : false;
00131 };
00133 virtual bool puts(const std::string &s) {
00134 return file ? (fwrite(s.data(),1,s.size(),file) == s.size()) : false;
00135 };
00136
00139 virtual bool vprintf(const char *fmt, va_list &ap) {
00140 return file ? (vfprintf(file,fmt,ap) >= 0) : false;
00141 };
00143 };
00144
00145
00146
00147
00148
00154 class mfstream : public mcstream {
00155 public:
00156 std::string mode;
00157 std::string default_mode;
00158 public:
00159
00160
00161
00163
00164
00165 mfstream(void)
00166 : mode("r"),
00167 default_mode("r")
00168 {};
00169
00171 mfstream(const char *filename, const char *open_mode=NULL)
00172 : default_mode("r")
00173 {
00174 open(filename,open_mode);
00175 };
00176
00178 virtual ~mfstream(void) { close(); };
00180
00181
00182
00183
00185
00186
00188 inline bool open(const char *filename, const char *open_mode=NULL) {
00189 name = filename;
00190 if (open_mode) mode = open_mode;
00191 return reopen();
00192 };
00195 inline bool open(const std::string &filename, const std::string &open_mode="") {
00196 mode = open_mode;
00197 name = filename;
00198 return reopen();
00199 };
00200
00202 virtual bool reopen(void) {
00203 close();
00204 if (mode.empty()) mode = default_mode;
00205
00206 if (name == "-") {
00207 if (mode.find('w') != mode.npos) file = stdout;
00208 else file = stdin;
00209 } else {
00210 file = fopen(name.c_str(), mode.c_str());
00211 }
00212 return file && !ferror(file);
00213 };
00214
00216 virtual bool close(void) {
00217 if (!file) return true;
00218 else if (file==stdin || file==stdout || file==stderr) {
00219
00220 file = NULL;
00221 return true;
00222 }
00223 bool rc = fclose(file) == 0;
00224 file = NULL;
00225 return rc;
00226 };
00228
00229
00230
00231
00233
00234
00235 inline void setparams(int level=0, int strategy=0) {};
00237 };
00238
00239
00240
00241
00247 class mifstream : public mfstream {
00248 public:
00249
00250
00251
00253
00254
00255 mifstream(void) {
00256 default_mode = "r";
00257 };
00258
00260 mifstream(const char *filename, const char *mode=NULL)
00261 {
00262 default_mode = "r";
00263 open(filename,mode);
00264 };
00265
00267 mifstream(const std::string &filename, const std::string &mode="")
00268 {
00269 default_mode = "r";
00270 open(filename,mode);
00271 };
00272
00274 virtual ~mifstream(void) { close(); };
00276 };
00277
00278
00279
00280
00286 class mofstream : public mfstream {
00287 public:
00288
00289
00290
00292
00293
00294 mofstream(void) {
00295 default_mode = "w";
00296 };
00297
00299 mofstream(const char *filename, const char *mode=NULL) {
00300 default_mode = "w";
00301 open(filename,mode);
00302 };
00303
00305 mofstream(const std::string &filename, const std::string &mode="") {
00306 default_mode = "w";
00307 open(filename,mode);
00308 };
00309
00311 virtual ~mofstream(void) { close(); };
00313 };
00314
00315
00316
00317
00318
00319 typedef mcstream micstream;
00320 typedef mcstream mocstream;
00321
00322 };
00323
00324
00325 #endif //_MOOT_CIO_H