libzmf_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 a part of the libzmf 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 LIBZMF_UTILS_H_INCLUDED
00011 #define LIBZMF_UTILS_H_INCLUDED
00012 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 #include <cmath>
00018 #include <memory>
00019 #include <string>
00020 #include <bitset>
00021 
00022 #include <boost/cstdint.hpp>
00023 
00024 #include <librevenge-stream/librevenge-stream.h>
00025 #include <librevenge/librevenge.h>
00026 
00027 #ifdef DEBUG
00028 #include <boost/type_index.hpp>
00029 #include <regex>
00030 #endif
00031 
00032 #define ZMF_EPSILON 1E-6
00033 #define ZMF_ALMOST_ZERO(m) (std::fabs(m) <= ZMF_EPSILON)
00034 
00035 #ifdef DEBUG
00036 
00037 #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
00038 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
00039 #else
00040 #define ZMF_ATTRIBUTE_PRINTF(fmt, arg)
00041 #endif
00042 
00043 #define ZMF_DEBUG_MSG(M) libzmf::debugPrint M
00044 #define ZMF_DEBUG(M) M
00045 
00046 #else // !DEBUG
00047 
00048 // do nothing with debug messages in a release compile
00049 #define ZMF_DEBUG_MSG(M)
00050 #define ZMF_DEBUG(M)
00051 
00052 #endif // DEBUG
00053 
00054 #define ZMF_NUM_ELEMENTS(array) sizeof(array)/sizeof(array[0])
00055 
00056 namespace libzmf
00057 {
00058 
00059 template<typename T>
00060 std::string prettyTypeName()
00061 {
00062 #ifdef DEBUG
00063   auto str = boost::typeindex::type_id<T>().pretty_name();
00064   str = std::regex_replace(str, std::regex("libzmf::"), "");
00065   str = std::regex_replace(str, std::regex("boost::"), "");
00066   return str;
00067 #else
00068   return "";
00069 #endif
00070 }
00071 
00072 typedef std::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr;
00073 
00074 struct ZMFDummyDeleter
00075 {
00076   void operator()(void *) {}
00077 };
00078 
00079 uint8_t readU8(const RVNGInputStreamPtr &input, bool = false);
00080 uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian=false);
00081 uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian=false);
00082 uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian=false);
00083 int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian=false);
00084 
00085 float readFloat(const RVNGInputStreamPtr &input, bool bigEndian=false);
00086 
00087 const unsigned char *readNBytes(const RVNGInputStreamPtr &input, unsigned long numBytes);
00088 
00089 void skip(const RVNGInputStreamPtr &input, unsigned long numBytes);
00090 
00091 void seek(const RVNGInputStreamPtr &input, unsigned long pos);
00092 void seekRelative(const RVNGInputStreamPtr &input, long pos);
00093 
00094 unsigned long getLength(const RVNGInputStreamPtr &input);
00095 
00096 void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size,
00097                       const char *encoding);
00098 
00099 void writeU16(librevenge::RVNGBinaryData &buffer, const int value);
00100 void writeU32(librevenge::RVNGBinaryData &buffer, const int value);
00101 
00102 double rad2deg(double value);
00103 double normalizeAngle(double radAngle);
00104 
00105 template<std::size_t numBytes>
00106 std::bitset<numBytes * 8> bytesToBitset(const uint8_t *data)
00107 {
00108   std::bitset<numBytes * 8> b;
00109 
00110   for (std::size_t i = 0; i < numBytes; ++i)
00111   {
00112     uint8_t cur = data[i];
00113     std::size_t offset = i * 8;
00114 
00115     for (int j = 0; j < 8; ++j)
00116     {
00117       b[offset++] = cur & 1;
00118       cur >>= 1;
00119     }
00120   }
00121 
00122   return b;
00123 }
00124 
00125 template<typename T>
00126 double um2in(T micrometers)
00127 {
00128   return micrometers / 1000.0 / 25.4;
00129 }
00130 
00131 #ifdef DEBUG
00132 void debugPrint(const char *format, ...) ZMF_ATTRIBUTE_PRINTF(1, 2);
00133 #endif
00134 
00135 struct EndOfStreamException
00136 {
00137   EndOfStreamException();
00138 };
00139 
00140 struct GenericException
00141 {
00142 };
00143 
00144 }
00145 
00146 #endif // LIBZMF_UTILS_H_INCLUDED
00147 
00148 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */