Added workaround for broken ifstream::putback() on MSVC
[hypercube:hypercube.git] / IO / io.h
1 #ifndef IO_H_
2 #define IO_H_
3
4 #include <string>
5 #include <sstream>
6 #include <fstream>
7 #include "CORE/graph.h"
8 #include "CORE/line.h"
9 #include "encoding.h"
10
11
12 class IO
13 {
14 public:
15         enum Error {Ok, OpenError, ReadError, WriteError, FormatError};
16         static std::ostringstream ioerr;
17 };
18
19
20 class InputProvider : public IO
21 {
22 public:
23         virtual Error readGraph(Graph *graph, const char *fileName,
24           Encoding *encoding) = 0;
25
26 protected:
27         static bool stringCaseCmp(const std::wstring &s1, const std::wstring &s2);
28 };
29
30 class OutputProvider : public IO
31 {
32 public:
33         virtual Error writeGraph(Graph *graph, const char *fileName) = 0;
34         virtual const char* type() = 0;
35         virtual const char* description() = 0;
36
37 protected:
38         struct Arrow {
39                 CoordinatesF p[3];
40         };
41
42         static CoordinatesF edgeTextPosition(const LineF &line, float size,
43           const CoordinatesF &textBox);
44         static CoordinatesF vertexTextPosition(const CoordinatesF &point,
45           float size);
46         static Arrow arrow(LineF &line, float size);
47
48         static void stringReplace(std::wstring &source, const std::wstring &find,
49           const std::wstring &replace);
50 };
51
52 class lexstream : public std::wifstream
53 {
54 public:
55         lexstream() : _unget(false) {}
56         int get() {
57                 if (_unget)
58                         _unget = false;
59                 else
60                         _last = std::wifstream::get();
61                 return std::wifstream::good() ? _last : -1;
62         }
63         void unget() {_unget = true;}
64
65 private:
66         int _last;
67         bool _unget;
68 };
69
70 #endif /* IO_H_ */