OSDN Git Service

implement pmd_import.py.
[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
8 namespace meshio {
9 namespace binary {
10
11 /**
12  * \83f\81[\83^\93Ç\82Ý\8d\9e\82Ý\83C\83\93\83^\81[\83t\83F\81[\83X
13  */
14 class IReader
15 {
16 public:
17         virtual ~IReader(){}
18         virtual unsigned int read(char *buf, unsigned int size)=0;
19         virtual unsigned int getPos()const=0;
20         virtual bool isEnd()const=0;
21
22         template<typename T>
23                 bool get(T &t)
24                 {
25                         if(read(reinterpret_cast<char*>(&t), sizeof(t))){
26                                 return true;
27                         }
28                         else{
29                                 return false;
30                         }
31                 }
32         char getChar()
33         {
34                 char byte;
35                 return get(byte) ? byte : 0;
36         }
37         std::string getString(unsigned int length, bool isTrim=false)
38         {
39                 std::vector<char> buf(length);
40                 read(&buf[0], buf.size());
41
42                 std::vector<char>::iterator it;
43                 if(isTrim){
44                         it=buf.begin();
45                         for(; it!=buf.end(); ++it){
46                                 if(*it=='\0'){
47                                         break;
48                                 }
49                         }
50                 }
51                 else{
52                         it=buf.end();
53                 }
54                 return std::string(buf.begin(), it);
55         }
56         unsigned char getUchar()
57         {
58                 unsigned char value;
59                 return get(value) ? value : 0;
60         }
61         unsigned short getUshort()
62         {
63                 unsigned short value;
64                 return get(value) ? value : 0;
65         }
66         unsigned int getUint()
67         {
68                 unsigned int value;
69                 return get(value) ? value : 0;
70         }
71 };
72
73 /**
74  * \83t\83@\83C\83\8b\82©\82ç\82Ì\93Ç\82Ý\8d\9e\82Ý
75  */
76 class FileReader : public IReader
77 {
78         std::ifstream io_;
79         unsigned int pos_;
80         bool eof_;
81
82 public:
83         FileReader(const char *path);
84         virtual ~FileReader();
85         virtual unsigned int read(char *buf, unsigned int size);
86         virtual unsigned int getPos()const;
87         virtual bool isEnd()const;
88 };
89
90 /**
91  * \83\81\83\82\83\8a\82©\82ç\82Ì\93Ç\82Ý\8d\9e\82Ý
92  */
93 class MemoryReader : public IReader
94 {
95         const char *buf_;
96         unsigned int size_;
97         unsigned int pos_;
98
99 public:
100         MemoryReader(const char *buf, unsigned int size);
101         virtual ~MemoryReader();
102         virtual unsigned int read(char *buf, unsigned int size);
103         virtual unsigned int getPos()const;
104         virtual bool isEnd()const;
105 };
106
107  void readAll(const char *path, std::vector<char> &all);
108
109 } // namespace binary
110 } // namespace meshio
111
112 #endif // MESH_IO_BINARY_H_INCLUDED