From: Gert van Valkenhoef Date: Tue, 9 Aug 2011 16:04:19 +0000 (+0100) Subject: Convert typepaths from SHORTCHAR** to TypePath class X-Git-Url: https://deadsoftware.ru/gitweb?p=odcread.git;a=commitdiff_plain;h=32b22b42cd974e98e8a56f5ac39a51c8990ef57c Convert typepaths from SHORTCHAR** to TypePath class --- diff --git a/alien.cc b/alien.cc index 7a225b7..d82a33c 100644 --- a/alien.cc +++ b/alien.cc @@ -14,14 +14,14 @@ std::string AlienPart::toString() { return store->toString(); } -Alien::Alien(INTEGER id, SHORTCHAR **path): Store(id), d_path(path), d_comps() {} +Alien::Alien(INTEGER id, const TypePath &path): Store(id), d_path(path), d_comps() {} std::vector & Alien::getComponents() { return d_comps; } std::string Alien::toString() { - std::string sofar = std::string(d_path[0]) + "{ "; + std::string sofar = d_path.toString() + "{ "; for (int i = 0; i < d_comps.size(); ++i) { sofar += d_comps[i]->toString() + " "; } diff --git a/alien.h b/alien.h index 40ef09a..0c4b51d 100644 --- a/alien.h +++ b/alien.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace odc { @@ -39,12 +40,12 @@ struct AlienPart : public AlienComponent { }; class Alien : public Store { - SHORTCHAR **d_path; + const TypePath d_path; std::vector d_comps; public: - Alien(INTEGER id, SHORTCHAR **path); + Alien(INTEGER id, const TypePath &path); std::vector & getComponents(); diff --git a/reader.cc b/reader.cc index 051ddf5..08bd498 100644 --- a/reader.cc +++ b/reader.cc @@ -85,12 +85,9 @@ Store *Reader::readNewLinkStore() { Store *Reader::readStoreOrElemStore(bool isElem) { INTEGER id = isElem ? d_elemList.size() : d_storeList.size(); - SHORTCHAR** path = newTypePath(); - readPath(path); - for (int i = 0; path[i] != 0; ++i) { - std::cout << path[i] << std::endl; - } - SHORTCHAR* type = path[0]; + TypePath path = readPath(); + std::cout << path.toString() << std::endl; + const std::string &type = path[0]; INTEGER comment = readInt(); std::streampos pos1 = d_rider.tellg(); std::streamoff next = readInt(); @@ -237,18 +234,22 @@ void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos } } -void Reader::readPath(SHORTCHAR **path) { +TypePath Reader::readPath() { + TypePath path; SHORTCHAR kind = readSChar(); + SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator). int i; for (i = 0; kind == Store::NEWEXT; ++i) { - readSString(path[i]); + readSString(buf); + path.push_back(std::string(buf)); addPathComponent(i == 0, path[i]); // IF path[i] # elemTName THEN INC(i) END; kind = readSChar(); } if (kind == Store::NEWBASE) { - readSString(path[i]); + readSString(buf); + path.push_back(std::string(buf)); addPathComponent(i == 0, path[i]); ++i; } else if (kind == Store::OLDTYPE) { @@ -257,7 +258,7 @@ void Reader::readPath(SHORTCHAR **path) { d_typeList[d_typeList.size() - 1]->baseId = id; } while (id != -1) { - path[i] = d_typeList[id]->name; + path.push_back(d_typeList[id]->name); id = d_typeList[id]->baseId; // IF path[i] # elemTName THEN INC(i) END ++i; @@ -265,10 +266,10 @@ void Reader::readPath(SHORTCHAR **path) { } else { throw 100; } - path[i] = 0; + return path; } -void Reader::addPathComponent(bool first, SHORTCHAR *typeName) { +void Reader::addPathComponent(bool first, const std::string &typeName) { int next = d_typeList.size(); int curr = next - 1; if (!first) { diff --git a/reader.h b/reader.h index 208cf28..764a276 100644 --- a/reader.h +++ b/reader.h @@ -10,12 +10,11 @@ namespace odc { -class TypeEntry { - public: - SHORTCHAR *name; +struct TypeEntry { + const std::string name; INTEGER baseId; - TypeEntry(SHORTCHAR *typeName) : name(typeName), baseId(-1) {} + TypeEntry(const std::string &typeName) : name(typeName), baseId(-1) {} }; /** @@ -261,26 +260,11 @@ private: Store *readNewLinkStore(); void internalizeAlien(Alien *alien, std::streampos down, std::streampos end); - /* - TypeName* = ARRAY 64 OF CHAR; - TypePath* = ARRAY 16 OF TypeName; - OpName* = ARRAY 32 OF CHAR; - */ - inline SHORTCHAR *newTypeName() { - return new SHORTCHAR[64]; - } - inline SHORTCHAR **newTypePath() { - SHORTCHAR **out = new SHORTCHAR*[16]; - for (int i = 0; i < 16; ++i) { - out[i] = newTypeName(); - } - return out; - } - void readPath(SHORTCHAR **path); + TypePath readPath(); /** * Add another component to the current path. If first==true, start a new path. */ - void addPathComponent(bool first, SHORTCHAR *typeName); + void addPathComponent(bool first, const std::string &typeName); }; } // namespace odc diff --git a/store.cc b/store.cc index d7db23b..82e20e6 100644 --- a/store.cc +++ b/store.cc @@ -2,6 +2,24 @@ namespace odc { +template T join(const A & begin, const A & end, const T &sep) { + T result; + + if (begin != end) { + A it = begin; + result.append(*it); + for (++it; it != end; ++it) { + result.append(sep).append(*it); + } + } + + return result; +} + +std::string TypePath::toString() const { + return join(begin(), end(), std::string("->")); +} + Store::Store(INTEGER id): d_id(id) {} INTEGER Store::getId() { diff --git a/store.h b/store.h index be351b7..039354d 100644 --- a/store.h +++ b/store.h @@ -5,9 +5,15 @@ #include #include +#include namespace odc { + class TypePath : public std::vector { + public: + std::string toString() const; + }; + /** * TYPE Store * ABSTRACT