OSDN Git Service

pmd_import sphere map.
[meshio/meshio.git] / src / binary.cpp
1 #include "binary.h"
2
3 namespace meshio {
4 namespace binary {
5
6 ///////////////////////////////////////////////////////////////////////////////
7 // FileReader
8 ///////////////////////////////////////////////////////////////////////////////
9 FileReader::FileReader(const char *path)
10 : io_(path, std::ios::binary), pos_(0), eof_(false)
11 {
12 }
13
14 FileReader::~FileReader()
15 {
16 }
17
18 unsigned int FileReader::read(char *buf, unsigned int size)
19 {
20         if(size==0){
21                 return 0;
22         }
23         io_.read(buf, size);
24         size=io_.gcount();
25         if(size==0){
26                 eof_=true;
27         }
28         pos_+=size;
29         return size;
30 }
31
32 unsigned int FileReader::getPos()const
33 {
34         return pos_;
35 }
36
37 bool FileReader::isEnd()const
38 {
39         return eof_;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // MemoryReader
44 ///////////////////////////////////////////////////////////////////////////////
45 MemoryReader::MemoryReader(const char *buf, unsigned int size)
46 : buf_(buf), size_(size), pos_(0)
47 {
48 }
49
50 MemoryReader::~MemoryReader()
51 {
52 }
53
54 unsigned int MemoryReader::read(char *buf, unsigned int size)
55 {
56         if(pos_+size>=size_){
57                 size=size_-pos_;
58         }
59         std::copy(&buf_[pos_], &buf_[pos_+size], buf);
60         pos_+=size;
61         return size;
62 }
63
64 unsigned int MemoryReader::getPos()const
65 {
66         return pos_;
67 }
68
69 bool MemoryReader::isEnd()const
70 {
71         return pos_>=size_;
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // readALL
76 ///////////////////////////////////////////////////////////////////////////////
77 static void readALL_(FILE *fp, std::vector<char> &buf)
78 {
79         int iRet = fseek(fp, 0L, SEEK_END);
80         if(iRet!=0){
81                 return;
82         }
83
84         fpos_t pos;
85         iRet = fgetpos(fp, &pos);
86         if(iRet != 0){
87                 return;
88         }
89
90         iRet = fseek(fp, 0L, SEEK_SET);
91         if(iRet != 0){
92                 return;
93         }
94
95         buf.resize((long)pos);
96         fread(&buf[0], (long)pos, 1, fp);
97 }
98
99 void readAll(const char *path, std::vector<char> &buf)
100 {
101         FILE* fp = fopen(path, "rb");
102         if(fp){
103                 readALL_(fp, buf);
104                 fclose(fp);
105         }
106 }
107
108 #ifdef _WIN32
109 void readAll(const wchar_t *path, std::vector<char> &buf)
110 {
111         FILE* fp = _wfopen(path, L"rb");
112         if(fp){
113                 readALL_(fp, buf);
114                 fclose(fp);
115         }
116 }
117 #endif
118
119 ///////////////////////////////////////////////////////////////////////////////
120 // FileWriter
121 ///////////////////////////////////////////////////////////////////////////////
122 FileWriter::FileWriter(const char *path)
123 {
124         io_=fopen(path, "wb");
125 }
126
127 FileWriter::FileWriter(const wchar_t *path)
128 {
129     io_=_wfopen(path, L"wb");
130 }
131
132 FileWriter::~FileWriter()
133 {
134         fclose(io_);
135 }
136
137 void FileWriter::write(const char *buf, unsigned int size)
138 {
139         fwrite(buf, size, 1, io_);
140 }
141
142
143 } // namespace binary
144 } // namespace meshio