From 6cb91cae88d4105793efe833640e34e115a51aa3 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 6 Aug 2011 21:17:36 +0900 Subject: Added fopen/fread/fwrite/fgets wrapper class. --- src/BufferedFile.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/BufferedFile.h (limited to 'src/BufferedFile.h') diff --git a/src/BufferedFile.h b/src/BufferedFile.h new file mode 100644 index 00000000..66acb7ff --- /dev/null +++ b/src/BufferedFile.h @@ -0,0 +1,78 @@ +/* */ +#ifndef D_BUFFERED_FILE_H +#define D_BUFFERED_FILE_H + +#include "common.h" + +#include +#include + +namespace aria2 { + +// This is a wrapper class for fopen/fclose/fread/fwrite/fgets. +class BufferedFile { +private: + typedef void (BufferedFile::*unspecified_bool_type)() const; + void good_state() const {} +public: + BufferedFile(const std::string& filename, const std::string& mode); + ~BufferedFile(); + // Returns true if file is opened and both ferror and feof returns + // 0. Otherwise returns false. + operator unspecified_bool_type() const; + // wrapper for fread. Using 1 for 2nd argument of fread. + size_t read(void* ptr, size_t count); + // wrapper for fwrite. Using 1 for 2nd argument of fwrite. + size_t write(const void* ptr, size_t count); + // wrapper for fgets + char* gets(char* s, int size); + // wrapper for fgets, but trailing '\n' is replaced with '\0'. + char* getsn(char* s, int size); + // wrapper for fclose + int close(); + // Mode for reading + static const std::string READ; + // Mode for writing + static const std::string WRITE; +private: + FILE* fp_; + // true when file has been opened. + bool open_; +}; + +} // namespace aria2 + +#endif // D_BUFFERED_FILE_H -- cgit