libcdr_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 libcdr 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 __LIBCDR_UTILS_H__
00011 #define __LIBCDR_UTILS_H__
00012 
00013 #include <stdio.h>
00014 #include <string>
00015 #include <math.h>
00016 #include <vector>
00017 #include <librevenge-stream/librevenge-stream.h>
00018 #include <librevenge/librevenge.h>
00019 
00020 #ifndef M_PI
00021 #define M_PI 3.14159265358979323846
00022 #endif
00023 
00024 #define CDR_EPSILON 1E-6
00025 #define CDR_ALMOST_ZERO(m) (fabs(m) <= CDR_EPSILON)
00026 
00027 #if defined(__clang__) || defined(__GNUC__)
00028 #  define CDR_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
00029 #else
00030 #  define CDR_ATTRIBUTE_PRINTF(fmt, arg)
00031 #endif
00032 
00033 #ifdef _MSC_VER
00034 
00035 typedef unsigned char uint8_t;
00036 typedef unsigned short uint16_t;
00037 typedef short int16_t;
00038 typedef unsigned uint32_t;
00039 typedef int int32_t;
00040 typedef unsigned __int64 uint64_t;
00041 typedef __int64 int64_t;
00042 
00043 #else
00044 
00045 #ifdef HAVE_CONFIG_H
00046 
00047 #include <config.h>
00048 
00049 #ifdef HAVE_STDINT_H
00050 #include <stdint.h>
00051 #endif
00052 
00053 #ifdef HAVE_INTTYPES_H
00054 #include <inttypes.h>
00055 #endif
00056 
00057 #else
00058 
00059 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
00060 #include <stdint.h>
00061 #include <inttypes.h>
00062 
00063 #endif
00064 
00065 #endif
00066 
00067 // debug message includes source file and line number
00068 //#define VERBOSE_DEBUG 1
00069 
00070 // do nothing with debug messages in a release compile
00071 #ifdef DEBUG
00072 namespace libcdr
00073 {
00074 void debugPrint(const char *format, ...) CDR_ATTRIBUTE_PRINTF(1, 2);
00075 }
00076 #ifdef VERBOSE_DEBUG
00077 #define CDR_DEBUG_MSG(M) libcdr::debugPrint("%15s:%5d: ", __FILE__, __LINE__); libcdr::debugPrint M
00078 #define CDR_DEBUG(M) M
00079 #else
00080 #define CDR_DEBUG_MSG(M) libcdr::debugPrint M
00081 #define CDR_DEBUG(M) M
00082 #endif
00083 #else
00084 #define CDR_DEBUG_MSG(M)
00085 #define CDR_DEBUG(M)
00086 #endif
00087 
00088 namespace libcdr
00089 {
00090 
00091 uint8_t readU8(librevenge::RVNGInputStream *input, bool bigEndian=false);
00092 uint16_t readU16(librevenge::RVNGInputStream *input, bool bigEndian=false);
00093 uint32_t readU32(librevenge::RVNGInputStream *input, bool bigEndian=false);
00094 uint64_t readU64(librevenge::RVNGInputStream *input, bool bigEndian=false);
00095 int32_t readS32(librevenge::RVNGInputStream *input, bool bigEndian=false);
00096 int16_t readS16(librevenge::RVNGInputStream *input, bool bigEndian=false);
00097 
00098 double readDouble(librevenge::RVNGInputStream *input, bool bigEndian=false);
00099 
00100 double readFixedPoint(librevenge::RVNGInputStream *input, bool bigEndian=false);
00101 
00102 unsigned long getLength(librevenge::RVNGInputStream *input);
00103 unsigned long getRemainingLength(librevenge::RVNGInputStream *input);
00104 
00105 int cdr_round(double d);
00106 
00107 void writeU16(librevenge::RVNGBinaryData &buffer, const int value);
00108 void writeU32(librevenge::RVNGBinaryData &buffer, const int value);
00109 void appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters, unsigned short charset);
00110 void appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters);
00111 
00112 #ifdef DEBUG
00113 const char *toFourCC(unsigned value, bool bigEndian=false);
00114 #endif
00115 
00116 class EndOfStreamException
00117 {
00118 };
00119 
00120 class GenericException
00121 {
00122 };
00123 
00124 class UnknownPrecisionException
00125 {
00126 };
00127 
00128 class EncodingException
00129 {
00130 };
00131 
00132 } // namespace libcdr
00133 
00134 #endif // __LIBCDR_UTILS_H__
00135 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */