STOFFDebug.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 /* libstaroffice
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 STOFF_DEBUG
00035 #  define STOFF_DEBUG
00036 
00037 #include <string>
00038 
00039 #include "STOFFInputStream.hxx"
00040 
00041 #  if defined(DEBUG_WITH_FILES)
00042 #include <fstream>
00043 #include <sstream>
00044 #include <string>
00045 #include <vector>
00047 namespace libstoff
00048 {
00050 namespace Debug
00051 {
00055 bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName);
00057 std::string flattenFileName(std::string const &name);
00058 }
00059 
00061 typedef std::stringstream DebugStream;
00062 
00065 class DebugFile
00066 {
00067 public:
00069   explicit DebugFile(STOFFInputStreamPtr ip=STOFFInputStreamPtr())
00070     : m_fileName(""), m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
00071 
00073   void setStream(STOFFInputStreamPtr 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_fileName="";
00089     m_file.close();
00090     m_on = false;
00091     m_notes.resize(0);
00092     m_skipZones.resize(0);
00093     m_actOffset = -1;
00094   }
00096   void write();
00098   void addPos(long pos);
00100   void addNote(char const *note);
00102   void addDelimiter(long pos, char c);
00103 
00105   void skipZone(long beginPos, long endPos)
00106   {
00107     if (m_on) m_skipZones.push_back(STOFFVec2<long>(beginPos, endPos));
00108   }
00109 
00110 protected:
00112   void sort();
00113 
00115   mutable std::string m_fileName;
00117   mutable std::ofstream m_file;
00119   mutable bool m_on;
00120 
00122   STOFFInputStreamPtr m_input;
00123 
00125   struct NotePos {
00127     NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
00128 
00130     NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
00132     long m_pos;
00134     std::string m_text;
00136     bool m_breaking;
00137 
00139     bool operator<(NotePos const &p) const
00140     {
00141       long diff = m_pos-p.m_pos;
00142       if (diff) return (diff < 0) ? true : false;
00143       if (m_breaking != p.m_breaking) return m_breaking;
00144       return m_text < p.m_text;
00145     }
00149     struct NotePosLt {
00151       bool operator()(NotePos const &s1, NotePos const &s2) const
00152       {
00153         return s1 < s2;
00154       }
00155     };
00156   };
00157 
00159   long m_actOffset;
00161   std::vector<NotePos> m_notes;
00163   std::vector<STOFFVec2<long> > m_skipZones;
00164 };
00165 }
00166 #  else
00167 namespace libstoff
00168 {
00169 namespace Debug
00170 {
00171 inline bool dumpFile(librevenge::RVNGBinaryData &, char const *)
00172 {
00173   return true;
00174 }
00176 inline std::string flattenFileName(std::string const &name)
00177 {
00178   return name;
00179 }
00180 }
00181 
00182 class DebugStream
00183 {
00184 public:
00185   template <class T>
00186   DebugStream &operator<<(T const &)
00187   {
00188     return *this;
00189   }
00190 
00191   static std::string str()
00192   {
00193     return std::string("");
00194   }
00195   static void str(std::string const &) { }
00196 };
00197 
00198 class DebugFile
00199 {
00200 public:
00201   explicit DebugFile(STOFFInputStreamPtr) {}
00202   DebugFile() {}
00203   static void setStream(STOFFInputStreamPtr) {  }
00204   ~DebugFile() { }
00205 
00206   static bool open(std::string const &)
00207   {
00208     return true;
00209   }
00210 
00211   static void addPos(long) {}
00212   static void addNote(char const *) {}
00213   static void addDelimiter(long, char) {}
00214 
00215   static void write() {}
00216   static void reset() { }
00217 
00218   static void skipZone(long , long) {}
00219 };
00220 }
00221 #  endif
00222 
00223 #endif
00224 
00225 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: