#ifndef _IOSTREAM_INCLUDED #define _IOSTREAM_INCLUDED #include #include namespace std { class istream { public: istream() {} virtual ~istream() {} virtual istream& operator>>(string& s)=0; virtual operator bool() const=0; virtual int getc()=0; virtual unsigned tellg()=0; virtual istream& seekg(unsigned off,ios::seek_dir dir)=0; virtual istream& getline(char* s, unsigned n, char delim)=0; }; class ifstream : public istream { FILE* f; public: void open(const char* s, /*ios::open_mode*/int mode=ios::in) {f=fopen(s,(mode&ios::binary)?"rb":"r");} ifstream():f(NULL){} ifstream(const char* s, /*ios::open_mode*/int mode=ios::in) {open(s,mode);} ~ifstream() {if (f) fclose(f);} bool fail() const {return f==NULL?true:ferror(f)||feof(f);} operator bool() const {return !fail();} bool eof() const {return f==NULL?false:feof(f);} virtual istream& seekg(unsigned off,ios::seek_dir dir) {fseek(f,off,dir);return *this;} ifstream& read(char* s, unsigned n) {if (fread(s,1,n,f)!=n) f->_flag|=0200;return *this;} virtual unsigned tellg() {return ftell(f);} ifstream& getline(char* s, unsigned n) {fgets(s,n,f);unsigned m=strlen(s); if(m&&s[m-1]=='\n')s[m-1]=0;else f->_flag|=0200;return *this;} istream& operator>>(string& s) {char res[512];fscanf(f,"%s",res);s=res;return *this;} virtual istream& getline(char* s, unsigned n, char delim) { int c; unsigned pos=0; for (;pos>(int& n) {int x;sscanf(s.c_str()+pos,"%d%n",&n,&x);pos+=x;return *this;} istream& operator>>(string& s) {int x;char res[512];sscanf(s.c_str()+pos,"%s%n",res,&x);pos+=x; s=res;return *this;} virtual operator bool() const {return s.c_str()[pos]!=0;} virtual int getc() { char c=s.c_str()[pos]; if (c==0) return EOF; pos++; return c; } virtual unsigned tellg() {return pos;} virtual istream& seekg(unsigned off,ios::seek_dir dir) { unsigned sz=s.size(); switch (dir) { case ios::beg: pos=off;break; case ios::cur: pos+=off;break; case ios::end: pos=sz-off;break; } if (pos>sz) pos=sz; return *this; } virtual istream& getline(char* str, unsigned n, char delim) { int c; unsigned p=0; for (;p