use ResType class in Resources lib, prevent dupicate resources

This commit is contained in:
Wolfgang Thaller 2014-10-12 19:12:10 +02:00
parent 127db48a26
commit df0e042120
7 changed files with 60 additions and 22 deletions

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <cassert>
#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)
{

View File

@ -4,14 +4,16 @@
#include <iosfwd>
#include <string>
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

View File

@ -1,11 +1,14 @@
#include "ResType.h"
#include <iostream>
#include <cassert>
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<char>(x >> 24);

View File

@ -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<ResType, int>
{
ResRef() : std::pair<ResType, int>(ResType(), 0) {}
ResRef(ResType t, int id) : std::pair<ResType, int>(t,id) {}
ResType& type() { return first; }
ResType type() const { return first; }
int& id() { return second; }
int id() const { return second; }
};
#endif // RESTYPE_H

View File

@ -3,10 +3,13 @@
#include <iostream>
#include <map>
#include <vector>
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<int, int> > resourceInfos;
std::streampos datastart = out.tellp();
for(std::vector<Resource>::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;
}

View File

@ -2,22 +2,25 @@
#define RESOURCEFILES_H
#include <string>
#include <vector>
#include <map>
#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<Resource> resources;
std::map<ResRef, Resource> 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);
};

View File

@ -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"