Debian Codegraph @

src/lib/WPSDebug.h

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 #ifndef WPS_DEBUG
27 # define WPS_DEBUG
28
29 #include <string>
30
31 #include "libwps_internal.h"
32
33 # if defined(DEBUG_WITH_FILES)
34 #include <fstream>
35 #include <map>
36 #include <sstream>
37 #include <vector>
38
39 //! some basic tools
40 namespace libwps
41 {
42 //! debugging tools
43 namespace Debug
44 {
45 //! a debug function to store in a datafile in the current directory
46 //! WARNING: this function erase the file fileName if it exists
47 //! (if debug_with_files is not defined, does nothing)
48 bool dumpFile(librevenge::RVNGBinaryData const &data, char const *fileName);
49
50 //! returns a file name from an ole/... name
51 std::string flattenFileName(std::string const &name);
52 }
53
54 //! a basic stream (if debug_with_files is not defined, does nothing)
55 typedef std::stringstream DebugStream;
56
57 //! an interface used to insert comment in a binary file,
58 //! written in ascii form (if debug_with_files is not defined, does nothing)
59 class DebugFile
60 {
61 public:
62 //! constructor given the input file
63 explicit DebugFile(RVNGInputStreamPtr const &ip=RVNGInputStreamPtr())
64 : m_file()
65 , m_on(false)
66 , m_input(ip)
67 , m_actOffset(-1)
68 , m_notes()
69 , m_skipZones() { }
70
71 //! resets the input
72 void setStream(RVNGInputStreamPtr const &ip)
73 {
74 m_input = ip;
75 }
76 //! destructor
77 ~DebugFile()
78 {
79 reset();
80 }
81 //! opens/creates a file to store a result in the current directory
82 bool open(std::string const &filename);
83 //! writes the current file and reset to zero
84 void reset()
85 {
86 write();
87 m_file.close();
88 m_on = false;
89 m_notes.resize(0);
90 m_skipZones.resize(0);
91 m_actOffset = -1;
92 }
93 //! adds a new position in the file
94 void addPos(long pos);
95 //! adds a note in the file, in actual position
96 void addNote(char const *note);
97 //! adds a not breaking delimiter in position \a pos
98 void addDelimiter(long pos, char c);
99
100 //! skips the file zone defined by beginPos-endPos
101 void skipZone(long beginPos, long endPos)
102 {
103 if (m_on) m_skipZones.push_back(Vec2<long>(beginPos, endPos));
104 }
105
106 protected:
107 //! flushes the file
108 void write();
109
110 //! sorts the position/note date
111 void sort();
112
113 //! a stream which is open to write the file
114 mutable std::ofstream m_file;
115 //! a flag to know if the result stream is open or note
116 mutable bool m_on;
117
118 //! the input
119 RVNGInputStreamPtr m_input;
120
121 //! \brief a note and its position (used to sort all notes)
122 struct NotePos
123 {
124 //! empty constructor used by std::vector
125 NotePos()
126 : m_pos(-1)
127 , m_text("")
128 , m_breaking(false) { }
129
130 //! constructor: given position and note
131 NotePos(long p, std::string const &n, bool br=true)
132 : m_pos(p)
133 , m_text(n)
134 , m_breaking(br) {}
135 //! note offset
136 long m_pos;
137 //! note text
138 std::string m_text;
139 //! flag to indicate a non breaking note
140 bool m_breaking;
141
142 //! comparison operator based on the position
143 bool operator<(NotePos const &p) const
144 {
145 long diff = m_pos-p.m_pos;
146 if (diff) return (diff < 0) ? true : false;
147 if (m_breaking != p.m_breaking) return m_breaking;
148 return m_text < p.m_text;
149 }
150 /*! \struct NotePosLt
151 * \brief internal struct used to sort the notes, sorted by position
152 */
153 struct NotePosLt
154 {
155 //! comparison operator
156 bool operator()(NotePos const &s1, NotePos const &s2) const
157 {
158 return s1 < s2;
159 }
160 };
161 /*! \typedef Map
162 * \brief map of notes
163 */
164 typedef std::map<NotePos, int,struct NotePosLt> Map;
165 };
166
167 //! the actual offset (used to store note)
168 long m_actOffset;
169 //! list of notes
170 std::vector<NotePos> m_notes;
171 //! list of skipZone
172 std::vector<Vec2<long> > m_skipZones;
173 };
174 }
175 # else
176 namespace libwps
177 {
178 namespace Debug
179 {
180 inline bool dumpFile(librevenge::RVNGBinaryData const &, char const *)
181 {
182 return true;
183 }
184
185 inline std::string flattenFileName(std::string const &name)
186 {
187 return name;
188 }
189 }
190
191 class DebugStream
192 {
193 public:
194 template <class T>
195 DebugStream &operator<<(T const &)
196 {
197 return *this;
198 }
199
200 static std::string str()
201 {
202 return std::string("");
203 }
204 static void str(std::string const &) { }
205 };
206
207 class DebugFile
208 {
209 public:
210 explicit DebugFile(RVNGInputStreamPtr) {}
211 DebugFile() {}
212 static void setStream(RVNGInputStreamPtr) { }
213 ~DebugFile() { }
214
215 static bool open(std::string const &)
216 {
217 return true;
218 }
219
220 static void addPos(long) {}
221 static void addNote(char const *) {}
222 static void addDelimiter(long, char) {}
223
224 static void reset() { }
225
226 static void skipZone(long, long) {}
227 };
228 }
229 # endif
230
231 #endif
232
233 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */