From b0799b6e4627ca7ad483c49e04f17bd40c8feae0 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 23 May 2013 23:12:32 +0900 Subject: Introduce IOFile abstract class to refactor BufferedFile and GZipFile IOFile provides the same interface of old BufferedFile. It provides extension points as pure virtual functions. Those functions are implemented by 2 concrete subclasses: BufferedFile and GZipFile. --- src/IOFile.h | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/IOFile.h (limited to 'src/IOFile.h') diff --git a/src/IOFile.h b/src/IOFile.h new file mode 100644 index 00000000..12666c2a --- /dev/null +++ b/src/IOFile.h @@ -0,0 +1,103 @@ +/* */ +#ifndef D_IO_FILE_H +#define D_IO_FILE_H + +#include "OutputFile.h" + +#include +#include + +namespace aria2 { + +// This is a wrapper base class intended to provide +// fopen/fclose/fread/fwrite/fgets functionality. +class IOFile:public OutputFile { +private: + typedef void (IOFile::*unspecified_bool_type)() const; + void goodState() const {} +public: + IOFile() {} + virtual ~IOFile() {} + // Returns true if file is opened and ferror 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); + virtual size_t write(const char* str); + // 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); + // Reads one line and returns it. The last '\n' is removed. + std::string getLine(); + // wrapper for fclose + int close(); + // wrapper for fflush + int flush(); + // Return true if file is opened && feof(fp_) != 0. Otherwise + // returns false. + bool eof(); + // Returns true if file supports ANSI color escape codes. + bool supportsColor(); + // Convenient method. Read data to end of file and write them into + // given stream. Returns written size. + size_t transfer(std::ostream& out); + int vprintf(const char* format, va_list va); + // Mode for reading + static const char READ[]; + // Mode for writing + static const char WRITE[]; + // Mode for append + static const char APPEND[]; +protected: + virtual size_t onRead(void* ptr, size_t count) = 0; + virtual size_t onWrite(const void* ptr, size_t count) = 0; + virtual char* onGets(char* s, int size) = 0; + virtual int onVprintf(const char* format, va_list va) = 0; + virtual int onFlush() = 0; + virtual int onClose() = 0; + virtual bool onSupportsColor() = 0; + + virtual bool isError() const = 0; + virtual bool isEOF() const = 0; + virtual bool isOpen() const = 0; +}; + +} // namespace aria2 + +#endif // D_IO_FILE_H -- cgit