#ifndef __File_h__ #define __File_h__ #include #include #include "ZString.h" /// /// File /// /// File abstraction class for accessing local file system files. /// namespace coreutils { class File { public: File(ZString fileName, int mode = O_RDONLY, int authority = 0664); virtual ~File(); void setBufferSize(size_t size); int read(); int readLine(); void write(std::string data); void write(coreutils::ZString &data); std::string asString(); coreutils::ZString asZString(); char *buffer; size_t size; bool eof(); /// /// Real, on-disk size of this file right now, queried directly /// from the OS via fstat() - not the "size" member above, which /// is only ever set at open()/read() time and goes stale the /// moment anything else appends to this fd (exactly the case for /// a long-lived, append-mode file such as Log's static logFile). /// size_t currentSize(); /// /// Close this file, shift any existing ".1" .. ".maxBackups-1" /// backups up by one (deleting whatever already sits at /// ".maxBackups"), rename the current file to ".1", then reopen /// a fresh file at the original path/mode/authority. Meant for a /// long-lived, append-mode file that needs a size cap - not /// meaningful for a file opened for reading. /// void rotate(int maxBackups); coreutils::ZString fileName; private: void open(coreutils::ZString fileName, int mode, int authority); int fd; int mode; int authority; coreutils::ZString zstring; bool mEof = false; }; } #endif