libxcks  0.1.0.1
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ckcalculator.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_CKCALCULATOR_HPP_71DAD55B_09B4_426A_920E_27BCDBE81D7B
25 #define INC_CKCALCULATOR_HPP_71DAD55B_09B4_426A_920E_27BCDBE81D7B
26 
27 //---------------------------------------------------------------------------
28 #include <istream>
29 #include <fstream>
30 #include <filesystem>
31 
32 #include "libxcks/ckvalue.hpp"
33 //---------------------------------------------------------------------------
34 
35 
36 namespace libxcks
37 {
38 //###########################################################################
39 // A checksums calculator helper
40 //###########################################################################
41 
45 class LIBXCKS_SO_EXPORT ChecksumCalculatorProgress
46 {
47  public:
50 
53 
56 
58  virtual ~ChecksumCalculatorProgress() = default;
59 
68  virtual void update(size_t read, bool& cancelled) = 0;
69 };
70 //---------------------------------------------------------------------------
71 
72 
79 {
80  public:
83 
86 
89 
92 
101  void update(size_t read, bool& cancelled) override {}
102 };
103 //---------------------------------------------------------------------------
104 
105 
112 class LIBXCKS_SO_EXPORT ChecksumCalculator
113 {
114  public:
116  enum class State
117  {
118  Ok = 0,
119  NoValidAlgoId,
120  OutOfMemory,
121  ReadError,
122  FileNotFound,
123  CantOpenFile,
124  CancelledByUser
125  };
126 
127  protected:
129  static constexpr size_t default_buffer_size = 4096u;
130  static constexpr size_t min_buffer_size = 1u;
131  static constexpr size_t max_buffer_size = 1048576u;
136  static constexpr unsigned int default_thread_number = 2u; // conservative value.
138  static constexpr unsigned int max_thread_number = 32u;
139 
141  size_t bufferSize;
142  unsigned int nbThreads;
143 
144  public:
146  static constexpr unsigned int automatic_thread_number = 0u;
147 
157  ChecksumCalculator(ChecksumCalculatorProgress& progressHandler = defaultChecksumCalculatorProgress,
158  const unsigned int newNbThreads = automatic_thread_number,
159  const size_t newBufferSize = default_buffer_size);
160 
163 
166 
168  virtual ~ChecksumCalculator() = default;
169 
170  // Accessors
176  size_t getBufferSize() const;
177 
188  size_t setBufferSize(const size_t newBufSize);
189 
195  unsigned int getNbThreads() const;
196 
203  unsigned int setNbThreads(const unsigned int newNbThreads);
204 
210  ChecksumCalculatorProgress& getChecksumProgress() const;
211 
212  // Operations
230  State calculate(ChecksumValue& sumValue, const ChecksumAlgoId id, std::istream& is);
231 
257  State calculate(ArrayChecksumValue& sumValues, const ArrayChecksumAlgoId& ids, std::istream& is);
258 };
259 //---------------------------------------------------------------------------
260 
261 
268 class LIBXCKS_SO_EXPORT ChecksumFileCalculator : public ChecksumCalculator
269 {
270  protected:
271  static constexpr size_t default_file_buffer_size = 0xFFFFu;
272 
273  public:
283  ChecksumFileCalculator(ChecksumCalculatorProgress& progressHandler = defaultChecksumCalculatorProgress,
284  const unsigned int newNbThreads = automatic_thread_number,
285  const size_t newBufferSize = default_file_buffer_size);
286 
287 
290 
293 
295  virtual ~ChecksumFileCalculator() = default;
296 
297  // Operations
317  State calculate(ChecksumValue& sumValue, const ChecksumAlgoId id,
318  const std::filesystem::path& filepath);
319 
347  State calculate(ArrayChecksumValue& sumValues, const ArrayChecksumAlgoId& ids,
348  const std::filesystem::path& filepath);
349 };
350 //---------------------------------------------------------------------------
351 
352 } // namespace libxcks
353 //---------------------------------------------------------------------------
354 
355 #endif // INC_CKCALCULATOR_HPP_71DAD55B_09B4_426A_920E_27BCDBE81D7B
356 
Stores the value of a checksum.
std::vector< ChecksumValue > ArrayChecksumValue
Array of values of checksum.
Definition: ckvalue.hpp:113
Handles the progression of the process of computing of a checksum.
ChecksumCalculatorProgress(const ChecksumCalculatorProgress &)=delete
Deleted copy constructor.
virtual void update(size_t read, bool &cancelled)=0
Updates the progress of the computing of a checksum.
ChecksumCalculatorProgress()=default
Default constructor.
ChecksumCalculatorProgress & operator=(const ChecksumCalculatorProgress &)=delete
Deleted assignment operator.
virtual ~ChecksumCalculatorProgress()=default
Destructor.
Calculates a checksum.
ChecksumCalculator & operator=(const ChecksumCalculator &)=delete
Deleted assignment operator.
static DefaultChecksumCalculatorProgress defaultChecksumCalculatorProgress
Default progress checksum calculator instance.
ChecksumCalculatorProgress & progress
The progress handler used to show the progression.
virtual ~ChecksumCalculator()=default
Destructor.
State
States that can be returned by the calculate or the check method.
size_t bufferSize
The size of the buffer to use for reading the input stream.
unsigned int nbThreads
Number of threads to compute checksums.
ChecksumCalculator(const ChecksumCalculator &)=delete
Deleted copy constructor.
Computes a checksum from a file.
virtual ~ChecksumFileCalculator()=default
Destructor.
ChecksumFileCalculator(const ChecksumFileCalculator &)=delete
Deleted copy constructor.
ChecksumFileCalculator & operator=(const ChecksumFileCalculator &)=delete
Deleted assignment operator.
Stores the value of a checksum.
Definition: ckvalue.hpp:45
Handles the progression of the process of computing of a checksum.
DefaultChecksumCalculatorProgress()=default
Default constructor.
DefaultChecksumCalculatorProgress(const DefaultChecksumCalculatorProgress &)=delete
Deleted copy constructor.
virtual ~DefaultChecksumCalculatorProgress()=default
Destructor.
void update(size_t read, bool &cancelled) override
Updates the progress of the computing of a checksum.
DefaultChecksumCalculatorProgress & operator=(const DefaultChecksumCalculatorProgress &)=delete
Deleted assignment operator.
std::vector< ChecksumAlgoId > ArrayChecksumAlgoId
Array of ids of algorithms of checksums.
Definition: types.hpp:92
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65