- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

libetonyek_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 part of the libetonyek 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 LIBETONYEK_UTILS_H_INCLUDED
11 #define LIBETONYEK_UTILS_H_INCLUDED
12
13 #include <cmath>
14 #include <string>
15
16 #include <boost/shared_ptr.hpp>
17
18 #include <librevenge/librevenge.h>
19 #include <librevenge-stream/librevenge-stream.h>
20
21 #ifdef _MSC_VER
22
23 typedef unsigned char uint8_t;
24 typedef unsigned short uint16_t;
25 typedef unsigned uint32_t;
26 typedef unsigned __int64 uint64_t;
27 typedef signed char int8_t;
28 typedef short int16_t;
29 typedef int int32_t;
30 typedef __int64 int64_t;
31
32 #else
33
34 #ifdef HAVE_CONFIG_H
35
36 #include <config.h>
37
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h>
40 #endif
41
42 #ifdef HAVE_INTTYPES_H
43 #include <inttypes.h>
44 #endif
45
46 #else
47
48 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
49 #include <stdint.h>
50 #include <inttypes.h>
51
52 #endif
53
54 #endif
55
56 #define ETONYEK_EPSILON 1e-9
57 #define ETONYEK_ALMOST_ZERO(x) (std::fabs(x) < ETONYEK_EPSILON)
58
59 #define ETONYEK_NUM_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))
60
61 #if defined(__clang__) || defined(__GNUC__)
62 # define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
63 #else
64 # define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg)
65 #endif
66
67 // debug message includes source file and line number
68 //#define VERBOSE_DEBUG 1
69
70 // do nothing with debug messages in a release compile
71 #ifdef DEBUG
72 namespace libetonyek
73 {
74 void debugPrint(const char *format, ...) ETONYEK_ATTRIBUTE_PRINTF(1, 2);
75 }
76 #ifdef VERBOSE_DEBUG
77 #define ETONYEK_DEBUG_MSG(M) libetonyek::debugPrint("%15s:%5d: ", FILE, LINE); libetonyek::debugPrint M
78 #define ETONYEK_DEBUG(M) M
79 #else
80 #define ETONYEK_DEBUG_MSG(M) libetonyek::debugPrint M
81 #define ETONYEK_DEBUG(M) M
82 #endif
83 #else
84 #define ETONYEK_DEBUG_MSG(M)
85 #define ETONYEK_DEBUG(M)
86 #endif
87
88 namespace libetonyek
89 {
90
91 struct IWORKColor;
92 struct IWORKStroke;
93
94 /* Constants */
95 const double etonyek_half_pi(1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404326e+00);
96 const double etonyek_third_pi(1.04719755119659774615421446109316762806572313312503527365831486410260546876206966620934494178070568932738269550e+00);
97 const double etonyek_pi(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00);
98 const double etonyek_two_pi(6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617303e+00);
99
100 const double etonyek_root_three(1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614142e+00);
101 const double etonyek_root_two(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623e+00);
102
103 // Apple Numbers timestamp starts from 01 Jan 2001 00:00:00
104 const unsigned ETONYEK_EPOCH_BEGIN(978307200);
105
106 struct EtonyekDummyDeleter
107 {
108 void operator()(void *) {}
109 };
110
111 typedef boost::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr_t;
112
113 uint8_t readU8(const RVNGInputStreamPtr_t &input, bool = false);
114 uint16_t readU16(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
115 uint32_t readU32(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
116 uint64_t readU64(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
117
118 uint64_t readUVar(const RVNGInputStreamPtr_t &input);
119 int64_t readSVar(const RVNGInputStreamPtr_t &input);
120
121 double readDouble(const RVNGInputStreamPtr_t &input);
122 float readFloat(const RVNGInputStreamPtr_t &input);
123
124 unsigned long getLength(const RVNGInputStreamPtr_t &input);
125 unsigned long getRemainingLength(const RVNGInputStreamPtr_t &input);
126
133 bool approxEqual(double x, double y, double eps = ETONYEK_EPSILON);
134
135 template<class T>
136 bool approxEqual(const T &left, const T &right, const double eps = ETONYEK_EPSILON)
137 {
138 assert(left.length() == right.length());
139
140 for (int i = 0; i != left.length(); ++i)
141 {
142 if (!approxEqual(left[i], right[i], eps))
143 return false;
144 }
145 return true;
146 }
147
153 double pt2in(double d);
154
160 double deg2rad(double value);
161
167 double rad2deg(double value);
168
169 librevenge::RVNGString makeColor(const IWORKColor &color);
170
171 void writeBorder(const IWORKStroke &stroke, const char *name, librevenge::RVNGPropertyList &props);
172
173 class EndOfStreamException
174 {
175 };
176
177 class GenericException
178 {
179 };
180
181 } // namespace libetonyek
182
183 #endif // LIBETONYEK_UTILS_H_INCLUDED
184
185 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libetonyek
Definition: IWORKBezierElement.cpp:18
ETONYEK_ATTRIBUTE_PRINTF
#define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg)
Definition: libetonyek_utils.h:64
libetonyek::etonyek_root_two
const double etonyek_root_two(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623e+00)
libetonyek::getRemainingLength
unsigned long getRemainingLength(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:215
libetonyek::IWORKStroke
Definition: IWORKTypes.h:202
libetonyek::getLength
unsigned long getLength(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:184
libetonyek::IWORKColor
Definition: IWORKTypes.h:71
libetonyek::deg2rad
double deg2rad(double value)
Convert an angle from degrees to radians.
Definition: libetonyek_utils.cpp:230
y
double y
Definition: IWORKShape.cpp:43
libetonyek::readFloat
float readFloat(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:173
libetonyek::GenericException
Definition: libetonyek_utils.h:177
libetonyek::EndOfStreamException
Definition: libetonyek_utils.h:173
libetonyek::pt2in
double pt2in(const double d)
Convert a length from points to inches.
Definition: libetonyek_utils.cpp:225
libetonyek::readU32
uint32_t readU32(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition: libetonyek_utils.cpp:76
libetonyek::IWORKToken::left
Definition: IWORKToken.h:320
libetonyek::etonyek_root_three
const double etonyek_root_three(1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614142e+00)
libetonyek::readUVar
uint64_t readUVar(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:108
libetonyek::approxEqual
bool approxEqual(const IWORKPath &left, const IWORKPath &right, double eps=ETONYEK_EPSILON)
x
double x
Definition: IWORKShape.cpp:42
libetonyek::IWORKToken::d
Definition: IWORKToken.h:94
libetonyek::IWORKToken::color
Definition: IWORKToken.h:79
name
const char * name
Definition: IWORKToken.cpp:43
libetonyek::IWORKToken::right
Definition: IWORKToken.h:335
libetonyek::KEY2Token::i
Definition: KEY2Token.h:70
libetonyek::readU8
uint8_t readU8(const RVNGInputStreamPtr_t &input, bool)
Definition: libetonyek_utils.cpp:48
libetonyek::rad2deg
double rad2deg(double value)
Convert an angle from radians to degrees.
Definition: libetonyek_utils.cpp:242
libetonyek::readSVar
int64_t readSVar(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:141
libetonyek::readU64
uint64_t readU64(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition: libetonyek_utils.cpp:92
libetonyek::EtonyekDummyDeleter::operator()
void operator()(void *)
Definition: libetonyek_utils.h:108
libetonyek::ETONYEK_EPOCH_BEGIN
const unsigned ETONYEK_EPOCH_BEGIN(978307200)
libetonyek::etonyek_two_pi
const double etonyek_two_pi(6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617303e+00)
libetonyek::makeColor
librevenge::RVNGString makeColor(const IWORKColor &color)
Definition: libetonyek_utils.cpp:254
libetonyek::IWORKToken::format
Definition: IWORKToken.h:307
libetonyek::EtonyekDummyDeleter
Definition: libetonyek_utils.h:106
libetonyek::RVNGInputStreamPtr_t
boost::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr_t
Definition: libetonyek_utils.h:111
libetonyek::etonyek_half_pi
const double etonyek_half_pi(1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404326e+00)
libetonyek::readU16
uint16_t readU16(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition: libetonyek_utils.cpp:60
ETONYEK_EPSILON
#define ETONYEK_EPSILON
Definition: libetonyek_utils.h:56
libetonyek::readDouble
double readDouble(const RVNGInputStreamPtr_t &input)
Definition: libetonyek_utils.cpp:162
libetonyek::writeBorder
void writeBorder(const IWORKStroke &stroke, const char *const name, librevenge::RVNGPropertyList &props)
Definition: libetonyek_utils.cpp:268
libetonyek::IWORKToken::stroke
Definition: IWORKToken.h:253
libetonyek::etonyek_pi
const double etonyek_pi(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00)
libetonyek::etonyek_third_pi
const double etonyek_third_pi(1.04719755119659774615421446109316762806572313312503527365831486410260546876206966620934494178070568932738269550e+00)