OSDN Git Service

5a0f4ba3aba458ca0f93ace0c5629623201ff9d4
[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 static void readALL_(FILE *fp, std::vector<char> &buf)
76 {
77         int iRet = fseek(fp, 0L, SEEK_END);
78         if(iRet!=0){
79                 return;
80         }
81
82         fpos_t pos;
83         iRet = fgetpos(fp, &pos);
84         if(iRet != 0){
85                 return;
86         }
87
88         iRet = fseek(fp, 0L, SEEK_SET);
89         if(iRet != 0){
90                 return;
91         }
92
93         buf.resize((long)pos);
94         fread(&buf[0], (long)pos, 1, fp);
95 }
96
97 void readAll(const char *path, std::vector<char> &buf)
98 {
99         /*
100         std::ifstream io(path, std::ios::binary);
101         if(!io){
102                 return;
103         }
104         io.seekg(0, std::fstream::end);
105         unsigned int eofPos = io.tellg();
106         io.clear();
107         io.seekg(0, std::fstream::beg);
108         unsigned int begPos = io.tellg();
109         unsigned int size = eofPos - begPos;
110         io.read(&buf[0], buf.size());
111         */
112
113         FILE* fp = fopen(path, "rb");
114         if(fp){
115                 readALL_(fp, buf);
116                 fclose(fp);
117         }
118 }
119
120 } // namespace binary
121 } // namespace meshio