initial checkin of C++ port (in progress)

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@2 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-10-01 14:08:07 +00:00
parent f5c0071da6
commit ddfb1a5089
56 changed files with 8781 additions and 0 deletions
+127
View File
@@ -0,0 +1,127 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include "reader.hpp"
#include <util/vector2d.hpp>
#include <util/error.hpp>
// ----------------------------------------------------------------------------- : Reader
Reader::Reader(const InputStreamP& input, String filename)
: input(input), filename(filename), lineNumber(0)
, indent(0), expectedIndent(0), justOpened(false)
, stream(*input)
{
moveNext();
}
bool Reader::enterBlock(const Char* name) {
if (justOpened) moveNext(); // on the key of the parent block, first move inside it
if (indent != expectedIndent) return false; // not enough indentation
if (name == key) {
justOpened = true;
expectedIndent += 1; // the indent inside the block must be at least this much
return true;
} else {
return false;
}
}
void Reader::exitBlock() {
assert(expectedIndent > 0);
expectedIndent -= 1;
multiLineStr.clear();
if (justOpened) moveNext(); // leave this key
// Dump the remainder of the block
// TODO: issue warnings?
while (indent > expectedIndent) {
moveNext();
}
}
void Reader::moveNext() {
justOpened = false;
key.clear();
multiLineStr.clear();
indent = -1; // if no line is read it never has the expected indentation
// repeat until we have a good line
while (key.empty() && !input->Eof()) {
readLine();
}
// did we reach the end of the file?
if (key.empty() && input->Eof()) {
lineNumber += 1;
indent = -1;
}
}
void Reader::readLine() {
// fix UTF8 in ascii builds; skip BOM
line = decodeUTF8BOM(stream.ReadLine());
// read indentation
indent = 0;
while ((UInt)indent < line.size() && line.GetChar(indent) == _('\t')) {
indent += 1;
}
// read key / value
size_t pos = line.find_first_of(_(':'), indent);
key = trim(line.substr(indent, pos - indent));
value = pos == String::npos ? _("") : trimLeft(line.substr(pos+1));
// we read a line
lineNumber += 1;
// was it a comment?
if (!key.empty() && key.GetChar(0) == _('#')) {
key.clear();
}
}
// ----------------------------------------------------------------------------- : Handling basic types
template <> void Reader::handle(String& s) {
if (!multiLineStr.empty()) {
s = multiLineStr;
} else if (value.empty()) {
// a multiline string
bool first = true;
// read all lines that are indented enough
readLine();
while (indent >= expectedIndent) {
if (!first) value += '\n';
first = false;
multiLineStr += line.substr(expectedIndent); // strip expected indent
readLine();
}
// moveNext(), but without emptying multiLineStr
justOpened = false;
while (key.empty() && !input->Eof()) {
readLine();
}
s = multiLineStr;
} else {
s = value;
}
}
template <> void Reader::handle(int& i) {
long l = 0;
value.ToLong(&l);
i = l;
}
template <> void Reader::handle(double& d) {
value.ToDouble(&d);
}
template <> void Reader::handle(bool& b) {
b = (value==_("true") || value==_("1") || value==_("yes"));
}
// ----------------------------------------------------------------------------- : Handling less basic util types
template <> void Reader::handle(Vector2D& vec) {
if (!wxSscanf(value.c_str(), _("(%lf,%lf)"), &vec.x, &vec.y)) {
throw ParseError(_("Expected (x,y)"));
}
}
+165
View File
@@ -0,0 +1,165 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_UTIL_IO_READER
#define HEADER_UTIL_IO_READER
// ----------------------------------------------------------------------------- : Includes
#include "../prec.hpp"
#include <wx/txtstrm.h>
// ----------------------------------------------------------------------------- : Reader
typedef wxInputStream InputStream;
typedef shared_ptr<wxInputStream> InputStreamP;
/// The Reader can be used for reading (deserializing) objects
/** This class makes use of the reflection functionality, in effect
* an object tells the Reader what fields it would like to read.
* The reader then sees if the requested field is currently available.
*
* The handle functions ensure that afterwards the reader is at the line after the
* object that was just read.
*/
class Reader {
public:
/// Construct a reader that reads from the given input stream
/** filename is used only for error messages
*/
Reader(const InputStreamP& input, String filename = _(""));
/// Construct a reader that reads a file in a package
Reader(String filename);
/// Tell the reflection code we are reading
inline bool reading() const { return true; }
// --------------------------------------------------- : Handling objects
/// Handle an object: read it if it's name matches
template <typename T>
void handle(const Char* name, T& object) {
if (enterBlock(name)) {
handle(object);
exitBlock();
}
}
/// Reads an object of type T from the input stream
template <typename T> void handle(T& object);
/// Reads a vector from the input stream
template <typename T> void handle(vector<T>& vector);
/// Reads a shared_ptr from the input stream
template <typename T> void handle(shared_ptr<T>& pointer);
/// Reads a map from the input stream
//template <typename K, typename V> void handle(map<K,V>& map);
private:
// --------------------------------------------------- : Data
/// The line we read
String line;
/// The key and value of the last line we read
String key, value;
/// A string spanning multiple lines
String multiLineStr;
/// Indentation of the last line we read
int indent;
/// Indentation of the block we are in
int expectedIndent;
/// Did we just open a block (i.e. not read any more lines of it)?
bool justOpened;
/// Filename for error messages
String filename;
/// Line number for error messages
UInt lineNumber;
/// Input stream we are reading from
InputStreamP input;
/// Text stream wrapping the input stream
wxTextInputStream stream;
// --------------------------------------------------- : Reading the stream
/// Is there a block with the given key under the current cursor?
bool enterBlock(const Char* name);
/// Leave the block we are in
void exitBlock();
/// Move to the next non empty line
void moveNext();
/// Reads the next line from the input, and stores it in line/key/value/indent
void readLine();
/// Issue a warning: "Unexpected key: $key"
void unexpected();
};
// ----------------------------------------------------------------------------- : Container types
template <typename T>
void Reader::handle(vector<T>& vector) {
String vectorKey = key;
while (key == vectorKey) { // TODO : check indent
moveNext(); // skip key
vector.resize(vector.size() + 1);
handle(vector.back());
}
}
template <typename T>
void Reader::handle(shared_ptr<T>& pointer) {
if (!pointer) pointer.reset(new T);
handle(*pointer);
}
// ----------------------------------------------------------------------------- : Reflection
/// Implement reflection as used by Reader
#define REFLECT_OBJECT_READER(Cls) \
template<> void Reader::handle<Cls>(Cls& object) { \
object.reflect(*this); \
}
// ----------------------------------------------------------------------------- : Reflection for enumerations
/// Implement enum reflection as used by Reader
#define REFLECT_ENUM_READER(Enum) \
template<> void Reader::handle<Enum>(Enum& enum_) { \
EnumReader reader(value); \
reflect_ ## Enum(enum_, reader); \
if (!reader.isDone()) { \
/* warning message */ \
} \
}
/// 'Tag' to be used when reflecting enumerations for Reader
class EnumReader {
public:
inline EnumReader(String read)
: read(read), first(true), done(false) {}
/// Handle a possible value for the enum, if the name matches the name in the input
template <typename Enum>
inline void handle(const Char* name, Enum value, Enum& enum_) {
if (!done && read == name) {
first = true;
enum_ = value;
} else if (first) {
first = false;
enum_ = value;
}
}
inline bool isDone() const { return done; }
private:
String read; //^ The string to match to a value name
bool first; //^ Has the first (default) value been matched?
bool done; //^ Was anything matched?
};
// ----------------------------------------------------------------------------- : EOF
#endif
+108
View File
@@ -0,0 +1,108 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include "writer.hpp"
#include <util/vector2d.hpp>
#include <util/error.hpp>
// ----------------------------------------------------------------------------- : Writer
Writer::Writer(const OutputStreamP& output)
: output(output)
, stream(*output)
, indentation(0)
, justOpened(false)
{
stream.WriteString(BYTE_ORDER_MARK);
}
void Writer::enterBlock(const Char* name) {
// indenting into a sub-block?
if (justOpened) {
writeKey();
stream.WriteString(_(":\n"));
}
// don't write the key yet
indentation += 1;
openedKey = name;
justOpened = true;
}
void Writer::exitBlock() {
assert(indentation > 0);
indentation -= 1;
justOpened = false;
}
void Writer::writeKey() {
writeIndentation();
writeUTF8(stream, openedKey);
}
void Writer::writeIndentation() {
for(int i = 1 ; i < indentation ; ++i) {
stream.PutChar(_('\t'));
}
}
// ----------------------------------------------------------------------------- : Handling basic types
void Writer::handle(const String& value) {
if (!justOpened) {
throw InternalError(_("Can only write a value in a key that was just opened"));
}
// write indentation and key
writeKey();
writeUTF8(stream, _(": "));
if (value.find_first_of(_('\n')) != String::npos) {
// multiline string
stream.PutChar(_('\n'));
indentation += 1;
// split lines, and write each line
size_t start = 0, end, size = value.size();
while (start < size) {
end = value.find_first_of(_("\n\r"), start); // until end of line
// write the line
writeIndentation();
writeUTF8(stream, value.substr(start, end - start));
stream.PutChar(_('\n'));
// Skip \r and \n
if (end == String::npos) break;
start = end + 1;
if (start < size) {
Char c1 = value.GetChar(start - 1);
Char c2 = value.GetChar(start);
// skip second character of \r\n or \n\r
if (c1 != c2 && (c2 == _('\r') || c2 == _('\n'))) start += 1;
}
}
indentation -= 1;
} else {
writeUTF8(stream, value);
}
stream.PutChar(_('\n'));
justOpened = false;
}
template <> void Writer::handle(const int& value) {
handle(String() << value);
}
template <> void Writer::handle(const double& value) {
handle(String() << value);
}
template <> void Writer::handle(const bool& value) {
handle(value ? _("true") : _("false"));
}
// ----------------------------------------------------------------------------- : Handling less basic util types
template <> void Writer::handle(const Vector2D& vec) {
String formated;
formated.Printf(_("(%.10lf,%.10lf)"), vec.x, vec.y);
handle(formated);
}
+131
View File
@@ -0,0 +1,131 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_UTIL_IO_WRITER
#define HEADER_UTIL_IO_WRITER
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <wx/txtstrm.h>
// ----------------------------------------------------------------------------- : Writer
typedef wxOutputStream OutputStream;
typedef shared_ptr<wxOutputStream> OutputStreamP;
/// The Writer can be used for writing (serializing) objects
class Writer {
public:
/// Construct a writer that writes to the given output stream
Writer(const OutputStreamP& output);
/// Tell the reflection code we are not reading
inline bool reading() const { return false; }
// --------------------------------------------------- : Handling objects
/// Handle an object: write it under the given name
template <typename T>
void handle(const Char* name, const T& object) {
enterBlock(name);
handle(object);
exitBlock();
}
/// Write a string to the output stream
void handle(const String& str);
void handle(const Char* str) { handle(String(str)); }
/// Write an object of type T to the output stream
template <typename T> void handle(const T& object);
/// Write a vector to the output stream
template <typename T> void handle(const vector<T>& vector);
/// Write a shared_ptr to the output stream
template <typename T> void handle(const shared_ptr<T>& pointer);
/// Write a map to the output stream
//template <typename K, typename V> void handle(map<K,V>& map);
private:
// --------------------------------------------------- : Data
/// Indentation of the current block
int indentation;
/// Did we just open a block (i.e. not written any lines of it)?
bool justOpened;
/// Last key opened
String openedKey;
/// Output stream we are writing to
OutputStreamP output;
/// Text stream wrapping the output stream
wxTextOutputStream stream;
// --------------------------------------------------- : Writing to the stream
/// Start a new block with the given name
void enterBlock(const Char* name);
/// Leave the block we are in
void exitBlock();
/// Write the openedKey and the required indentation
void writeKey();
/// Output some taps to represent the indentation level
void writeIndentation();
};
// ----------------------------------------------------------------------------- : Container types
template <typename T>
void Writer::handle(const vector<T>& vector) {
/*String vectorKey = key;
while (key == vectorKey) { // TODO : check indent
moveNext(); // skip key
vector.resize(vector.size() + 1);
handle(vector.back());
}*/
}
template <typename T>
void Writer::handle(const shared_ptr<T>& pointer) {
if (pointer) handle(*pointer);
}
// ----------------------------------------------------------------------------- : Reflection
/// Implement reflection as used by Writer
#define REFLECT_OBJECT_WRITER(Cls) \
template<> void Writer::handle<Cls>(const Cls& object) { \
const_cast<Cls&>(object).reflect(*this); \
}
// ----------------------------------------------------------------------------- : Reflection for enumerations
/// Implement enum reflection as used by Writer
#define REFLECT_ENUM_WRITER(Enum) \
template<> void Writer::handle<Enum>(const Enum& enum_) { \
EnumWriter writer(*this); \
reflect_ ## Enum(const_cast<Enum&>(enum_), writer); \
}
/// 'Tag' to be used when reflecting enumerations for Writer
class EnumWriter {
public:
inline EnumWriter(Writer& writer)
: writer(writer) {}
/// Handle a possible value for the enum, if the name matches the name in the input
template <typename Enum>
inline void handle(const Char* name, Enum value, Enum enum_) {
if (enum_ == value) {
writer.handle(name);
}
}
private:
Writer& writer; //^ The writer to write output to
};
// ----------------------------------------------------------------------------- : EOF
#endif