From 7ff55502f04f9f75b01879ea04518c14d4706046 Mon Sep 17 00:00:00 2001 From: yexo Date: Thu, 25 Feb 2010 20:06:11 +0000 Subject: (svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized --- src/newgrf_config.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/newgrf_config.cpp') diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 31e51d9af..a942279c8 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -20,6 +20,21 @@ #include "fileio_func.h" #include "fios.h" +GRFConfig::GRFConfig(const char *filename) +{ + if (filename != NULL) this->filename = strdup(filename); +} + +GRFConfig::~GRFConfig() +{ + /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */ + if (!HasBit(this->flags, GCF_COPY)) { + free(this->filename); + free(this->name); + free(this->info); + delete this->error; + } +} GRFConfig *_all_grfs; GRFConfig *_grfconfig; @@ -105,27 +120,13 @@ bool FillGRFDetails(GRFConfig *config, bool is_static) } -void ClearGRFConfig(GRFConfig **config) -{ - /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */ - if (!HasBit((*config)->flags, GCF_COPY)) { - free((*config)->filename); - free((*config)->name); - free((*config)->info); - delete (*config)->error; - } - free(*config); - *config = NULL; -} - - /* Clear a GRF Config list */ void ClearGRFConfigList(GRFConfig **config) { GRFConfig *c, *next; for (c = *config; c != NULL; c = next) { next = c->next; - ClearGRFConfig(&c); + delete c; } *config = NULL; } @@ -138,7 +139,7 @@ void ClearGRFConfigList(GRFConfig **config) */ GRFConfig *DuplicateGRFConfig(const GRFConfig *c) { - GRFConfig *config = MallocT(1); + GRFConfig *config = new GRFConfig(); *config = *c; if (c->filename != NULL) config->filename = strdup(c->filename); @@ -203,7 +204,7 @@ static void RemoveDuplicatesFromGRFConfigList(GRFConfig *list) if (cur->ident.grfid != list->ident.grfid) continue; prev->next = cur->next; - ClearGRFConfig(&cur); + delete cur; cur = prev; // Just go back one so it continues as normal later on } @@ -320,8 +321,7 @@ public: bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length) { - GRFConfig *c = CallocT(1); - c->filename = strdup(filename + basepath_length); + GRFConfig *c = new GRFConfig(filename + basepath_length); bool added = true; if (FillGRFDetails(c, false)) { @@ -355,7 +355,7 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length) if (!added) { /* File couldn't be opened, or is either not a NewGRF or is a * 'system' NewGRF or it's already known, so forget about it. */ - ClearGRFConfig(&c); + delete c; } return added; -- cgit v1.2.3-54-g00ecf