| 1 |
#ifndef CPP_UTILITIES_ARCHIVE_H |
| 2 |
#define CPP_UTILITIES_ARCHIVE_H |
| 3 |
|
| 4 |
#include "../chrono/datetime.h" |
| 5 |
#include "../global.h" |
| 6 |
|
| 7 |
#include <functional> |
| 8 |
#include <map> |
| 9 |
#include <stdexcept> |
| 10 |
#include <string> |
| 11 |
#include <string_view> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
namespace CppUtilities { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class CPP_UTILITIES_EXPORT ArchiveException : public std::runtime_error { |
| 22 |
public: |
| 23 |
explicit ArchiveException() noexcept; |
| 24 |
explicit ArchiveException(std::string_view what) noexcept; |
| 25 |
~ArchiveException() override; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
inline ArchiveException::ArchiveException() noexcept |
| 32 |
: std::runtime_error("unable to convert") |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
inline ArchiveException::ArchiveException(std::string_view what) noexcept |
| 40 |
: std::runtime_error(what.data()) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
enum class ArchiveFileType { Regular, Link }; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct CPP_UTILITIES_EXPORT ArchiveFile { |
| 61 |
explicit ArchiveFile( |
| 62 |
std::string &&name, std::string &&content, ArchiveFileType type, CppUtilities::DateTime creationTime, CppUtilities::DateTime modificationTime) |
| 63 |
: name(name) |
| 64 |
, content(content) |
| 65 |
, creationTime(creationTime) |
| 66 |
, modificationTime(modificationTime) |
| 67 |
, type(type) |
| 68 |
{ |
| 69 |
} |
| 70 |
std::string name; |
| 71 |
std::string content; |
| 72 |
CppUtilities::DateTime creationTime; |
| 73 |
CppUtilities::DateTime modificationTime; |
| 74 |
ArchiveFileType type; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
using FileMap = std::map<std::string, std::vector<ArchiveFile>>; |
| 79 |
|
| 80 |
using FilePredicate = std::function<bool(const char *, const char *, mode_t)>; |
| 81 |
|
| 82 |
using DirectoryHandler = std::function<bool(std::string_view path)>; |
| 83 |
|
| 84 |
using FileHandler = std::function<bool(std::string_view path, ArchiveFile &&file)>; |
| 85 |
|
| 86 |
CPP_UTILITIES_EXPORT FileMap extractFiles(std::string_view archivePath, const FilePredicate &isFileRelevant = FilePredicate()); |
| 87 |
CPP_UTILITIES_EXPORT void walkThroughArchive(std::string_view archivePath, const FilePredicate &isFileRelevant = FilePredicate(), |
| 88 |
FileHandler &&fileHandler = FileHandler(), DirectoryHandler &&directoryHandler = DirectoryHandler()); |
| 89 |
CPP_UTILITIES_EXPORT FileMap extractFilesFromBuffer( |
| 90 |
std::string_view archiveData, std::string_view archiveName, const FilePredicate &isFileRelevant = FilePredicate()); |
| 91 |
CPP_UTILITIES_EXPORT void walkThroughArchiveFromBuffer(std::string_view archiveData, std::string_view archiveName, |
| 92 |
const FilePredicate &isFileRelevant = FilePredicate(), FileHandler &&fileHandler = FileHandler(), |
| 93 |
DirectoryHandler &&directoryHandler = DirectoryHandler()); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
#endif |