Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4973 right-hear 1
#ifndef _IOSTREAM_INCLUDED
2
#define _IOSTREAM_INCLUDED
3
#include 
4
#include 
5
namespace std
6
{
7
  class istream
8
  {
9
  public:
10
    istream() {}
11
    virtual ~istream() {}
12
    virtual istream& operator>>(string& s)=0;
13
    virtual operator bool() const=0;
14
    virtual int getc()=0;
15
    virtual unsigned tellg()=0;
16
    virtual istream& seekg(unsigned off,ios::seek_dir dir)=0;
17
    virtual istream& getline(char* s, unsigned n, char delim)=0;
18
  };
19
  class ifstream : public istream
20
  {
21
    FILE* f;
22
  public:
23
    void open(const char* s, /*ios::open_mode*/int mode=ios::in)
24
    {f=fopen(s,(mode&ios::binary)?"rb":"r");}
25
    ifstream():f(NULL){}
26
    ifstream(const char* s, /*ios::open_mode*/int mode=ios::in)
27
    {open(s,mode);}
28
    ~ifstream() {if (f) fclose(f);}
29
    bool fail() const {return f==NULL?true:ferror(f)||feof(f);}
30
    operator bool() const {return !fail();}
31
    bool eof() const {return f==NULL?false:feof(f);}
32
    virtual istream& seekg(unsigned off,ios::seek_dir dir)
33
    {fseek(f,off,dir);return *this;}
34
    ifstream& read(char* s, unsigned n)
35
    {if (fread(s,1,n,f)!=n) f->_flag|=0200;return *this;}
36
    virtual unsigned tellg() {return ftell(f);}
37
    ifstream& getline(char* s, unsigned n)
38
    {fgets(s,n,f);unsigned m=strlen(s);
39
     if(m&&s[m-1]=='\n')s[m-1]=0;else f->_flag|=0200;return *this;}
40
    istream& operator>>(string& s)
41
    {char res[512];fscanf(f,"%s",res);s=res;return *this;}
42
    virtual istream& getline(char* s, unsigned n, char delim)
43
    {
44
      int c;
45
      unsigned pos=0;
46
      for (;pos
47
      {
48
        c=fgetc(f);
49
        if (c==EOF) break;
50
        if (c==delim) break;
51
        s[pos++]=c;
52
      }
53
      s[pos]=0;
54
      return *this;
55
    }
56
    int getc(void)
57
    { return fgetc(f); }
58
  };
59
  inline istream& getline(istream& is, string& str, char delim='\n')
60
  {
61
    str.erase();
62
    while (is)
63
    {
64
      int c = is.getc();
65
      if (c == EOF || c == delim)
66
        break;
67
      str += c;
68
    }
69
    return is;
70
  }
71
  class istringstream : public istream
72
  {
73
    string s;
74
    unsigned pos;
75
  public:
76
    istringstream(const char* str):s(str),pos(0) {}
77
    istringstream(const string& str):s(str.c_str()),pos(0) {}
78
    istringstream& operator>>(int& n)
79
    {int x;sscanf(s.c_str()+pos,"%d%n",&n,&x);pos+=x;return *this;}
80
    istream& operator>>(string& s)
81
    {int x;char res[512];sscanf(s.c_str()+pos,"%s%n",res,&x);pos+=x;
82
    s=res;return *this;}
83
    virtual operator bool() const {return s.c_str()[pos]!=0;}
84
    virtual int getc()
85
    {
86
      char c=s.c_str()[pos];
87
      if (c==0)
88
        return EOF;
89
      pos++;
90
      return c;
91
    }
92
    virtual unsigned tellg() {return pos;}
93
    virtual istream& seekg(unsigned off,ios::seek_dir dir)
94
    {
95
      unsigned sz=s.size();
96
      switch (dir)
97
      {
98
        case ios::beg: pos=off;break;
99
        case ios::cur: pos+=off;break;
100
        case ios::end: pos=sz-off;break;
101
      }
102
      if (pos>sz) pos=sz;
103
      return *this;
104
    }
105
    virtual istream& getline(char* str, unsigned n, char delim)
106
    {
107
      int c;
108
      unsigned p=0;
109
      for (;p
110
      {
111
        c=s[pos++];
112
        if (c==0) {pos--;break;}
113
        if (c==delim) break;
114
        str[p++]=c;
115
      }
116
      str[p]=0;
117
      return *this;
118
    }
119
  };
120
}
121
#endif