From df0e042120d453426215ba8750876130560d2fff Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Sun, 12 Oct 2014 19:12:10 +0200 Subject: [PATCH] use ResType class in Resources lib, prevent dupicate resources --- MakeAPPL/BinaryIO.cc | 14 ++++++-------- MakeAPPL/BinaryIO.h | 6 ++++-- MakeAPPL/ResType.cc | 20 ++++++++++++++++++++ MakeAPPL/ResType.h | 12 ++++++++++++ MakeAPPL/ResourceFiles.cc | 12 ++++++++---- MakeAPPL/ResourceFiles.h | 16 +++++++++------- MakeAPPL/main.cc | 2 +- 7 files changed, 60 insertions(+), 22 deletions(-) diff --git a/MakeAPPL/BinaryIO.cc b/MakeAPPL/BinaryIO.cc index 32070edbd4..b8bf73b3ec 100644 --- a/MakeAPPL/BinaryIO.cc +++ b/MakeAPPL/BinaryIO.cc @@ -2,6 +2,8 @@ #include #include +#include "ResType.h" + void byte(std::ostream& out, int byte) { out.put((unsigned char)byte); @@ -11,10 +13,9 @@ void word(std::ostream& out, int word) byte(out,(word >> 8) & 0xFF); byte(out,word & 0xFF); } -void ostype(std::ostream& out, std::string type) +void ostype(std::ostream& out, ResType type) { - assert(type.size() == 4); - out << type; + longword(out, type); } void longword(std::ostream& out, int longword) { @@ -34,12 +35,9 @@ int word(std::istream& in) int b = byte(in); return (a << 8) | b; } -std::string ostype(std::istream& in) +ResType ostype(std::istream& in) { - char s[5]; - in.read(s,4); - s[4] = 0; - return s; + return longword(in); } int longword(std::istream& in) { diff --git a/MakeAPPL/BinaryIO.h b/MakeAPPL/BinaryIO.h index e4b84d4336..a23f004bfa 100644 --- a/MakeAPPL/BinaryIO.h +++ b/MakeAPPL/BinaryIO.h @@ -4,14 +4,16 @@ #include #include +class ResType; + void byte(std::ostream& out, int byte); void word(std::ostream& out, int word); -void ostype(std::ostream& out, std::string type); +void ostype(std::ostream& out, ResType type); void longword(std::ostream& out, int longword); int byte(std::istream& in); int word(std::istream& in); -std::string ostype(std::istream& in); +ResType ostype(std::istream& in); int longword(std::istream& in); #endif // BINARYIO_H diff --git a/MakeAPPL/ResType.cc b/MakeAPPL/ResType.cc index add8a6302a..87ece75f3e 100644 --- a/MakeAPPL/ResType.cc +++ b/MakeAPPL/ResType.cc @@ -1,11 +1,14 @@ #include "ResType.h" #include +#include ResType::ResType(const std::string &str) { auto p = str.begin(); auto e = str.end(); + assert(str.size() == 4); + x = 0; while(p != e) { @@ -15,6 +18,23 @@ ResType::ResType(const std::string &str) } } +ResType::ResType(const char *s) +{ + auto p = s; + auto e = s + 4; + + assert(s[0] && s[1] && s[2] && s[3] && !s[4]); + + x = 0; + while(p != e) + { + x <<= 8; + x |= (*p) & 0xFF; + ++p; + } +} + + ResType::operator std::string() { char c1 = static_cast(x >> 24); diff --git a/MakeAPPL/ResType.h b/MakeAPPL/ResType.h index 324d94c002..0b44d48e75 100644 --- a/MakeAPPL/ResType.h +++ b/MakeAPPL/ResType.h @@ -11,6 +11,7 @@ public: ResType() : x(0) {} ResType(int x) : x(x) {} ResType(const std::string& s); + ResType(const char* s); operator int() const { return x; } bool operator<(ResType y) const { return x < y.x; } @@ -20,4 +21,15 @@ public: std::ostream& operator<<(std::ostream& out, ResType t); +struct ResRef : public std::pair +{ + ResRef() : std::pair(ResType(), 0) {} + ResRef(ResType t, int id) : std::pair(t,id) {} + + ResType& type() { return first; } + ResType type() const { return first; } + int& id() { return second; } + int id() const { return second; } +}; + #endif // RESTYPE_H diff --git a/MakeAPPL/ResourceFiles.cc b/MakeAPPL/ResourceFiles.cc index 9f84426d7f..ad6ce0ca1c 100644 --- a/MakeAPPL/ResourceFiles.cc +++ b/MakeAPPL/ResourceFiles.cc @@ -3,10 +3,13 @@ #include #include +#include void Resources::addResources(const Resources& res) { - resources.insert(resources.end(),res.resources.begin(), res.resources.end()); + for(auto& rr : res.resources) + resources.insert(rr); +// resources.insert(resources.end(),res.resources.begin(), res.resources.end()); } void Resources::writeFork(std::ostream& out) const @@ -19,10 +22,11 @@ void Resources::writeFork(std::ostream& out) const out.seekp(start + std::streampos(0x100)); std::map< std::string, std::map > resourceInfos; std::streampos datastart = out.tellp(); - for(std::vector::const_iterator p = resources.begin(); p != resources.end(); ++p) + for(auto& rr : resources) { - const std::string& data = p->getData(); - resourceInfos[ p->getType() ][ p->getID() ] = out.tellp() - datastart; + const Resource& r = rr.second; + const std::string& data = r.getData(); + resourceInfos[ r.getType() ][ r.getID() ] = out.tellp() - datastart; longword(out, data.size()); out << data; } diff --git a/MakeAPPL/ResourceFiles.h b/MakeAPPL/ResourceFiles.h index 8e24ca80cb..7ea6b96e73 100644 --- a/MakeAPPL/ResourceFiles.h +++ b/MakeAPPL/ResourceFiles.h @@ -2,22 +2,25 @@ #define RESOURCEFILES_H #include -#include +#include +#include "ResType.h" class Resource { - std::string type; + ResType type; int id; std::string name; std::string data; int attr; public: - Resource(std::string type, int id, std::string data, std::string name = "", int attr = 0) + Resource() {} + Resource(ResType type, int id, std::string data, std::string name = "", int attr = 0) : type(type), id(id), data(data), name(name), attr(attr) {} const std::string& getData() const { return data; } - inline std::string getType() const { return type; } + inline ResType getType() const { return type; } inline int getID() const { return id; } + inline ResRef getTypeAndID() const { return ResRef(type, id); } }; class Fork @@ -29,13 +32,12 @@ public: class Resources : public Fork { - std::vector resources; + std::map resources; public: Resources() {} Resources(std::istream& in); void writeFork(std::ostream& out) const; - void addResource(Resource res) { resources.push_back(res); } - + void addResource(Resource res) { resources[res.getTypeAndID()] = res; } void addResources(const Resources& res); }; diff --git a/MakeAPPL/main.cc b/MakeAPPL/main.cc index e2baaa3d5d..0da7a782e3 100644 --- a/MakeAPPL/main.cc +++ b/MakeAPPL/main.cc @@ -233,7 +233,7 @@ int main(int argc, char *argv[]) std::string fn = argv[i++]; std::string flt = readfile(fn); - rsrc.addResource(Resource("CODE", 0, + rsrc.addResource(Resource(ResType("CODE"), 0, fromhex( "00000028 00000000 00000008 00000020" "0000 3F3C 0001 A9F0"