libxcks  0.1.0.1
ckformatter.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_CKFORMATTER_HPP_443C2F6F_1843_46B8_B365_FC45329314CD
25 #define INC_CKFORMATTER_HPP_443C2F6F_1843_46B8_B365_FC45329314CD
26 
27 //---------------------------------------------------------------------------
28 #include <set>
29 //#include <string> // included in "libxcks/types.hpp"
30 
31 // #include "libxcks/types.hpp" // included in "libxcks/ckvalue.hpp"
32 #include "libxcks/ckvalue.hpp"
33 //---------------------------------------------------------------------------
34 
35 
36 namespace libxcks
37 {
51 class LIBXCKS_SO_EXPORT ChecksumFormatter
52 {
53  public:
55  using Positions = std::set<uint_fast16_t>;
56 
58  enum class NumericBase : uint_least8_t
59  {
60  hexadecimal = 0,
61  decimal = 1,
62  octal = 2
63  };
64 
68  class LIBXCKS_SO_EXPORT ConfigurationProvider
69  {
70  public:
72  ConfigurationProvider() = default;
73 
76 
79 
81  virtual ~ConfigurationProvider() = default;
82 
89  virtual NumericBase getNumericBase(const ChecksumAlgoId type) const = 0;
90 
99  virtual bool getUpperCase(const ChecksumAlgoId type) const = 0;
100 
109  virtual const Positions& getSpacesPositions(const ChecksumAlgoId type) const = 0;
110  };
111 
112 
113  protected:
114  bool upperCase;
117 
118  public:
119  #if defined(_MSC_VER) && defined(LIBXCKS_SHARED)
121  ChecksumFormatter() : upperCase(false), numericBase(NumericBase::hexadecimal),
122  spacesPos() {}
123  #else
125  ChecksumFormatter() = delete;
126  #endif
127 
130 
133 
134  // Constructs a new checksum's formatter for the given algorithm name or
135  // alternative name.
137  const std::string& algoName, const bool lookInAltNames = true);
138 
139  // Constructs a new checksum's formatter for the given algorithm type.
140  ChecksumFormatter(const ConfigurationProvider& provider, const ChecksumAlgoId type);
141 
142  // Constructs a new checksum's formatter with the given parameters.
143  #if defined(_MSC_VER) && defined(LIBXCKS_SHARED)
144  ChecksumFormatter(const bool uc,
145  const NumericBase nb = NumericBase::hexadecimal,
146  const Positions& sp = Positions());
147  #else
148  ChecksumFormatter(const bool uc = false,
149  const NumericBase nb = NumericBase::hexadecimal,
150  const Positions& sp = Positions());
151  #endif
152 
153  protected:
154  // Initializes the instance.
155  void initialise(const ConfigurationProvider& provider, const ChecksumAlgoId type);
156 
157  public:
158  // Format the given checksum's value.
159  std::string format(const uint8_t* value, const size_t size);
160 
161  // Returns whether the output will be in uppercase.
162  bool getUpperCase() const;
163 
164  // Sets whether the output will be in uppercase.
165  void setUpperCase(const bool isUpperCase);
166 
167  // Returns the numeric base for the output.
168  NumericBase getNumericBase() const;
169 
170  // Sets the numeric base for the output.
171  void setNumericBase(const NumericBase base);
172 
173  // Returns the positions of the spaces between two bytes.
174  Positions getSpacesPositions() const;
175 
176  // Sets the positions of the spaces between two bytes.
177  void setSpacesPositions(const std::string& spacesPositions);
178 
179  // Sets the positions of the spaces between two bytes.
180  void setSpacesPositions(const Positions& spacesPositions);
181 
182  // Format the given checksum's value with the given algorithm's name.
183  static std::string format(const ConfigurationProvider& provider,
184  const uint8_t* value, const size_t size,
185  const std::string& algoName,
186  const bool lookInAltNames = true);
187 
188  // Format the given checksum's value with the given algorithm's type.
189  static std::string format(const ConfigurationProvider& provider,
190  const uint8_t* value, const size_t size,
191  const ChecksumAlgoId type);
192 
193  // Format the given checksum's value.
194  static std::string format(const ConfigurationProvider& provider,
195  const ChecksumValue& value);
196 
197  // Gets the positions of the spaces between two bytes from a string.
198  static Positions toSpacesPositions(const std::string& spacesPositions);
199 };
200 //---------------------------------------------------------------------------
201 
202 
210 {
211  private:
212  const ChecksumFormatter::Positions defaultPositions;
213 
214  public:
217 
220 
223 
225  virtual ~DefaultCKFConfigurationProvider() = default;
226 
234  {
235  return ChecksumFormatter::NumericBase::hexadecimal;
236  }
237 
246  bool getUpperCase(const ChecksumAlgoId type) const override
247  {
248  return true;
249  }
250 
260  {
261  return defaultPositions;
262  }
263 };
264 //---------------------------------------------------------------------------
265 
266 } // namespace libxcks
267 //---------------------------------------------------------------------------
268 
269 #endif // INC_CKFORMATTER_HPP_443C2F6F_1843_46B8_B365_FC45329314CD
Stores the value of a checksum.
Configuration provider for ChecksumFormatter.
Definition: ckformatter.hpp:69
ConfigurationProvider(const ConfigurationProvider &)=delete
Deleted copy constructor.
virtual ~ConfigurationProvider()=default
Destructor.
virtual NumericBase getNumericBase(const ChecksumAlgoId type) const =0
Returns the numeric base for a given type of algorithm of checksums.
virtual const Positions & getSpacesPositions(const ChecksumAlgoId type) const =0
Returns the positions of the spaces between two bytes for a given type of algorithm of checksums.
ConfigurationProvider & operator=(const ConfigurationProvider &)=delete
Deleted assignment operator.
virtual bool getUpperCase(const ChecksumAlgoId type) const =0
Returns whether the output will be in uppercase for a given type of algorithm of checksums.
ConfigurationProvider()=default
Default constructor.
This class provides some convenient tools to format checksum and hash values.
Definition: ckformatter.hpp:52
ChecksumFormatter(const ChecksumFormatter &)=delete
Deleted copy constructor.
ChecksumFormatter()=delete
Deleted default constructor.
Positions spacesPos
Positions of spaces between two bytes.
ChecksumFormatter & operator=(const ChecksumFormatter &)=delete
Deleted assignment operator.
std::set< uint_fast16_t > Positions
Set of unsigned integers.
Definition: ckformatter.hpp:55
NumericBase
The available numeric bases.
Definition: ckformatter.hpp:59
NumericBase numericBase
Numeric base of the output.
bool upperCase
Case of the output if numeric base is hexadecimal.
Stores the value of a checksum.
Definition: ckvalue.hpp:45
Default configuration provider for ChecksumFormatter.
ChecksumFormatter::NumericBase getNumericBase(const ChecksumAlgoId type) const override
Returns the numeric base for a given type of algorithm of checksums.
DefaultCKFConfigurationProvider(const DefaultCKFConfigurationProvider &)=delete
Deleted copy constructor.
DefaultCKFConfigurationProvider & operator=(const DefaultCKFConfigurationProvider &)=delete
Deleted assignment operator.
bool getUpperCase(const ChecksumAlgoId type) const override
Returns whether the output will be in uppercase for a given type of algorithm of checksums.
const ChecksumFormatter::Positions & getSpacesPositions(const ChecksumAlgoId type) const override
Returns the positions of the spaces between two bytes for a given type of algorithm of checksums.
DefaultCKFConfigurationProvider()=default
Default constructor.
virtual ~DefaultCKFConfigurationProvider()=default
Destructor.
ChecksumAlgoId
Ids of algorithms of checksums.
Definition: types.hpp:65