libcdr_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 libcdr 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 __LIBCDR_UTILS_H__
11 #define __LIBCDR_UTILS_H__
12
13 #include <stdio.h>
14 #include <string>
15 #include <math.h>
16 #include <vector>
17 #include <librevenge-stream/librevenge-stream.h>
18 #include <librevenge/librevenge.h>
19
20 #ifndef M_PI
21 #define M_PI 3.14159265358979323846
22 #endif
23
24 #define CDR_EPSILON 1E-6
25 #define CDR_ALMOST_ZERO(m) (fabs(m) <= CDR_EPSILON)
26
27 #if defined(__clang__) || defined(__GNUC__)
28 # define CDR_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
29 #else
30 # define CDR_ATTRIBUTE_PRINTF(fmt, arg)
31 #endif
32
33 #ifdef _MSC_VER
34
35 typedef unsigned char uint8_t;
36 typedef unsigned short uint16_t;
37 typedef short int16_t;
38 typedef unsigned uint32_t;
39 typedef int int32_t;
40 typedef unsigned __int64 uint64_t;
41 typedef __int64 int64_t;
42
43 #else
44
45 #ifdef HAVE_CONFIG_H
46
47 #include <config.h>
48
49 #ifdef HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
52
53 #ifdef HAVE_INTTYPES_H
54 #include <inttypes.h>
55 #endif
56
57 #else
58
59 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
60 #include <stdint.h>
61 #include <inttypes.h>
62
63 #endif
64
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 libcdr
73 {
74 void debugPrint(const char *format, ...) CDR_ATTRIBUTE_PRINTF(1, 2);
75 }
76 #ifdef VERBOSE_DEBUG
77 #define CDR_DEBUG_MSG(M) libcdr::debugPrint("%15s:%5d: ", __FILE__, __LINE__); libcdr::debugPrint M
78 #define CDR_DEBUG(M) M
79 #else
80 #define CDR_DEBUG_MSG(M) libcdr::debugPrint M
81 #define CDR_DEBUG(M) M
82 #endif
83 #else
84 #define CDR_DEBUG_MSG(M)
85 #define CDR_DEBUG(M)
86 #endif
87
88 namespace libcdr
89 {
90
91 uint8_t readU8(librevenge::RVNGInputStream *input, bool bigEndian=false);
92 uint16_t readU16(librevenge::RVNGInputStream *input, bool bigEndian=false);
93 uint32_t readU32(librevenge::RVNGInputStream *input, bool bigEndian=false);
94 uint64_t readU64(librevenge::RVNGInputStream *input, bool bigEndian=false);
95 int32_t readS32(librevenge::RVNGInputStream *input, bool bigEndian=false);
96 int16_t readS16(librevenge::RVNGInputStream *input, bool bigEndian=false);
97
98 double readDouble(librevenge::RVNGInputStream *input, bool bigEndian=false);
99
100 double readFixedPoint(librevenge::RVNGInputStream *input, bool bigEndian=false);
101
102 unsigned long getLength(librevenge::RVNGInputStream *input);
103 unsigned long getRemainingLength(librevenge::RVNGInputStream *input);
104
105 int cdr_round(double d);
106
107 void writeU16(librevenge::RVNGBinaryData &buffer, const int value);
108 void writeU32(librevenge::RVNGBinaryData &buffer, const int value);
109 void appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters, unsigned short charset);
110 void appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters);
111
112 #ifdef DEBUG
113 const char *toFourCC(unsigned value, bool bigEndian=false);
114 #endif
115
116 class EndOfStreamException
117 {
118 };
119
120 class GenericException
121 {
122 };
123
124 class UnknownPrecisionException
125 {
126 };
127
128 class EncodingException
129 {
130 };
131
132 } // namespace libcdr
133
134 #endif // __LIBCDR_UTILS_H__
135 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libcdr::EndOfStreamException
Definition: libcdr_utils.h:116
libcdr::UnknownPrecisionException
Definition: libcdr_utils.h:124
CDR_ATTRIBUTE_PRINTF
#define CDR_ATTRIBUTE_PRINTF(fmt, arg)
Definition: libcdr_utils.h:30
libcdr::readFixedPoint
double readFixedPoint(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:262
libcdr::readU16
uint16_t readU16(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:179
libcdr::cdr_round
int cdr_round(double d)
Definition: libcdr_utils.cpp:306
libcdr::getLength
unsigned long getLength(librevenge::RVNGInputStream *input)
Definition: libcdr_utils.cpp:270
libcdr::readS16
int16_t readS16(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:199
libcdr::GenericException
Definition: libcdr_utils.h:120
libcdr::writeU32
void writeU32(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libcdr_utils.cpp:317
libcdr
Definition: CDRCollector.h:29
libcdr::appendCharacters
void appendCharacters(librevenge::RVNGString &text, std::vector< unsigned char > characters, unsigned short charset)
Definition: libcdr_utils.cpp:325
libcdr::readS32
int32_t readS32(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:224
libcdr::EncodingException
Definition: libcdr_utils.h:128
libcdr::readDouble
double readDouble(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:249
libcdr::readU64
uint64_t readU64(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:229
libcdr::readU8
uint8_t readU8(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:163
libcdr::writeU16
void writeU16(librevenge::RVNGBinaryData &buffer, const int value)
Definition: libcdr_utils.cpp:311
libcdr::getRemainingLength
unsigned long getRemainingLength(librevenge::RVNGInputStream *input)
Definition: libcdr_utils.cpp:301
libcdr::readU32
uint32_t readU32(librevenge::RVNGInputStream *input, bool bigEndian=false)
Definition: libcdr_utils.cpp:204