libmspub_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 libmspub 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 INCLUDED_LIBMSPUB_UTILS_H
11 #define INCLUDED_LIBMSPUB_UTILS_H
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <cmath>
18 #include <map>
19 #include <vector>
20
21 #include <boost/cstdint.hpp>
22
23 #include <librevenge/librevenge.h>
24
25 #include <librevenge-stream/librevenge-stream.h>
26
27 #include "MSPUBTypes.h"
28
29 #ifndef M_PI
30 #define M_PI 3.14159265358979323846
31 #endif
32
33 #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
34 # define MSPUB_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
35 #else
36 # define MSPUB_ATTRIBUTE_PRINTF(fmt, arg)
37 #endif
38
39 #if defined(HAVE_CLANG_ATTRIBUTE_FALLTHROUGH)
40 # define MSPUB_FALLTHROUGH [[clang::fallthrough]]
41 #elif defined(HAVE_GCC_ATTRIBUTE_FALLTHROUGH)
42 # define MSPUB_FALLTHROUGH __attribute__((fallthrough))
43 #else
44 # define MSPUB_FALLTHROUGH ((void) 0)
45 #endif
46
47 // do nothing with debug messages in a release compile
48 #ifdef DEBUG
49 namespace libmspub
50 {
51 void debugPrint(const char *format, ...) MSPUB_ATTRIBUTE_PRINTF(1, 2);
52 }
53 #define MSPUB_DEBUG_MSG(M) libmspub::debugPrint M
54 #define MSPUB_DEBUG(M) M
55 #else
56 #define MSPUB_DEBUG_MSG(M)
57 #define MSPUB_DEBUG(M)
58 #endif
59
60 namespace libmspub
61 {
62 const char *mimeByImgType(ImgType type);
63 const char *windowsCharsetNameByOriginalCharset(const char *name);
64
65 uint8_t readU8(librevenge::RVNGInputStream *input);
66 uint16_t readU16(librevenge::RVNGInputStream *input);
67 uint32_t readU32(librevenge::RVNGInputStream *input);
68 uint64_t readU64(librevenge::RVNGInputStream *input);
69 int8_t readS8(librevenge::RVNGInputStream *input);
70 int16_t readS16(librevenge::RVNGInputStream *input);
71 int32_t readS32(librevenge::RVNGInputStream *input);
72 double readFixedPoint(librevenge::RVNGInputStream *input);
73 double toFixedPoint(int fp);
74 void readNBytes(librevenge::RVNGInputStream *input, unsigned long length, std::vector<unsigned char> &out);
75
76 unsigned long getLength(librevenge::RVNGInputStream *input);
77
78 void appendCharacters(librevenge::RVNGString &text, const std::vector<unsigned char> &characters, const char *encoding);
79
80 bool stillReading(librevenge::RVNGInputStream *input, unsigned long until);
81
82 void rotateCounter(double &x, double &y, double centerX, double centerY, short rotation);
83 void flipIfNecessary(double &x, double &y, double centerX, double centerY, bool flipVertical, bool flipHorizontal);
84
85 unsigned correctModulo(int x, unsigned n);
86 double doubleModulo(double x, double y);
87
88 template <class MapT> typename MapT::mapped_type *getIfExists(MapT &map, const typename MapT::key_type &key)
89 {
90 auto i = map.find(key);
91 return i == map.end() ? nullptr : &(i->second);
92 }
93
94 template <class MapT> const typename MapT::mapped_type *getIfExists_const(MapT &map, const typename MapT::key_type &key)
95 {
96 auto i = map.find(key);
97 return i == map.end() ? nullptr : &(i->second);
98 }
99
100 template <class MapT> typename MapT::mapped_type ptr_getIfExists(MapT &map, const typename MapT::key_type &key)
101 {
102 typename MapT::iterator i = map.find(key);
103 return i == map.end() ? NULL : i->second;
104 }
105
106 class EndOfStreamException
107 {
108 };
109
110 class GenericException
111 {
112 };
113
114 librevenge::RVNGBinaryData inflateData(librevenge::RVNGBinaryData);
115
116 } // namespace libmspub
117
118 #endif // INCLUDED_LIBMSPUB_UTILS_H
119 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libmspub::correctModulo
unsigned correctModulo(int x, unsigned n)
Definition: libmspub_utils.cpp:151
libmspub::stillReading
bool stillReading(librevenge::RVNGInputStream *input, unsigned long until)
Definition: libmspub_utils.cpp:416
libmspub::rotateCounter
void rotateCounter(double &x, double &y, double centerX, double centerY, short rotation)
Definition: libmspub_utils.cpp:96
libmspub::EndOfStreamException
Definition: libmspub_utils.h:106
libmspub::mimeByImgType
const char * mimeByImgType(ImgType type)
Definition: libmspub_utils.cpp:72
libmspub::readU32
uint32_t readU32(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:292
libmspub::appendCharacters
void appendCharacters(librevenge::RVNGString &text, const std::vector< unsigned char > &characters, const char *encoding)
Definition: libmspub_utils.cpp:383
libmspub::getIfExists
MapT::mapped_type * getIfExists(MapT &map, const typename MapT::key_type &key)
Definition: libmspub_utils.h:88
libmspub::readU64
uint64_t readU64(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:316
libmspub::getIfExists_const
const MapT::mapped_type * getIfExists_const(MapT &map, const typename MapT::key_type &key)
Definition: libmspub_utils.h:94
libmspub::readS32
int32_t readS32(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:311
libmspub::readFixedPoint
double readFixedPoint(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:132
libmspub::flipIfNecessary
void flipIfNecessary(double &x, double &y, double centerX, double centerY, bool flipVertical, bool flipHorizontal)
Definition: libmspub_utils.cpp:137
libmspub::getLength
unsigned long getLength(librevenge::RVNGInputStream *const input)
Definition: libmspub_utils.cpp:349
libmspub::readS8
int8_t readS8(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:301
libmspub::readU8
uint8_t readU8(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:266
libmspub::ptr_getIfExists
MapT::mapped_type ptr_getIfExists(MapT &map, const typename MapT::key_type &key)
Definition: libmspub_utils.h:100
libmspub::readNBytes
void readNBytes(librevenge::RVNGInputStream *input, unsigned long length, std::vector< unsigned char > &out)
Definition: libmspub_utils.cpp:329
libmspub::ImgType
ImgType
Definition: MSPUBTypes.h:213
libmspub::inflateData
librevenge::RVNGBinaryData inflateData(librevenge::RVNGBinaryData deflated)
Definition: libmspub_utils.cpp:167
libmspub::doubleModulo
double doubleModulo(double x, double y)
Definition: libmspub_utils.cpp:108
libmspub
Definition: Arrow.h:13
libmspub::windowsCharsetNameByOriginalCharset
const char * windowsCharsetNameByOriginalCharset(const char *name)
Definition: libmspub_utils.cpp:39
libmspub::GenericException
Definition: libmspub_utils.h:110
libmspub::toFixedPoint
double toFixedPoint(int fp)
Definition: libmspub_utils.cpp:125
MSPUBTypes.h
libmspub::readU16
uint16_t readU16(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:285
libmspub::readS16
int16_t readS16(librevenge::RVNGInputStream *input)
Definition: libmspub_utils.cpp:306
MSPUB_ATTRIBUTE_PRINTF
#define MSPUB_ATTRIBUTE_PRINTF(fmt, arg)
Definition: libmspub_utils.h:36