WPSDebug.h
Go to the documentation of this file.
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* libwps
* Version: MPL 2.0 / LGPLv2.1+
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Major Contributor(s):
* Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
* Copyright (C) 2006, 2007 Andrew Ziem
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
* Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
* Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
*
* 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.1 or later
* (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
* applicable instead of those above.
*
* For further information visit http://libwps.sourceforge.net
*/
#ifndef WPS_DEBUG
# define WPS_DEBUG
#include <string>
#include "libwps_internal.h"
class WPXBinaryData;
# if defined(DEBUG_WITH_FILES)
#include <fstream>
#include <map>
#include <sstream>
#include <vector>
namespace libwps
{
namespace Debug
{
bool dumpFile(WPXBinaryData &data, char const *fileName);
std::string flattenFileName(std::string const &name);
}
typedef std::stringstream DebugStream;
class DebugFile
{
public:
DebugFile(WPXInputStreamPtr ip=WPXInputStreamPtr())
: m_file(), m_on(false), m_input(ip), m_actOffset(-1), m_notes(), m_skipZones() { }
void setStream(WPXInputStreamPtr 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 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 write();
void sort();
mutable std::ofstream m_file;
mutable bool m_on;
WPXInputStreamPtr 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 libwps
{
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;
}
std::string str() const
{
return std::string("");
}
void str(std::string const &) { }
};
class DebugFile
{
public:
DebugFile(WPXInputStreamPtr) {}
DebugFile() {}
void setStream(WPXInputStreamPtr) { }
~DebugFile() { }
bool open(std::string const &)
{
return true;
}
void addPos(long ) {}
void addNote(char const *) {}
void addDelimiter(long, char) {}
void reset() { }
void skipZone(long , long ) {}
};
}
# endif
#endif
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */