- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

WPSDebug.h
Go to the documentation of this file.
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 (alonso@loria.fr)
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
13 * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
14 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
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 class WPXBinaryData;
34
35 # if defined(DEBUG_WITH_FILES)
36 #include <fstream>
37 #include <map>
38 #include <sstream>
39 #include <vector>
40
42 namespace libwps
43 {
45 namespace Debug
46 {
50 bool dumpFile(WPXBinaryData &data, char const *fileName);
51
53 std::string flattenFileName(std::string const &name);
54 }
55
57 typedef std::stringstream DebugStream;
58
61 class DebugFile
62 {
63 public:
65 DebugFile(WPXInputStreamPtr ip=WPXInputStreamPtr())
66 : m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
67
69 void setStream(WPXInputStreamPtr ip)
70 {
71 m_input = ip;
72 }
74 ~DebugFile()
75 {
76 reset();
77 }
79 bool open(std::string const &filename);
81 void reset()
82 {
83 write();
84 m_file.close();
85 m_on = false;
86 m_notes.resize(0);
87 m_skipZones.resize(0);
88 m_actOffset = -1;
89 }
91 void addPos(long pos);
93 void addNote(char const *note);
95 void addDelimiter(long pos, char c);
96
98 void skipZone(long beginPos, long endPos)
99 {
100 if (m_on) m_skipZones.push_back(Vec2<long>(beginPos, endPos));
101 }
102
103 protected:
105 void write();
106
108 void sort();
109
111 mutable std::ofstream m_file;
113 mutable bool m_on;
114
116 WPXInputStreamPtr m_input;
117
119 struct NotePos
120 {
122 NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
123
125 NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
127 long m_pos;
129 std::string m_text;
131 bool m_breaking;
132
134 bool operator<(NotePos const &p) const
135 {
136 long diff = m_pos-p.m_pos;
137 if (diff) return (diff < 0) ? true : false;
138 if (m_breaking != p.m_breaking) return m_breaking;
139 return m_text < p.m_text;
140 }
144 struct NotePosLt
145 {
147 bool operator()(NotePos const &s1, NotePos const &s2) const
148 {
149 return s1 < s2;
150 }
151 };
155 typedef std::map<NotePos, int,struct NotePosLt> Map;
156 };
157
159 long m_actOffset;
161 std::vector<NotePos> m_notes;
163 std::vector<Vec2<long> > m_skipZones;
164 };
165 }
166 # else
167 namespace libwps
168 {
169 namespace Debug
170 {
171 inline bool dumpFile(WPXBinaryData &, char const *)
172 {
173 return true;
174 }
175
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 std::string str() const
192 {
193 return std::string("");
194 }
195 void str(std::string const &) { }
196 };
197
198 class DebugFile
199 {
200 public:
201 DebugFile(WPXInputStreamPtr) {}
202 DebugFile() {}
203 void setStream(WPXInputStreamPtr) { }
204 ~DebugFile() { }
205
206 bool open(std::string const &)
207 {
208 return true;
209 }
210
211 void addPos(long ) {}
212 void addNote(char const *) {}
213 void addDelimiter(long, char) {}
214
215 void reset() { }
216
217 void skipZone(long , long ) {}
218 };
219 }
220 # endif
221
222 #endif
223
224 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */