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