libetonyek_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 libetonyek 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 LIBETONYEK_UTILS_H_INCLUDED
00011 #define LIBETONYEK_UTILS_H_INCLUDED
00012 
00013 #include <cmath>
00014 #include <string>
00015 
00016 #include <boost/shared_ptr.hpp>
00017 
00018 #include <librevenge/librevenge.h>
00019 #include <librevenge-stream/librevenge-stream.h>
00020 
00021 #ifdef _MSC_VER
00022 
00023 typedef unsigned char uint8_t;
00024 typedef unsigned short uint16_t;
00025 typedef unsigned uint32_t;
00026 typedef unsigned __int64 uint64_t;
00027 typedef signed char int8_t;
00028 typedef short int16_t;
00029 typedef int int32_t;
00030 typedef __int64 int64_t;
00031 
00032 #else
00033 
00034 #ifdef HAVE_CONFIG_H
00035 
00036 #include <config.h>
00037 
00038 #ifdef HAVE_STDINT_H
00039 #include <stdint.h>
00040 #endif
00041 
00042 #ifdef HAVE_INTTYPES_H
00043 #include <inttypes.h>
00044 #endif
00045 
00046 #else
00047 
00048 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
00049 #include <stdint.h>
00050 #include <inttypes.h>
00051 
00052 #endif
00053 
00054 #endif
00055 
00056 #define ETONYEK_EPSILON 1e-9
00057 #define ETONYEK_ALMOST_ZERO(x) (std::fabs(x) < ETONYEK_EPSILON)
00058 
00059 #define ETONYEK_NUM_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))
00060 
00061 #if defined(__clang__) || defined(__GNUC__)
00062 #  define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
00063 #else
00064 #  define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg)
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 libetonyek
00073 {
00074 void debugPrint(const char *format, ...) ETONYEK_ATTRIBUTE_PRINTF(1, 2);
00075 }
00076 #ifdef VERBOSE_DEBUG
00077 #define ETONYEK_DEBUG_MSG(M) libetonyek::debugPrint("%15s:%5d: ", FILE, LINE); libetonyek::debugPrint M
00078 #define ETONYEK_DEBUG(M) M
00079 #else
00080 #define ETONYEK_DEBUG_MSG(M) libetonyek::debugPrint M
00081 #define ETONYEK_DEBUG(M) M
00082 #endif
00083 #else
00084 #define ETONYEK_DEBUG_MSG(M)
00085 #define ETONYEK_DEBUG(M)
00086 #endif
00087 
00088 namespace libetonyek
00089 {
00090 
00091 struct IWORKColor;
00092 struct IWORKStroke;
00093 
00094 /* Constants */
00095 const double etonyek_half_pi(1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404326e+00);
00096 const double etonyek_third_pi(1.04719755119659774615421446109316762806572313312503527365831486410260546876206966620934494178070568932738269550e+00);
00097 const double etonyek_pi(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00);
00098 const double etonyek_two_pi(6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617303e+00);
00099 
00100 const double etonyek_root_three(1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614142e+00);
00101 const double etonyek_root_two(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623e+00);
00102 
00103 // Apple Numbers timestamp starts from 01 Jan 2001 00:00:00
00104 const unsigned ETONYEK_EPOCH_BEGIN(978307200);
00105 
00106 struct EtonyekDummyDeleter
00107 {
00108   void operator()(void *) {}
00109 };
00110 
00111 typedef boost::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr_t;
00112 
00113 uint8_t readU8(const RVNGInputStreamPtr_t &input, bool = false);
00114 uint16_t readU16(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
00115 uint32_t readU32(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
00116 uint64_t readU64(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
00117 
00118 uint64_t readUVar(const RVNGInputStreamPtr_t &input);
00119 int64_t readSVar(const RVNGInputStreamPtr_t &input);
00120 
00121 double readDouble(const RVNGInputStreamPtr_t &input);
00122 float readFloat(const RVNGInputStreamPtr_t &input);
00123 
00124 unsigned long getLength(const RVNGInputStreamPtr_t &input);
00125 unsigned long getRemainingLength(const RVNGInputStreamPtr_t &input);
00126 
00133 bool approxEqual(double x, double y, double eps = ETONYEK_EPSILON);
00134 
00135 template<class T>
00136 bool approxEqual(const T &left, const T &right, const double eps = ETONYEK_EPSILON)
00137 {
00138   assert(left.length() == right.length());
00139 
00140   for (int i = 0; i != left.length(); ++i)
00141   {
00142     if (!approxEqual(left[i], right[i], eps))
00143       return false;
00144   }
00145   return true;
00146 }
00147 
00153 double pt2in(double d);
00154 
00160 double deg2rad(double value);
00161 
00167 double rad2deg(double value);
00168 
00169 librevenge::RVNGString makeColor(const IWORKColor &color);
00170 
00171 void writeBorder(const IWORKStroke &stroke, const char *name, librevenge::RVNGPropertyList &props);
00172 
00173 class EndOfStreamException
00174 {
00175 };
00176 
00177 class GenericException
00178 {
00179 };
00180 
00181 } // namespace libetonyek
00182 
00183 #endif // LIBETONYEK_UTILS_H_INCLUDED
00184 
00185 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */