MWAWDebug.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
00002 
00003 /* libmwaw
00004 * Version: MPL 2.0 / LGPLv2+
00005 *
00006 * The contents of this file are subject to the Mozilla Public License Version
00007 * 2.0 (the "License"); you may not use this file except in compliance with
00008 * the License or as specified alternatively below. You may obtain a copy of
00009 * the License at http://www.mozilla.org/MPL/
00010 *
00011 * Software distributed under the License is distributed on an "AS IS" basis,
00012 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013 * for the specific language governing rights and limitations under the
00014 * License.
00015 *
00016 * Major Contributor(s):
00017 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
00018 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00019 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00020 * Copyright (C) 2006, 2007 Andrew Ziem
00021 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
00022 *
00023 *
00024 * All Rights Reserved.
00025 *
00026 * For minor contributions see the git repository.
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00030 * in which case the provisions of the LGPLv2+ are applicable
00031 * instead of those above.
00032 */
00033 
00034 #ifndef MWAW_DEBUG
00035 #  define MWAW_DEBUG
00036 
00037 #include <string>
00038 
00039 #include "MWAWInputStream.hxx"
00040 
00041 class WPXBinaryData;
00042 
00043 #  if defined(DEBUG_WITH_FILES)
00044 #include <fstream>
00045 #include <sstream>
00046 #include <vector>
00048 namespace libmwaw
00049 {
00051 namespace Debug
00052 {
00056 bool dumpFile(WPXBinaryData &data, char const *fileName);
00058 std::string flattenFileName(std::string const &name);
00059 }
00060 
00062 typedef std::stringstream DebugStream;
00063 
00066 class DebugFile
00067 {
00068 public:
00070   DebugFile(MWAWInputStreamPtr ip=MWAWInputStreamPtr())
00071     : m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
00072 
00074   void setStream(MWAWInputStreamPtr ip) {
00075     m_input = ip;
00076   }
00078   ~DebugFile() {
00079     reset();
00080   }
00082   bool open(std::string const &filename);
00084   void reset() {
00085     write();
00086     m_file.close();
00087     m_on = false;
00088     m_notes.resize(0);
00089     m_skipZones.resize(0);
00090     m_actOffset = -1;
00091   }
00093   void write();
00095   void addPos(long pos);
00097   void addNote(char const *note);
00099   void addDelimiter(long pos, char c);
00100 
00102   void skipZone(long beginPos, long endPos) {
00103     if (m_on) m_skipZones.push_back(Vec2<long>(beginPos, endPos));
00104   }
00105 
00106 protected:
00108   void sort();
00109 
00111   mutable std::ofstream m_file;
00113   mutable bool m_on;
00114 
00116   MWAWInputStreamPtr m_input;
00117 
00119   struct NotePos {
00121     NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
00122 
00124     NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
00126     long m_pos;
00128     std::string m_text;
00130     bool m_breaking;
00131 
00133     bool operator<(NotePos const &p) const {
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 {
00144       bool operator()(NotePos const &s1, NotePos const &s2) const {
00145         return s1 < s2;
00146       }
00147     };
00148   };
00149 
00151   long m_actOffset;
00153   std::vector<NotePos> m_notes;
00155   std::vector<Vec2<long> > m_skipZones;
00156 };
00157 }
00158 #  else
00159 namespace libmwaw
00160 {
00161 namespace Debug
00162 {
00163 inline bool dumpFile(WPXBinaryData &, char const *)
00164 {
00165   return true;
00166 }
00168 inline std::string flattenFileName(std::string const &name)
00169 {
00170   return name;
00171 }
00172 }
00173 
00174 class DebugStream
00175 {
00176 public:
00177   template <class T>
00178   DebugStream &operator<<(T const &) {
00179     return *this;
00180   }
00181 
00182   static std::string str() {
00183     return std::string("");
00184   }
00185   static void str(std::string const &) { }
00186 };
00187 
00188 class DebugFile
00189 {
00190 public:
00191   DebugFile(MWAWInputStreamPtr) {}
00192   DebugFile() {}
00193   static void setStream(MWAWInputStreamPtr) {  }
00194   ~DebugFile() { }
00195 
00196   static bool open(std::string const &) {
00197     return true;
00198   }
00199 
00200   static void addPos(long ) {}
00201   static void addNote(char const *) {}
00202   static void addDelimiter(long, char) {}
00203 
00204   static void write() {}
00205   static void reset() { }
00206 
00207   static void skipZone(long , long ) {}
00208 };
00209 }
00210 #  endif
00211 
00212 #endif
00213 
00214 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: