X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=src%2Fbinary.cpp;h=33257bd54f136171048b3fdc2d83f54efc174482;hb=984c94cd3b0a966c03bf1ff470797f7337ad5fee;hp=5a0f4ba3aba458ca0f93ace0c5629623201ff9d4;hpb=5b1d24fcf5aec37d73d79c1e7bd4dc0c7414d7f4;p=meshio%2Fmeshio.git diff --git a/src/binary.cpp b/src/binary.cpp index 5a0f4ba..33257bd 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -1,121 +1,137 @@ #include "binary.h" namespace meshio { -namespace binary { - -/////////////////////////////////////////////////////////////////////////////// -// FileReader -/////////////////////////////////////////////////////////////////////////////// -FileReader::FileReader(const char *path) -: io_(path, std::ios::binary), pos_(0), eof_(false) -{ -} - -FileReader::~FileReader() -{ -} - -unsigned int FileReader::read(char *buf, unsigned int size) -{ - if(size==0){ - return 0; - } - io_.read(buf, size); - size=io_.gcount(); - if(size==0){ - eof_=true; - } - pos_+=size; - return size; -} - -unsigned int FileReader::getPos()const -{ - return pos_; -} - -bool FileReader::isEnd()const -{ - return eof_; -} - -/////////////////////////////////////////////////////////////////////////////// -// MemoryReader -/////////////////////////////////////////////////////////////////////////////// -MemoryReader::MemoryReader(const char *buf, unsigned int size) -: buf_(buf), size_(size), pos_(0) -{ -} - -MemoryReader::~MemoryReader() -{ -} - -unsigned int MemoryReader::read(char *buf, unsigned int size) -{ - if(pos_+size>=size_){ - size=size_-pos_; - } - std::copy(&buf_[pos_], &buf_[pos_+size], buf); - pos_+=size; - return size; -} - -unsigned int MemoryReader::getPos()const -{ - return pos_; -} - -bool MemoryReader::isEnd()const -{ - return pos_>=size_; -} - -/////////////////////////////////////////////////////////////////////////////// -static void readALL_(FILE *fp, std::vector &buf) -{ - int iRet = fseek(fp, 0L, SEEK_END); - if(iRet!=0){ - return; - } - - fpos_t pos; - iRet = fgetpos(fp, &pos); - if(iRet != 0){ - return; - } - - iRet = fseek(fp, 0L, SEEK_SET); - if(iRet != 0){ - return; - } - - buf.resize((long)pos); - fread(&buf[0], (long)pos, 1, fp); -} - -void readAll(const char *path, std::vector &buf) -{ - /* - std::ifstream io(path, std::ios::binary); - if(!io){ - return; - } - io.seekg(0, std::fstream::end); - unsigned int eofPos = io.tellg(); - io.clear(); - io.seekg(0, std::fstream::beg); - unsigned int begPos = io.tellg(); - unsigned int size = eofPos - begPos; - io.read(&buf[0], buf.size()); - */ - - FILE* fp = fopen(path, "rb"); - if(fp){ - readALL_(fp, buf); - fclose(fp); - } -} - -} // namespace binary + namespace binary { + + // FileReader + FileReader::FileReader(const char *path) + : io_(path, std::ios::binary), pos_(0), eof_(false) + { + } + + FileReader::~FileReader() + { + } + + unsigned int FileReader::read(char *buf, unsigned int size) + { + if(size==0){ + return 0; + } + io_.read(buf, size); + size_t read_size=static_cast(io_.gcount()); + if(read_size==0){ + eof_=true; + } + pos_+=read_size; + return read_size; + } + + unsigned int FileReader::getPos()const + { + return pos_; + } + + bool FileReader::isEnd()const + { + return eof_; + } + + // MemoryReader + MemoryReader::MemoryReader(const char *buf, unsigned int size) + : buf_(buf), size_(size), pos_(0) + { + } + + MemoryReader::~MemoryReader() + { + } + + unsigned int MemoryReader::read(char *buf, unsigned int size) + { + if(pos_+size>=size_){ + size=size_-pos_; + } + std::copy(&buf_[pos_], &buf_[pos_+size], buf); + pos_+=size; + return size; + } + + unsigned int MemoryReader::getPos()const + { + return pos_; + } + + bool MemoryReader::isEnd()const + { + return pos_>=size_; + } + + // readALL + static void readALL_(FILE *fp, std::vector &buf) + { + int iRet = fseek(fp, 0L, SEEK_END); + if(iRet!=0){ + return; + } + + long pos=ftell(fp); + if(pos == -1){ + return; + } + + iRet = fseek(fp, 0L, SEEK_SET); + if(iRet != 0){ + return; + } + + buf.resize(pos); + iRet=fread(&buf[0], pos, 1, fp); + } + + void readAll(const char *path, std::vector &buf) + { + FILE* fp = fopen(path, "rb"); + if(fp){ + readALL_(fp, buf); + fclose(fp); + } + } + +#ifdef _MSC_VER + void readAll(const wchar_t *path, std::vector &buf) + { + FILE* fp = _wfopen(path, L"rb"); + if(fp){ + readALL_(fp, buf); + fclose(fp); + } + } +#endif + + // FileWriter + FileWriter::FileWriter(const char *path) + { + io_=fopen(path, "wb"); + } + +#if _MSC_VER + FileWriter::FileWriter(const wchar_t *path) + { + io_=_wfopen(path, L"wb"); + } +#endif + + FileWriter::~FileWriter() + { + fclose(io_); + } + + void FileWriter::write(const char *buf, unsigned int size) + { + fwrite(buf, size, 1, io_); + } + + + } // namespace binary } // namespace meshio