MWAWDebug.hxx
Go to the documentation of this file.
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
/* libmwaw
* Version: MPL 2.0 / LGPLv2+
*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
* Copyright (C) 2006, 2007 Andrew Ziem
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
*
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
* in which case the provisions of the LGPLv2+ are applicable
* instead of those above.
*/
#ifndef MWAW_DEBUG
# define MWAW_DEBUG
#include <string>
#include "MWAWInputStream.hxx"
class WPXBinaryData;
# if defined(DEBUG_WITH_FILES)
#include <fstream>
#include <map>
#include <sstream>
#include <vector>
namespace libmwaw
{
namespace Debug
{
bool dumpFile(WPXBinaryData &data, char const *fileName);
std::string flattenFileName(std::string const &name);
}
typedef std::stringstream DebugStream;
class DebugFile
{
public:
DebugFile(MWAWInputStreamPtr ip=MWAWInputStreamPtr())
: m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
void setStream(MWAWInputStreamPtr ip) {
m_input = ip;
}
~DebugFile() {
reset();
}
bool open(std::string const &filename);
void reset() {
write();
m_file.close();
m_on = false;
m_notes.resize(0);
m_skipZones.resize(0);
m_actOffset = -1;
}
void write();
void addPos(long pos);
void addNote(char const *note);
void addDelimiter(long pos, char c);
void skipZone(long beginPos, long endPos) {
if (m_on) m_skipZones.push_back(Vec2<long>(beginPos, endPos));
}
protected:
void sort();
mutable std::ofstream m_file;
mutable bool m_on;
MWAWInputStreamPtr m_input;
struct NotePos {
NotePos() : m_pos(-1), m_text(""), m_breaking(false) { }
NotePos(long p, std::string const &n, bool br=true) : m_pos(p), m_text(n), m_breaking(br) {}
long m_pos;
std::string m_text;
bool m_breaking;
bool operator<(NotePos const &p) const {
long diff = m_pos-p.m_pos;
if (diff) return (diff < 0) ? true : false;
if (m_breaking != p.m_breaking) return m_breaking;
return m_text < p.m_text;
}
struct NotePosLt {
bool operator()(NotePos const &s1, NotePos const &s2) const {
return s1 < s2;
}
};
typedef std::map<NotePos, int,struct NotePosLt> Map;
};
long m_actOffset;
std::vector<NotePos> m_notes;
std::vector<Vec2<long> > m_skipZones;
};
}
# else
namespace libmwaw
{
namespace Debug
{
inline bool dumpFile(WPXBinaryData &, char const *)
{
return true;
}
inline std::string flattenFileName(std::string const &name)
{
return name;
}
}
class DebugStream
{
public:
template <class T>
DebugStream &operator<<(T const &) {
return *this;
}
static std::string str() {
return std::string("");
}
static void str(std::string const &) { }
};
class DebugFile
{
public:
DebugFile(MWAWInputStreamPtr) {}
DebugFile() {}
static void setStream(MWAWInputStreamPtr) { }
~DebugFile() { }
static bool open(std::string const &) {
return true;
}
static void addPos(long ) {}
static void addNote(char const *) {}
static void addDelimiter(long, char) {}
static void write() {}
static void reset() { }
static void skipZone(long , long ) {}
};
}
# endif
#endif
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
libmwaw::DebugFile::skipZone
void skipZone(long beginPos, long endPos)
skips the file zone defined by beginPos-endPos
Definition: MWAWDebug.hxx:103
libmwaw::DebugFile::write
void write()
flushes the file
Definition: MWAWDebug.cxx:111
libmwaw::DebugFile::NotePos::NotePos
NotePos(long p, std::string const &n, bool br=true)
constructor: given position and note
Definition: MWAWDebug.hxx:125
libmwaw::DebugFile::setStream
void setStream(MWAWInputStreamPtr ip)
resets the input
Definition: MWAWDebug.hxx:75
libmwaw::DebugFile
an interface used to insert comment in a binary file, written in ascii form (if debug_with_files is n...
Definition: MWAWDebug.hxx:67
libmwaw::DebugFile::sort
void sort()
sorts the position/note date
Definition: MWAWDebug.cxx:82
libmwaw::operator<<
std::ostream & operator<<(std::ostream &o, PrinterInfo const &r)
operator<< for a PrinterInfo
Definition: MWAWPrinter.cxx:203
libmwaw::DebugFile::NotePos
a note and its position (used to sort all notes)
Definition: MWAWDebug.hxx:120
libmwaw::DebugFile::NotePos::operator<
bool operator<(NotePos const &p) const
comparison operator based on the position
Definition: MWAWDebug.hxx:134
libmwaw::DebugFile::NotePos::m_breaking
bool m_breaking
flag to indicate a non breaking note
Definition: MWAWDebug.hxx:131
libmwaw::DebugFile::m_actOffset
long m_actOffset
the actual offset (used to store note)
Definition: MWAWDebug.hxx:156
libmwaw::DebugFile::NotePos::m_text
std::string m_text
note text
Definition: MWAWDebug.hxx:129
libmwaw::DebugFile::open
bool open(std::string const &filename)
opens/creates a file to store a result
Definition: MWAWDebug.cxx:45
libmwaw::DebugFile::addDelimiter
void addDelimiter(long pos, char c)
adds a not breaking delimiter in position pos
Definition: MWAWDebug.cxx:74
libmwaw::DebugFile::addPos
void addPos(long pos)
adds a new position in the file
Definition: MWAWDebug.cxx:53
libmwaw::DebugFile::m_notes
std::vector< NotePos > m_notes
list of notes
Definition: MWAWDebug.hxx:158
libmwaw::DebugFile::NotePos::NotePosLt
internal struct used to sort the notes, sorted by position
Definition: MWAWDebug.hxx:143
libmwaw::DebugFile::NotePos::NotePos
NotePos()
empty constructor used by std::vector
Definition: MWAWDebug.hxx:122
libmwaw::DebugFile::NotePos::Map
std::map< NotePos, int, struct NotePosLt > Map
map of notes
Definition: MWAWDebug.hxx:152
libmwaw::DebugFile::DebugFile
DebugFile(MWAWInputStreamPtr ip=MWAWInputStreamPtr())
constructor given the input file
Definition: MWAWDebug.hxx:71
libmwaw::DebugFile::~DebugFile
~DebugFile()
destructor
Definition: MWAWDebug.hxx:79
MWAWInputStreamPtr
shared_ptr< MWAWInputStream > MWAWInputStreamPtr
a smart pointer of MWAWInputStream
Definition: libmwaw_internal.hxx:336
libmwaw::DebugFile::m_input
MWAWInputStreamPtr m_input
the input
Definition: MWAWDebug.hxx:117
MWAWInputStream.hxx
libmwaw::DebugFile::m_file
std::ofstream m_file
a stream which is open to write the file
Definition: MWAWDebug.hxx:112
libmwaw::Debug::flattenFileName
std::string flattenFileName(std::string const &name)
returns a file name from an ole/... name
Definition: MWAWDebug.cxx:205
libmwaw::DebugFile::reset
void reset()
writes the current file and reset to zero
Definition: MWAWDebug.hxx:85
libmwaw::DebugFile::m_skipZones
std::vector< Vec2< long > > m_skipZones
list of skipZone
Definition: MWAWDebug.hxx:160
libmwaw::DebugStream
std::stringstream DebugStream
a basic stream (if debug_with_files is not defined, does nothing)
Definition: MWAWDebug.hxx:63
libmwaw::DebugFile::NotePos::m_pos
long m_pos
note offset
Definition: MWAWDebug.hxx:127
Vec2< long >
libmwaw::DebugFile::m_on
bool m_on
a flag to know if the result stream is open or note
Definition: MWAWDebug.hxx:114
libmwaw::Debug::dumpFile
bool dumpFile(WPXBinaryData &data, char const *fileName)
a debug function to store in a datafile in the current directory WARNING: this function erase the fil...
Definition: MWAWDebug.cxx:190
libmwaw::DebugFile::NotePos::NotePosLt::operator()
bool operator()(NotePos const &s1, NotePos const &s2) const
comparison operator
Definition: MWAWDebug.hxx:145
libmwaw::DebugFile::addNote
void addNote(char const *note)
adds a note in the file, in actual position
Definition: MWAWDebug.cxx:59