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>
46 //! some basic tools
47 namespace libmwaw
48 {
49 //! debugging tools
50 namespace Debug
51 {
52 //! a debug function to store in a datafile in the current directory
53 //! WARNING: this function erase the file fileName if it exists
54 //! (if debug_with_files is not defined, does nothing)
55 bool dumpFile(librevenge::RVNGBinaryData &data, char const *fileName);
56 //! returns a file name from an ole/... name
57 std::string flattenFileName(std::string const &name);
58 }
59
60 //! a basic stream (if debug_with_files is not defined, does nothing)
61 typedef std::stringstream DebugStream;
62
63 //! an interface used to insert comment in a binary file,
64 //! written in ascii form (if debug_with_files is not defined, does nothing)
65 class DebugFile
66 {
67 public:
68 //! constructor given the input file
DebugFile(MWAWInputStreamPtr const & ip)69 explicit DebugFile(MWAWInputStreamPtr const &ip)
70 : m_fileName("")
71 , m_file()
72 , m_on(false)
73 , m_input(ip)
74 , m_actOffset(-1)
75 , m_notes()
76 , m_skipZones()
77 {
78 }
79
80 //! resets the input
setStream(MWAWInputStreamPtr const & ip)81 void setStream(MWAWInputStreamPtr const &ip)
82 {
83 m_input = ip;
84 }
85 //! destructor
~DebugFile()86 ~DebugFile()
87 {
88 reset();
89 }
90 //! opens/creates a file to store a result
91 bool open(std::string const &filename);
92 //! writes the current file and reset to zero
reset()93 void reset()
94 {
95 write();
96 m_fileName="";
97 m_file.close();
98 m_on = false;
99 m_notes.resize(0);
100 m_skipZones.resize(0);
101 m_actOffset = -1;
102 }
103 //! flushes the file
104 void write();
105 //! adds a new position in the file
106 void addPos(long pos);
107 //! adds a note in the file, in actual position
108 void addNote(char const *note);
109 //! adds a not breaking delimiter in position \a pos
110 void addDelimiter(long pos, char c);
111
112 //! skips the file zone defined by beginPos-endPos
skipZone(long beginPos,long endPos)113 void skipZone(long beginPos, long endPos)
114 {
115 if (m_on) m_skipZones.push_back(MWAWVec2<long>(beginPos, endPos));
116 }
117
118 protected:
119 //! sorts the position/note date
120 void sort();
121
122 //! the file name
123 mutable std::string m_fileName;
124 //! a stream which is open to write the file
125 mutable std::ofstream m_file;
126 //! a flag to know if the result stream is open or note
127 mutable bool m_on;
128
129 //! the input
130 MWAWInputStreamPtr m_input;
131
132 //! \brief a note and its position (used to sort all notes)
133 struct NotePos {
134 //! empty constructor used by std::vector
NotePoslibmwaw::DebugFile::NotePos135 NotePos()
136 : m_pos(-1)
137 , m_text("")
138 , m_breaking(false)
139 {
140 }
141
142 //! constructor: given position and note
NotePoslibmwaw::DebugFile::NotePos143 NotePos(long p, std::string const &n, bool br=true)
144 : m_pos(p)
145 , m_text(n)
146 , m_breaking(br)
147 {
148 }
149 //! note offset
150 long m_pos;
151 //! note text
152 std::string m_text;
153 //! flag to indicate a non breaking note
154 bool m_breaking;
155
156 //! comparison operator based on the position
operator <libmwaw::DebugFile::NotePos157 bool operator<(NotePos const &p) const
158 {
159 long diff = m_pos-p.m_pos;
160 if (diff) return (diff < 0) ? true : false;
161 if (m_breaking != p.m_breaking) return m_breaking;
162 return m_text < p.m_text;
163 }
164 /*! \struct NotePosLt
165 * \brief internal struct used to sort the notes, sorted by position
166 */
167 struct NotePosLt {
168 //! comparison operator
operator ()libmwaw::DebugFile::NotePos::NotePosLt169 bool operator()(NotePos const &s1, NotePos const &s2) const
170 {
171 return s1 < s2;
172 }
173 };
174 };
175
176 //! the actual offset (used to store note)
177 long m_actOffset;
178 //! list of notes
179 std::vector<NotePos> m_notes;
180 //! list of skipZone
181 std::vector<MWAWVec2<long> > m_skipZones;
182 };
183 }
184 # else
185 namespace libmwaw
186 {
187 namespace Debug
188 {
dumpFile(librevenge::RVNGBinaryData &,char const *)189 inline bool dumpFile(librevenge::RVNGBinaryData &, char const *)
190 {
191 return true;
192 }
193 //! returns a file name from an ole/... name
flattenFileName(std::string const & name)194 inline std::string flattenFileName(std::string const &name)
195 {
196 return name;
197 }
198 }
199
200 class DebugStream
201 {
202 public:
203 template <class T>
operator <<(T const &)204 DebugStream &operator<<(T const &)
205 {
206 return *this;
207 }
208
str()209 static std::string str()
210 {
211 return std::string("");
212 }
str(std::string const &)213 static void str(std::string const &) { }
214 };
215
216 class DebugFile
217 {
218 public:
DebugFile(MWAWInputStreamPtr const &)219 explicit DebugFile(MWAWInputStreamPtr const &) {}
DebugFile()220 DebugFile() {}
setStream(MWAWInputStreamPtr const &)221 static void setStream(MWAWInputStreamPtr const &) { }
~DebugFile()222 ~DebugFile() { }
223
open(std::string const &)224 static bool open(std::string const &)
225 {
226 return true;
227 }
228
addPos(long)229 static void addPos(long) {}
addNote(char const *)230 static void addNote(char const *) {}
addDelimiter(long,char)231 static void addDelimiter(long, char) {}
232
write()233 static void write() {}
reset()234 static void reset() { }
235
skipZone(long,long)236 static void skipZone(long, long) {}
237 };
238 }
239 # endif
240
241 #endif
242
243 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
244