C++ Utilities: io/archive.h Source File

C++ Utilities 5.34.1 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
archive.h
Go to the documentation of this file.
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
14namespace CppUtilities {
15
21 class CPP_UTILITIES_EXPORT ArchiveException : public std::runtime_error {
22public:
23 explicit ArchiveException() noexcept;
24 explicit ArchiveException(std::string_view what) noexcept;
25 ~ArchiveException() override;
26};
27
31 inline ArchiveException::ArchiveException() noexcept
32 : std::runtime_error("unable to convert")
33{
34}
35
39 inline ArchiveException::ArchiveException(std::string_view what) noexcept
40 : std::runtime_error(what.data())
41{
42}
43
47 enum class ArchiveFileType { Regular, Link };
48
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
78 using FileMap = std::map<std::string, std::vector<ArchiveFile>>;
80 using FilePredicate = std::function<bool(const char *, const char *, mode_t)>;
82 using DirectoryHandler = std::function<bool(std::string_view path)>;
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} // namespace CppUtilities
96
97#endif // CPP_UTILITIES_ARCHIVE_H
CppUtilities::ArchiveException::ArchiveException
ArchiveException() noexcept
Constructs a new ArchiveException.
Definition archive.h:31
CppUtilities::DateTime
Represents an instant in time, typically expressed as a date and time of day.
Definition datetime.h:55
datetime.h
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::walkThroughArchiveFromBuffer
CPP_UTILITIES_EXPORT void walkThroughArchiveFromBuffer(std::string_view archiveData, std::string_view archiveName, const FilePredicate &isFileRelevant=FilePredicate(), FileHandler &&fileHandler=FileHandler(), DirectoryHandler &&directoryHandler=DirectoryHandler())
Invokes callbacks for files and directories in the specified archive.
Definition archive.cpp:191
CppUtilities::extractFilesFromBuffer
CPP_UTILITIES_EXPORT FileMap extractFilesFromBuffer(std::string_view archiveData, std::string_view archiveName, const FilePredicate &isFileRelevant=FilePredicate())
Extracts the specified archive.
Definition archive.cpp:216
CppUtilities::walkThroughArchive
CPP_UTILITIES_EXPORT void walkThroughArchive(std::string_view archivePath, const FilePredicate &isFileRelevant=FilePredicate(), FileHandler &&fileHandler=FileHandler(), DirectoryHandler &&directoryHandler=DirectoryHandler())
Invokes callbacks for files and directories in the specified archive.
Definition archive.cpp:226
CppUtilities::ArchiveFileType
ArchiveFileType
The ArchiveFileType enum specifies the type of a file within an archive.
Definition archive.h:47
CppUtilities::ArchiveFileType::Link
@ Link
Definition archive.h:47
CppUtilities::ArchiveFileType::Regular
@ Regular
Definition archive.h:47
CppUtilities::FilePredicate
std::function< bool(const char *, const char *, mode_t)> FilePredicate
A function that is invoked for each file within an archive. If it returns true, the file is considere...
Definition archive.h:80
CppUtilities::extractFiles
CPP_UTILITIES_EXPORT FileMap extractFiles(std::string_view archivePath, const FilePredicate &isFileRelevant=FilePredicate())
Extracts the specified archive.
Definition archive.cpp:258
CppUtilities::DirectoryHandler
std::function< bool(std::string_view path)> DirectoryHandler
A function that is invoked by the walk-through-functions to return a directory.
Definition archive.h:82
CppUtilities::FileMap
std::map< std::string, std::vector< ArchiveFile > > FileMap
A map of files extracted from an archive. Keys represent directories and values files within those di...
Definition archive.h:78
CppUtilities::FileHandler
std::function< bool(std::string_view path, ArchiveFile &&file)> FileHandler
A function that is invoked by the walk-through-functions to return a file.
Definition archive.h:84
std
STL namespace.
CppUtilities::ArchiveFile
The ArchiveFile class holds data about a file within an archive.
Definition archive.h:60
CppUtilities::ArchiveFile::content
std::string content
Definition archive.h:71
CppUtilities::ArchiveFile::modificationTime
CppUtilities::DateTime modificationTime
Definition archive.h:73
CppUtilities::ArchiveFile::type
ArchiveFileType type
Definition archive.h:74
CppUtilities::ArchiveFile::creationTime
CppUtilities::DateTime creationTime
Definition archive.h:72
CppUtilities::ArchiveFile::ArchiveFile
ArchiveFile(std::string &&name, std::string &&content, ArchiveFileType type, CppUtilities::DateTime creationTime, CppUtilities::DateTime modificationTime)
Definition archive.h:61
CppUtilities::ArchiveFile::name
std::string name
Definition archive.h:70