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 #ifdef DEBUG
36
37 #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
38 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
39 #else
40 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg)
41 #endif
42
43 #define ZMF_DEBUG_MSG(M) libzmf::debugPrint M
44 #define ZMF_DEBUG(M) M
45
46 #else // !DEBUG
47
48 // do nothing with debug messages in a release compile
49 #define ZMF_DEBUG_MSG(M)
50 #define ZMF_DEBUG(M)
51
52 #endif // DEBUG
53
54 #define ZMF_NUM_ELEMENTS(array) sizeof(array)/sizeof(array[0])
55
56 namespace libzmf
57 {
58
59 template<typename T>
60 std::string prettyTypeName()
61 {
62 #ifdef DEBUG
63 auto str = boost::typeindex::type_id<T>().pretty_name();
64 str = std::regex_replace(str, std::regex("libzmf::"), "");
65 str = std::regex_replace(str, std::regex("boost::"), "");
66 return str;
67 #else
68 return "";
69 #endif
70 }
71
72 typedef std::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr;
73
74 struct ZMFDummyDeleter
75 {
76 void operator()(void *) {}
77 };
78
79 uint8_t readU8(const RVNGInputStreamPtr &input, bool = false);
80 uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian=false);
81 uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian=false);
82 uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian=false);
83 int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian=false);
84
85 float readFloat(const RVNGInputStreamPtr &input, bool bigEndian=false);
86
87 const unsigned char *readNBytes(const RVNGInputStreamPtr &input, unsigned long numBytes);
88
89 void skip(const RVNGInputStreamPtr &input, unsigned long numBytes);
90
91 void seek(const RVNGInputStreamPtr &input, unsigned long pos);
92 void seekRelative(const RVNGInputStreamPtr &input, long pos);
93
94 unsigned long getLength(const RVNGInputStreamPtr &input);
95
96 void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size,
97 const char *encoding);
98
99 void writeU16(librevenge::RVNGBinaryData &buffer, const int value);
100 void writeU32(librevenge::RVNGBinaryData &buffer, const int value);
101
102 double rad2deg(double value);
103 double normalizeAngle(double radAngle);
104
105 template<std::size_t numBytes>
106 std::bitset<numBytes * 8> bytesToBitset(const uint8_t *data)
107 {
108 std::bitset<numBytes * 8> b;
109
110 for (std::size_t i = 0; i < numBytes; ++i)
111 {
112 uint8_t cur = data[i];
113 std::size_t offset = i * 8;
114
115 for (int j = 0; j < 8; ++j)
116 {
117 b[offset++] = cur & 1;
118 cur >>= 1;
119 }
120 }
121
122 return b;
123 }
124
125 template<typename T>
126 double um2in(T micrometers)
127 {
128 return micrometers / 1000.0 / 25.4;
129 }
130
131 #ifdef DEBUG
132 void debugPrint(const char *format, ...) ZMF_ATTRIBUTE_PRINTF(1, 2);
133 #endif
134
135 struct EndOfStreamException
136 {
137 EndOfStreamException();
138 };
139
140 struct GenericException
141 {
142 };
143
144 }
145
146 #endif // LIBZMF_UTILS_H_INCLUDED
147
148 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libzmf::readNBytes
const unsigned char * readNBytes(const RVNGInputStreamPtr &input, const unsigned long numBytes)
Definition: libzmf_utils.cpp:161
libzmf::prettyTypeName
std::string prettyTypeName()
Definition: libzmf_utils.h:60
libzmf::ZMFDummyDeleter::operator()
void operator()(void *)
Definition: libzmf_utils.h:76
libzmf::um2in
double um2in(T micrometers)
Definition: libzmf_utils.h:126
libzmf::seekRelative
void seekRelative(const RVNGInputStreamPtr &input, const long pos)
Definition: libzmf_utils.cpp:189
libzmf::getLength
unsigned long getLength(const RVNGInputStreamPtr &input)
Definition: libzmf_utils.cpp:197
libzmf::RVNGInputStreamPtr
std::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr
Definition: libzmf_utils.h:72
libzmf::readS32
int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:148
libzmf::readU8
uint8_t readU8(const RVNGInputStreamPtr &input, bool)
Definition: libzmf_utils.cpp:88
libzmf::readU64
uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:132
libzmf::skip
void skip(const RVNGInputStreamPtr &input, unsigned long numBytes)
Definition: libzmf_utils.cpp:174
libzmf
Definition: BMIHeader.cpp:13
libzmf::bytesToBitset
std::bitset< numBytes *8 > bytesToBitset(const uint8_t *data)
Definition: libzmf_utils.h:106
libzmf::writeU16
void writeU16(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libzmf_utils.cpp:254
libzmf::rad2deg
double rad2deg(double value)
Definition: libzmf_utils.cpp:268
libzmf::ZMFDummyDeleter
Definition: libzmf_utils.h:74
libzmf::seek
void seek(const RVNGInputStreamPtr &input, const unsigned long pos)
Definition: libzmf_utils.cpp:181
libzmf::readU16
uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:100
libzmf::EndOfStreamException
Definition: libzmf_utils.h:135
libzmf::readFloat
float readFloat(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:153
libzmf::normalizeAngle
double normalizeAngle(double radAngle)
Definition: libzmf_utils.cpp:275
libzmf::GenericException
Definition: libzmf_utils.h:140
libzmf::writeU32
void writeU32(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libzmf_utils.cpp:260
libzmf::readU32
uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian)
Definition: libzmf_utils.cpp:116
libzmf::appendCharacters
void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size, const char *encoding)
Definition: libzmf_utils.cpp:221