libqxp_utils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /*
00003  * This file is part of the libqxp project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  */
00009 
00010 #ifndef INCLUDED_LIBQXP_UTILS_H
00011 #define INCLUDED_LIBQXP_UTILS_H
00012 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 #include <cmath>
00018 #include <memory>
00019 #include <string>
00020 
00021 #include <boost/cstdint.hpp>
00022 
00023 #include <librevenge-stream/librevenge-stream.h>
00024 #include <librevenge/librevenge.h>
00025 
00026 #define QXP_EPSILON 1E-6
00027 #define QXP_ALMOST_ZERO(m) (std::fabs(m) <= QXP_EPSILON)
00028 
00029 #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
00030 #define QXP_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
00031 #else
00032 #define QXP_ATTRIBUTE_PRINTF(fmt, arg)
00033 #endif
00034 
00035 #if defined(HAVE_CLANG_ATTRIBUTE_FALLTHROUGH)
00036 #  define QXP_FALLTHROUGH [[clang::fallthrough]]
00037 #elif defined(HAVE_GCC_ATTRIBUTE_FALLTHROUGH)
00038 #  define QXP_FALLTHROUGH __attribute__((fallthrough))
00039 #else
00040 #  define QXP_FALLTHROUGH ((void) 0)
00041 #endif
00042 
00043 // do nothing with debug messages in a release compile
00044 #ifdef DEBUG
00045 namespace libqxp
00046 {
00047 void debugPrint(const char *format, ...) QXP_ATTRIBUTE_PRINTF(1, 2);
00048 }
00049 
00050 #define QXP_DEBUG_MSG(M) libqxp::debugPrint M
00051 #define QXP_DEBUG(M) M
00052 #else
00053 #define QXP_DEBUG_MSG(M)
00054 #define QXP_DEBUG(M)
00055 #endif
00056 
00057 #define QXP_NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
00058 
00059 namespace libqxp
00060 {
00061 
00062 struct QXPDummyDeleter
00063 {
00064   void operator()(void *) {}
00065 };
00066 
00067 template<typename T, typename... Args>
00068 std::unique_ptr<T> make_unique(Args &&... args)
00069 {
00070   return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
00071 }
00072 
00073 uint8_t readU8(librevenge::RVNGInputStream *input, bool = false);
00074 uint16_t readU16(librevenge::RVNGInputStream *input, bool bigEndian=false);
00075 uint32_t readU32(librevenge::RVNGInputStream *input, bool bigEndian=false);
00076 uint64_t readU64(librevenge::RVNGInputStream *input, bool bigEndian=false);
00077 int16_t readS16(librevenge::RVNGInputStream *input, bool bigEndian=false);
00078 int32_t readS32(librevenge::RVNGInputStream *input, bool bigEndian=false);
00079 double readFloat16(librevenge::RVNGInputStream *input, bool bigEndian=false);
00080 double readFraction(librevenge::RVNGInputStream *input, bool bigEndian=false);
00081 
00082 const unsigned char *readNBytes(librevenge::RVNGInputStream *input, unsigned long numBytes);
00083 
00084 std::string readCString(librevenge::RVNGInputStream *input);
00085 std::string readPascalString(librevenge::RVNGInputStream *input);
00086 std::string readString(librevenge::RVNGInputStream *input, const unsigned length);
00087 std::string readPlatformString(librevenge::RVNGInputStream *input, bool bigEndian=false);
00088 
00089 void skip(librevenge::RVNGInputStream *input, unsigned long numBytes);
00090 
00091 void seek(librevenge::RVNGInputStream *input, unsigned long pos);
00092 void seekRelative(librevenge::RVNGInputStream *input, long pos);
00093 
00094 unsigned long getRemainingLength(librevenge::RVNGInputStream *input);
00095 
00096 uint8_t readU8(std::shared_ptr<librevenge::RVNGInputStream> input, bool = false);
00097 uint16_t readU16(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00098 uint32_t readU32(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00099 uint64_t readU64(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00100 int16_t readS16(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00101 int32_t readS32(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00102 double readFloat16(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00103 double readFraction(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00104 
00105 const unsigned char *readNBytes(std::shared_ptr<librevenge::RVNGInputStream> input, unsigned long numBytes);
00106 
00107 std::string readCString(std::shared_ptr<librevenge::RVNGInputStream> input);
00108 std::string readPascalString(std::shared_ptr<librevenge::RVNGInputStream> input);
00109 std::string readString(std::shared_ptr<librevenge::RVNGInputStream> input, const unsigned length);
00110 std::string readPlatformString(std::shared_ptr<librevenge::RVNGInputStream> input, bool bigEndian=false);
00111 
00112 void skip(std::shared_ptr<librevenge::RVNGInputStream> input, unsigned long numBytes);
00113 
00114 void seek(std::shared_ptr<librevenge::RVNGInputStream> input, unsigned long pos);
00115 void seekRelative(std::shared_ptr<librevenge::RVNGInputStream> input, long pos);
00116 
00117 unsigned long getRemainingLength(const std::shared_ptr<librevenge::RVNGInputStream> &input);
00118 
00119 double deg2rad(double value);
00120 double normalizeDegAngle(double degAngle);
00121 double normalizeRadAngle(double radAngle);
00122 
00123 void appendCharacters(librevenge::RVNGString &text, const char *characters, const size_t size,
00124                       const char *encoding);
00125 
00126 class EndOfStreamException
00127 {
00128 public:
00129   EndOfStreamException();
00130 };
00131 
00132 class GenericException
00133 {
00134 };
00135 
00136 // parser exceptions
00137 
00138 class FileAccessError
00139 {
00140 };
00141 
00142 class ParseError
00143 {
00144 };
00145 
00146 class UnsupportedFormat
00147 {
00148 };
00149 
00150 } // namespace libqxp
00151 
00152 #endif // INCLUDED_LIBQXP_UTILS_H
00153 
00154 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */