| 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 |
|
| 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 |
| 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 |
| 81 |
void setStream(MWAWInputStreamPtr const &ip) |
| 82 |
{ |
| 83 |
m_input = ip; |
| 84 |
} |
| 85 |
//! destructor |
| 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 |
| 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 |
| 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 |
| 135 |
NotePos() |
| 136 |
: m_pos(-1) |
| 137 |
, m_text("") |
| 138 |
, m_breaking(false) |
| 139 |
{ |
| 140 |
} |
| 141 |
|
| 142 |
//! constructor: given position and note |
| 143 |
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 |
| 157 |
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 |
| 169 |
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 |
{ |
| 189 |
inline bool dumpFile(librevenge::RVNGBinaryData &, char const *) |
| 190 |
{ |
| 191 |
return true; |
| 192 |
} |
| 193 |
//! returns a file name from an ole/... 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> |
| 204 |
DebugStream &operator<<(T const &) |
| 205 |
{ |
| 206 |
return *this; |
| 207 |
} |
| 208 |
|
| 209 |
static std::string str() |
| 210 |
{ |
| 211 |
return std::string(""); |
| 212 |
} |
| 213 |
static void str(std::string const &) { } |
| 214 |
}; |
| 215 |
|
| 216 |
class DebugFile |
| 217 |
{ |
| 218 |
public: |
| 219 |
explicit DebugFile(MWAWInputStreamPtr const &) {} |
| 220 |
DebugFile() {} |
| 221 |
static void setStream(MWAWInputStreamPtr const &) { } |
| 222 |
~DebugFile() { } |
| 223 |
|
| 224 |
static bool open(std::string const &) |
| 225 |
{ |
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
static void addPos(long) {} |
| 230 |
static void addNote(char const *) {} |
| 231 |
static void addDelimiter(long, char) {} |
| 232 |
|
| 233 |
static void write() {} |
| 234 |
static void reset() { } |
| 235 |
|
| 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: |