/*! \file pifile.h
* \ingroup IO
* \~\brief
* \~english Local file
* \~russian Локальный файл
*/
/*
PIP - Platform Independent Primitives
File
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
#ifndef PIFILE_H
#define PIFILE_H
#include "piiodevice.h"
#include "pipropertystorage.h"
//! \ingroup IO
//! \~\brief
//! \~english Local file.
//! \~russian Локальный файл.
class PIP_EXPORT PIFile: public PIIODevice {
PIIODEVICE(PIFile, "file");
public:
//! \~english Constructs file with empty path
//! \~russian Создает файл с пустым путём
explicit PIFile();
//! \~english Constructs a file with path "path" and open mode "mode". Open if "path" is not empty
//! \~russian Создает файл с путём "path" и режимом открытия "mode". Открывает если "path" не пустой
explicit PIFile(const PIString & path, DeviceMode mode = ReadWrite);
virtual ~PIFile();
//! \ingroup IO
//! \~\brief
//! \~english Local file or directory information.
//! \~russian Информация о локальном файле или директории.
struct PIP_EXPORT FileInfo {
//! \~english Constructs %FileInfo with path "path_". No information gathered
//! \~russian Создает %FileInfo с путём "path_". Информация не собирается
FileInfo(const PIString & path_ = PIString()) {
path = path_;
size = 0;
id_group = id_user = 0;
}
//! \~english Type flags
//! \~russian Флаги типа
enum Flag {
File /*! \~english File \~russian Файл */ = 0x01,
Dir /*! \~english Directory \~russian Директория */ = 0x02,
Dot /*! \~english '.', current directory \~russian '.', текущая директория */ = 0x04,
DotDot /*! \~english '..', parent directory \~russian '..', родительская директория */ = 0x08,
SymbolicLink /*! \~english Symbolic link \~russian Символическая ссылка */ = 0x10,
Hidden /*! \~english Hidden \~russian Скрытый */ = 0x20
};
typedef PIFlags Flags;
//! \ingroup IO
//! \~\brief
//! \~english Local file or directory permissions.
//! \~russian Разрешения локального файла или директории.
struct PIP_EXPORT Permissions {
Permissions(uchar r = 0): raw(r) {}
Permissions(bool r, bool w, bool e): raw(0) {
read = r;
write = w;
exec = e;
}
//! \~english Returns as string (from "---" to "rwx")
//! \~russian Возвращает как строку (от "---" до "rwx")
PIString toString() const { return PIString(read ? "r" : "-") + PIString(write ? "w" : "-") + PIString(exec ? "x" : "-"); }
//! \~english Convertion to \c int
//! \~russian Преобразование в \c int
operator int() const { return raw; }
Permissions & operator=(int v) {
raw = v;
return *this;
}
union {
uchar raw;
struct {
uchar read : 1;
uchar write: 1;
uchar exec : 1;
};
};
};
//! \~english Path
//! \~russian Путь
PIString path;
//! \~english File size
//! \~russian Размер файла
llong size;
//! \~english Last access time
//! \~russian Время последнего доступа
PIDateTime time_access;
//! \~english Last modification time
//! \~russian Время последнего изменения
PIDateTime time_modification;
//! \~english Flags
//! \~russian Флаги
Flags flags;
//! \~english User ID
//! \~russian ID пользователя
uint id_user;
//! \~english Group ID
//! \~russian ID группы
uint id_group;
//! \~english Permissions for user
//! \~russian Разрешения для пользователя
Permissions perm_user;
//! \~english Permissions for group
//! \~russian Разрешения для группы
Permissions perm_group;
//! \~english Permissions for other
//! \~russian Разрешения для остальных
Permissions perm_other;
//! \~english Returns name, without directory
//! \~russian Возвращает имя, без директории
PIString name() const;
//! \~english Returns base name, without directory and extension
//! \~russian Возвращает базовое имя, без директории и расширения
PIString baseName() const;
//! \~english Returns extension
//! \~russian Возвращает расширение
PIString extension() const;
//! \~english Returns directory
//! \~russian Возвращает директорию
PIString dir() const;
//! \~english Returns if it`s directory
//! \~russian Возвращает директория ли это
bool isDir() const { return flags[Dir]; }
//! \~english Returns if it`s file
//! \~russian Возвращает файл ли это
bool isFile() const { return flags[File]; }
//! \~english Returns if it`s symbolic link
//! \~russian Возвращает символическая ссылка ли это
bool isSymbolicLink() const { return flags[SymbolicLink]; }
//! \~english Returns if Hidden flag set
//! \~russian Возвращает установлен ли флаг Hidden
bool isHidden() const { return flags[Hidden]; }
//! \~english Returns if path is absolute
//! \~russian Возвращает абсолютный ли путь
bool isAbsolute() const;
};
//! \~english Open temporary file with open mode "mode"
//! \~russian Открывает временный файл с режимом открытия "mode"
bool openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Immediate write all buffered data to disk
//! \~russian Немедленно записывает все буферизированные данные на диск
void flush() override;
//! \~english Move read/write position to "position"
//! \~russian Перемещает позицию чтения/записи на "position"
void seek(llong position);
//! \~english Move read/write position to the begin of the file
//! \~russian Перемещает позицию чтения/записи на начало файла
void seekToBegin();
//! \~english Move read/write position to the end of the file
//! \~russian Перемещает позицию чтения/записи на конец файла
void seekToEnd();
//! \~english Move read/write position to text line number "line" beginning
//! \~russian Перемещает позицию чтения/записи на начало текстовой строки номер "line"
void seekToLine(llong line);
//! \~english Skip "bytes" bytes (move position next to "bytes" bytes)
//! \~russian Пропускает "bytes" байт (перемещает позицию на "bytes" байт вперёд)
void skip(llong bytes);
//! \~english Read one char and return it
//! \~russian Читает один байт и возвращает его
char readChar();
//! \~english Read all file content to "data" and return readed bytes count. Position leaved unchanged
//! \~russian Читает всё содержимое файла в "data" и возвращает количество прочитанных байт. Позиция остаётся неизменной
llong readAll(void * data);
//! \~english Read all file content to byte array and return it. Position leaved unchanged
//! \~russian Читает всё содержимое файла и возвращает его как массив байтов. Позиция остаётся неизменной
PIByteArray readAll(bool forceRead = false);
//! \~english Set file path to "path" and reopen file if need
//! \~russian Устанавливает путь файла на "path" и переоткрывает его при необходимости
void setPath(const PIString & path);
//! \~english Returns file size in bytes
//! \~russian Возвращает размер файла в байтах
llong size() const;
ssize_t bytesAvailable() const override { return size() - pos(); }
//! \~english Returns read/write position
//! \~russian Возвращает позицию чтения/записи
llong pos() const;
//! \~english Returns if position is at the end of file
//! \~russian Возвращает достигнут ли конец файла
bool isEnd() const;
//! \~english Returns if file is empty
//! \~russian Возвращает пустой ли файл
bool isEmpty() const { return (size() <= 0); }
//! \~english Returns \a PIFile::FileInfo of current file
//! \~russian Возвращает \a PIFile::FileInfo текущего файла
FileInfo fileInfo() const { return fileInfo(path()); }
//! \~english Write size and content of "v" (serialize)
//! \~russian Пишет в файл размер и содержимое "v" (сериализация)
PIFile & put(const PIByteArray & v);
//! \~english Read size of byte array and it content (deserialize)
//! \~russian Читает из файла размер байтового массива и его содержимое (десериализация)
PIByteArray get();
EVENT_HANDLER(void, clear);
EVENT_HANDLER(void, remove);
EVENT_HANDLER1(void, resize, llong, new_size) { resize(new_size, 0); }
EVENT_HANDLER2(void, resize, llong, new_size, uchar, fill);
//! \~english Returns if file with path "path" exists
//! \~russian Возвращает существует ли файл с путём "path"
static bool isExists(const PIString & path);
//! \~english Remove file with path "path" and returns if remove successful
//! \~russian Удаляет файл с путём "path" и возвращает успешность операции
static bool remove(const PIString & path);
//! \~english Rename file with path "from" to path "to" and returns if rename successful
//! \~russian Переименовывает файл с путём "path" на "to" и возвращает успешность операции
static bool rename(const PIString & from, const PIString & to);
//! \~english Returns \a PIFile::FileInfo of file or dir with path "path"
//! \~russian Возвращает \a PIFile::FileInfo файла или директории с путём "path"
static FileInfo fileInfo(const PIString & path);
//! \~english Apply "info" parameters to file or dir with path "path"
//! \~russian Применяет параметры "info" к файлу или директории с путём "path"
static bool applyFileInfo(const PIString & path, const FileInfo & info);
//! \~english Apply "info" parameters to file or dir with path "info".path
//! \~russian Применяет параметры "info" к файлу или директории с путём "info".path
static bool applyFileInfo(const FileInfo & info) { return applyFileInfo(info.path, info); }
//! \handlers
//! \{
//! \fn void clear()
//! \~english Clear content of file
//! \~russian Очищает содержимое файла
//! \fn void resize(llong new_size)
//! \~english Resize file to "new_size" with null-byte fill
//! \~russian Изменяет размер файла на "new_size" с заполнением нулевыми байтами
//! \fn void resize(llong new_size, uchar fill)
//! \~english Resize file to "new_size" with "fill" fill
//! \~russian Изменяет размер файла на "new_size" с заполнением байтами "fill"
//! \fn void remove()
//! \~english Remove file
//! \~russian Удаляет файл
//! \}
//! \ioparams
//! \{
#ifdef DOXYGEN
#endif
//! \}
protected:
PIString constructFullPathDevice() const override;
void configureFromFullPathDevice(const PIString & full_path) override;
PIPropertyStorage constructVariantDevice() const override;
void configureFromVariantDevice(const PIPropertyStorage & d) override;
ssize_t readDevice(void * read_to, ssize_t max_size) override;
ssize_t writeDevice(const void * data, ssize_t max_size) override;
bool openDevice() override;
bool closeDevice() override;
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Sequential | PIIODevice::Reliable; }
private:
PIString strType(const PIIODevice::DeviceMode type);
PRIVATE_DECLARATION(PIP_EXPORT)
int fdi = -1;
llong _size = -1;
PIString prec_str;
};
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator<<(PICout s, const PIFile::FileInfo & v) {
s.saveAndSetControls(0);
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.restoreControls();
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIFile::FileInfo) {
s << v.path << v.size << v.time_access << v.time_modification << v.flags << v.id_user << v.id_group << v.perm_user.raw
<< v.perm_group.raw << v.perm_other.raw;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ(PIFile::FileInfo) {
s >> v.path >> v.size >> v.time_access >> v.time_modification >> v.flags >> v.id_user >> v.id_group >> v.perm_user.raw >>
v.perm_group.raw >> v.perm_other.raw;
return s;
}
#endif // PIFILE_H