Skip to content

Commit

Permalink
AURORA: Use ScopedPtr/ScopedArray in the 2DA reader and registry
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent fcebd74 commit 9180dfa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
34 changes: 13 additions & 21 deletions src/aurora/2dafile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/strutil.h"
#include "src/common/encoding.h"
#include "src/common/readstream.h"
Expand Down Expand Up @@ -247,10 +248,10 @@ void TwoDAFile::readRows2a(Common::SeekableReadStream &twoda,

/* And now read the individual cells in the rows. */

size_t columnCount = _headers.size();
const size_t columnCount = _headers.size();

while (!twoda.eos()) {
TwoDARow *row = new TwoDARow(*this);
Common::ScopedPtr<TwoDARow> row(new TwoDARow(*this));

/* Skip the first token, which is the row index, possibly indented.
* The row index is implicit in the data and its use in the 2DA
Expand All @@ -265,13 +266,11 @@ void TwoDAFile::readRows2a(Common::SeekableReadStream &twoda,
// And move to the next line
tokenize.nextChunk(twoda);

if (count == 0) {
// Ignore empty lines
delete row;
// Ignore empty lines
if (count == 0)
continue;
}

_rows.push_back(row);
_rows.push_back(row.release());
}
}

Expand Down Expand Up @@ -319,11 +318,11 @@ void TwoDAFile::readRows2b(Common::SeekableReadStream &twoda) {
* cell data.
*/

size_t columnCount = _headers.size();
size_t rowCount = _rows.size();
size_t cellCount = columnCount * rowCount;
const size_t columnCount = _headers.size();
const size_t rowCount = _rows.size();
const size_t cellCount = columnCount * rowCount;

uint32 *offsets = new uint32[cellCount];
Common::ScopedArray<uint32> offsets(new uint32[cellCount]);

Common::StreamTokenizer tokenize(Common::StreamTokenizer::kRuleHeed);

Expand All @@ -334,30 +333,23 @@ void TwoDAFile::readRows2b(Common::SeekableReadStream &twoda) {

twoda.skip(2); // Size of the data segment in bytes

size_t dataOffset = twoda.pos();
const size_t dataOffset = twoda.pos();

for (size_t i = 0; i < rowCount; i++) {
_rows[i] = new TwoDARow(*this);

_rows[i]->_data.resize(columnCount);

for (size_t j = 0; j < columnCount; j++) {
size_t offset = dataOffset + offsets[i * columnCount + j];
const size_t offset = dataOffset + offsets[i * columnCount + j];

try {
twoda.seek(offset);
} catch (...) {
delete[] offsets;
throw;
}
twoda.seek(offset);

_rows[i]->_data[j] = tokenize.getToken(twoda);
if (_rows[i]->_data[j].empty())
_rows[i]->_data[j] = "****";
}
}

delete[] offsets;
}

void TwoDAFile::createHeaderMap() {
Expand Down
3 changes: 3 additions & 0 deletions src/aurora/2dafile.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class TwoDARow : boost::noncopyable {
const Common::UString &getCell(size_t n) const;

friend class TwoDAFile;

template<typename T>
friend void Common::DeallocatorDefault::destroy(T *);
};

/** Class to hold the two-dimensional array of a 2DA file.
Expand Down
48 changes: 22 additions & 26 deletions src/aurora/2dareg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/readstream.h"

#include "src/aurora/2dareg.h"
Expand Down Expand Up @@ -157,46 +158,41 @@ void TwoDARegistry::removeGDA(const Common::UString &name) {
}

TwoDAFile *TwoDARegistry::load2DA(const Common::UString &name) {
Common::SeekableReadStream *twodaFile = 0;
TwoDAFile *twoda = 0;
Common::ScopedPtr<Common::SeekableReadStream> twodaFile;
Common::ScopedPtr<TwoDAFile> twoda;

try {
if (!(twodaFile = ResMan.getResource(name, kFileType2DA)))
twodaFile.reset(ResMan.getResource(name, kFileType2DA));
if (!twodaFile)
throw Common::Exception("No such 2DA");

twoda = new TwoDAFile(*twodaFile);
} catch (Common::Exception &e) {
delete twoda;
twoda.reset(new TwoDAFile(*twodaFile));

} catch (Common::Exception &e) {
e.add("Failed loading 2DA \"%s\"", name.c_str());
throw;

} catch (...) {
delete twoda;
throw;
}

delete twodaFile;

return twoda;
return twoda.release();
}

GDAFile *TwoDARegistry::loadGDA(const Common::UString &name) {
Common::SeekableReadStream *gdaFile = 0;
GDAFile *gda = 0;
Common::ScopedPtr<Common::SeekableReadStream> gdaFile;
Common::ScopedPtr<GDAFile> gda;

try {
if (!(gdaFile = ResMan.getResource(name, kFileTypeGDA)))
gdaFile.reset(ResMan.getResource(name, kFileTypeGDA));
if (!gdaFile)
throw Common::Exception("No such GDA");

gda = new GDAFile(gdaFile);
} catch (Common::Exception &e) {
gda.reset(new GDAFile(gdaFile.release()));

} catch (Common::Exception &e) {
e.add("Failed loading GDA \"%s\"", name.c_str());
throw;
}

return gda;
return gda.release();
}

GDAFile *TwoDARegistry::loadMGDA(Common::UString prefix) {
Expand All @@ -210,36 +206,36 @@ GDAFile *TwoDARegistry::loadMGDA(Common::UString prefix) {
std::list<ResourceManager::ResourceID> gdas;
ResMan.getAvailableResources(kFileTypeGDA, gdas);

GDAFile *gda = 0;
Common::ScopedPtr<GDAFile> gda;

try {
for (std::list<ResourceManager::ResourceID>::const_iterator g = gdas.begin(); g != gdas.end(); ++g) {
// Find all GDAs that match the prefix
if (!g->name.toLower().beginsWith(prefix))
continue;

// Load the GDA
Common::SeekableReadStream *stream = ResMan.getResource(g->name, kFileTypeGDA);

Common::ScopedPtr<Common::SeekableReadStream> stream(ResMan.getResource(g->name, kFileTypeGDA));
if (!stream)
throw Common::Exception("No such GDA \"%s\"", g->name.c_str());

// If this is the first GDA, plain load it. Otherwise, merge it into the first one
if (!gda)
gda = new GDAFile(stream);
gda.reset(new GDAFile(stream.release()));
else
gda->add(stream);
gda->add(stream.release());
}

if (!gda)
throw Common::Exception("No such GDA");

} catch (Common::Exception &e) {
delete gda;

e.add("Failed loading multiple GDA \"%s\"", prefix.c_str());
throw;
}

return gda;
return gda.release();
}

} // End of namespace Aurora

0 comments on commit 9180dfa

Please sign in to comment.