MWAWDebug.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
18 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #ifndef MWAW_DEBUG
35 # define MWAW_DEBUG
36
37 #include <string>
38
39 #include "MWAWInputStream.hxx"
40
41 # if defined(DEBUG_WITH_FILES)
42 #include <fstream>
43 #include <sstream>
44 #include <string>
45 #include <vector>
47 namespace libmwaw
48 {
50 namespace Debug
51 {
55 bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName);
57 std::string flattenFileName(std::string const &name);
58 }
59
61 typedef std::stringstream DebugStream;
62
65 class DebugFile
66 {
67 public:
69 explicit DebugFile(MWAWInputStreamPtr ip)
70 : m_fileName(""), m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
71
73 void setStream(MWAWInputStreamPtr ip)
74 {
75 m_input = ip;
76 }
78 ~DebugFile()
79 {
80 reset();
81 }
83 bool open(std::string const &filename);
85 void reset()
86 {
87 write();
88 m_fileName="";
89 m_file.close();
90 m_on = false;
91 m_notes.resize(0);
92 m_skipZones.resize(0);
93 m_actOffset = -1;
94 }
96 void write();
98 void addPos(long pos);
100 void addNote(char const *note);
102 void addDelimiter(long pos, char c);
103
105 void skipZone(long beginPos, long endPos)
106 {
107 if (m_on) m_skipZones.push_back(MWAWVec2<long>(beginPos, endPos));
108 }
109
110 protected:
112 void sort();
113
115 mutable std::string m_fileName;
117 mutable std::ofstream m_file;
119 mutable bool m_on;
120
122 MWAWInputStreamPtr m_input;
123
125 struct NotePos {
127 NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
128
130 NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
132 long m_pos;
134 std::string m_text;
136 bool m_breaking;
137
139 bool operator<(NotePos const &p) const
140 {
141 long diff = m_pos-p.m_pos;
142 if (diff) return (diff < 0) ? true : false;
143 if (m_breaking != p.m_breaking) return m_breaking;
144 return m_text < p.m_text;
145 }
149 struct NotePosLt {
151 bool operator()(NotePos const &s1, NotePos const &s2) const
152 {
153 return s1 < s2;
154 }
155 };
156 };
157
159 long m_actOffset;
161 std::vector<NotePos> m_notes;
163 std::vector<MWAWVec2<long> > m_skipZones;
164 };
165 }
166 # else
167 namespace libmwaw
168 {
169 namespace Debug
170 {
171 inline bool dumpFile(librevenge::RVNGBinaryData &, char const *)
172 {
173 return true;
174 }
176 inline std::string flattenFileName(std::string const &name)
177 {
178 return name;
179 }
180 }
181
182 class DebugStream
183 {
184 public:
185 template <class T>
186 DebugStream &operator<<(T const &)
187 {
188 return *this;
189 }
190
191 static std::string str()
192 {
193 return std::string("");
194 }
195 static void str(std::string const &) { }
196 };
197
198 class DebugFile
199 {
200 public:
201 explicit DebugFile(MWAWInputStreamPtr) {}
202 DebugFile() {}
203 static void setStream(MWAWInputStreamPtr) { }
204 ~DebugFile() { }
205
206 static bool open(std::string const &)
207 {
208 return true;
209 }
210
211 static void addPos(long) {}
212 static void addNote(char const *) {}
213 static void addDelimiter(long, char) {}
214
215 static void write() {}
216 static void reset() { }
217
218 static void skipZone(long, long) {}
219 };
220 }
221 # endif
222
223 #endif
224
225 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
libmwaw::DebugFile::skipZone
void skipZone(long beginPos, long endPos)
skips the file zone defined by beginPos-endPos
Definition: MWAWDebug.hxx:105
libmwaw::DebugFile::DebugFile
DebugFile(MWAWInputStreamPtr ip)
constructor given the input file
Definition: MWAWDebug.hxx:69
libmwaw::DebugFile::NotePos::NotePosLt::operator()
bool operator()(NotePos const &s1, NotePos const &s2) const
comparison operator
Definition: MWAWDebug.hxx:151
libmwaw::DebugFile::NotePos::NotePos
NotePos(long p, std::string const &n, bool br=true)
constructor: given position and note
Definition: MWAWDebug.hxx:130
libmwaw::DebugFile::setStream
void setStream(MWAWInputStreamPtr ip)
resets the input
Definition: MWAWDebug.hxx:73
libmwaw::DebugFile
an interface used to insert comment in a binary file, written in ascii form (if debug_with_files is n...
Definition: MWAWDebug.hxx:65
libmwaw::DebugFile::m_fileName
std::string m_fileName
the file name
Definition: MWAWDebug.hxx:115
libmwaw::operator<<
std::ostream & operator<<(std::ostream &o, PrinterInfo const &r)
operator<< for a PrinterInfo
Definition: MWAWPrinter.cxx:211
libmwaw::DebugFile::NotePos
a note and its position (used to sort all notes)
Definition: MWAWDebug.hxx:125
libmwaw
namespace used to regroup all libwpd functions, enumerations which we have redefined for internal usa...
Definition: libmwaw_internal.cxx:50
libmwaw::Debug::dumpFile
bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName)
a debug function to store in a datafile in the current directory WARNING: this function erase the fil...
Definition: MWAWDebug.cxx:197
libmwaw::DebugFile::NotePos::m_breaking
bool m_breaking
flag to indicate a non breaking note
Definition: MWAWDebug.hxx:136
libmwaw::DebugFile::m_actOffset
long m_actOffset
the actual offset (used to store note)
Definition: MWAWDebug.hxx:159
libmwaw::DebugFile::NotePos::operator<
bool operator<(NotePos const &p) const
comparison operator based on the position
Definition: MWAWDebug.hxx:139
libmwaw::DebugFile::NotePos::m_text
std::string m_text
note text
Definition: MWAWDebug.hxx:134
libmwaw::DebugFile::m_notes
std::vector< NotePos > m_notes
list of notes
Definition: MWAWDebug.hxx:161
libmwaw::DebugFile::NotePos::NotePosLt
internal struct used to sort the notes, sorted by position
Definition: MWAWDebug.hxx:149
libmwaw::DebugFile::NotePos::NotePos
NotePos()
empty constructor used by std::vector
Definition: MWAWDebug.hxx:127
libmwaw::DebugFile::~DebugFile
~DebugFile()
destructor
Definition: MWAWDebug.hxx:78
MWAWInputStreamPtr
shared_ptr< MWAWInputStream > MWAWInputStreamPtr
a smart pointer of MWAWInputStream
Definition: libmwaw_internal.hxx:517
libmwaw::DebugFile::m_input
MWAWInputStreamPtr m_input
the input
Definition: MWAWDebug.hxx:122
MWAWInputStream.hxx
libmwaw::DebugFile::m_file
std::ofstream m_file
a stream which is open to write the file
Definition: MWAWDebug.hxx:117
libmwaw::Debug::flattenFileName
std::string flattenFileName(std::string const &name)
returns a file name from an ole/... name
Definition: MWAWDebug.cxx:212
libmwaw::DebugFile::reset
void reset()
writes the current file and reset to zero
Definition: MWAWDebug.hxx:85
libmwaw::DebugStream
std::stringstream DebugStream
a basic stream (if debug_with_files is not defined, does nothing)
Definition: MWAWDebug.hxx:61
libmwaw::DebugFile::NotePos::m_pos
long m_pos
note offset
Definition: MWAWDebug.hxx:132
libmwaw::DebugFile::m_skipZones
std::vector< MWAWVec2< long > > m_skipZones
list of skipZone
Definition: MWAWDebug.hxx:163
libmwaw::DebugFile::m_on
bool m_on
a flag to know if the result stream is open or note
Definition: MWAWDebug.hxx:119
MWAWVec2< long >