libzmf_utils.h Source File documentation

libzmf_utils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is a part of the libzmf project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #ifndef LIBZMF_UTILS_H_INCLUDED
11 #define LIBZMF_UTILS_H_INCLUDED
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <cmath>
18 #include <memory>
19 #include <string>
20 #include <bitset>
21
22 #include <boost/cstdint.hpp>
23
24 #include <librevenge-stream/librevenge-stream.h>
25 #include <librevenge/librevenge.h>
26
27 #ifdef DEBUG
28 #include <boost/type_index.hpp>
29 #include <regex>
30 #endif
31
32 #define ZMF_EPSILON 1E-6
33 #define ZMF_ALMOST_ZERO(m) (std::fabs(m) <= ZMF_EPSILON)
34
35 #if defined(HAVE_CLANG_ATTRIBUTE_FALLTHROUGH)
36 # define ZMF_FALLTHROUGH [[clang::fallthrough]]
37 #elif defined(HAVE_GCC_ATTRIBUTE_FALLTHROUGH)
38 # define ZMF_FALLTHROUGH __attribute__((fallthrough))
39 #else
40 # define ZMF_FALLTHROUGH ((void) 0)
41 #endif
42
43 #ifdef DEBUG
44
45 #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
46 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
47 #else
48 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg)
49 #endif
50
51 #define ZMF_DEBUG_MSG(M) libzmf::debugPrint M
52 #define ZMF_DEBUG(M) M
53
54 #else // !DEBUG
55
56 // do nothing with debug messages in a release compile
57 #define ZMF_DEBUG_MSG(M)
58 #define ZMF_DEBUG(M)
59
60 #endif // DEBUG
61
62 #define ZMF_NUM_ELEMENTS(array) sizeof(array)/sizeof(array[0])
63
64 namespace libzmf
65 {
66
67 template<typename T>
68 std::string prettyTypeName()
69 {
70 #ifdef DEBUG
71 auto str = boost::typeindex::type_id<T>().pretty_name();
72 str = std::regex_replace(str, std::regex("libzmf::"), "");
73 str = std::regex_replace(str, std::regex("boost::"), "");
74 return str;
75 #else
76 return "";
77 #endif
78 }
79
80 typedef std::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr;
81
82 struct ZMFDummyDeleter
83 {
84 void operator()(void *) {}
85 };
86
87 uint8_t readU8(const RVNGInputStreamPtr &input, bool = false);
88 uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian=false);
89 uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian=false);
90 uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian=false);
91 int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian=false);
92
93 float readFloat(const RVNGInputStreamPtr &input, bool bigEndian=false);
94
95 const unsigned char *readNBytes(const RVNGInputStreamPtr &input, unsigned long numBytes);
96
97 void skip(const RVNGInputStreamPtr &input, unsigned long numBytes);
98
99 void seek(const RVNGInputStreamPtr &input, unsigned long pos);
100 void seekRelative(const RVNGInputStreamPtr &input, long pos);
101
102 unsigned long getLength(const RVNGInputStreamPtr &input);
103
104 void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size,
105 const char *encoding);
106
107 void writeU16(librevenge::RVNGBinaryData &buffer, const int value);
108 void writeU32(librevenge::RVNGBinaryData &buffer, const int value);
109
110 double rad2deg(double value);
111 double normalizeAngle(double radAngle);
112
113 template<std::size_t numBytes>
114 std::bitset<numBytes * 8> bytesToBitset(const uint8_t *data)
115 {
116 std::bitset<numBytes * 8> b;
117
118 for (std::size_t i = 0; i < numBytes; ++i)
119 {
120 uint8_t cur = data[i];
121 std::size_t offset = i * 8;
122
123 for (int j = 0; j < 8; ++j)
124 {
125 b[offset++] = cur & 1;
126 cur >>= 1;
127 }
128 }
129
130 return b;
131 }
132
133 template<typename T>
134 double um2in(T micrometers)
135 {
136 return micrometers / 1000.0 / 25.4;
137 }
138
139 #ifdef DEBUG
140 void debugPrint(const char *format, ...) ZMF_ATTRIBUTE_PRINTF(1, 2);
141 #endif
142
143 struct EndOfStreamException
144 {
145 EndOfStreamException();
146 };
147
148 struct GenericException
149 {
150 };
151
152 }
153
154 #endif // LIBZMF_UTILS_H_INCLUDED
155
156 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libzmf::readNBytes
const unsigned char * readNBytes(const RVNGInputStreamPtr &input, const unsigned long numBytes)
Definition: libzmf_utils.cpp:162
libzmf::EndOfStreamException::EndOfStreamException
EndOfStreamException()
Definition: libzmf_utils.cpp:287
libzmf::prettyTypeName
std::string prettyTypeName()
Definition: libzmf_utils.h:68
libzmf::ZMFDummyDeleter::operator()
void operator()(void *)
Definition: libzmf_utils.h:84
libzmf::um2in
double um2in(T micrometers)
Definition: libzmf_utils.h:134
libzmf::seekRelative
void seekRelative(const RVNGInputStreamPtr &input, const long pos)
Definition: libzmf_utils.cpp:190
libzmf::getLength
unsigned long getLength(const RVNGInputStreamPtr &input)
Definition: libzmf_utils.cpp:198
libzmf::RVNGInputStreamPtr
std::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr
Definition: libzmf_utils.h:80
libzmf::readS32
int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:149
libzmf::readU8
uint8_t readU8(const RVNGInputStreamPtr &input, bool)
Definition: libzmf_utils.cpp:89
libzmf::readU64
uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:133
libzmf::skip
void skip(const RVNGInputStreamPtr &input, unsigned long numBytes)
Definition: libzmf_utils.cpp:175
libzmf
Definition: BMIHeader.cpp:13
libzmf::bytesToBitset
std::bitset< numBytes *8 > bytesToBitset(const uint8_t *data)
Definition: libzmf_utils.h:114
libzmf::writeU16
void writeU16(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libzmf_utils.cpp:247
libzmf::rad2deg
double rad2deg(double value)
Definition: libzmf_utils.cpp:261
libzmf::ZMFDummyDeleter
Definition: libzmf_utils.h:82
libzmf::seek
void seek(const RVNGInputStreamPtr &input, const unsigned long pos)
Definition: libzmf_utils.cpp:182
libzmf::readU16
uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:101
libzmf::EndOfStreamException
Definition: libzmf_utils.h:143
libzmf::readFloat
float readFloat(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:154
libzmf::normalizeAngle
double normalizeAngle(double radAngle)
Definition: libzmf_utils.cpp:268
libzmf::GenericException
Definition: libzmf_utils.h:148
libzmf::writeU32
void writeU32(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libzmf_utils.cpp:253
libzmf::readU32
uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:117
libzmf::appendCharacters
void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size, const char *encoding)
Definition: libzmf_utils.cpp:219