diff options
Diffstat (limited to 'src/lib/SW602Debug.h')
-rw-r--r-- | src/lib/SW602Debug.h | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/src/lib/SW602Debug.h b/src/lib/SW602Debug.h new file mode 100644 index 0000000..bfca716 --- /dev/null +++ b/src/lib/SW602Debug.h @@ -0,0 +1,219 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libsw602 project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_SW602_DEBUG_H +#define INCLUDED_SW602_DEBUG_H + +#include <string> + +#include <boost/shared_ptr.hpp> + +#include <librevenge/librevenge.h> +#include <librevenge-stream/librevenge-stream.h> + +#if defined(DEBUG_WITH_FILES) + +#include <fstream> +#include <sstream> +#include <string> +#include <vector> + +#include "SW602Types.h" + +//! some basic tools +namespace libsw602 +{ + +//! debugging tools +namespace Debug +{ +//! a debug function to store in a datafile in the current directory +//! WARNING: this function erase the file fileName if it exists +//! (if debug_with_files is not defined, does nothing) +bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName); +//! returns a file name from an ole/... name +std::string flattenFileName(std::string const &name); +} + +//! a basic stream (if debug_with_files is not defined, does nothing) +typedef std::stringstream DebugStream; + +//! an interface used to insert comment in a binary file, +//! written in ascii form (if debug_with_files is not defined, does nothing) +class DebugFile +{ +public: + //! constructor given the input file + explicit DebugFile(boost::shared_ptr<librevenge::RVNGInputStream> ip) + : m_fileName(""), m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { } + + //! resets the input + void setStream(boost::shared_ptr<librevenge::RVNGInputStream> ip) + { + m_input = ip; + } + //! destructor + ~DebugFile() + { + reset(); + } + //! opens/creates a file to store a result + bool open(std::string const &filename); + //! writes the current file and reset to zero + void reset() + { + write(); + m_fileName=""; + m_file.close(); + m_on = false; + m_notes.resize(0); + m_skipZones.resize(0); + m_actOffset = -1; + } + //! flushes the file + void write(); + //! adds a new position in the file + void addPos(long pos); + //! adds a note in the file, in actual position + void addNote(char const *note); + //! adds a not breaking delimiter in position \a pos + void addDelimiter(long pos, char c); + + //! skips the file zone defined by beginPos-endPos + void skipZone(long beginPos, long endPos) + { + if (m_on) m_skipZones.push_back(SW602Vec2<long>(beginPos, endPos)); + } + +protected: + //! sorts the position/note date + void sort(); + + //! the file name + mutable std::string m_fileName; + //! a stream which is open to write the file + mutable std::ofstream m_file; + //! a flag to know if the result stream is open or note + mutable bool m_on; + + //! the input + boost::shared_ptr<librevenge::RVNGInputStream> m_input; + + //! \brief a note and its position (used to sort all notes) + struct NotePos + { + //! empty constructor used by std::vector + NotePos() : m_pos(-1), m_text(""), m_breaking(false) { } + + //! constructor: given position and note + NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {} + //! note offset + long m_pos; + //! note text + std::string m_text; + //! flag to indicate a non breaking note + bool m_breaking; + + //! comparison operator based on the position + bool operator<(NotePos const &p) const + { + long diff = m_pos-p.m_pos; + if (diff) return (diff < 0) ? true : false; + if (m_breaking != p.m_breaking) return m_breaking; + return m_text < p.m_text; + } + /*! \struct NotePosLt + * \brief internal struct used to sort the notes, sorted by position + */ + struct NotePosLt + { + //! comparison operator + bool operator()(NotePos const &s1, NotePos const &s2) const + { + return s1 < s2; + } + }; + }; + + //! the actual offset (used to store note) + long m_actOffset; + //! list of notes + std::vector<NotePos> m_notes; + //! list of skipZone + std::vector<SW602Vec2<long> > m_skipZones; +}; + +} + +#else + +namespace libsw602 +{ + +namespace Debug +{ + +bool dumpFile(librevenge::RVNGBinaryData &, char const *) +{ + return true; +} +//! returns a file name from an ole/... name +std::string flattenFileName(std::string const &name) +{ + return name; +} + +} + +class DebugStream +{ +public: + template <class T> + DebugStream &operator<<(T const &) + { + return *this; + } + + static std::string str() + { + return std::string(""); + } + static void str(std::string const &) { } +}; + +class DebugFile +{ +public: + explicit DebugFile(boost::shared_ptr<librevenge::RVNGInputStream>) {} + DebugFile() {} + static void setStream(boost::shared_ptr<librevenge::RVNGInputStream>) { } + ~DebugFile() { } + + static bool open(std::string const &) + { + return true; + } + + static void addPos(long) {} + static void addNote(char const *) {} + static void addDelimiter(long, char) {} + + static void write() {} + static void reset() { } + + static void skipZone(long , long) {} +}; + +} + +#endif + +#endif + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |