Skip to content

Commit

Permalink
ALL: Use ScopedPtr in the tools
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent 202c6a5 commit 76b1bd2
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 274 deletions.
60 changes: 18 additions & 42 deletions src/convert2da.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "src/version/version.h"

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/util.h"
#include "src/common/strutil.h"
Expand Down Expand Up @@ -185,69 +186,44 @@ static const uint32 k2DAIDTab = MKTAG('2', 'D', 'A', '\t');
static const uint32 kGFFID = MKTAG('G', 'F', 'F', ' ');

void write2DA(Aurora::TwoDAFile &twoDA, const Common::UString &outFile, Format format) {
Common::WriteStream *out = 0;
Common::ScopedPtr<Common::WriteStream> out;
if (!outFile.empty())
out = new Common::WriteFile(outFile);
out.reset(new Common::WriteFile(outFile));
else
out = new Common::StdOutStream;
out.reset(new Common::StdOutStream);

try {
if (format == kFormat2DA)
twoDA.writeASCII(*out);
else if (format == kFormat2DAb)
twoDA.writeBinary(*out);
else
twoDA.writeCSV(*out);

} catch (...) {
delete out;
throw;
}
if (format == kFormat2DA)
twoDA.writeASCII(*out);
else if (format == kFormat2DAb)
twoDA.writeBinary(*out);
else
twoDA.writeCSV(*out);

out->flush();

delete out;
}

Aurora::TwoDAFile *get2DAGDA(Common::SeekableReadStream *stream) {
uint32 id = 0;
Common::ScopedPtr<Common::SeekableReadStream> fStream(stream);

try {
id = Aurora::AuroraFile::readHeaderID(*stream);
stream->seek(0);
} catch (...) {
delete stream;
throw;
}

if ((id == k2DAID) || (id == k2DAIDTab)) {
Aurora::TwoDAFile *twoDA = new Aurora::TwoDAFile(*stream);
const uint32 id = Aurora::AuroraFile::readHeaderID(*fStream);
fStream->seek(0);

delete stream;
return twoDA;
}
if ((id == k2DAID) || (id == k2DAIDTab))
return new Aurora::TwoDAFile(*fStream);

if (id == kGFFID) {
Aurora::GDAFile gda(stream);
Aurora::GDAFile gda(fStream.release());

return new Aurora::TwoDAFile(gda);
}

delete stream;
throw Common::Exception("Not a 2DA or GDA file");
}

void convert2DA(const Common::UString &file, const Common::UString &outFile, Format format) {
Aurora::TwoDAFile *twoDA = get2DAGDA(new Common::ReadFile(file));

try {
write2DA(*twoDA, outFile, format);
} catch (...) {
delete twoDA;
throw;
}
Common::ScopedPtr<Aurora::TwoDAFile> twoDA(get2DAGDA(new Common::ReadFile(file)));

delete twoDA;
write2DA(*twoDA, outFile, format);
}

