libxcks  0.1.0.1
xcksfilereader_1.hpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef INC_XCKSFILEREADER_1_HPP_1B941C5D_4F8F_4379_9279_18E35D5DD8EB
25 #define INC_XCKSFILEREADER_1_HPP_1B941C5D_4F8F_4379_9279_18E35D5DD8EB
26 
27 //---------------------------------------------------------------------------
28 #include <set>
29 #include <stack>
30 #include <vector>
31 #include <cstdint>
32 #include <ctime>
33 
34 #if defined(_MSC_VER)
35 #include <BaseTsd.h>
36 #endif
37 
38 #include "libxcks/handlers.hpp"
39 // #include "libxcks/types.hpp" // included in "libxcks/handlers.hpp"
40 #include "libxcks/ckvalue.hpp"
41 #include "xmlparser.hpp"
42 //---------------------------------------------------------------------------
43 
44 
45 namespace libxcks
46 {
47 #if defined(_MSC_VER)
48 typedef SSIZE_T ssize_t;
49 #endif
50 
55 {
56  protected:
58  enum class XMLTags : int16_t
59  {
60  BAD_TAG = INT16_MIN,
61  SEQ_BEGIN = INT16_MIN / 2,
62  CHECKSUMSFILE = 0x001,
63  GENERATED = 0x100,
64  ALGORITHMS = 0x300,
65  ALGORITHM = 0x201,
66  LOCALFILES = 0x500,
67  PARENTDIRECTORY = 0x510,
68  DIRECTORY = 0x520,
69  FILE = 0x530,
70  INFORMATION = 0x531,
71  CHECKSUM = 0x532
72  };
73 
74  static const std::string parentDir; // Parent directory.
75 
77  const std::filesystem::path baseDirectory;
78  const std::vector<std::filesystem::path> baseDirectoryDirs;
80  // Position in the checksums' file.
81  std::stack<XMLTags> readTags;
83  // Algos for the checksums file.
86  // Current directory
88  std::stack<StringSet> namesInDirectories;
89  // Information on current file.
90  std::string fileName;
92  uintmax_t fileLength;
96 
97  public:
99  XCKS1FileHandler() = delete;
100 
103 
106 
107  // Constructor.
108  XCKS1FileHandler(XCKSReaderHandler& handler, const std::filesystem::path& baseDir);
109 
110  // Destructor.
111  virtual ~XCKS1FileHandler();
112 
113  protected:
114  // Gets a XML tag identifier from a string.
115  XMLTags stringToXMLTags(const std::string& str);
116 
117  // Receives notification of the beginning of a "generated" element.
118  inline void startGeneratedElement(const XMLParserAttributes& atts) noexcept(false);
119 
120  // Receives notification of the beginning of an "algorithms" element.
121  inline void startAlgorithmsElement(const XMLParserAttributes& atts) noexcept(false);
122 
123  // Receives notification of the beginning of an "algorithm" element.
124  inline void startAlgorithmElement(const XMLParserAttributes& atts) noexcept(false);
125 
126  // Receives notification of the beginning of a "localFiles" element.
127  inline void startLocalFilesElement(const XMLParserAttributes& atts) noexcept(false);
128 
129  // Receives notification of the beginning of a "parentDirectory" element.
130  inline void startParentDirectoryElement(const XMLParserAttributes& atts) noexcept(false);
131 
132  // Receives notification of the beginning of a "directory" element.
133  inline void startDirectoryElement(const XMLParserAttributes& atts) noexcept(false);
134 
135  // Receives notification of the beginning of a "file" element.
136  inline void startFileElement(const XMLParserAttributes& atts) noexcept(false);
137 
138  // Receives notification of the beginning of a "information" element.
139  inline void startInformationElement(const XMLParserAttributes& atts) noexcept(false);
140 
141  // Receives notification of the beginning of a "checksum" element.
142  inline void startChecksumElement(const XMLParserAttributes& atts) noexcept(false);
143 
144  // Receives notification of the end of a "file" element.
145  inline void endFileElement() noexcept(false);
146 
147  public:
148  // Receives notification of the beginning of an element.
149  void startElement(const std::string& name, const XMLParserAttributes& atts) noexcept(false) override;
150 
151  // Receives notification of the end of an element.
152  void endElement(const std::string& name) noexcept(false) override;
153 
154  // Receives notification of character data.
155  void characters(const std::string& chars) noexcept(false) override;
156 
157  // Receive notification of a non-recoverable error.
158  void fatalError(XML_Error errorCode, const std::string& errorMessage, int line, int column) noexcept override;
159 };
160 //---------------------------------------------------------------------------
161 } // namespace libxcks
162 //---------------------------------------------------------------------------
163 
164 #endif // INC_XCKSFILEREADER_1_HPP_1B941C5D_4F8F_4379_9279_18E35D5DD8EB
Stores the value of a checksum.
std::vector< ChecksumValue > ArrayChecksumValue
Array of values of checksum.
Definition: ckvalue.hpp:113
XCKS file version 1 handler.
void startDirectoryElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "directory" element.
XCKS1FileHandler & operator=(const XCKS1FileHandler &)=delete
Deleted copy assignment operator.
virtual ~XCKS1FileHandler()
Destructor.
ArrayChecksumValue readChecksumsValues
Read checksums values.
void fatalError(XML_Error errorCode, const std::string &errorMessage, int line, int column) noexcept override
Receive notification of a non-recoverable error.
XCKS1FileHandler(const XCKS1FileHandler &)=delete
Deleted copy constructor.
ArrayChecksumAlgoId sumsAlgos
Type of algorithms' checksums to read for each file.
void startLocalFilesElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "localFiles" element.
XMLTags tagInChecksumsFileTag
Current tag in "checksumsFile" tag.
void startElement(const std::string &name, const XMLParserAttributes &atts) noexcept(false) override
Receives notification of the beginning of an element.
void endFileElement() noexcept(false)
Receives notification of the end of a "file" element.
static const std::string parentDir
Parent directory on Unix and windows.
void startGeneratedElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "generated" element.
ArrayChecksumAlgoId algosToRead
Remaining algorithms to read for a file.
ssize_t curBaseDirectoryDir
Current directory in the base directory of the checksums' file name.
uintmax_t fileLength
The current file size.
std::string fileName
Name of checksums' file.
time_t lastFileModicationTime
The current last file modification time.
std::stack< XMLTags > readTags
Stack of read tags.
const StringSet possibleSumsAlgosName
Possible algorithms names for a (Z)XCKS file.
void startFileElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "file" element.
std::stack< StringSet > namesInDirectories
Read filenames in directories.
void startAlgorithmElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "algorithm" element.
@ SEQ_BEGIN
Beginning of a sequence.
@ CHECKSUMSFILE
checksumsFile element
@ PARENTDIRECTORY
parentDirectory element
@ INFORMATION
information element
void startChecksumElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "checksum" element.
XCKS1FileHandler()=delete
Deleted default constructor.
ArrayString currentRelativeDirectory
The current relative directory.
void startAlgorithmsElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "algorithms" element.
void characters(const std::string &chars) noexcept(false) override
Receives notification of character data.
XMLTags tagInFileTag
Information tag seen inside a file tag.
XMLTags stringToXMLTags(const std::string &str)
Gets a XML tag identifier from a string.
void startParentDirectoryElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "parentDirectory" element.
const std::filesystem::path baseDirectory
The base directory of the checksums' file.
void startInformationElement(const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of a "information" element.
void endElement(const std::string &name) noexcept(false) override
Receives notification of the end of an element.
const std::vector< std::filesystem::path > baseDirectoryDirs
The base directory of the checksums' file directories.
XCKSReaderHandler & readerHandler
The XCKS file reader handler.
Handler for reading XCKS files.
Definition: handlers.hpp:50
Manages elements' attributes.
Definition: xmlparser.hpp:84
Default XML parser handler.
Definition: xmlparser.hpp:198
Handlers for reading and writing XCKS files.
std::vector< ChecksumAlgoId > ArrayChecksumAlgoId
Array of ids of algorithms of checksums.
Definition: types.hpp:92
std::vector< std::string > ArrayString
Array of strings.
Definition: types.hpp:44
std::set< std::string > StringSet
Set of strings.
Definition: types.hpp:39
A very simple XML Parser.