Skip to content

Commit

Permalink
Use unique_ptr for FileWriter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 7, 2023
1 parent af0f4e3 commit eeae371
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
22 changes: 5 additions & 17 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,11 @@ std::string PlayerTypeNames[static_cast<int>(PlayerTypes::PlayerRescueActive) +
// Write the map presentation file
static int WriteMapPresentation(const fs::path &mapname, CMap &map, Vec2i newSize)
{
FileWriter *f = nullptr;

int numplayers = 0;
int topplayer = PlayerMax - 2;

try {
f = CreateFileWriter(mapname);
std::unique_ptr<FileWriter> f = CreateFileWriter(mapname);
f->printf("-- Stratagus Map Presentation\n");
f->printf("-- File generated by the Stratagus V" VERSION " builtin map editor.\n");
// MAPTODO Copyright notice in generated file
Expand Down Expand Up @@ -299,11 +297,8 @@ static int WriteMapPresentation(const fs::path &mapname, CMap &map, Vec2i newSiz
}
} catch (const FileException &) {
fprintf(stderr, "ERROR: cannot write the map presentation\n");
delete f;
return -1;
}

delete f;
return 1;
}

Expand All @@ -317,10 +312,8 @@ static int WriteMapPresentation(const fs::path &mapname, CMap &map, Vec2i newSiz
*/
static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain, Vec2i newSize, Vec2i offset)
{
FileWriter *f = nullptr;

try {
f = CreateFileWriter(mapSetup);
std::unique_ptr<FileWriter> f = CreateFileWriter(mapSetup);

f->printf("-- Stratagus Map Setup\n");
f->printf("-- File generated by the Stratagus V" VERSION " builtin map editor.\n");
Expand All @@ -330,9 +323,8 @@ static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain,
f->printf("-- preamble\n");
f->printf("if CanAccessFile(__file__ .. \".preamble\") then Load(__file__ .. \".preamble\", Editor.Running == 0) end\n\n");
if (!Map.Info.Preamble.empty()) {
FileWriter *preamble = CreateFileWriter(mapSetup.string() + ".preamble");
std::unique_ptr<FileWriter> preamble = CreateFileWriter(mapSetup.string() + ".preamble");
preamble->write(Map.Info.Preamble.c_str(), Map.Info.Preamble.size());
delete preamble;
}

f->printf("-- player configuration\n");
Expand Down Expand Up @@ -361,7 +353,7 @@ static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain,
f->printf("LoadTileModels(\"%s\")\n\n", map.TileModelsFileName.string().c_str());

if (writeTerrain) {
if (newSize.x != 0 && newSize.y != 0) {
if (newSize.x != 0 && newSize.y != 0) {
f->printf("for x=0,%d,1 do\n", newSize.x - 1);
f->printf(" for y=0,%d,1 do\n", newSize.y - 1);
f->printf(" SetTile(%d, x, y, 0)\n", Map.Tileset->getDefaultTileIndex());
Expand Down Expand Up @@ -486,18 +478,14 @@ static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain,
f->printf("-- postamble\n");
f->printf("if CanAccessFile(__file__ .. \".postamble\") then Load(__file__ .. \".postamble\", Editor.Running == 0) end\n\n");
if (!Map.Info.Postamble.empty()) {
FileWriter *postamble = CreateFileWriter(mapSetup.string() + ".postamble");
std::unique_ptr<FileWriter> postamble = CreateFileWriter(mapSetup.string() + ".postamble");
postamble->write(Map.Info.Postamble.c_str(), Map.Info.Postamble.size());
delete postamble;
}

} catch (const FileException &) {
fprintf(stderr, "Can't save map setup : '%s' \n", mapSetup.u8string().c_str());
delete f;
return -1;
}

delete f;
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/include/iolib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
-- Includes
----------------------------------------------------------------------------*/

#include <memory>
#include <vector>
#include "filesystem.h"
#include "SDL.h"
Expand Down Expand Up @@ -72,7 +73,7 @@ class FileWriter
** If the file name ends with '.gz', the file writer returned
** will compress the data with zlib.
*/
FileWriter *CreateFileWriter(const fs::path &filename);
std::unique_ptr<FileWriter> CreateFileWriter(const fs::path &filename);

/**
** FileList struct used by directory access routine
Expand Down
10 changes: 5 additions & 5 deletions src/stratagus/iolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ class RawFileWriter : public FileWriter
if (file) { fclose(file); }
}

virtual int write(const char *data, unsigned int size)
int write(const char *data, unsigned int size) override
{
return fwrite(data, size, 1, file);
}
Expand All @@ -769,7 +769,7 @@ class GzFileWriter : public FileWriter
if (file) { gzclose(file); }
}

virtual int write(const char *data, unsigned int size)
int write(const char *data, unsigned int size) override
{
return gzwrite(file, data, size);
}
Expand All @@ -778,12 +778,12 @@ class GzFileWriter : public FileWriter
/**
** Create FileWriter
*/
FileWriter *CreateFileWriter(const fs::path &filename)
std::unique_ptr<FileWriter> CreateFileWriter(const fs::path &filename)
{
if (filename.extension() == ".gz") {
return new GzFileWriter(filename);
return std::make_unique<GzFileWriter>(filename);
} else {
return new RawFileWriter(filename);
return std::make_unique<RawFileWriter>(filename);
}
}

Expand Down

0 comments on commit eeae371

Please sign in to comment.