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  * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
00004  * Copyright (C) 2006, 2007 Andrew Ziem
00005  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00006  * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
00007  * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Library General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00022  *
00023  * For further information visit http://libwps.sourceforge.net
00024  */
00025 
00026 /* "This product is not manufactured, approved, or supported by
00027  * Corel Corporation or Corel Corporation Limited."
00028  */
00029 
00030 #ifndef WPS_DEBUG
00031 #  define WPS_DEBUG
00032 
00033 #include <string>
00034 
00035 #include "libwps_internal.h"
00036 
00037 class WPXBinaryData;
00038 
00039 #  if defined(DEBUG_WITH_FILES)
00040 #include <fstream>
00041 #include <map>
00042 #include <sstream>
00043 #include <vector>
00044 
00046 namespace libwps
00047 {
00049 namespace Debug
00050 {
00054 bool dumpFile(WPXBinaryData &data, char const *fileName);
00055 
00057 std::string flattenFileName(std::string const &name);
00058 }
00059 
00061 typedef std::stringstream DebugStream;
00062 
00065 class DebugFile
00066 {
00067 public:
00069         DebugFile(WPXInputStreamPtr ip=WPXInputStreamPtr())
00070                 : m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
00071 
00073         void setStream(WPXInputStreamPtr ip)
00074         {
00075                 m_input = ip;
00076         }
00078         ~DebugFile()
00079         {
00080                 reset();
00081         }
00083         bool open(std::string const &filename);
00085         void reset()
00086         {
00087                 write();
00088                 m_file.close();
00089                 m_on = false;
00090                 m_notes.resize(0);
00091                 m_skipZones.resize(0);
00092                 m_actOffset = -1;
00093         }
00095         void addPos(long pos);
00097         void addNote(char const *note);
00099         void addDelimiter(long pos, char c);
00100 
00102         void skipZone(int beginPos, int endPos)
00103         {
00104                 if (m_on) m_skipZones.push_back(Vec2i(beginPos, endPos));
00105         }
00106 
00107 protected:
00109         void write();
00110 
00112         void sort();
00113 
00115         mutable std::ofstream m_file;
00117         mutable bool m_on;
00118 
00120         WPXInputStreamPtr m_input;
00121 
00123         struct NotePos
00124         {
00126                 NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
00127 
00129                 NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
00131                 long m_pos;
00133                 std::string m_text;
00135                 bool m_breaking;
00136 
00138                 bool operator<(NotePos const &p) const
00139                 {
00140                         long diff = m_pos-p.m_pos;
00141                         if (diff) return (diff < 0) ? true : false;
00142                         if (m_breaking != p.m_breaking) return m_breaking;
00143                         return m_text < p.m_text;
00144                 }
00148                 struct NotePosLt
00149                 {
00151                         bool operator()(NotePos const &s1, NotePos const &s2) const
00152                         {
00153                                 return s1 < s2;
00154                         }
00155                 };
00159                 typedef std::map<NotePos, int,struct NotePosLt> Map;
00160         };
00161 
00163         int m_actOffset;
00165         std::vector<NotePos> m_notes;
00167         std::vector<Vec2i> m_skipZones;
00168 };
00169 }
00170 #  else
00171 namespace libwps
00172 {
00173 namespace Debug
00174 {
00175 inline bool dumpFile(WPXBinaryData &, char const *)
00176 {
00177         return true;
00178 }
00179 
00180 inline std::string flattenFileName(std::string const &name)
00181 {
00182         return name;
00183 }
00184 }
00185 
00186 class DebugStream
00187 {
00188 public:
00189         template <class T>
00190         DebugStream &operator<<(T const &)
00191         {
00192                 return *this;
00193         }
00194 
00195         std::string str() const
00196         {
00197                 return std::string("");
00198         }
00199         void str(std::string const &) { }
00200 };
00201 
00202 class DebugFile
00203 {
00204 public:
00205         DebugFile(WPXInputStreamPtr) {}
00206         DebugFile() {}
00207         void setStream(WPXInputStreamPtr) {  }
00208         ~DebugFile() { }
00209 
00210         bool open(std::string const &)
00211         {
00212                 return true;
00213         }
00214 
00215         void addPos(long ) {}
00216         void addNote(char const *) {}
00217         void addDelimiter(long, char) {}
00218 
00219         void reset() { }
00220 
00221         void skipZone(int , int ) {}
00222 };
00223 }
00224 #  endif
00225 
00226 #endif
00227 
00228 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */