OSDN Git Service

fix 2.4.
[meshio/meshio.git] / include / binary.h
1 #ifndef MESH_IO_BINARY_H_INCLUDED
2 #define MESH_IO_BINARY_H_INCLUDED
3
4 #include <fstream>
5 #include <vector>
6 #include <assert.h>
7 #include <stdarg.h>
8
9 namespace meshio {
10 namespace binary {
11
12 /**
13  * \83f\81[\83^\93Ç\82Ý\8d\9e\82Ý\83C\83\93\83^\81[\83t\83F\81[\83X
14  */
15 class IReader
16 {
17 public:
18         virtual ~IReader(){}
19         virtual unsigned int read(char *buf, unsigned int size)=0;
20         virtual unsigned int getPos()const=0;
21         virtual bool isEnd()const=0;
22
23         template<typename T>
24                 bool get(T &t)
25                 {
26                         if(read(reinterpret_cast<char*>(&t), sizeof(t))){
27                                 return true;
28                         }
29                         else{
30                                 return false;
31                         }
32                 }
33         char getChar()
34         {
35                 char byte;
36                 return get(byte) ? byte : 0;
37         }
38         std::string getString(unsigned int length, bool isTrim=false)
39         {
40                 std::vector<char> buf(length);
41                 read(&buf[0], buf.size());
42
43                 std::vector<char>::iterator it;
44                 if(isTrim){
45                         it=buf.begin();
46                         for(; it!=buf.end(); ++it){
47                                 if(*it=='\0'){
48                                         break;
49                                 }
50                         }
51                 }
52                 else{
53                         it=buf.end();
54                 }
55                 return std::string(buf.begin(), it);
56         }
57         unsigned char getUchar()
58         {
59                 unsigned char value;
60                 return get(value) ? value : 0;
61         }
62         unsigned short getUshort()
63         {
64                 unsigned short value;
65                 return get(value) ? value : 0;
66         }
67         unsigned int getUint()
68         {
69                 unsigned int value;
70                 return get(value) ? value : 0;
71         }
72 };
73
74 /**
75  * \83t\83@\83C\83\8b\82©\82ç\82Ì\93Ç\82Ý\8d\9e\82Ý
76  */
77 class FileReader : public IReader
78 {
79         std::ifstream io_;
80         unsigned int pos_;
81         bool eof_;
82
83 public:
84         FileReader(const char *path);
85         virtual ~FileReader();
86         virtual unsigned int read(char *buf, unsigned int size);
87         virtual unsigned int getPos()const;
88         virtual bool isEnd()const;
89 };
90
91 /**
92  * \83\81\83\82\83\8a\82©\82ç\82Ì\93Ç\82Ý\8d\9e\82Ý
93  */
94 class MemoryReader : public IReader
95 {
96         const char *buf_;
97         unsigned int size_;
98         unsigned int pos_;
99
100 public:
101         MemoryReader(const char *buf, unsigned int size);
102         virtual ~MemoryReader();
103         virtual unsigned int read(char *buf, unsigned int size);
104         virtual unsigned int getPos()const;
105         virtual bool isEnd()const;
106 };
107
108  void readAll(const char *path, std::vector<char> &all);
109 #ifdef _WIN32
110  void readAll(const wchar_t *path, std::vector<char> &all);
111 #endif
112
113 /**
114  * \83f\81[\83^\8f\91\82«\8d\9e\82Ý\83C\83\93\83^\81[\83t\83F\81[\83X
115  */
116 class IWriter
117 {
118 public:
119         virtual ~IWriter(){}
120         virtual void write(const char *buf, unsigned int size)=0;
121         void printLn(const char *fmt, ...)
122         {
123                 char buf[1024];
124                 va_list list;
125                 va_start(list, fmt);
126                 vsprintf(buf, fmt, list);
127                 write(buf, strlen(buf));
128                 write("\r\n", 2);
129                 va_end(list);
130         }
131         template<typename T>
132                 void writeArray(const T *array, size_t element_count)
133                 {
134                         write(
135                                         reinterpret_cast<const char*>(array), 
136                                         sizeof(T)*element_count);
137                 }
138         template<typename T>
139                 void writeValue(T value)
140                 {
141                         writeArray(&value, 1);
142                 }
143 };
144
145 class FileWriter : public IWriter
146 {
147         FILE *io_;
148
149 public:
150         FileWriter(const char *path);
151         FileWriter(const wchar_t *path);
152         virtual ~FileWriter();
153         virtual void write(const char *buf, unsigned int size);
154 };
155
156 } // namespace binary
157 } // namespace meshio
158
159 #endif // MESH_IO_BINARY_H_INCLUDED