//! \~\ingroup IO
//! \~\{
//! \~\file pifile.h
//! \~\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 Локальный файл.
//! \~\details
//! \~english PIFile provides interface for local file operations including reading, writing, seeking, and file management.
//! \~russian PIFile предоставляет интерфейс для операций с локальными файлами, включая чтение, запись, позиционирование и управление
//! файлами.
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". Opens 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 Информация о локальном файле или директории.
//! \~\details
//! \~english Contains detailed information about a file or directory including path, size, permissions, timestamps, and flags.
//! \~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 Разрешения локального файла или директории.
//! \~\details
//! \~english Contains read, write, and execute permissions for user, group, and others.
//! \~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 Conversion 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 Возвращает имя, без директории.
//! \~\sa dir()
PIString name() const;
//! \~english Returns base name, without directory and extension.
//! \~russian Возвращает базовое имя, без директории и расширения.
//! \~\sa name(), extension()
PIString baseName() const;
//! \~english Returns extension.
//! \~russian Возвращает расширение.
//! \~\sa baseName()
PIString extension() const;
//! \~english Returns directory.
//! \~russian Возвращает директорию.
//! \~\sa name()
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 Opens temporary file with open mode "mode".
//! \~russian Открывает временный файл с режимом открытия "mode".
bool openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Immediately writes all buffered data to disk.
//! \~russian Немедленно записывает все буферизированные данные на диск.
void flush() override;
//! \~english Moves read/write position to "position".
//! \~russian Перемещает позицию чтения/записи на "position".
void seek(llong position);
//! \~english Moves read/write position to the beginning of the file.
//! \~russian Перемещает позицию чтения/записи на начало файла.
void seekToBegin();
//! \~english Moves read/write position to the end of the file.
//! \~russian Перемещает позицию чтения/записи на конец файла.
void seekToEnd();
//! \~english Moves read/write position to text line number "line" beginning.
//! \~russian Перемещает позицию чтения/записи на начало текстовой строки номер "line".
void seekToLine(llong line);
//! \~english Skips "bytes" bytes (moves position next to "bytes" bytes).
//! \~russian Пропускает "bytes" байт (перемещает позицию на "bytes" байт вперёд).
void skip(llong bytes);
//! \~english Reads one byte and returns it.
//! \~russian Читает один байт и возвращает его.
char readChar();
//! \~english Reads all file content to "data" and returns read bytes count. Position left unchanged.
//! \~russian Читает всё содержимое файла в "data" и возвращает количество прочитанных байт. Позиция остаётся неизменной.
llong readAll(void * data);
//! \~english Reads all file content to byte array and returns it. Position left unchanged.
//! \~russian Читает всё содержимое файла и возвращает его как массив байтов. Позиция остаётся неизменной.
PIByteArray readAll();
//! \~english Sets file path to "path" and reopens file if needed.
//! \~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 Writes size and content of "v" (serialization).
//! \~russian Пишет в файл размер и содержимое "v" (сериализация).
PIFile & put(const PIByteArray & v);
//! \~english Reads size of byte array and its content (deserialization).
//! \~russian Читает из файла размер байтового массива и его содержимое (десериализация).
PIByteArray get();
//! \handlers
//! \{
//! \fn void clear()
//! \~english Clears content of file.
//! \~russian Очищает содержимое файла.
EVENT_HANDLER(void, clear);
//! \fn void remove()
//! \~english Removes file.
//! \~russian Удаляет файл.
EVENT_HANDLER(void, remove);
//! \fn void resize(llong new_size)
//! \~english Resizes file to "new_size" with null-byte fill.
//! \~russian Изменяет размер файла на "new_size" с заполнением нулевыми байтами.
EVENT_HANDLER1(void, resize, llong, new_size) { resize(new_size, 0); }
//! \fn void resize(llong new_size, uchar fill)
//! \~english Resizes file to "new_size" with "fill" fill.
//! \~russian Изменяет размер файла на "new_size" с заполнением байтами "fill".
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 Removes file with path "path" and returns if remove successful.
//! \~russian Удаляет файл с путём "path" и возвращает успешность операции.
static bool remove(const PIString & path);
//! \~english Renames file with path "from" to path "to" and returns if rename successful.
//! \~russian Переименовывает файл с путём "from" на "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 Applies "info" parameters to file or dir with path "path".
//! \~russian Применяет параметры "info" к файлу или директории с путём "path".
static bool applyFileInfo(const PIString & path, const FileInfo & info);
//! \~english Applies "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); }
//! \~english Reads all file content at path "path" to byte array and returns it.
//! \~russian Читает всё содержимое файла по пути "path" и возвращает его как массив байтов.
static PIByteArray readAll(const PIString & path);
//! \~english Clears file at path "path" and writes "data", returns written bytes.
//! \~russian Очищает файл по пути "path", пишет туда "data" и возвращает количество записанных байт.
static int writeAll(const PIString & path, const PIByteArray & data);
//! \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 Оператор сохранения.
//! \~\sa BINARY_STREAM_READ
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 Оператор извлечения.
//! \~\sa BINARY_STREAM_WRITE
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