/src/exiv2/src/image_int.hpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | |
3 | | #ifndef IMAGE_INT_HPP_ |
4 | | #define IMAGE_INT_HPP_ |
5 | | |
6 | | #include "config.h" |
7 | | |
8 | | // ***************************************************************************** |
9 | | // included header files |
10 | | #include "slice.hpp" // for Slice |
11 | | |
12 | | #include <cstddef> // for size_t |
13 | | #include <ostream> // for ostream, basic_ostream::put |
14 | | #include <string> |
15 | | |
16 | | #ifdef EXV_HAVE_STD_FORMAT |
17 | | #include <format> |
18 | 1.40M | #define stringFormat std::format |
19 | 0 | #define stringFormatTo std::format_to |
20 | | #else |
21 | | #include <fmt/format.h> |
22 | | #define stringFormat fmt::format |
23 | | #define stringFormatTo fmt::format_to |
24 | | #endif |
25 | | |
26 | | // ***************************************************************************** |
27 | | // namespace extensions |
28 | | namespace Exiv2::Internal { |
29 | | // ***************************************************************************** |
30 | | // class definitions |
31 | | |
32 | | /*! |
33 | | * @brief Helper struct for binary data output via @ref binaryToString. |
34 | | * |
35 | | * The only purpose of this struct is to provide a custom |
36 | | * `operator<<(std::ostream&)` for the output of binary data, that is not |
37 | | * used for all Slices by default. |
38 | | */ |
39 | | template <typename T> |
40 | | struct binaryToStringHelper; |
41 | | |
42 | | /*! |
43 | | * @brief Actual implementation of the output algorithm described in @ref |
44 | | * binaryToString |
45 | | * |
46 | | * @note Does not throw |
47 | | */ |
48 | | template <typename T> |
49 | 0 | std::ostream& operator<<(std::ostream& stream, const binaryToStringHelper<T>& binToStr) noexcept { |
50 | 0 | for (size_t i = 0; i < binToStr.buf_.size(); ++i) { |
51 | 0 | auto c = static_cast<unsigned char>(binToStr.buf_.at(i)); |
52 | 0 | if (c != 0 || i != binToStr.buf_.size() - 1) { |
53 | 0 | if (c < 32 || c > 126) |
54 | 0 | stream.put('.'); |
55 | 0 | else |
56 | 0 | stream.put(static_cast<char>(c)); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | return stream; |
60 | 0 | } Unexecuted instantiation: std::__1::basic_ostream<char, std::__1::char_traits<char> >& Exiv2::Internal::operator<< <unsigned char*>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Exiv2::Internal::binaryToStringHelper<unsigned char*> const&) Unexecuted instantiation: std::__1::basic_ostream<char, std::__1::char_traits<char> >& Exiv2::Internal::operator<< <Exiv2::Slice<unsigned char*> const>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Exiv2::Internal::binaryToStringHelper<Exiv2::Slice<unsigned char*> const> const&) |
61 | | |
62 | | template <typename T> |
63 | | struct binaryToStringHelper { |
64 | 0 | explicit constexpr binaryToStringHelper(Slice<T>&& myBuf) noexcept : buf_(std::move(myBuf)) { |
65 | 0 | } Unexecuted instantiation: Exiv2::Internal::binaryToStringHelper<unsigned char*>::binaryToStringHelper(Exiv2::Slice<unsigned char*>&&) Unexecuted instantiation: Exiv2::Internal::binaryToStringHelper<Exiv2::Slice<unsigned char*> const>::binaryToStringHelper(Exiv2::Slice<Exiv2::Slice<unsigned char*> const>&&) |
66 | | |
67 | | // the Slice is stored by value to avoid dangling references, in case we |
68 | | // invoke: |
69 | | // binaryToString(makeSlice(buf, 0, n)); |
70 | | // <- buf_ would be now dangling, were it a reference |
71 | | Slice<T> buf_; |
72 | | }; |
73 | | |
74 | | /*! |
75 | | * @brief format binary data for display in @ref Image::printStructure() |
76 | | * |
77 | | * This function creates a new helper class that can be passed to a |
78 | | * `std::ostream` for output. It creates a printable version of the binary |
79 | | * data in the slice sl according to the following rules: |
80 | | * - characters with numeric values larger than 0x20 (= space) and smaller |
81 | | * or equal to 0x7F (Delete) are printed as ordinary characters |
82 | | * - characters outside of that range are printed as '.' |
83 | | * - if the last element of the slice is 0, then it is omitted |
84 | | * |
85 | | * @param[in] sl Slice containing binary data buffer that should be |
86 | | * printed. |
87 | | * |
88 | | * @return Helper object, that can be passed into a std::ostream and |
89 | | * produces an output according to the aforementioned rules. |
90 | | * |
91 | | * @throw This function does not throw. The output of the helper object to |
92 | | * the stream throws neither. |
93 | | */ |
94 | | template <typename T> |
95 | 0 | constexpr auto binaryToString(Slice<T>&& sl) noexcept { |
96 | 0 | return binaryToStringHelper<T>(std::move(sl)); |
97 | 0 | } Unexecuted instantiation: auto Exiv2::Internal::binaryToString<unsigned char*>(Exiv2::Slice<unsigned char*>&&) Unexecuted instantiation: auto Exiv2::Internal::binaryToString<Exiv2::Slice<unsigned char*> const>(Exiv2::Slice<Exiv2::Slice<unsigned char*> const>&&) |
98 | | |
99 | | /// @brief indent output for kpsRecursive in \em printStructure() \em . |
100 | | std::string indent(size_t i); |
101 | | |
102 | | } // namespace Exiv2::Internal |
103 | | |
104 | | #endif // #ifndef IMAGE_INT_HPP_ |