void convert2DA(const std::vector<Common::UString> &files, const Common::UString &outFile, Format format) {
Expand Down
82 changes: 38 additions & 44 deletions src/fixpremiumgff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "src/version/version.h"

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/util.h"
#include "src/common/error.h"
Expand Down Expand Up @@ -170,68 +171,61 @@ void fixPremiumGFF(Common::UString &inFile, Common::UString &outFile, Common::US
if (id.size() > 4)
throw Common::Exception("\"%s\" is not a valid GFF id", id.c_str());

Common::SeekableReadStream *in = Common::ReadFile::readIntoMemory(inFile);
Common::ScopedPtr<Common::SeekableReadStream> in(Common::ReadFile::readIntoMemory(inFile));

try {
const uint32 inID = in->readUint32BE();
const uint32 inVersion = in->readUint32BE();

if ((inVersion == kVersion32) || (inVersion == kVersion33) ||
(inVersion == kVersion40) || (inVersion == kVersion41)) {

status("\"%s\" is already a standard GFF file", inFile.c_str());
const uint32 inID = in->readUint32BE();
const uint32 inVersion = in->readUint32BE();

if (inFile == outFile)
return;
if ((inVersion == kVersion32) || (inVersion == kVersion33) ||
(inVersion == kVersion40) || (inVersion == kVersion41)) {

Common::WriteFile out(outFile);
status("\"%s\" is already a standard GFF file", inFile.c_str());

in->seek(0);
out.writeStream(*in);

out.flush();
out.close();
if (inFile == outFile)
return;
}

if ((FROM_BE_32(inID) < 0x30) || (FROM_BE_32(inID) > 0x12F))
throw Common::Exception("File \"%s\" is neither a standard, nor a premium GFF file", inFile.c_str());
Common::WriteFile out(outFile);

const uint32 value = FROM_BE_32(inID) - 0x30;
in->seek(0);
out.writeStream(*in);

status("Repairing \"%s\" to a GFF with an ID of \"%s\" a correction value of %u",
inFile.c_str(), id.c_str(), value);
out.flush();
out.close();
return;
}

in->seek(0);
if ((FROM_BE_32(inID) < 0x30) || (FROM_BE_32(inID) > 0x12F))
throw Common::Exception("File \"%s\" is neither a standard, nor a premium GFF file", inFile.c_str());

Common::WriteFile out(outFile);
const uint32 value = FROM_BE_32(inID) - 0x30;

out.writeString(id);
status("Repairing \"%s\" to a GFF with an ID of \"%s\" a correction value of %u",
inFile.c_str(), id.c_str(), value);

if (id.size() < 4)
out.writeString(Common::UString(' ', 4 - id.size()));
in->seek(0);

out.writeUint32BE(kVersion32);
Common::WriteFile out(outFile);

for (size_t i = 0; i < 6; i++) {
const uint32 offset = in->readUint32LE();
const uint32 count = in->readUint32LE();
out.writeString(id);

if (offset < value)
throw Common::Exception("File \"%s\" is neither a standard, nor a premium GFF file", inFile.c_str());
if (id.size() < 4)
out.writeString(Common::UString(' ', 4 - id.size()));

out.writeUint32LE(offset - value + 0x08);
out.writeUint32LE(count);
}
out.writeUint32BE(kVersion32);

out.writeStream(*in);
for (size_t i = 0; i < 6; i++) {
const uint32 offset = in->readUint32LE();
const uint32 count = in->readUint32LE();

out.flush();
out.close();
return;
if (offset < value)
throw Common::Exception("File \"%s\" is neither a standard, nor a premium GFF file", inFile.c_str());

} catch (...) {
delete in;
throw;
out.writeUint32LE(offset - value + 0x08);
out.writeUint32LE(count);
}

out.writeStream(*in);

out.flush();
out.close();
}
34 changes: 9 additions & 25 deletions src/gff2xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "src/version/version.h"

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/util.h"
#include "src/common/strutil.h"
Expand Down Expand Up @@ -261,37 +262,20 @@ void printUsage(FILE *stream, const Common::UString &name) {
void dumpGFF(const Common::UString &inFile, const Common::UString &outFile,
Common::Encoding encoding, bool nwnPremium) {

Common::SeekableReadStream *gff = new Common::ReadFile(inFile);
Common::ScopedPtr<Common::SeekableReadStream> gff(new Common::ReadFile(inFile));

XML::GFFDumper *dumper = 0;
try {
dumper = XML::GFFDumper::identify(*gff, nwnPremium);
} catch (...) {
delete gff;
throw;
}

Common::WriteStream *out = 0;
try {
Common::ScopedPtr<XML::GFFDumper> dumper(XML::GFFDumper::identify(*gff, nwnPremium));

if (!outFile.empty())
out = new Common::WriteFile(outFile);
else
out = new Common::StdOutStream;
Common::ScopedPtr<Common::WriteStream> out;
if (!outFile.empty())
out.reset(new Common::WriteFile(outFile));
else
out.reset(new Common::StdOutStream);

dumper->dump(*out, gff, encoding, nwnPremium);

} catch (...) {
delete dumper;
delete out;
throw;
}
dumper->dump(*out, gff.release(), encoding, nwnPremium);

out->flush();

if (!outFile.empty())
status("Converted \"%s\" to \"%s\"", inFile.c_str(), outFile.c_str());

delete dumper;
delete out;
}
28 changes: 12 additions & 16 deletions src/ncgr2tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <cstdio>
#include <cstdlib>

#include <boost/scope_exit.hpp>

#include "src/version/version.h"

#include "src/common/ustring.h"
Expand Down Expand Up @@ -160,24 +162,18 @@ void convert(std::vector<Common::UString> &ncgrFiles, Common::UString &nclrFile,
std::vector<Common::SeekableReadStream *> ncgrs;
ncgrs.resize(ncgrFiles.size(), 0);

try {
for (size_t i = 0; i < ncgrFiles.size(); i++)
if (!ncgrFiles[i].empty() && (ncgrFiles[i] != "\"\"") && (ncgrFiles[i] != "\'\'"))
ncgrs[i] = new Common::ReadFile(ncgrFiles[i]);

Images::NCGR image(ncgrs, width, height, nclr);

image.flipVertically();

image.dumpTGA(outFile);

} catch (...) {
BOOST_SCOPE_EXIT( (&ncgrs) ) {
for (size_t i = 0; i < ncgrs.size(); i++)
delete ncgrs[i];
} BOOST_SCOPE_EXIT_END

throw;
}
for (size_t i = 0; i < ncgrFiles.size(); i++)
if (!ncgrFiles[i].empty() && (ncgrFiles[i] != "\"\"") && (ncgrFiles[i] != "\'\'"))
ncgrs[i] = new Common::ReadFile(ncgrFiles[i]);

Images::NCGR image(ncgrs, width, height, nclr);

image.flipVertically();

for (size_t i = 0; i < ncgrs.size(); i++)
delete ncgrs[i];
image.dumpTGA(outFile);
}

0 comments on commit 76b1bd2

Please sign in to comment.