Refactor map_resave tool

Add log messages for errors and success.

Extract function `ResaveMap`.

Ensure reader is closed when writer could not be opened.
This commit is contained in:
Robert Müller 2023-12-17 12:39:35 +01:00
parent 4af28fd2d8
commit a883018c88

View file

@ -1,24 +1,30 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <base/logger.h>
#include <base/system.h>
#include <engine/shared/datafile.h>
#include <engine/storage.h>
int main(int argc, const char **argv)
static const char *TOOL_NAME = "map_resave";
static int ResaveMap(const char *pSourceMap, const char *pDestinationMap, IStorage *pStorage)
{
CCmdlineFix CmdlineFix(&argc, &argv);
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_BASIC, argc, argv);
if(!pStorage || argc != 3)
return -1;
CDataFileReader Reader;
if(!Reader.Open(pStorage, argv[1], IStorage::TYPE_ABSOLUTE))
if(!Reader.Open(pStorage, pSourceMap, IStorage::TYPE_ABSOLUTE))
{
log_error(TOOL_NAME, "Failed to open source map '%s' for reading", pSourceMap);
return -1;
}
CDataFileWriter Writer;
if(!Writer.Open(pStorage, argv[2]))
if(!Writer.Open(pStorage, pDestinationMap))
{
log_error(TOOL_NAME, "Failed to open destination map '%s' for writing", pDestinationMap);
Reader.Close();
return -1;
}
// add all items
for(int Index = 0; Index < Reader.NumItems(); Index++)
@ -44,5 +50,27 @@ int main(int argc, const char **argv)
Reader.Close();
Writer.Finish();
log_info(TOOL_NAME, "Resaved '%s' to '%s'", pSourceMap, pDestinationMap);
return 0;
}
int main(int argc, const char **argv)
{
CCmdlineFix CmdlineFix(&argc, &argv);
log_set_global_logger_default();
if(argc != 3)
{
log_error(TOOL_NAME, "Usage: %s <source map> <destination map>", TOOL_NAME);
return -1;
}
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_BASIC, argc, argv);
if(!pStorage)
{
log_error(TOOL_NAME, "Error creating basic storage");
return -1;
}
return ResaveMap(argv[1], argv[2], pStorage);
}