/*! \file pifile.h * \brief Local file */ /* PIP - Platform Independent Primitives File Copyright (C) 2015 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef PIFILE_H #define PIFILE_H #include "piiodevice.h" class PIP_EXPORT PIFile: public PIIODevice { PIIODEVICE(PIFile) public: //! Constructs an empty file PIFile(); struct FileInfo { FileInfo() {size = 0; id_group = id_user = 0;} enum Flag { File = 0x01, Dir = 0x02, Dot = 0x04, DotDot = 0x08, SymbolicLink = 0x10, Hidden = 0x20 }; typedef PIFlags Flags; struct Permissions { Permissions(uchar r = 0): raw(r) {} Permissions(bool r, bool w, bool e): raw(0) {read = w; write = w; exec = e;} PIString toString() const {return PIString(read ? "r" : "-") + PIString(write ? "w" : "-") + PIString(exec ? "x" : "-");} union { uchar raw; struct { uchar read : 1; uchar write: 1; uchar exec : 1; }; }; }; PIString path; llong size; PIDateTime time_access; PIDateTime time_modification; Flags flags; uint id_user; uint id_group; Permissions perm_user; Permissions perm_group; Permissions perm_other; PIString name() const; PIString baseName() const; PIString extension() const; PIString dir() const; bool isDir() const {return flags[Dir];} bool isFile() const {return flags[File];} bool isSymbolicLink() const {return flags[SymbolicLink];} bool isHidden() const {return flags[Hidden];} }; //! Constructs a file with path "path" nad open mode "type" PIFile(const PIString & path, DeviceMode mode = ReadWrite); ~PIFile() {close();} //PIFile & operator =(const PIFile & f) {path_ = f.path_; type_ = f.type_; return *this;} //! Immediate write all buffered data to disk void flush() {if (opened_) fflush(fd);} //! Move read/write position to "position" void seek(llong position) {if (!opened_) return; fseek(fd, position, SEEK_SET); clearerr(fd);} //! Move read/write position to the begin of the file void seekToBegin() {if (!opened_) return; fseek(fd, 0, SEEK_SET); clearerr(fd);} //! Move read/write position to the end of the file void seekToEnd() {if (!opened_) return; fseek(fd, 0, SEEK_END); clearerr(fd);} //! Move read/write position to text line number "line" void seekToLine(llong line) {if (!opened_) return; seekToBegin(); piForTimes (line) readLine(); clearerr(fd);} // line 0 - begin of file //void fill(char c) {stream.fill(c);} //! Read one char and return it char readChar() {return (char)fgetc(fd);} //! Read one text line and return it PIString readLine(); //! Read all file content to "data" and return readed bytes count. Position leaved unchanged llong readAll(void * data); //! Read all file content to byte array and return it. Position leaved unchanged PIByteArray readAll(bool forceRead = false); //! Set file path to "path" and reopen file if need void setPath(const PIString & path) {PIIODevice::setPath(path); if (opened_) openDevice();} //! Returns file size llong size() const; //! Returns read/write position llong pos() const {if (!opened_) return -1; return ftell(fd);} //! Returns if position is at the end of file bool isEnd() const {if (!opened_) return true; return (feof(fd) || ferror(fd));} //! Returns if file is empty bool isEmpty() const {return (size() <= 0);} //! Returns FileInfo of current file FileInfo fileInfo() const {return fileInfo(path());} //! Returns float numbers write precision int precision() const {return prec_;} //! Set float numbers write precision to "prec_" digits void setPrecision(int prec) {prec_ = prec; if (prec_ >= 0) prec_str = "." + PIString::fromNumber(prec_); else prec_str = "";} //! Read from file to "read_to" no more than "max_size" and return readed bytes count int read(void * read_to, int max_size) {if (!canRead() || fd == 0) return -1; return fread(read_to, 1, max_size, fd);} //! Write to file "data" with size "max_size" and return written bytes count int write(const void * data, int max_size) {if (!canWrite() || fd == 0) return -1; return fwrite(data, 1, max_size, fd);} PIFile & writeToBinLog(ushort id, const void * data, int size) {if (!isWriteable() || fd == 0) return *this; writeBinary(id).writeBinary((ushort)size); write(data, size); flush(); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const char v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const short v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const int v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const long v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const llong v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const uchar v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const ushort v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const uint v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const ulong v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const ullong v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const float v) {write(&v, sizeof(v)); return *this;} //! Write to file binary content of "v" PIFile & writeBinary(const double v) {write(&v, sizeof(v)); return *this;} PIFile & operator =(const PIFile & f) {PIIODevice::setPath(f.path()); mode_ = f.mode_; return *this;} //! Write to file text representation of "v" PIFile & operator <<(const char v) {if (canWrite() && fd != 0) write(&v, 1); return *this;} //PIFile & operator <<(const string & v) {write(v.c_str(), v.size()); return *this;} //! Write to file string "v" PIFile & operator <<(const PIString & v) {if (canWrite() && fd != 0) write(v.data(), v.lengthAscii()); return *this;} //! Write to file text representation of "v" PIFile & operator <<(const PIByteArray & v) {if (canWrite() && fd != 0) write(v.data(), v.size()); return *this;} //! Write to file text representation of "v" PIFile & operator <<(short v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%hd", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(int v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%d", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(long v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%ld", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(llong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%lld", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(uchar v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%u", int(v)); return *this;} //! Write to file text representation of "v" PIFile & operator <<(ushort v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%hu", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(uint v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%u", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(ulong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%lu", v); return *this;} //! Write to file text representation of "v" PIFile & operator <<(ullong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%llu", v); return *this;} //! Write to file text representation of "v" with precision \a precision() PIFile & operator <<(float v) {if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "f").data(), v); return *this;} //! Write to file text representation of "v" with precision \a precision() PIFile & operator <<(double v) {if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "lf").data(), v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(char & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(short & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(int & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(long & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(llong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(uchar & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(ushort & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(uint & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(ulong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(ullong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(float & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%f", &v); return *this;} //! Read from file text representation of "v" PIFile & operator >>(double & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lf", &v); return *this;} PIString constructFullPath() const; EVENT_HANDLER(void, clear) {close(); fd = fopen(path().data(), "w"); if (fd != 0) fclose(fd); fd = 0; opened_ = false; open();} EVENT_HANDLER0(void, remove) {close(); std::remove(path().data());} EVENT_HANDLER1(void, resize, llong, new_size) {resize(new_size, 0);} EVENT_HANDLER2(void, resize, llong, new_size, uchar, fill); //! Returns not opened temporary file with open mode "mode" static PIFile openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite) {return PIFile(PIString(tmpnam(0)), mode);} //! Returns if file with path "path" does exists static bool isExists(const PIString & path); //! Remove file with path "path" and returns if remove was successful static bool remove(const PIString & path) {return std::remove(path.data()) == 0;} //! Rename file with path "from" to path "to" and returns if rename was successful static bool rename(const PIString & from, const PIString & to) {return ::rename(from.data(), to.data()) == 0;} //! Returns FileInfo of file or dir with path "path" static FileInfo fileInfo(const PIString & path); //! \handlers //! \{ //! \fn void clear() //! \brief Clear content of file //! \fn void resize(llong new_size) //! \brief Resize file to "new_size" with "fill" filling //! \fn void resize(llong new_size, uchar fill) //! \brief Resize file to "new_size" with "fill" filling //! \fn void remove() //! \brief Remove file //! \} //! \ioparams //! \{ #ifdef DOXYGEN #endif //! \} protected: PIString fullPathPrefix() const {return "file";} void configureFromFullPath(const PIString & full_path); bool openDevice(); bool closeDevice(); private: PIString strType(const PIIODevice::DeviceMode type); FILE * fd; int ret, prec_; PIString prec_str; }; inline PICout operator <<(PICout s, const PIFile::FileInfo & v) { s.setControl(0, true); s << "FileInfo(\"" << v.path << "\", " << PIString::readableSize(v.size) << ", " << v.perm_user.toString() << " " << v.perm_group.toString() << " " << v.perm_other.toString() << ", " << v.time_access.toString() << ", " << v.time_modification.toString() << ", 0x" << PICoutManipulators::Hex << v.flags << ")"; s.restoreControl(); return s; } #endif // PIFILE_H