mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
rewritten retrying part of reader, it is now implemented how it should have been from the start.
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@175 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -71,7 +71,7 @@ String to_string(Alignment align) {
|
||||
// we need custom io, because there can be both a horizontal and a vertical component
|
||||
|
||||
template <> void Reader::handle(Alignment& align) {
|
||||
align = from_string(value);
|
||||
align = from_string(getValue());
|
||||
}
|
||||
template <> void Writer::handle(const Alignment& align) {
|
||||
handle(to_string(align));
|
||||
|
||||
@@ -424,7 +424,7 @@ void Packaged::open(const String& package) {
|
||||
Package::open(package);
|
||||
Reader reader(openIn(typeName()), absoluteFilename() + _("/") + typeName());
|
||||
try {
|
||||
reader.handle(*this);
|
||||
reader.handle_greedy(*this);
|
||||
validate(reader.file_app_version);
|
||||
} catch (const ParseError& err) {
|
||||
throw FileParseError(err.what(), absoluteFilename() + _("/") + typeName()); // more detailed message
|
||||
|
||||
@@ -117,7 +117,7 @@ class Package {
|
||||
void readFile(const String& file, T& obj) {
|
||||
Reader reader(openIn(file), absoluteFilename() + _("/") + file);
|
||||
try {
|
||||
reader.handle(obj);
|
||||
reader.handle_greedy(obj);
|
||||
} catch (const ParseError& err) {
|
||||
throw FileParseError(err.what(), absoluteFilename() + _("/") + file); // more detailed message
|
||||
}
|
||||
|
||||
+20
-14
@@ -81,6 +81,7 @@ void Reader::exitBlock() {
|
||||
while (indent > expected_indent) {
|
||||
moveNext();
|
||||
}
|
||||
handled = true;
|
||||
}
|
||||
|
||||
void Reader::moveNext() {
|
||||
@@ -110,12 +111,12 @@ void Reader::readLine() {
|
||||
}
|
||||
// read key / value
|
||||
size_t pos = line.find_first_of(_(':'), indent);
|
||||
if (!pos || line.GetChar(indent) == _('#')) {
|
||||
if (trim(line).empty() || line.GetChar(indent) == _('#')) {
|
||||
// empty line or comment
|
||||
key.clear();
|
||||
return;
|
||||
}
|
||||
if (key.empty() && input->Eof()) {
|
||||
if (input->Eof()) {
|
||||
// end of file
|
||||
indent = -1;
|
||||
return;
|
||||
@@ -147,9 +148,10 @@ void Reader::unknownKey() {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Handling basic types
|
||||
|
||||
template <> void Reader::handle(String& s) {
|
||||
const String& Reader::getValue() {
|
||||
handled = true;
|
||||
if (!multi_line_str.empty()) {
|
||||
s = multi_line_str;
|
||||
return multi_line_str;
|
||||
} else if (value.empty()) {
|
||||
// a multiline string
|
||||
bool first = true;
|
||||
@@ -161,50 +163,54 @@ template <> void Reader::handle(String& s) {
|
||||
multi_line_str += line.substr(expected_indent); // strip expected indent
|
||||
readLine();
|
||||
}
|
||||
// moveNext(), but without emptying multiLineStr
|
||||
// moveNext(), but without emptying multi_line_str
|
||||
just_opened = false;
|
||||
while (key.empty() && !input->Eof()) {
|
||||
readLine();
|
||||
}
|
||||
s = multi_line_str;
|
||||
return multi_line_str;
|
||||
} else {
|
||||
s = value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
template <> void Reader::handle(String& s) {
|
||||
s = getValue();
|
||||
}
|
||||
template <> void Reader::handle(int& i) {
|
||||
long l = 0;
|
||||
value.ToLong(&l);
|
||||
getValue().ToLong(&l);
|
||||
i = l;
|
||||
}
|
||||
template <> void Reader::handle(unsigned int& i) {
|
||||
long l = 0;
|
||||
value.ToLong(&l);
|
||||
getValue().ToLong(&l);
|
||||
i = abs(l); // abs, because it will seem strange if -1 comes out as MAX_INT
|
||||
}
|
||||
template <> void Reader::handle(double& d) {
|
||||
value.ToDouble(&d);
|
||||
getValue().ToDouble(&d);
|
||||
}
|
||||
template <> void Reader::handle(bool& b) {
|
||||
b = (value==_("true") || value==_("1") || value==_("yes"));
|
||||
b = (getValue()==_("true") || getValue()==_("1") || getValue()==_("yes"));
|
||||
}
|
||||
// ----------------------------------------------------------------------------- : Handling less basic util types
|
||||
|
||||
template <> void Reader::handle(Vector2D& vec) {
|
||||
if (!wxSscanf(value.c_str(), _("(%lf,%lf)"), &vec.x, &vec.y)) {
|
||||
if (!wxSscanf(getValue().c_str(), _("(%lf,%lf)"), &vec.x, &vec.y)) {
|
||||
throw ParseError(_("Expected (x,y)"));
|
||||
}
|
||||
}
|
||||
|
||||
template <> void Reader::handle(Color& col) {
|
||||
UInt r,g,b;
|
||||
if (wxSscanf(value.c_str(),_("rgb(%u,%u,%u)"),&r,&g,&b)) {
|
||||
if (wxSscanf(getValue().c_str(),_("rgb(%u,%u,%u)"),&r,&g,&b)) {
|
||||
col.Set(r, g, b);
|
||||
}
|
||||
}
|
||||
|
||||
template <> void Reader::handle(FileName& f) {
|
||||
if (clipboard_package()) {
|
||||
String str; handle(str);
|
||||
String str = getValue();
|
||||
if (!str.empty()) {
|
||||
// copy file into current package
|
||||
try {
|
||||
|
||||
+26
-17
@@ -62,11 +62,23 @@ class Reader {
|
||||
void showWarnings();
|
||||
|
||||
// --------------------------------------------------- : Handling objects
|
||||
/// Handle an object that can read as much as it can eat
|
||||
template <typename T>
|
||||
void handle_greedy(T& object) {
|
||||
do {
|
||||
// UInt l = line_number;
|
||||
handled = false;
|
||||
handle(object);
|
||||
// if (l == line_number && !handled) unknownKey(object);
|
||||
if (!handled) unknownKey(object);
|
||||
} while (indent >= expected_indent);
|
||||
}
|
||||
|
||||
/// 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);
|
||||
handle_greedy(object);
|
||||
exitBlock();
|
||||
}
|
||||
}
|
||||
@@ -100,6 +112,8 @@ class Reader {
|
||||
String key, value;
|
||||
/// A string spanning multiple lines
|
||||
String multi_line_str;
|
||||
/// Has the current line been handled?
|
||||
bool handled;
|
||||
/// Indentation of the last line we read
|
||||
int indent;
|
||||
/// Indentation of the block we are in
|
||||
@@ -132,13 +146,16 @@ class Reader {
|
||||
/// Reads the next line from the input, and stores it in line/key/value/indent
|
||||
void readLine();
|
||||
|
||||
/// Return the value on the current line
|
||||
const String& getValue();
|
||||
|
||||
/// No line was read, because nothing mathes the current key
|
||||
/** Maybe the key is "include file" */
|
||||
template <typename T>
|
||||
void unknownKey(T& v) {
|
||||
if (key == _("include file")) {
|
||||
Reader reader(value);
|
||||
reader.handle(v);
|
||||
reader.handle_greedy(v);
|
||||
moveNext();
|
||||
} else {
|
||||
unknownKey();
|
||||
@@ -166,7 +183,7 @@ void Reader::handle(const Char* name, vector<T>& vector) {
|
||||
String vectorKey = singular_form(name);
|
||||
while (enterBlock(vectorKey)) {
|
||||
vector.resize(vector.size() + 1);
|
||||
handle(vector.back());
|
||||
handle_greedy(vector.back());
|
||||
update_index(vector.back(), vector.size() - 1); // update index for IndexMap
|
||||
exitBlock();
|
||||
}
|
||||
@@ -187,20 +204,16 @@ void Reader::handle(map<String, V>& m) {
|
||||
just_opened = true;
|
||||
expected_indent += 1;
|
||||
// now read the value
|
||||
handle(m[key]);
|
||||
handle_greedy(m[key]);
|
||||
exitBlock();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void Reader::handle(IndexMap<K,V>& m) {
|
||||
//do {
|
||||
// UInt l = line_number;
|
||||
for (typename IndexMap<K,V>::iterator it = m.begin() ; it != m.end() ; ++it) {
|
||||
handle(get_key_name(*it).c_str(), *it);
|
||||
}
|
||||
// if (l == line_number) unknownKey(m);
|
||||
//} while (indent >= expected_indent);
|
||||
for (typename IndexMap<K,V>::iterator it = m.begin() ; it != m.end() ; ++it) {
|
||||
handle(get_key_name(*it).c_str(), *it);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Reflection
|
||||
@@ -208,11 +221,7 @@ void Reader::handle(IndexMap<K,V>& m) {
|
||||
/// Implement reflection as used by Reader
|
||||
#define REFLECT_OBJECT_READER(Cls) \
|
||||
template<> void Reader::handle<Cls>(Cls& object) { \
|
||||
do { \
|
||||
UInt l = line_number; \
|
||||
object.reflect(*this); \
|
||||
if (l == line_number) unknownKey(object); \
|
||||
} while (indent >= expected_indent); \
|
||||
object.reflect(*this); \
|
||||
} \
|
||||
void Cls::reflect(Reader& reader) { \
|
||||
reflect_impl(reader); \
|
||||
@@ -223,7 +232,7 @@ void Reader::handle(IndexMap<K,V>& m) {
|
||||
/// Implement enum reflection as used by Reader
|
||||
#define REFLECT_ENUM_READER(Enum) \
|
||||
template<> void Reader::handle<Enum>(Enum& enum_) { \
|
||||
EnumReader reader(value); \
|
||||
EnumReader reader(getValue()); \
|
||||
reflect_ ## Enum(enum_, reader); \
|
||||
if (!reader.isDone()) { \
|
||||
/* warning: unknown value */ \
|
||||
|
||||
@@ -29,7 +29,7 @@ Version Version::fromString(const String& version) {
|
||||
|
||||
|
||||
template <> void Reader::handle(Version& v) {
|
||||
v = Version::fromString(value);
|
||||
v = Version::fromString(getValue());
|
||||
}
|
||||
template <> void Writer::handle(const Version& v) {
|
||||
handle(v.toString());
|
||||
|
||||
Reference in New Issue
Block a user