WPSDebug.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* libwps
00003  * Version: MPL 2.0 / LGPLv2.1+
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  * Major Contributor(s):
00010  * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
00011  * Copyright (C) 2006, 2007 Andrew Ziem
00012  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00013  * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
00014  * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
00015  *
00016  * For minor contributions see the git repository.
00017  *
00018  * Alternatively, the contents of this file may be used under the terms
00019  * of the GNU Lesser General Public License Version 2.1 or later
00020  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
00021  * applicable instead of those above.
00022  *
00023  * For further information visit http://libwps.sourceforge.net
00024  */
00025 
00026 #ifndef WPS_DEBUG
00027 #  define WPS_DEBUG
00028 
00029 #include <string>
00030 
00031 #include "libwps_internal.h"
00032 
00033 #  if defined(DEBUG_WITH_FILES)
00034 #include <fstream>
00035 #include <map>
00036 #include <sstream>
00037 #include <vector>
00038 
00040 namespace libwps
00041 {
00043 namespace Debug
00044 {
00048 bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName);
00049 
00051 std::string flattenFileName(std::string const &name);
00052 }
00053 
00055 typedef std::stringstream DebugStream;
00056 
00059 class DebugFile
00060 {
00061 public:
00063         DebugFile(RVNGInputStreamPtr ip=RVNGInputStreamPtr())
00064                 : m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
00065 
00067         void setStream(RVNGInputStreamPtr ip)
00068         {
00069                 m_input = ip;
00070         }
00072         ~DebugFile()
00073         {
00074                 reset();
00075         }
00077         bool open(std::string const &filename);
00079         void reset()
00080         {
00081                 write();
00082                 m_file.close();
00083                 m_on = false;
00084                 m_notes.resize(0);
00085                 m_skipZones.resize(0);
00086                 m_actOffset = -1;
00087         }
00089         void addPos(long pos);
00091         void addNote(char const *note);
00093         void addDelimiter(long pos, char c);
00094 
00096         void skipZone(long beginPos, long endPos)
00097         {
00098                 if (m_on) m_skipZones.push_back(Vec2<long>(beginPos, endPos));
00099         }
00100 
00101 protected:
00103         void write();
00104 
00106         void sort();
00107 
00109         mutable std::ofstream m_file;
00111         mutable bool m_on;
00112 
00114         RVNGInputStreamPtr m_input;
00115 
00117         struct NotePos
00118         {
00120                 NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
00121 
00123                 NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
00125                 long m_pos;
00127                 std::string m_text;
00129                 bool m_breaking;
00130 
00132                 bool operator<(NotePos const &p) const
00133                 {
00134                         long diff = m_pos-p.m_pos;
00135                         if (diff) return (diff < 0) ? true : false;
00136                         if (m_breaking != p.m_breaking) return m_breaking;
00137                         return m_text < p.m_text;
00138                 }
00142                 struct NotePosLt
00143                 {
00145                         bool operator()(NotePos const &s1, NotePos const &s2) const
00146                         {
00147                                 return s1 < s2;
00148                         }
00149                 };
00153                 typedef std::map<NotePos, int,struct NotePosLt> Map;
00154         };
00155 
00157         long m_actOffset;
00159         std::vector<NotePos> m_notes;
00161         std::vector<Vec2<long> > m_skipZones;
00162 };
00163 }
00164 #  else
00165 namespace libwps
00166 {
00167 namespace Debug
00168 {
00169 inline bool dumpFile(librevenge::RVNGBinaryData &, char const *)
00170 {
00171         return true;
00172 }
00173 
00174 inline std::string flattenFileName(std::string const &name)
00175 {
00176         return name;
00177 }
00178 }
00179 
00180 class DebugStream
00181 {
00182 public:
00183         template <class T>
00184         DebugStream &operator<<(T const &)
00185         {
00186                 return *this;
00187         }
00188 
00189         static std::string str()
00190         {
00191                 return std::string("");
00192         }
00193         static void str(std::string const &) { }
00194 };
00195 
00196 class DebugFile
00197 {
00198 public:
00199         DebugFile(RVNGInputStreamPtr) {}
00200         DebugFile() {}
00201         static void setStream(RVNGInputStreamPtr) {  }
00202         ~DebugFile() { }
00203 
00204         static bool open(std::string const &)
00205         {
00206                 return true;
00207         }
00208 
00209         static void addPos(long) {}
00210         static void addNote(char const *) {}
00211         static void addDelimiter(long, char) {}
00212 
00213         static void reset() { }
00214 
00215         static void skipZone(long , long) {}
00216 };
00217 }
00218 #  endif
00219 
00220 #endif
00221 
00222 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */