Debian Codegraph @

src/lib/WPSDebug.cpp

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 #include <iomanip>
27 #include <sstream>
28
29 #include <librevenge/librevenge.h>
30 #include <librevenge-stream/librevenge-stream.h>
31
32
33 #include "WPSDebug.h"
34
35 #if defined(DEBUG_WITH_FILES)
36 namespace libwps
37 {
38 bool DebugFile::open(std::string const &filename)
39 {
40 std::string name=Debug::flattenFileName(filename);
41 name += ".ascii";
42 m_file.open(name.c_str());
43 return m_on = m_file.is_open();
44 }
45
46 void DebugFile::addPos(long pos)
47 {
48 if (!m_on) return;
49 m_actOffset = pos;
50 }
51
52 void DebugFile::addNote(char const *note)
53 {
54 if (!m_on || note == nullptr) return;
55
56 size_t numNotes = m_notes.size();
57
58 if (!numNotes || m_notes[numNotes-1].m_pos != m_actOffset)
59 {
60 std::string empty("");
61 m_notes.push_back(NotePos(m_actOffset, empty));
62 numNotes++;
63 }
64
65 m_notes[numNotes-1].m_text += std::string(note);
66 }
67
68 void DebugFile::addDelimiter(long pos, char c)
69 {
70 if (!m_on) return;
71 std::string s;
72 s+=c;
73 m_notes.push_back(NotePos(pos,s,false));
74 }
75
76 void DebugFile::sort()
77 {
78 if (!m_on) return;
79 size_t numNotes = m_notes.size();
80
81 if (m_actOffset >= 0 && (numNotes == 0 || m_notes[numNotes-1].m_pos != m_actOffset))
82 {
83 std::string empty("");
84 m_notes.push_back(NotePos(m_actOffset, empty));
85 numNotes++;
86 }
87
88 NotePos::Map map;
89 for (auto const &note : m_notes) map[note] = 0;
90
91 size_t i = 0;
92 for (auto const &it : map)
93 m_notes[i++] = it.first;
94 if (i != numNotes) m_notes.resize(i);
95
96 std::map<Vec2<long>,int,Vec2<long>::PosSizeLtX> sMap;
97 for (auto const &s : m_skipZones) sMap[s] = 0;
98
99 i = 0;
100 for (auto const &it : sMap)
101 m_skipZones[i++] = it.first;
102 if (i < m_skipZones.size()) m_skipZones.resize(i);
103 }
104
105 void DebugFile::write()
106 {
107 if (!m_on || m_input.get() == nullptr) return;
108
109 sort();
110
111 long readPos = m_input->tell();
112
113 auto noteIter = m_notes.begin();
114
115 //! write the notes which does not have any position
116 while (noteIter != m_notes.end() && noteIter->m_pos < 0)
117 {
118 if (!noteIter->m_text.empty())
119 std::cerr << "DebugFile::write: skipped: " << noteIter->m_text << std::endl;
120 ++noteIter;
121 }
122
123 long actualPos = 0;
124 int numSkip = int(m_skipZones.size()), actSkip = (numSkip == 0) ? -1 : 0;
125 long actualSkipEndPos = (numSkip == 0) ? -1 : m_skipZones[0].x();
126
127 m_input->seek(0,librevenge::RVNG_SEEK_SET);
128 m_file << std::hex << std::right << std::setfill('0') << std::setw(6) << 0 << " ";
129
130 do
131 {
132 bool printAdr = false;
133 bool stop = false;
134 while (actualSkipEndPos != -1 && actualPos >= actualSkipEndPos)
135 {
136 printAdr = true;
137 actualPos = m_skipZones[size_t(actSkip)].y()+1;
138 m_file << "\nSkip : " << std::hex << std::setw(6) << actualSkipEndPos << "-"
139 << actualPos-1 << "\n\n";
140 m_input->seek(actualPos, librevenge::RVNG_SEEK_SET);
141 stop = m_input->isEnd();
142 actSkip++;
143 actualSkipEndPos = (actSkip < numSkip) ? m_skipZones[size_t(actSkip)].x() : -1;
144 }
145 if (stop) break;
146 while (noteIter != m_notes.end() && noteIter->m_pos < actualPos)
147 {
148 if (!noteIter->m_text.empty())
149 m_file << "Skipped: " << noteIter->m_text << std::endl;
150 ++noteIter;
151 }
152 bool printNote = noteIter != m_notes.end() && noteIter->m_pos == actualPos;
153 if (printAdr || (printNote && noteIter->m_breaking))
154 m_file << "\n" << std::setw(6) << actualPos << " ";
155 while (noteIter != m_notes.end() && noteIter->m_pos == actualPos)
156 {
157 if (noteIter->m_text.empty())
158 {
159 ++noteIter;
160 continue;
161 }
162 if (noteIter->m_breaking)
163 m_file << "[" << noteIter->m_text << "]";
164 else
165 m_file << noteIter->m_text;
166 ++noteIter;
167 }
168
169 long ch = libwps::readU8(m_input);
170 m_file << std::setw(2) << ch;
171 actualPos++;
172
173 }
174 while (!m_input->isEnd());
175
176 m_file << "\n\n";
177
178 m_input->seek(readPos,librevenge::RVNG_SEEK_SET);
179
180 m_actOffset=-1;
181 m_notes.resize(0);
182 }
183
184 ////////////////////////////////////////////////////////////
185 //
186 // save librevenge::RVNGBinaryData in a file
187 //
188 ////////////////////////////////////////////////////////////
189 namespace Debug
190 {
191 bool dumpFile(librevenge::RVNGBinaryData const &data, char const *fileName)
192 {
193 if (!fileName) return false;
194 if (!data.size() || !data.getDataBuffer())
195 {
196 WPS_DEBUG_MSG(("Debug::dumpFile: can not find data for %s\n", fileName));
197 return false;
198 }
199 std::string fName = Debug::flattenFileName(fileName);
200 FILE *file = fopen(fName.c_str(), "wb");
201 if (!file) return false;
202 fwrite(data.getDataBuffer(), data.size(), 1, file);
203 fclose(file);
204 return true;
205 }
206
207 std::string flattenFileName(std::string const &name)
208 {
209 std::string res;
210 for (auto c : name)
211 {
212 switch (c)
213 {
214 case '\0':
215 case '/':
216 case '\\':
217 case ':': // potential file system separator
218 case ' ':
219 case '\t':
220 case '\n': // potential text separator
221 res += '_';
222 break;
223 default:
224 if (c <= 28) res += '#'; // potential trouble potential char
225 else res += c;
226 }
227 }
228 return res;
229 }
230 }
231
232 }
233 #endif
234 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */