FULLPIPE: Improve memory safety and typing of NGIArchive

This commit is contained in:
Colin Snover 2017-11-12 10:15:59 -06:00 committed by Eugene Sandulenko
parent 92f5718e2b
commit 8f1d76d261
3 changed files with 9 additions and 16 deletions

View File

@ -70,7 +70,6 @@ NGIArchive::NGIArchive(const Common::String &filename) : _ngiFilename(filename)
}
NgiHeader header;
NgiHeader *head;
for (uint i = 0; i < count; i++) {
memcpy(header.filename, &fat[i * 32], 12);
@ -84,9 +83,7 @@ NGIArchive::NGIArchive(const Common::String &filename) : _ngiFilename(filename)
warning("File has flags: %.8x\n", header.flags & 0x1e0);
}
head = new NgiHeader(header);
_headers[header.filename] = head;
_headers[header.filename].reset(new NgiHeader(header));
}
free(fat);
@ -98,19 +95,14 @@ NGIArchive::NGIArchive(const Common::String &filename) : _ngiFilename(filename)
NGIArchive::~NGIArchive() {
debugC(0, kDebugLoading, "NGIArchive Destructor Called");
NgiHeadersMap::iterator it = _headers.begin();
for ( ; it != _headers.end(); ++it) {
delete it->_value;
}
g_fp->_currArchive = 0;
g_fp->_currArchive = nullptr;
}
bool NGIArchive::hasFile(const Common::String &name) const {
return _headers.contains(name);
}
int NGIArchive::listMembers(Common::ArchiveMemberList &list) const {
int NGIArchive::listMembers(Common::ArchiveMemberList &list) const {
int matches = 0;
NgiHeadersMap::const_iterator it = _headers.begin();
@ -134,7 +126,7 @@ Common::SeekableReadStream *NGIArchive::createReadStreamForMember(const Common::
return 0;
}
NgiHeader *hdr = _headers[name];
NgiHeader *hdr = _headers[name].get();
Common::File archiveFile;
archiveFile.open(_ngiFilename);
@ -149,7 +141,7 @@ Common::SeekableReadStream *NGIArchive::createReadStreamForMember(const Common::
return new Common::MemoryReadStream(data, hdr->size, DisposeAfterUse::YES);
}
Common::Archive *makeNGIArchive(const Common::String &name) {
NGIArchive *makeNGIArchive(const Common::String &name) {
return new NGIArchive(name);
}

View File

@ -23,6 +23,7 @@
#ifndef FULLPIPE_NGIARCHIVE_H
#define FULLPIPE_NGIARCHIVE_H
#include "common/ptr.h"
#include "common/str.h"
namespace Fullpipe {
@ -39,7 +40,7 @@ struct NgiHeader {
char filename[NGI_FILENAME_MAX];
};
typedef Common::HashMap<Common::String, NgiHeader*, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> NgiHeadersMap;
typedef Common::HashMap<Common::String, Common::ScopedPtr<NgiHeader>, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> NgiHeadersMap;
class NGIArchive : public Common::Archive {
NgiHeadersMap _headers;
@ -62,7 +63,7 @@ public:
*
* May return 0 in case of a failure.
*/
Common::Archive *makeNGIArchive(const Common::String &name);
NGIArchive *makeNGIArchive(const Common::String &name);
} // End of namespace Fullpipe

View File

@ -57,7 +57,7 @@ bool SoundList::load(MfcArchive &file, const Common::String &fname) {
_soundItems = (Sound **)calloc(_soundItemsCount, sizeof(Sound *));
if (!fname.empty()) {
_libHandle = (NGIArchive *)makeNGIArchive(fname);
_libHandle = makeNGIArchive(fname);
} else {
_libHandle = 0;
}