X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=src%2Fbinary.cpp;h=229a98eb5bfe31e3903eba79e623de38ae9ca718;hb=af0f7ea2da9ace619ff51edef564fe4669582074;hp=7e6ec6c9bc29eb8b3bda38b88f1d5b255fadec78;hpb=1c78f8bbdfe52be741185762123fba2c169d738a;p=meshio%2Fmeshio.git diff --git a/src/binary.cpp b/src/binary.cpp index 7e6ec6c..229a98e 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -72,21 +72,73 @@ bool MemoryReader::isEnd()const } /////////////////////////////////////////////////////////////////////////////// -void readAll(const char *path, std::vector &buf) +// readALL +/////////////////////////////////////////////////////////////////////////////// +static void readALL_(FILE *fp, std::vector &buf) { - std::ifstream io(path, std::ios::binary); - if(!io){ + int iRet = fseek(fp, 0L, SEEK_END); + if(iRet!=0){ + return; + } + + fpos_t pos; + iRet = fgetpos(fp, &pos); + if(iRet != 0){ 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; - buf.resize(size); - io.read(&buf[0], buf.size()); + + 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) +{ + FILE* fp = fopen(path, "rb"); + if(fp){ + readALL_(fp, buf); + fclose(fp); + } +} + +#ifdef _WIN32 +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"); +} + +FileWriter::FileWriter(const wchar_t *path) +{ + io_=_wfopen(path, L"wb"); +} + +FileWriter::~FileWriter() +{ + fclose(io_); +} + +void FileWriter::write(const char *buf, unsigned int size) +{ + fwrite(buf, size, 1, io_); +} + + } // namespace binary } // namespace meshio