AGI: Replace use of strdup with Common::String

It was also necessary to make sure that the Common::String objects
were initialised correctly by switching to use a C++ container
for engine objects instead of calloc, since they were no longer
C-compatible PODs.
This commit is contained in:
Colin Snover 2017-12-06 16:23:48 -06:00 committed by Eugene Sandulenko
parent 4db0f20f47
commit 0851a30769
3 changed files with 9 additions and 34 deletions

View File

@ -214,7 +214,7 @@ int AgiEngine::agiDeinit() {
agiUnloadResources(); // unload resources in memory
_loader->unloadResource(RESOURCETYPE_LOGIC, 0);
ec = _loader->deinit();
unloadObjects();
_objects.clear();
_words->unloadDictionary();
clearImageStack();
@ -386,8 +386,6 @@ AgiEngine::AgiEngine(OSystem *syst, const AGIGameDescription *gameDesc) : AgiBas
memset(&_stringdata, 0, sizeof(struct StringData));
_objects = NULL;
_restartGame = false;
_firstSlot = 0;

View File

@ -31,6 +31,7 @@
#include "common/rect.h"
#include "common/rendermode.h"
#include "common/stack.h"
#include "common/str.h"
#include "common/system.h"
#include "engines/engine.h"
@ -354,7 +355,7 @@ struct AgiControllerKeyMapping {
struct AgiObject {
int location;
char *name;
Common::String name;
};
struct AgiDir {
@ -763,7 +764,7 @@ private:
int _firstSlot;
public:
AgiObject *_objects; // objects in the game
Common::Array<AgiObject> _objects; // objects in the game
StringData _stringdata;
@ -852,14 +853,12 @@ public:
int showObjects();
int loadObjects(const char *fname);
int loadObjects(Common::File &fp);
void unloadObjects();
const char *objectName(uint16 objectNr);
int objectGetLocation(uint16 objectNr);
void objectSetLocation(uint16 objectNr, int);
private:
int decodeObjects(uint8 *mem, uint32 flen);
int readObjects(Common::File &fp, int flen);
int allocObjects(int);
// Logic
public:

View File

@ -26,20 +26,12 @@
namespace Agi {
int AgiEngine::allocObjects(int n) {
if ((_objects = (AgiObject *)calloc(n, sizeof(struct AgiObject))) == NULL)
return errNotEnoughMemory;
return errOK;
}
int AgiEngine::decodeObjects(uint8 *mem, uint32 flen) {
unsigned int i, so, padsize, spos;
padsize = _game.gameFlags & ID_AMIGA ? 4 : 3;
_game.numObjects = 0;
_objects = NULL;
// check if first pointer exceeds file size
// if so, its encrypted, else it is not
@ -60,8 +52,7 @@ int AgiEngine::decodeObjects(uint8 *mem, uint32 flen) {
_game.numObjects = READ_LE_UINT16(mem) / padsize;
debugC(5, kDebugLevelResources, "num_objects = %d (padsize = %d)", _game.numObjects, padsize);
if (allocObjects(_game.numObjects) != errOK)
return errNotEnoughMemory;
_objects.resize(_game.numObjects);
// build the object list
spos = getVersion() >= 0x2000 ? padsize : 0;
@ -72,15 +63,15 @@ int AgiEngine::decodeObjects(uint8 *mem, uint32 flen) {
offset = READ_LE_UINT16(mem + so) + spos;
if ((uint) offset < flen) {
_objects[i].name = (char *)strdup((const char *)mem + offset);
_objects[i].name = (const char *)mem + offset;
} else {
warning("object %i name beyond object filesize (%04x > %04x)", i, offset, flen);
_objects[i].name = strdup("");
_objects[i].name.clear();
}
// Don't show the invalid "?" object in ego's inventory in the fanmade
// game Beyond the Titanic 2 (bug #3116541).
if (!strcmp(_objects[i].name, "?") && _objects[i].location == EGO_OWNED)
if (_objects[i].name == "?" && _objects[i].location == EGO_OWNED)
_objects[i].location = 0;
}
debug(0, "Reading objects: %d objects read.", _game.numObjects);
@ -131,19 +122,6 @@ int AgiEngine::readObjects(Common::File &fp, int flen) {
return errOK;
}
void AgiEngine::unloadObjects() {
unsigned int i;
if (_objects != NULL) {
for (i = 0; i < _game.numObjects; i++) {
free(_objects[i].name);
_objects[i].name = NULL;
}
free(_objects);
_objects = NULL;
}
}
void AgiEngine::objectSetLocation(uint16 objectNr, int i) {
if (objectNr >= _game.numObjects) {
warning("AgiEngine::objectSetLocation: Can't access object %d.\n", objectNr);
@ -165,7 +143,7 @@ const char *AgiEngine::objectName(uint16 objectNr) {
warning("AgiEngine::objectName: Can't access object %d.\n", objectNr);
return "";
}
return _objects[objectNr].name;
return _objects[objectNr].name.c_str();
}
} // End of namespace Agi