file.h 3.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef _SPELL_FILE_H_
#define _SPELL_FILE_H_

#ifndef MSG_DEBUG
#include <error.h>
#endif

#include <fstream>

struct file {
    std::fstream m_impl;
    std::string m_path;

    struct error : public std::ios_base::failure {
        error(const std::string& msg) : std::ios_base::failure(msg) {}
    };

    file() : m_impl(), m_path("<undefined>") {}

    file(const std::string& path, std::ios_base::openmode mode)
        : m_impl(path, mode), m_path(path)
    {
        if (!m_impl) {
            throw file::error(MESSAGE("Couldn't open file " << path));
        }
    }

    bool good() const { return m_impl.good(); }
    bool fail() const { return m_impl.fail(); }
    bool eof() const { return m_impl.eof(); }
    bool bad() const { return m_impl.bad(); }

    operator bool () const { return good(); }
    bool operator ! () const { return !good(); }

    operator std::fstream& () { return m_impl; }
    operator std::ostream& () { return m_impl; }

    void
        close() { m_impl.close(); m_path = "<undefined>"; }

    void
        open(const std::string& path, std::ios_base::openmode mode)
        {
            m_path = path;
            m_impl.open(path, mode);
            check_state();
        }

    void
        check_state() const
        {
            if (m_impl.eof()) {
                throw file::error(MESSAGE("Reached EOF while reading file " << m_path));
            }
            if (m_impl.fail()) {
                throw file::error(MESSAGE("Couldn't I/O file " << m_path));
            }
            if (m_impl.bad()) {
                throw file::error(MESSAGE("Corrupted stream. Can't I/O file " << m_path));
            }
        }

    template <typename ARG>
        friend
        file& operator << (file& f, ARG&& arg)
        {
            if (f.m_impl) {
                f.m_impl << arg;
                f.check_state();
            }
            return f;
        }

    template <typename ARG>
        friend
        file& operator >> (file& f, ARG&& arg)
        {
            if (f.m_impl) {
                f.m_impl >> arg;
                f.check_state();
            }
            return f;
        }

    friend
        file& operator << (file& f, std::ostream& (&func) (std::ostream&))
        {
            if (f.m_impl) {
                f.m_impl << func;
                f.check_state();
            }
            return f;
        }

    file&
        write(const char* s, std::streamsize n)
        {
            m_impl.write(s, n);
            check_state();
            return *this;
        }

   file&
       read(char* s, std::streamsize n)
       {
           m_impl.read(s, n);
           check_state();
           return *this;
       }

   file&
       put(char c)
       {
           m_impl.put(c);
           check_state();
           return *this;
       }

   int
       get()
       {
           int ret = m_impl.get();
           check_state();
           return ret;
       }

   template <typename Arg0, typename... Args>
       file&
       get(Arg0& arg, Args&... args)
       {
           m_impl.get(arg, args...);
           check_state();
           return *this;
       }
};


struct ifile : public file {
    ifile(const std::string& path)
        : file(path, std::fstream::in)
    {}

    using file::get;
};


struct ofile : public file {
    ofile(const std::string& path, std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc)
        : file(path, mode)
    {}
};



#endif