From f2e8d4220d2e2b0e460d6280c165aff678a6a651 Mon Sep 17 00:00:00 2001 From: tron Date: Sun, 14 Jan 2007 08:37:16 +0000 Subject: (svn r8114) Allocate struct GRFText in the C++ way --- src/newgrf_text.cpp | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/newgrf_text.cpp b/src/newgrf_text.cpp index 021defb9c..4a2a10f5b 100644 --- a/src/newgrf_text.cpp +++ b/src/newgrf_text.cpp @@ -131,11 +131,29 @@ const iso_grf iso_codes[] = { * Each of those elements represent the string, * but according to a different lang. */ -typedef struct GRFText { - struct GRFText *next; - byte langid; - char text[VARARRAY_SIZE]; -} GRFText; +struct GRFText { + public: + static GRFText* New(byte langid, const char* text) + { + return new(strlen(text) + 1) GRFText(langid, text); + } + + private: + GRFText(byte langid_, const char* text_) : next(NULL), langid(langid_) + { + strcpy(text, text_); + } + + void* operator new(size_t size, size_t extra) + { + return ::operator new(size + extra); + } + + public: + GRFText *next; + byte langid; + char text[VARARRAY_SIZE]; +}; /** @@ -265,7 +283,6 @@ char *TranslateTTDPatchCodes(const char *str) StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string) { char *translatedtext; - GRFText *newtext; uint id; /* When working with the old language scheme (grf_version is less than 7) and @@ -297,10 +314,7 @@ StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool ne translatedtext = TranslateTTDPatchCodes(text_to_add); - newtext = (GRFText*)malloc(sizeof(*newtext) + strlen(translatedtext) + 1); - newtext->next = NULL; - newtext->langid = langid_to_add; - strcpy(newtext->text, translatedtext); + GRFText *newtext = GRFText::New(langid_to_add, translatedtext); free(translatedtext); @@ -321,7 +335,7 @@ StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool ne if (text->langid != langid_to_add) continue; newtext->next = text->next; *ptext = newtext; - free(text); + delete text; replaced = true; break; } @@ -426,7 +440,7 @@ void CleanUpStrings(void) GRFText *grftext = _grf_text[id].textholder; while (grftext != NULL) { GRFText *grftext2 = grftext->next; - free(grftext); + delete grftext; grftext = grftext2; } _grf_text[id].grfid = 0; -- cgit v1.2.3-70-g09d2