From 800987ab95c4faa5532d735a85516fd09f5de724 Mon Sep 17 00:00:00 2001 From: yinsimei Date: Tue, 11 Jul 2017 14:57:31 +0200 Subject: SLUDGE: replace char *by Common::String --- engines/sludge/bg_effects.cpp | 16 ++-- engines/sludge/builtin.cpp | 195 ++++++++++++++---------------------------- engines/sludge/builtin.h | 2 +- engines/sludge/fileset.cpp | 7 +- engines/sludge/fileset.h | 2 +- engines/sludge/fonttext.cpp | 17 ++-- engines/sludge/fonttext.h | 14 +-- engines/sludge/graphics.cpp | 1 - engines/sludge/language.cpp | 104 ++++++++-------------- engines/sludge/language.h | 4 +- engines/sludge/loadsave.cpp | 23 +++-- engines/sludge/loadsave.h | 4 +- engines/sludge/main_loop.cpp | 9 +- engines/sludge/module.mk | 1 - engines/sludge/moreio.cpp | 152 ++++++++++++++------------------ engines/sludge/moreio.h | 6 +- engines/sludge/newfatal.cpp | 5 +- engines/sludge/newfatal.h | 2 +- engines/sludge/objtypes.cpp | 7 +- engines/sludge/objtypes.h | 2 +- engines/sludge/people.cpp | 2 +- engines/sludge/savedata.cpp | 45 ++++------ engines/sludge/savedata.h | 4 +- engines/sludge/sludger.cpp | 135 ++++++++++++++--------------- engines/sludge/sludger.h | 4 +- engines/sludge/statusba.cpp | 13 ++- engines/sludge/statusba.h | 8 +- engines/sludge/stringy.cpp | 48 ----------- engines/sludge/stringy.h | 32 ------- engines/sludge/talk.cpp | 32 +++---- engines/sludge/talk.h | 4 +- engines/sludge/thumbnail.cpp | 2 +- engines/sludge/thumbnail.h | 2 +- engines/sludge/variable.cpp | 98 +++++++++------------ engines/sludge/variable.h | 11 +-- 35 files changed, 383 insertions(+), 630 deletions(-) delete mode 100644 engines/sludge/stringy.cpp delete mode 100644 engines/sludge/stringy.h diff --git a/engines/sludge/bg_effects.cpp b/engines/sludge/bg_effects.cpp index 40f60e3b1d..0ec538dc49 100644 --- a/engines/sludge/bg_effects.cpp +++ b/engines/sludge/bg_effects.cpp @@ -158,7 +158,7 @@ void blur_loadSettings(Common::SeekableReadStream *stream) { bool blur_createSettings(int numParams, variableStack *&stack) { bool createNullThing = true; - const char *error = NULL; + Common::String error = ""; if (numParams >= 3) { // PARAMETERS: base, divide, stack (, stack (, stack...)) @@ -187,18 +187,18 @@ bool blur_createSettings(int numParams, variableStack *&stack) { } } - if (width == 0 && !error) { + if (width == 0 && error.empty()) { error = "Empty arrays found in setBackgroundEffect parameters"; } - if (!error) { + if (error.empty()) { s_matrixEffectWidth = width; s_matrixEffectHeight = height; if (blur_allocateMemoryForEffect()) { for (int y = height - 1; y >= 0; y--) { variableStack *eachNumber = stack->thisVar.varData.theStack->first; - if (!error) { + if (error.empty()) { for (int x = 0; x < width; x++) { int arraySlot = x + (y * width); // s_matrixEffectData[arraySlot] = (rand() % 4); @@ -211,13 +211,13 @@ bool blur_createSettings(int numParams, variableStack *&stack) { trimStack(stack); } } - if (!error && !getValueType(s_matrixEffectDivide, SVT_INT, stack->thisVar)) + if (error.empty() && !getValueType(s_matrixEffectDivide, SVT_INT, stack->thisVar)) error = ""; trimStack(stack); - if (!error && !getValueType(s_matrixEffectBase, SVT_INT, stack->thisVar)) + if (error.empty() && !getValueType(s_matrixEffectBase, SVT_INT, stack->thisVar)) error = ""; trimStack(stack); - if (!error) { + if (error.empty()) { if (s_matrixEffectDivide) { createNullThing = false; } else { @@ -243,7 +243,7 @@ bool blur_createSettings(int numParams, variableStack *&stack) { s_matrixEffectData = NULL; } - if (error && error[0]) { + if (!error.empty()) { fatal(error); } diff --git a/engines/sludge/builtin.cpp b/engines/sludge/builtin.cpp index 225c2f1a05..6d7ee428d8 100644 --- a/engines/sludge/builtin.cpp +++ b/engines/sludge/builtin.cpp @@ -25,7 +25,6 @@ #include "sludge/allfiles.h" #include "sludge/sludger.h" #include "sludge/builtin.h" -#include "sludge/stringy.h" #include "sludge/newfatal.h" #include "sludge/cursors.h" #include "sludge/statusba.h" @@ -55,14 +54,14 @@ namespace Sludge { -extern char *gamePath; +extern Common::String gamePath; int speechMode = 0; int cameraX, cameraY; float cameraZoom = 1.0; spritePalette pastePalette; -char *launchMe = NULL; +Common::String launchMe = NULL; variable *launchResult = NULL; extern int lastFramesPerSecond, thumbWidth, thumbHeight; @@ -78,10 +77,10 @@ extern uint sceneWidth, sceneHeight; extern int numBIFNames, numUserFunc; extern char builtInFunctionNames[][25]; -extern char **allUserFunc; -extern char **allBIFNames; +extern Common::String *allUserFunc; +extern Common::String *allBIFNames; extern inputType input; -extern char *loadNow; +extern Common::String loadNow; #if 0 extern GLuint backdropTextureName; @@ -121,14 +120,12 @@ int paramNum[] = { -1, 0, 1, 1, -1, -1, 1, 3, 4, 1, 0, 0, 8, -1, // SAY->MOVE 1, 0, 0 // playMovie, stopMovie, pauseMovie }; -bool failSecurityCheck(char *fn) { - if (fn == NULL) +bool failSecurityCheck(const Common::String &fn) { + if (fn.empty()) return true; - int a = 0; - - while (fn[a]) { - switch (fn[a]) { + for (int i = 0; i < fn.size(); ++i) { + switch (fn[i]) { case ':': case '\\': case '/': @@ -141,7 +138,6 @@ bool failSecurityCheck(char *fn) { fatal("Filenames may not contain the following characters: \n\n\\ / : \" < > | ? *\n\nConsequently, the following filename is not allowed:", fn); return true; } - a++; } return false; } @@ -158,7 +154,7 @@ struct builtInFunctionData { static builtReturn sayCore(int numParams, loadedFunction *fun, bool sayIt) { int fileNum = -1; - char *newText; + Common::String newText; int objT, p; killSpeechTimers(); @@ -171,8 +167,6 @@ static builtReturn sayCore(int numParams, loadedFunction *fun, bool sayIt) { case 2: newText = getTextFromAnyVar(fun->stack->thisVar); - if (!newText) - return BR_ERROR; trimStack(fun->stack); if (!getValueType(objT, SVT_OBJTYPE, fun->stack->thisVar)) return BR_ERROR; @@ -181,8 +175,6 @@ static builtReturn sayCore(int numParams, loadedFunction *fun, bool sayIt) { fun->timeLeft = p; //debugOut ("BUILTIN: sayCore: %s (%i)\n", newText, p); fun->isSpeech = true; - delete[] newText; - newText = NULL; return BR_KEEP_AND_PAUSE; } @@ -264,9 +256,7 @@ builtIn(getStatusText) { builtIn(getMatchingFiles) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); - if (!newText) - return BR_ERROR; + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); unlinkVar(fun->reg); @@ -280,8 +270,6 @@ builtIn(getMatchingFiles) { fun->reg.varData.theStack->timesUsed = 1; if (!getSavedGamesStack(fun->reg.varData.theStack, newText)) return BR_ERROR; - delete newText; - newText = NULL; return BR_CONTINUE; } @@ -295,13 +283,12 @@ builtIn(saveGame) { loadNow = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); - char *aaaaa = encodeFilename(loadNow); - delete[] loadNow; + Common::String aaaaa = encodeFilename(loadNow); + loadNow.clear(); if (failSecurityCheck(aaaaa)) return BR_ERROR; // Won't fail if encoded, how cool is that? OK, not very. - loadNow = joinStrings(":", aaaaa); - delete[] aaaaa; + loadNow = ":" + aaaaa; setVariable(fun->reg, SVT_INT, 0); saverFunc = fun; @@ -312,8 +299,8 @@ builtIn(fileExists) { UNUSEDALL loadNow = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); - char *aaaaa = encodeFilename(loadNow); - delete loadNow; + Common::String aaaaa = encodeFilename(loadNow); + loadNow.clear(); if (failSecurityCheck(aaaaa)) return BR_ERROR; #if 0 @@ -345,28 +332,24 @@ builtIn(fileExists) { builtIn(loadGame) { UNUSEDALL - char *aaaaa = getTextFromAnyVar(fun->stack->thisVar); + Common::String aaaaa = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); + loadNow.clear(); loadNow = encodeFilename(aaaaa); - delete []aaaaa; if (frozenStuff) { fatal("Can't load a saved game while the engine is frozen"); } if (failSecurityCheck(loadNow)) return BR_ERROR; - Common::File fd; - if (fd.open(loadNow)) { - fd.close(); Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(loadNow); if (fp) { delete fp; return BR_KEEP_AND_PAUSE; } - delete []loadNow; - loadNow = NULL; - debug("not find sav file"); + + loadNow.clear(); return BR_CONTINUE; } @@ -547,8 +530,7 @@ builtIn(pickOne) { builtIn(substring) { UNUSEDALL - char *wholeString; - char *newString; + Common::String wholeString; int start, length; //debugOut ("BUILTIN: substring\n"); @@ -562,12 +544,13 @@ builtIn(substring) { wholeString = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); - UTF8Converter convert(wholeString); + UTF8Converter convert; + convert.setUTF8String(wholeString); Common::U32String str32 = convert.getU32String(); - if (str32.size() < start + length) { + if ((int)str32.size() < start + length) { length = str32.size() - start; - if (str32.size() < start) { + if ((int)str32.size() < start) { start = 0; } } @@ -578,25 +561,17 @@ builtIn(substring) { int startoffset = convert.getOriginOffset(start); int endoffset = convert.getOriginOffset(start + length); - newString = new char[endoffset - startoffset + 1]; - if (!checkNew(newString)) { - return BR_ERROR; - } - - memcpy(newString, wholeString + startoffset, endoffset - startoffset); - newString[endoffset - startoffset] = 0; + Common::String newString(wholeString.begin() + startoffset, wholeString.begin() + endoffset); makeTextVar(fun->reg, newString); - delete []newString; return BR_CONTINUE; } builtIn(stringLength) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); setVariable(fun->reg, SVT_INT, stringLength(newText)); - delete[] newText; return BR_CONTINUE; } @@ -883,9 +858,7 @@ builtIn(setFont) { return BR_ERROR; // newDebug (" Height:", newHeight); trimStack(fun->stack); - char *newText = getTextFromAnyVar(fun->stack->thisVar); - if (!newText) - return BR_ERROR; + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); // newDebug (" Character supported:", newText); trimStack(fun->stack); if (!getValueType(fileNumber, SVT_FILE, fun->stack->thisVar)) @@ -895,27 +868,22 @@ builtIn(setFont) { if (!loadFont(fileNumber, newText, newHeight)) return BR_ERROR; // newDebug (" Done!"); - delete[] newText; - return BR_CONTINUE; } builtIn(inFont) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); - if (!newText) - return BR_ERROR; + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); // Return value - setVariable(fun->reg, SVT_INT, isInFont(newText)); return BR_CONTINUE; } builtIn(pasteString) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); int y, x; if (!getValueType(y, SVT_INT, fun->stack->thisVar)) @@ -928,7 +896,6 @@ builtIn(pasteString) { x = (winWidth - stringWidth(newText)) >> 1; fixFont(pastePalette); pasteStringToBackdrop(newText, x, y, pastePalette); - delete[] newText; return BR_CONTINUE; } @@ -986,26 +953,24 @@ builtIn(costume) { builtIn(launch) { UNUSEDALL - char *newTextA = getTextFromAnyVar(fun->stack->thisVar); - if (!newTextA) - return BR_ERROR; + Common::String newTextA = getTextFromAnyVar(fun->stack->thisVar); - char *newText = encodeFilename(newTextA); + Common::String newText = encodeFilename(newTextA); trimStack(fun->stack); if (newTextA[0] == 'h' && newTextA[1] == 't' && newTextA[2] == 't' && newTextA[3] == 'p' && (newTextA[4] == ':' || (newTextA[4] == 's' && newTextA[5] == ':'))) { // IT'S A WEBSITE! - launchMe = copyString(newTextA); + launchMe.clear(); + launchMe = newTextA; } else { - char *gameDir; - gameDir = joinStrings(gamePath, "/"); - launchMe = joinStrings(gameDir, newText); - delete newText; - if (!launchMe) + Common::String gameDir = gamePath; + gameDir += "/"; + launchMe.clear(); + launchMe = gameDir + newText; + if (launchMe.empty()) return BR_ERROR; } - delete newTextA; setGraphicsWindow(false); setVariable(fun->reg, SVT_INT, 1); launchResult = &fun->reg; @@ -1421,16 +1386,14 @@ builtIn(getOverObject) { builtIn(rename) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); int objT; - if (!newText) - return BR_ERROR; trimStack(fun->stack); if (!getValueType(objT, SVT_OBJTYPE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); objectType *o = findObjectType(objT); - delete o->screenName; + o->screenName.clear(); o->screenName = newText; return BR_CONTINUE; } @@ -1865,12 +1828,9 @@ builtIn(addStatus) { builtIn(statusText) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); - if (!newText) - return BR_ERROR; + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); setStatusBar(newText); - delete[] newText; return BR_CONTINUE; } @@ -2024,14 +1984,11 @@ builtIn(cancelSub) { builtIn(stringWidth) { UNUSEDALL - char *theText = getTextFromAnyVar(fun->stack->thisVar); - if (!theText) - return BR_ERROR; + Common::String theText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); // Return value setVariable(fun->reg, SVT_INT, stringWidth(theText)); - delete theText; return BR_CONTINUE; } @@ -2207,39 +2164,36 @@ builtIn(fetchEvent) { builtIn(deleteFile) { UNUSEDALL - char *namNormal = getTextFromAnyVar(fun->stack->thisVar); + Common::String namNormal = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); - char *nam = encodeFilename(namNormal); - delete namNormal; + Common::String nam = encodeFilename(namNormal); + namNormal.clear(); if (failSecurityCheck(nam)) return BR_ERROR; - setVariable(fun->reg, SVT_INT, remove(nam)); - delete nam; + setVariable(fun->reg, SVT_INT, remove(nam.c_str())); return BR_CONTINUE; } builtIn(renameFile) { UNUSEDALL - char *temp; + Common::String temp; + temp.clear(); temp = getTextFromAnyVar(fun->stack->thisVar); - char *newnam = encodeFilename(temp); + Common::String newnam = encodeFilename(temp); trimStack(fun->stack); if (failSecurityCheck(newnam)) return BR_ERROR; - delete temp; + temp.clear(); temp = getTextFromAnyVar(fun->stack->thisVar); - char *nam = encodeFilename(temp); + Common::String nam = encodeFilename(temp); trimStack(fun->stack); if (failSecurityCheck(nam)) return BR_ERROR; - delete temp; - setVariable(fun->reg, SVT_INT, rename(nam, newnam)); - delete nam; - delete newnam; + setVariable(fun->reg, SVT_INT, rename(nam.c_str(), newnam.c_str())); return BR_CONTINUE; } @@ -2257,7 +2211,7 @@ builtIn(cacheSound) { builtIn(burnString) { UNUSEDALL - char *newText = getTextFromAnyVar(fun->stack->thisVar); + Common::String newText = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); int y, x; if (!getValueType(y, SVT_INT, fun->stack->thisVar)) @@ -2270,7 +2224,6 @@ builtIn(burnString) { x = (winWidth - stringWidth(newText)) >> 1; fixFont(pastePalette); burnStringToBackdrop(newText, x, y, pastePalette); - delete[] newText; return BR_CONTINUE; } @@ -2352,12 +2305,9 @@ builtIn(getSoundCache) { builtIn(saveCustomData) { UNUSEDALL // saveCustomData (thisStack, fileName); - char *fileNameB = getTextFromAnyVar(fun->stack->thisVar); - if (!checkNew(fileNameB)) - return BR_ERROR; + Common::String fileNameB = getTextFromAnyVar(fun->stack->thisVar); - char *fileName = encodeFilename(fileNameB); - delete fileNameB; + Common::String fileName = encodeFilename(fileNameB); if (failSecurityCheck(fileName)) return BR_ERROR; @@ -2370,19 +2320,15 @@ builtIn(saveCustomData) { if (!stackToFile(fileName, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - delete fileName; return BR_CONTINUE; } builtIn(loadCustomData) { UNUSEDALL - char *newTextA = getTextFromAnyVar(fun->stack->thisVar); - if (!checkNew(newTextA)) - return BR_ERROR; + Common::String newTextA = getTextFromAnyVar(fun->stack->thisVar); - char *newText = encodeFilename(newTextA); - delete newTextA; + Common::String newText = encodeFilename(newTextA); if (failSecurityCheck(newText)) return BR_ERROR; @@ -2398,7 +2344,6 @@ builtIn(loadCustomData) { fun->reg.varData.theStack->timesUsed = 1; if (!fileToStack(newText, fun->reg.varData.theStack)) return BR_ERROR; - delete newText; return BR_CONTINUE; } @@ -2553,17 +2498,10 @@ builtIn(showThumbnail) { trimStack(fun->stack); // Encode the name!Encode the name! - char *aaaaa = getTextFromAnyVar(fun->stack->thisVar); - // deb ("Got name:", aaaaa;) - trimStack(fun->stack); - // deb ("About to encode", aaaaa); - char *file = encodeFilename(aaaaa); - // deb ("Made new name", file); - // deb ("aaaaa is still ", aaaaa); - delete[] aaaaa; - // deb ("Deleted", "aaaaa"); + Common::String aaaaa = getTextFromAnyVar(fun->stack->thisVar); + trimStack(fun->stack); + Common::String file = encodeFilename(aaaaa); showThumbnail(file, x, y); - delete[] file; return BR_CONTINUE; } @@ -2631,7 +2569,7 @@ builtIn(_rem_registryGetString) { builtIn(quitWithFatalError) { UNUSEDALL - char *mess = getTextFromAnyVar(fun->stack->thisVar); + Common::String mess = getTextFromAnyVar(fun->stack->thisVar); trimStack(fun->stack); fatal(mess); return BR_ERROR; @@ -2693,12 +2631,7 @@ char builtInFunctionNames[][25] = { #define NUM_FUNCS (sizeof (builtInFunctionArray) / sizeof (builtInFunctionArray[0])) builtReturn callBuiltIn(int whichFunc, int numParams, loadedFunction *fun) { - // fprintf (stderr, "Calling function %d: %s\n", whichFunc, builtInFunctionNames[whichFunc]); fflush (stderr); if (numBIFNames) { - - // deb ("IN:", (fun->originalNumber < numUserFunc) ? allUserFunc[fun->originalNumber] : "Unknown user function"); - // deb ("GO:", (whichFunc < numBIFNames) ? allBIFNames[whichFunc] : "Unknown built-in function"); - setFatalInfo((fun->originalNumber < numUserFunc) ? allUserFunc[fun->originalNumber] : "Unknown user function", (whichFunc < numBIFNames) ? allBIFNames[whichFunc] : "Unknown built-in function"); } @@ -2708,8 +2641,8 @@ builtReturn callBuiltIn(int whichFunc, int numParams, loadedFunction *fun) { if (paramNum[whichFunc] != numParams) { char buff[100]; sprintf(buff, "Built in function must have %i parameter%s", paramNum[whichFunc], (paramNum[whichFunc] == 1) ? "" : "s"); - - fatal(copyString(buff)); + Common::String msg = buff; + fatal(msg); return BR_ERROR; } } diff --git a/engines/sludge/builtin.h b/engines/sludge/builtin.h index 0fb15d1be5..304926800d 100644 --- a/engines/sludge/builtin.h +++ b/engines/sludge/builtin.h @@ -34,7 +34,7 @@ enum builtReturn { BR_ALREADY_GONE }; -bool failSecurityCheck(char *fn); +bool failSecurityCheck(const Common::String &fn); builtReturn callBuiltIn(int whichFunc, int numParams, loadedFunction *fun); } // End of namespace Sludge diff --git a/engines/sludge/fileset.cpp b/engines/sludge/fileset.cpp index 35b9d3b06b..a019f82f57 100644 --- a/engines/sludge/fileset.cpp +++ b/engines/sludge/fileset.cpp @@ -23,7 +23,6 @@ #include "common/file.h" #include "common/debug.h" -#include "sludge/stringy.h" #include "sludge/allfiles.h" #include "sludge/moreio.h" #include "sludge/newfatal.h" @@ -95,7 +94,7 @@ uint openFileFromNum(int num) { // Converts a string from Windows CP-1252 to UTF-8. // This is needed for old games. -char *convertString(char *s) { +Common::String convertString(const Common::String &s) { #if 0 static char *buf = NULL; @@ -158,7 +157,7 @@ char *convertString(char *s) { return s; //TODO: false value } -char *getNumberedString(int value) { +Common::String getNumberedString(int value) { if (sliceBusy) { fatal("Can't read from data file", "I'm already reading something"); @@ -169,7 +168,7 @@ char *getNumberedString(int value) { value = bigDataFile->readUint32LE(); bigDataFile->seek(value, 0); - char *s = readString(bigDataFile); + Common::String s = readString(bigDataFile); if (gameVersion < VERSION(2, 2)) { // This is an older game - We need to convert the string to UTF-8 diff --git a/engines/sludge/fileset.h b/engines/sludge/fileset.h index 61ab417570..b18c87c573 100644 --- a/engines/sludge/fileset.h +++ b/engines/sludge/fileset.h @@ -33,7 +33,7 @@ void setFileIndices(Common::File *fp, int, uint); uint openFileFromNum(int num); bool openSubSlice(int num); bool openObjectSlice(int num); -char *getNumberedString(int value); +Common::String getNumberedString(int value); bool startAccess(); void finishAccess(); diff --git a/engines/sludge/fonttext.cpp b/engines/sludge/fonttext.cpp index 721968946b..591e4a3d2e 100644 --- a/engines/sludge/fonttext.cpp +++ b/engines/sludge/fonttext.cpp @@ -21,7 +21,6 @@ */ #include "sludge/allfiles.h" -#include "sludge/stringy.h" #include "sludge/sprites.h" #include "sludge/fonttext.h" #include "sludge/newfatal.h" @@ -42,10 +41,10 @@ uint fontTableSize = 0; extern float cameraZoom; -bool isInFont(char *theText) { +bool isInFont(const Common::String &theText) { if (!fontTableSize) return 0; - if (!theText[0]) + if (theText.empty()) return 0; Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText); @@ -60,12 +59,12 @@ bool isInFont(char *theText) { return fontOrder.getU32String().contains(c); } -int stringLength(char *theText) { +int stringLength(const Common::String &theText) { Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText); return str32.size(); } -int stringWidth(char *theText) { +int stringWidth(const Common::String &theText) { int xOff = 0; if (!fontTableSize) @@ -81,7 +80,7 @@ int stringWidth(char *theText) { return xOff; } -void pasteString(char *theText, int xOff, int y, spritePalette &thePal) { +void pasteString(const Common::String &theText, int xOff, int y, spritePalette &thePal) { if (!fontTableSize) return; @@ -97,7 +96,7 @@ void pasteString(char *theText, int xOff, int y, spritePalette &thePal) { } } -void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) { +void pasteStringToBackdrop(const Common::String &theText, int xOff, int y, spritePalette &thePal) { if (!fontTableSize) return; @@ -112,7 +111,7 @@ void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal } } -void burnStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) { +void burnStringToBackdrop(const Common::String &theText, int xOff, int y, spritePalette &thePal) { if (!fontTableSize) return; @@ -160,7 +159,7 @@ void setFontColour(spritePalette &sP, byte r, byte g, byte b) { sP.originalBlue = b; } -bool loadFont(int filenum, const char *charOrder, int h) { +bool loadFont(int filenum, const Common::String &charOrder, int h) { fontOrder.setUTF8String(charOrder); forgetSpriteBank(theFont); diff --git a/engines/sludge/fonttext.h b/engines/sludge/fonttext.h index cabb52d476..63a38c71c7 100644 --- a/engines/sludge/fonttext.h +++ b/engines/sludge/fonttext.h @@ -26,15 +26,15 @@ namespace Sludge { -bool loadFont(int filenum, const char *charOrder, int); -void pasteString(char *theText, int, int, spritePalette &); +bool loadFont(int filenum, const Common::String &charOrder, int); +void pasteString(const Common::String &theText, int, int, spritePalette &); void fixFont(spritePalette &spal); void setFontColour(spritePalette &sP, byte r, byte g, byte b); -int stringWidth(char *theText); -int stringLength(char *theText); -void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal); -void burnStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal); -bool isInFont(char *theText); +int stringWidth(const Common::String &theText); +int stringLength(const Common::String &theText); +void pasteStringToBackdrop(const Common::String &theText, int xOff, int y, spritePalette &thePal); +void burnStringToBackdrop(const Common::String &theText, int xOff, int y, spritePalette &thePal); +bool isInFont(const Common::String &theText); } // End of namespace Sludge diff --git a/engines/sludge/graphics.cpp b/engines/sludge/graphics.cpp index 7211979d0f..3c7cdbf38c 100644 --- a/engines/sludge/graphics.cpp +++ b/engines/sludge/graphics.cpp @@ -28,7 +28,6 @@ #include "sludge/zbuffer.h" #include "sludge/backdrop.h" #include "sludge/movie.h" -#include "sludge/stringy.h" #include "sludge/CommonCode/specialsettings.h" namespace Sludge { diff --git a/engines/sludge/language.cpp b/engines/sludge/language.cpp index b7a45ed9dc..2f37eb35c4 100644 --- a/engines/sludge/language.cpp +++ b/engines/sludge/language.cpp @@ -21,7 +21,6 @@ */ #include "sludge/allfiles.h" -#include "sludge/stringy.h" #include "sludge/newfatal.h" #include "sludge/moreio.h" #include "sludge/language.h" @@ -33,60 +32,39 @@ namespace Sludge { int *languageTable; -char **languageName; +Common::String *languageName; settingsStruct gameSettings; -uint stringToInt(char *s) { - int i = 0; - bool negative = false; - for (;;) { - if (*s >= '0' && *s <= '9') { - i *= 10; - i += *s - '0'; - s++; - } else if (*s == '-') { - negative = !negative; - s++; - } else { - if (negative) - return -i; - return i; - } - } -} - -char *getPrefsFilename(char *filename) { +Common::String getPrefsFilename(Common::String filename) { // Yes, this trashes the original string, but // we also free it at the end (warning!)... - int n, i; - - n = strlen(filename); + int n = filename.size(); if (n > 4 && filename[n - 4] == '.') { - filename[n - 4] = 0; + filename.setChar(0, n - 4); } - char *f = filename; - for (i = 0; i < n; i++) { + // get file name from dir + int pos = 0; + for (int i = 0; i < n; i++) { if (filename[i] == '/') - f = filename + i + 1; + pos = i + 1; } - char *joined = joinStrings(f, ".ini"); + Common::String f = filename.c_str() + pos; + Common::String joined = f + ".ini"; - delete[] filename; - filename = NULL; return joined; } -void readIniFile(const char *filename) { +void readIniFile(const Common::String &filename) { - char *langName = getPrefsFilename(copyString(filename)); + Common::String langName = getPrefsFilename(filename); Common::File fd; if (!fd.open(langName)) { - debug(kSludgeDebugDataLoad, "Fail to open language file : %s", langName); + debug(kSludgeDebugDataLoad, "Fail to open language file : %s", langName.c_str()); return; } @@ -98,12 +76,8 @@ void readIniFile(const char *filename) { gameSettings.noStartWindow = false; gameSettings.debugMode = false; - delete langName; - langName = NULL; - - char lineSoFar[257] = ""; - char secondSoFar[257] = ""; - byte here = 0; + Common::String lineSoFar = ""; + Common::String secondSoFar = ""; char readChar = ' '; bool keepGoing = true; bool doingSecond = false; @@ -118,40 +92,36 @@ void readIniFile(const char *filename) { case '\n': case '\r': if (doingSecond) { - if (strcmp(lineSoFar, "LANGUAGE") == 0) { - gameSettings.languageID = stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "WINDOW") == 0) { - gameSettings.userFullScreen = !stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "REFRESH") == 0) { - gameSettings.refreshRate = stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "ANTIALIAS") == 0) { - gameSettings.antiAlias = stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "FIXEDPIXELS") == 0) { - gameSettings.fixedPixels = stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "NOSTARTWINDOW") == 0) { - gameSettings.noStartWindow = stringToInt(secondSoFar); - } else if (strcmp(lineSoFar, "DEBUGMODE") == 0) { - gameSettings.debugMode = stringToInt(secondSoFar); + if (lineSoFar == "LANGUAGE") { + gameSettings.languageID = (uint)secondSoFar.asUint64(); + } else if (lineSoFar == "WINDOW") { + gameSettings.userFullScreen = !secondSoFar.asUint64(); + } else if (lineSoFar == "REFRESH") { + gameSettings.refreshRate = (uint)secondSoFar.asUint64(); + } else if (lineSoFar == "ANTIALIAS") { + gameSettings.antiAlias = (int)secondSoFar.asUint64(); + } else if (lineSoFar == "FIXEDPIXELS") { + gameSettings.fixedPixels = secondSoFar.asUint64(); + } else if (lineSoFar == "NOSTARTWINDOW") { + gameSettings.noStartWindow = secondSoFar.asUint64(); + } else if (lineSoFar == "DEBUGMODE") { + gameSettings.debugMode = secondSoFar.asUint64(); } } - here = 0; doingSecond = false; - lineSoFar[0] = 0; - secondSoFar[0] = 0; + lineSoFar.clear(); + secondSoFar.clear(); break; case '=': doingSecond = true; - here = 0; break; default: if (doingSecond) { - secondSoFar[here++] = readChar; - secondSoFar[here] = 0; + secondSoFar += readChar; } else { - lineSoFar[here++] = readChar; - lineSoFar[here] = 0; + lineSoFar += readChar; } break; } @@ -160,7 +130,7 @@ void readIniFile(const char *filename) { fd.close(); } -void saveIniFile(const char *filename) { +void saveIniFile(const Common::String &filename) { #if 0 char *langName = getPrefsFilename(copyString(filename)); FILE *fp = fopen(langName, "wt"); @@ -182,18 +152,18 @@ void makeLanguageTable(Common::File *table) { if (!checkNew(languageTable)) return; - languageName = new char *[gameSettings.numLanguages + 1]; + languageName = new Common::String[gameSettings.numLanguages + 1]; if (!checkNew(languageName)) return; for (uint i = 0; i <= gameSettings.numLanguages; i++) { languageTable[i] = i ? table->readUint16BE() : 0; debug(kSludgeDebugDataLoad, "languageTable %i: %i", i, languageTable[i]); - languageName[i] = 0; + languageName[i].clear(); if (gameVersion >= VERSION(2, 0)) { if (gameSettings.numLanguages) { languageName[i] = readString(table); - debug(kSludgeDebugDataLoad, "languageName %i: %s\n", i, languageName[i]); + debug(kSludgeDebugDataLoad, "languageName %i: %s\n", i, languageName[i].c_str()); } } } diff --git a/engines/sludge/language.h b/engines/sludge/language.h index 57bc97e2d4..5c2774fae4 100644 --- a/engines/sludge/language.h +++ b/engines/sludge/language.h @@ -41,8 +41,8 @@ struct settingsStruct { extern settingsStruct gameSettings; -void readIniFile(const char *filename); -void saveIniFile(const char *filename); +void readIniFile(const Common::String &filename); +void saveIniFile(const Common::String &filename); int getLanguageForFileB(); void makeLanguageTable(Common::File *table); diff --git a/engines/sludge/loadsave.cpp b/engines/sludge/loadsave.cpp index 893d2a8def..ccd22f06b2 100644 --- a/engines/sludge/loadsave.cpp +++ b/engines/sludge/loadsave.cpp @@ -281,7 +281,7 @@ bool loadVariable(variable *to, Common::SeekableReadStream *stream) { return true; case SVT_STRING: - to->varData.theString = readString(stream); + to->varData.theString = createCString(readString(stream)); return true; case SVT_STACK: @@ -384,7 +384,7 @@ loadedFunction *loadFunction(Common::SeekableReadStream *stream) { // Save everything //---------------------------------------------------------------------- -bool saveGame(char *fname) { +bool saveGame(const Common::String &fname) { Common::OutSaveFile *fp = g_system->getSavefileManager()->openForSaving(fname); if (fp == NULL) @@ -511,7 +511,7 @@ bool saveGame(char *fname) { int ssgVersion; -bool loadGame(char *fname) { +bool loadGame(const Common::String &fname) { Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(fname); FILETIME savedGameTime; @@ -572,28 +572,25 @@ bool loadGame(char *fname) { bool fontLoaded = fp->readByte(); int fontNum; - char *charOrder; + Common::String charOrder = ""; if (fontLoaded) { fontNum = fp->readUint16BE(); fontHeight = fp->readUint16BE(); if (ssgVersion < VERSION(2, 2)) { - int x; - charOrder = new char[257]; - if (!checkNew(charOrder)) - return false; - + char *tmp = new char[257]; for (int a = 0; a < 256; a++) { - x = fp->readByte(); - charOrder[x] = a; + int x = fp->readByte(); + tmp[x] = a; } - charOrder[256] = 0; + tmp[256] = 0; + charOrder = tmp; + delete []tmp; } else { charOrder = readString(fp); } } loadFont(fontNum, charOrder, fontHeight); - delete []charOrder; fontSpace = fp->readSint16LE(); diff --git a/engines/sludge/loadsave.h b/engines/sludge/loadsave.h index 87024f246d..b02fd2dd11 100644 --- a/engines/sludge/loadsave.h +++ b/engines/sludge/loadsave.h @@ -24,8 +24,8 @@ namespace Sludge { -bool saveGame(char *fname); -bool loadGame(char *fname); +bool saveGame(const Common::String &fname); +bool loadGame(const Common::String &fname); bool saveVariable(variable *from, Common::WriteStream *stream); bool loadVariable(variable *to, Common::SeekableReadStream *stream); diff --git a/engines/sludge/main_loop.cpp b/engines/sludge/main_loop.cpp index 4fbdad5366..04d38f5831 100644 --- a/engines/sludge/main_loop.cpp +++ b/engines/sludge/main_loop.cpp @@ -30,7 +30,6 @@ #include "sludge/allfiles.h" #include "sludge/language.h" -#include "sludge/stringy.h" #include "sludge/sludger.h" #include "sludge/backdrop.h" #include "sludge/language.h" @@ -71,9 +70,9 @@ Graphics::Surface renderSurface; int dialogValue = 0; -char *gameName = NULL; -char *gamePath = NULL; -char *bundleFolder; +Common::String gameName = ""; +Common::String gamePath = ""; +Common::String bundleFolder = ""; void setGameFilePath(char *f) { char currentDir[1000]; @@ -329,8 +328,6 @@ int main_loop(const char *filename) Wait_Frame(); } - delete[] gamePath; - killSoundStuff(); #if 0 diff --git a/engines/sludge/module.mk b/engines/sludge/module.mk index 03bcc72e8b..f96a5b68da 100644 --- a/engines/sludge/module.mk +++ b/engines/sludge/module.mk @@ -32,7 +32,6 @@ MODULE_OBJS := \ sprbanks.o \ sprites.o \ statusba.o \ - stringy.o \ talk.o \ thumbnail.o \ timing.o \ diff --git a/engines/sludge/moreio.cpp b/engines/sludge/moreio.cpp index 6635c3d3b7..bc1c57eaf2 100644 --- a/engines/sludge/moreio.cpp +++ b/engines/sludge/moreio.cpp @@ -26,7 +26,6 @@ #include "sludge/allfiles.h" #include "sludge/moreio.h" #include "sludge/newfatal.h" -#include "sludge/stringy.h" #include "sludge/sludge.h" namespace Sludge { @@ -41,157 +40,132 @@ void writeString(Common::String s, Common::WriteStream *stream) { } } -char *readString(Common::SeekableReadStream *stream) { - int a, len = stream->readUint16BE(); - char *s = new char[len + 1]; - if (!checkNew(s)) { - return NULL; +Common::String readString(Common::SeekableReadStream *stream) { + int len = stream->readUint16BE(); + Common::String res = ""; + for (int a = 0; a < len; a++) { + res += (char)(stream->readByte() - 1); } - for (a = 0; a < len; a++) { - s[a] = (char)(stream->readByte() - 1); - } - s[len] = 0; - debug(kSludgeDebugDataLoad, "Read string of length %i: %s", len, s); - return s; + debug(kSludgeDebugDataLoad, "Read string of length %i: %s", len, res.c_str()); + return res; } -char *encodeFilename(char *nameIn) { - if (!nameIn) - return NULL; +Common::String encodeFilename(const Common::String &nameIn) { + Common::String newName = ""; + if (nameIn.empty()) + return newName; if (allowAnyFilename) { - char *newName = new char[strlen(nameIn) * 2 + 1]; - if (!checkNew(newName)) - return NULL; - - int i = 0; - while (*nameIn) { - switch (*nameIn) { + for (uint i = 0; i < nameIn.size(); ++i) { + switch (nameIn[i]) { case '<': - newName[i++] = '_'; - newName[i++] = 'L'; + newName += '_'; + newName += 'L'; break; case '>': - newName[i++] = '_'; - newName[i++] = 'G'; + newName += '_'; + newName += 'G'; break; case '|': - newName[i++] = '_'; - newName[i++] = 'P'; + newName += '_'; + newName += 'P'; break; case '_': - newName[i++] = '_'; - newName[i++] = 'U'; + newName += '_'; + newName += 'U'; break; case '\"': - newName[i++] = '_'; - newName[i++] = 'S'; + newName += '_'; + newName += 'S'; break; case '\\': - newName[i++] = '_'; - newName[i++] = 'B'; + newName += '_'; + newName += 'B'; break; case '/': - newName[i++] = '_'; - newName[i++] = 'F'; + newName += '_'; + newName += 'F'; break; case ':': - newName[i++] = '_'; - newName[i++] = 'C'; + newName += '_'; + newName += 'C'; break; case '*': - newName[i++] = '_'; - newName[i++] = 'A'; + newName += '_'; + newName += 'A'; break; case '?': - newName[i++] = '_'; - newName[i++] = 'Q'; + newName += '_'; + newName += 'Q'; break; default: - newName[i++] = *nameIn; + newName += nameIn[i]; break; } - newName[i] = 0; - nameIn++; } - return newName; } else { - int a; - for (a = 0; nameIn[a]; a++) { - if (nameIn[a] == '\\') - nameIn[a] = '/'; + newName.clear(); + newName = nameIn; + for (uint i = 0; i < newName.size(); ++i) { + if (newName[i] == '\\') + newName.setChar('/', i); } - - return copyString(nameIn); } + return newName; } -char *decodeFilename(char *nameIn) { +Common::String decodeFilename(const Common::String &nameIn) { + Common::String newName =""; if (allowAnyFilename) { - char *newName = new char[strlen(nameIn) + 1]; - if (!checkNew(newName)) - return NULL; - - int i = 0; - while (*nameIn) { - if (*nameIn == '_') { - nameIn++; - switch (*nameIn) { + for (uint i = 0; i < nameIn.size(); ++i) { + if (nameIn[i] == '_') { + ++i; + switch (nameIn[i]) { case 'L': - newName[i] = '<'; - nameIn++; + newName += '<'; break; case 'G': - newName[i] = '>'; - nameIn++; + newName += '>'; break; case 'P': - newName[i] = '|'; - nameIn++; + newName += '|'; break; case 'U': - newName[i] = '_'; - nameIn++; + newName += '_'; break; case 'S': - newName[i] = '\"'; - nameIn++; + newName += '\"'; break; case 'B': - newName[i] = '\\'; - nameIn++; + newName += '\\'; break; case 'F': - newName[i] = '/'; - nameIn++; + newName += '/'; break; case 'C': - newName[i] = ':'; - nameIn++; + newName += ':'; break; case 'A': - newName[i] = '*'; - nameIn++; + newName += '*'; break; case 'Q': - newName[i] = '?'; - nameIn++; + newName += '?'; break; default: - newName[i] = '_'; + newName += '_'; + --i; + break; } } else { - newName[i] = *nameIn; - nameIn++; + newName += nameIn[i]; } - i++; - } - newName[i] = 0; return newName; } else { - return copyString(nameIn); + newName.clear(); + newName = nameIn; } + return newName; } } // End of namespace Sludge diff --git a/engines/sludge/moreio.h b/engines/sludge/moreio.h index 94c82f2a3a..09235ae8a6 100644 --- a/engines/sludge/moreio.h +++ b/engines/sludge/moreio.h @@ -25,11 +25,11 @@ namespace Sludge { // Read & Write -char *readString(Common::SeekableReadStream *stream); +Common::String readString(Common::SeekableReadStream *stream); void writeString(Common::String s, Common::WriteStream *stream); -char *encodeFilename(char *nameIn); -char *decodeFilename(char *nameIn); +Common::String encodeFilename(const Common::String &nameIn); +Common::String decodeFilename(const Common::String &nameIn); } // End of namespace Sludge diff --git a/engines/sludge/newfatal.cpp b/engines/sludge/newfatal.cpp index 52234002b4..e9cd860d8a 100644 --- a/engines/sludge/newfatal.cpp +++ b/engines/sludge/newfatal.cpp @@ -24,7 +24,6 @@ #include "sludge/allfiles.h" #include "sludge/sound.h" -#include "sludge/stringy.h" #include "sludge/errors.h" #include "sludge/graphics.h" #include "sludge/sludge.h" @@ -38,11 +37,11 @@ static Common::String fatalMessage; static Common::String fatalInfo = "Initialisation error! Something went wrong before we even got started!"; extern int numResourceNames /* = 0*/; -extern char **allResourceNames /*= NULL*/; +extern Common::String *allResourceNames /*= ""*/; int resourceForFatal = -1; -const char *resourceNameFromNum(int i) { +const Common::String &resourceNameFromNum(int i) { if (i == -1) return NULL; if (numResourceNames == 0) diff --git a/engines/sludge/newfatal.h b/engines/sludge/newfatal.h index 47d49e2cc8..c26579a46a 100644 --- a/engines/sludge/newfatal.h +++ b/engines/sludge/newfatal.h @@ -37,7 +37,7 @@ void displayFatal(); void registerWindowForFatal(); void setFatalInfo(const Common::String &userFunc, const Common::String &BIF); void setResourceForFatal(int n); -const char *resourceNameFromNum(int i); +const Common::String &resourceNameFromNum(int i); } // End of namespace Sludge diff --git a/engines/sludge/objtypes.cpp b/engines/sludge/objtypes.cpp index ddb7553cf4..bb9d707ad5 100644 --- a/engines/sludge/objtypes.cpp +++ b/engines/sludge/objtypes.cpp @@ -118,7 +118,7 @@ objectType *loadObjectType(int i) { objectType *loadObjectRef(Common::SeekableReadStream *stream) { objectType *r = loadObjectType(stream->readUint16BE()); - delete r->screenName; + r->screenName.clear(); r->screenName = readString(stream); return r; } @@ -161,13 +161,8 @@ void removeObjectType(objectType *oT) { while (*huntRegion) { if ((*huntRegion) == oT) { -// FILE * debuggy2 = fopen ("debug.txt", "at"); -// fprintf (debuggy2, "DELETING OBJECT TYPE: %p %s\n", oT, oT -> screenName); -// fclose (debuggy2); - *huntRegion = oT->next; delete []oT->allCombis; - delete []oT->screenName; delete oT; return; } else { diff --git a/engines/sludge/objtypes.h b/engines/sludge/objtypes.h index f11160614e..af9abe3fd5 100644 --- a/engines/sludge/objtypes.h +++ b/engines/sludge/objtypes.h @@ -29,7 +29,7 @@ struct combination { }; struct objectType { - char *screenName; + Common::String screenName; int objectNum; objectType *next; byte r, g, b; diff --git a/engines/sludge/people.cpp b/engines/sludge/people.cpp index 94663eedf3..9453b21faf 100644 --- a/engines/sludge/people.cpp +++ b/engines/sludge/people.cpp @@ -480,7 +480,7 @@ void drawPeople() { bool r = false; r = scaleSprite(myAnim->theSprites->bank.sprites[fNum], myAnim->theSprites->bank.myPalette, thisPerson, m); if (r) { - if (thisPerson->thisType->screenName[0]) { + if (!thisPerson->thisType->screenName.empty()) { if (personRegion.thisType != thisPerson->thisType) lastRegion = NULL; personRegion.thisType = thisPerson->thisType; diff --git a/engines/sludge/savedata.cpp b/engines/sludge/savedata.cpp index 1059cc1cff..c4e1915750 100644 --- a/engines/sludge/savedata.cpp +++ b/engines/sludge/savedata.cpp @@ -35,7 +35,7 @@ uint16 saveEncoding = false; char encode1 = 0; char encode2 = 0; -extern char *gamePath; +extern Common::String gamePath; /* void loadSaveDebug (char * com) { @@ -56,28 +56,25 @@ extern char *gamePath; fclose (ffpp); } */ - -void writeStringEncoded(const char *s, Common::WriteStream *stream) { - int a, len = strlen(s); +void writeStringEncoded(const Common::String &s, Common::WriteStream *stream) { + int len = s.size(); stream->writeUint16BE(len); - for (a = 0; a < len; a++) { + for (int a = 0; a < len; a++) { stream->writeByte(s[a] ^ encode1); encode1 += encode2; } } -char *readStringEncoded(Common::File *fp) { - int a, len = fp->readUint16BE(); - char *s = new char[len + 1]; - if (!checkNew(s)) - return NULL; - for (a = 0; a < len; a++) { - s[a] = (char)(fp->readByte() ^ encode1); +Common::String readStringEncoded(Common::File *fp) { + int len = fp->readUint16BE(); + Common::String res = ""; + + for (int a = 0; a < len; a++) { + res += (char)(fp->readByte() ^ encode1); encode1 += encode2; } - s[len] = 0; - return s; + return res; } char *readTextPlain(Common::File *fp) { @@ -117,11 +114,11 @@ char *readTextPlain(Common::File *fp) { return reply; } -bool fileToStack(char *filename, stackHandler *sH) { +bool fileToStack(const Common::String &filename, stackHandler *sH) { variable stringVar; stringVar.varType = SVT_NULL; - const char *checker = saveEncoding ? "[Custom data (encoded)]\r\n" : "[Custom data (ASCII)]\n"; + Common::String checker = saveEncoding ? "[Custom data (encoded)]\r\n" : "[Custom data (ASCII)]\n"; Common::File fd; @@ -150,23 +147,20 @@ bool fileToStack(char *filename, stackHandler *sH) { encode1 = (byte)saveEncoding & 255; encode2 = (byte)(saveEncoding >> 8); - while (*checker) { - if (fd.readByte() != *checker) { + for (uint i = 0; i < checker.size(); ++i) { + if (fd.readByte() != checker[i]) { fd.close(); return fatal(LOAD_ERROR "This isn't a SLUDGE custom data file:", filename); } - checker++; } if (saveEncoding) { - char *checker = readStringEncoded(&fd); - if (strcmp(checker, "UN�LO�CKED")) { + checker = readStringEncoded(&fd); + if (checker == "UN�LO�CKED") { fd.close(); return fatal( LOAD_ERROR "The current file encoding setting does not match the encoding setting used when this file was created:", filename); } - delete checker; - checker = NULL; } for (;;) { @@ -177,9 +171,8 @@ bool fileToStack(char *filename, stackHandler *sH) { break; switch (i) { case 0: { - char *g = readStringEncoded(&fd); + Common::String g = readStringEncoded(&fd); makeTextVar(stringVar, g); - delete g; } break; @@ -220,7 +213,7 @@ bool fileToStack(char *filename, stackHandler *sH) { return true; } -bool stackToFile(char *filename, const variable &from) { +bool stackToFile(const Common::String &filename, const variable &from) { #if 0 FILE *fp = fopen(filename, saveEncoding ? "wb" : "wt"); if (!fp) return fatal("Can't create file", filename); diff --git a/engines/sludge/savedata.h b/engines/sludge/savedata.h index e6422b8cca..823d2dd0ba 100644 --- a/engines/sludge/savedata.h +++ b/engines/sludge/savedata.h @@ -24,8 +24,8 @@ namespace Sludge { -bool fileToStack(char *filename, stackHandler *sH); -bool stackToFile(char *filename, const variable &from); +bool fileToStack(const Common::String &filename, stackHandler *sH); +bool stackToFile(const Common::String &filename, const variable &from); } // End of namespace Sludge diff --git a/engines/sludge/sludger.cpp b/engines/sludge/sludger.cpp index 5c5e6c66f2..bbec72f3f6 100644 --- a/engines/sludge/sludger.cpp +++ b/engines/sludge/sludger.cpp @@ -33,7 +33,6 @@ #include "sludge/people.h" #include "sludge/talk.h" #include "sludge/newfatal.h" -#include "sludge/stringy.h" #include "sludge/moreio.h" #include "sludge/statusba.h" #include "sludge/builtin.h" @@ -58,18 +57,18 @@ extern personaAnimation *mouseCursorAnim; extern spritePalette pastePalette; extern int dialogValue; extern uint sceneWidth, sceneHeight; -extern char *launchMe; +extern Common::String launchMe; extern variable *launchResult; extern bool reallyWantToQuit; extern Graphics::Surface renderSurface; int numBIFNames = 0; -char **allBIFNames = NULL; +Common::String *allBIFNames; int numUserFunc = 0; -char **allUserFunc = NULL; +Common::String *allUserFunc = NULL; int numResourceNames = 0; -char **allResourceNames = NULL; +Common::String *allResourceNames = NULL; int selectedLanguage = 0; int languageNum = -1; @@ -99,7 +98,7 @@ extern loadedFunction *saverFunc; loadedFunction *allRunningFunctions = NULL; screenRegion *lastRegion = NULL; variableStack *noStack = NULL; -char *loadNow = NULL; +Common::String loadNow; inputType input; variable *globalVars; int numGlobals; @@ -133,7 +132,7 @@ void saveHandlers(Common::WriteStream *stream) { stream->writeUint16BE(currentEvents->spaceFunction); } -Common::File *openAndVerify(const char *filename, char extra1, char extra2, +Common::File *openAndVerify(const Common::String &filename, char extra1, char extra2, const char *er, int &fileVersion) { Common::File *fp = new Common::File(); if (!fp->open(filename)) { @@ -184,12 +183,11 @@ Common::File *openAndVerify(const char *filename, char extra1, char extra2, return fp; } -bool initSludge(const char *filename) { +bool initSludge(const Common::String &filename) { int a = 0; mouseCursorAnim = makeNullAnim(); - Common::File *fp = openAndVerify(filename, 'G', 'E', ERROR_BAD_HEADER, - gameVersion); + Common::File *fp = openAndVerify(filename, 'G', 'E', ERROR_BAD_HEADER, gameVersion); if (!fp) return false; @@ -197,31 +195,34 @@ bool initSludge(const char *filename) { if (c) { numBIFNames = fp->readUint16BE(); debug(kSludgeDebugDataLoad, "numBIFNames %i", numBIFNames); - allBIFNames = new char *[numBIFNames]; + allBIFNames = new Common::String[numBIFNames]; if (!checkNew(allBIFNames)) return false; for (int fn = 0; fn < numBIFNames; fn++) { + allBIFNames[fn].clear(); allBIFNames[fn] = readString(fp); } numUserFunc = fp->readUint16BE(); debug(kSludgeDebugDataLoad, "numUserFunc %i", numUserFunc); - allUserFunc = new char *[numUserFunc]; + allUserFunc = new Common::String[numUserFunc]; if (!checkNew(allUserFunc)) return false; for (int fn = 0; fn < numUserFunc; fn++) { + allUserFunc[fn].clear(); allUserFunc[fn] = readString(fp); } if (gameVersion >= VERSION(1, 3)) { numResourceNames = fp->readUint16BE(); debug(kSludgeDebugDataLoad, "numResourceNames %i", numResourceNames); - allResourceNames = new char *[numResourceNames]; + allResourceNames = new Common::String[numResourceNames]; if (!checkNew(allResourceNames)) return false; for (int fn = 0; fn < numResourceNames; fn++) { + allResourceNames[fn].clear(); allResourceNames[fn] = readString(fp); } } @@ -235,17 +236,15 @@ bool initSludge(const char *filename) { debug(kSludgeDebugDataLoad, "specialSettings : %i", specialSettings); desiredfps = 1000 / fp->readByte(); - delete[] readString(fp); // Unused - was used for registration purposes. + readString(fp); // Unused - was used for registration purposes. uint bytes_read = fp->read(&fileTime, sizeof(FILETIME)); if (bytes_read != sizeof(FILETIME) && fp->err()) { debug("Reading error in initSludge."); } - char *dataFol = - (gameVersion >= VERSION(1, 3)) ? - readString(fp) : joinStrings("", ""); - debug(kSludgeDebugDataLoad, "dataFol : %s", dataFol); + Common::String dataFol = (gameVersion >= VERSION(1, 3)) ? readString(fp) : ""; + debug(kSludgeDebugDataLoad, "dataFol : %s", dataFol.c_str()); gameSettings.numLanguages = (gameVersion >= VERSION(1, 3)) ? (fp->readByte()) : 0; @@ -260,13 +259,11 @@ bool initSludge(const char *filename) { fp->readFloatLE(); } - char *checker = readString(fp); - debug(kSludgeDebugDataLoad, "checker : %s", checker); + Common::String checker = readString(fp); + debug(kSludgeDebugDataLoad, "checker : %s", checker.c_str()); - if (strcmp(checker, "okSoFar")) + if (checker != "okSoFar") return fatal(ERROR_BAD_HEADER, filename); - delete[] checker; - checker = NULL; byte customIconLogo = fp->readByte(); debug(kSludgeDebugDataLoad, "Game icon type: %i", customIconLogo); @@ -504,11 +501,9 @@ bool initSludge(const char *filename) { // Get the original (untranslated) name of the game and convert it to Unicode. // We use this to find saved preferences and saved games. setFileIndices(fp, gameSettings.numLanguages, 0); - char *gameNameOrig = getNumberedString(1); + Common::String gameNameOrig = getNumberedString(1); - char *gameName = encodeFilename(gameNameOrig); - - delete[] gameNameOrig; + Common::String gameName = encodeFilename(gameNameOrig); #if 0 changeToUserDir(); @@ -517,7 +512,6 @@ bool initSludge(const char *filename) { if (chdir(gameName)) return fatal("This game's preference folder is inaccessible!\nI can't access the following directory (maybe there's a file with the same name, or maybe it's read-protected):", gameName); #endif - delete[] gameName; // Get user settings readIniFile(filename); @@ -533,14 +527,13 @@ bool initSludge(const char *filename) { return fatal("Can't find the translation data specified!"); setFileIndices(NULL, gameSettings.numLanguages, languageNum); - if (dataFol[0]) { - char *dataFolder = encodeFilename(dataFol); + if (!dataFol.empty()) { + Common::String dataFolder = encodeFilename(dataFol); #if 0 mkdir(dataFolder, 0000777); if (chdir(dataFolder)) return fatal("This game's data folder is inaccessible!\nI can't access the following directory (maybe there's a file with the same name, or maybe it's read-protected):", dataFolder); #endif - delete []dataFolder; } positionStatus(10, winHeight - 15); @@ -647,10 +640,10 @@ void abortFunction(loadedFunction *fun) { pauseFunction(fun); while (fun->stack) trimStack(fun->stack); - delete fun->compiledLines; + delete []fun->compiledLines; for (a = 0; a < fun->numLocals; a++) unlinkVar(fun->localVars[a]); - delete[] fun->localVars; + delete []fun->localVars; unlinkVar(fun->reg); if (fun->calledBy) abortFunction(fun->calledBy); @@ -1201,16 +1194,15 @@ bool runSludge() { thisFunction = nextFunction; } - if (loadNow) { + if (!loadNow.empty()) { if (loadNow[0] == ':') { - saveGame(loadNow + 1); + saveGame(loadNow.c_str() + 1); setVariable(saverFunc->reg, SVT_INT, 1); } else { if (!loadGame(loadNow)) return false; } - delete loadNow; - loadNow = NULL; + loadNow.clear(); } return true; @@ -1315,14 +1307,14 @@ bool handleInput() { } // lastFramesPerSecond = theTime.wSecond; #endif - if (launchMe) { + if (!launchMe.empty()) { if (l) { // Still paused because of spawned thingy... } else { l = 1; setVariable(*launchResult, SVT_INT, 0/*launch(launchMe) > 31*/); //TODO:false value - launchMe = NULL; + launchMe.clear(); launchResult = NULL; } return true; @@ -1379,19 +1371,19 @@ bool handleInput() { return false; } if (input.keyPressed && currentEvents->spaceFunction) { - char *tempString = NULL; + Common::String tempString = ""; switch (input.keyPressed) { case 127: - tempString = copyString("BACKSPACE"); + tempString = "BACKSPACE"; break; case 9: - tempString = copyString("TAB"); + tempString = "TAB"; break; case 13: - tempString = copyString("ENTER"); + tempString = "ENTER"; break; case 27: - tempString = copyString("ESCAPE"); + tempString = "ESCAPE"; break; /* case 1112: tempString = copyString ("ALT+F1"); break; @@ -1410,90 +1402,89 @@ bool handleInput() { case 2019: tempString = copyString ("PAUSE"); break; */ case 63276: - tempString = copyString("PAGE UP"); + tempString = "PAGE UP"; break; case 63277: - tempString = copyString("PAGE DOWN"); + tempString = "PAGE DOWN"; break; case 63275: - tempString = copyString("END"); + tempString = "END"; break; case 63273: - tempString = copyString("HOME"); + tempString = "HOME"; break; case 63234: - tempString = copyString("LEFT"); + tempString = "LEFT"; break; case 63232: - tempString = copyString("UP"); + tempString = "UP"; break; case 63235: - tempString = copyString("RIGHT"); + tempString = "RIGHT"; break; case 63233: - tempString = copyString("DOWN"); + tempString = "DOWN"; break; /* case 2045: tempString = copyString ("INSERT"); break; case 2046: tempString = copyString ("DELETE"); break; */ case 63236: - tempString = copyString("F1"); + tempString = "F1"; break; case 63237: - tempString = copyString("F2"); + tempString = "F2"; break; case 63238: - tempString = copyString("F3"); + tempString = "F3"; break; case 63239: - tempString = copyString("F4"); + tempString = "F4"; break; case 63240: - tempString = copyString("F5"); + tempString = "F5"; break; case 63241: - tempString = copyString("F6"); + tempString = "F6"; break; case 63242: - tempString = copyString("F7"); + tempString = "F7"; break; case 63243: - tempString = copyString("F8"); + tempString = "F8"; break; case 63244: - tempString = copyString("F9"); + tempString = "F9"; break; case 63245: - tempString = copyString("F10"); + tempString = "F10"; break; case 63246: - tempString = copyString("F11"); + tempString = "F11"; break; case 63247: - tempString = copyString("F12"); + tempString = "F12"; break; default: if (input.keyPressed >= 256) { - //if (captureAllKeys) { - tempString = copyString("ABCDEF"); - sprintf(tempString, "%i", input.keyPressed); + char tmp[7] = "ABCDEF"; + sprintf(tmp, "%i", input.keyPressed); + tempString = tmp; //} } else { - tempString = copyString(" "); - tempString[0] = input.keyPressed; + char tmp[2] = " "; + tmp[0] = input.keyPressed; + tempString = tmp; } } - if (tempString) { + if (!tempString.empty()) { variableStack *tempStack = new variableStack; if (!checkNew(tempStack)) return false; initVarNew(tempStack->thisVar); makeTextVar(tempStack->thisVar, tempString); - delete tempString; - tempString = NULL; tempStack->next = NULL; if (!startNewFunctionNum(currentEvents->spaceFunction, 1, NULL, tempStack)) return false; diff --git a/engines/sludge/sludger.h b/engines/sludge/sludger.h index 19c6f53c57..02dd107218 100644 --- a/engines/sludge/sludger.h +++ b/engines/sludge/sludger.h @@ -76,7 +76,7 @@ struct inputType { extern byte *gameIcon; extern int iconW, iconH; -bool initSludge(const char *); +bool initSludge(const Common::String &); void displayBase(); void sludgeDisplay(); int startNewFunctionNum(uint, uint, loadedFunction *, variableStack*&, bool = true); @@ -88,7 +88,7 @@ void saveHandlers(Common::WriteStream *stream); void finishFunction(loadedFunction *fun); void abortFunction(loadedFunction *fun); -Common::File *openAndVerify(const char *filename, char extra1, char extra2, const char *er, int &fileVersion); +Common::File *openAndVerify(const Common::String &filename, char extra1, char extra2, const char *er, int &fileVersion); void freezeSubs(); void unfreezeSubs(); diff --git a/engines/sludge/statusba.cpp b/engines/sludge/statusba.cpp index 2f1eb6fc15..de434ae22e 100644 --- a/engines/sludge/statusba.cpp +++ b/engines/sludge/statusba.cpp @@ -27,7 +27,6 @@ #include "sludge/sprites.h" #include "sludge/fonttext.h" #include "sludge/moreio.h" -#include "sludge/stringy.h" #include "sludge/newfatal.h" #include "sludge/statusba.h" @@ -49,7 +48,6 @@ void killLastStatus() { if (nowStatus->firstStatusBar) { statusBar *kill = nowStatus->firstStatusBar; nowStatus->firstStatusBar = kill->next; - delete kill->text; delete kill; } } @@ -61,7 +59,6 @@ void clearStatusBar() { while (stat) { kill = stat; stat = stat->next; - delete kill->text; delete kill; } nowStatus->firstStatusBar = NULL; @@ -71,15 +68,15 @@ void addStatusBar() { statusBar *newStat = new statusBar; if (checkNew(newStat)) { newStat->next = nowStatus->firstStatusBar; - newStat->text = copyString(""); + newStat->text.clear(); nowStatus->firstStatusBar = newStat; } } -void setStatusBar(char *txt) { +void setStatusBar(Common::String &txt) { if (nowStatus->firstStatusBar) { - delete[] nowStatus->firstStatusBar->text; - nowStatus->firstStatusBar->text = copyString(txt); + nowStatus->firstStatusBar->text.clear(); + nowStatus->firstStatusBar->text = txt; } } @@ -166,7 +163,7 @@ void initStatusBar() { statusBarLitColour(255, 255, 128); } -const char *statusBarText() { +const Common::String &statusBarText() { if (nowStatus->firstStatusBar) { return nowStatus->firstStatusBar->text; } else { diff --git a/engines/sludge/statusba.h b/engines/sludge/statusba.h index b6c0c7822f..65895f3b4a 100644 --- a/engines/sludge/statusba.h +++ b/engines/sludge/statusba.h @@ -22,10 +22,12 @@ #ifndef SLUDGE_STATUSBA_H #define SLUDGE_STATUSBA_H +#include "common/str.h" + namespace Sludge { struct statusBar { - char *text; + Common::String text; statusBar *next; }; @@ -40,14 +42,14 @@ struct statusStuff { void initStatusBar(); -void setStatusBar(char *txt); +void setStatusBar(Common::String &txt); void clearStatusBar(); void addStatusBar(); void killLastStatus(); void statusBarColour(byte r, byte g, byte b); void statusBarLitColour(byte r, byte g, byte b); void setLitStatus(int i); -const char *statusBarText(); +const Common::String &statusBarText(); void positionStatus(int, int); void drawStatusBar(); diff --git a/engines/sludge/stringy.cpp b/engines/sludge/stringy.cpp deleted file mode 100644 index 484f014ccb..0000000000 --- a/engines/sludge/stringy.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -#include "sludge/allfiles.h" -#include "sludge/newfatal.h" - -#ifndef SLUDGE_STRINGY_H -#define SLUDGE_STRINGY_H - -namespace Sludge { - -char *copyString(const char *copyMe) { - char *newString = new char[strlen(copyMe) + 1]; - if (!checkNew(newString)) - return NULL; - strcpy(newString, copyMe); - return newString; -} - -char *joinStrings(const char *s1, const char *s2) { - char *newString = new char[strlen(s1) + strlen(s2) + 1]; - if (!checkNew(newString)) - return NULL; - sprintf(newString, "%s%s", s1, s2); - return newString; -} - -} // End of namespace Sludge - -#endif diff --git a/engines/sludge/stringy.h b/engines/sludge/stringy.h deleted file mode 100644 index 7c6f9ed492..0000000000 --- a/engines/sludge/stringy.h +++ /dev/null @@ -1,32 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ -#ifndef SLUDGE_STRINGY_H -#define SLUDGE_STRINGY_H - -namespace Sludge { - -char *copyString(const char *copyMe); -char *joinStrings(const char *s1, const char *s2); - -} // End of namespace Sludge - -#endif diff --git a/engines/sludge/talk.cpp b/engines/sludge/talk.cpp index d72db2c218..a1f51bbaaf 100644 --- a/engines/sludge/talk.cpp +++ b/engines/sludge/talk.cpp @@ -32,7 +32,6 @@ #include "sludge/sound.h" #include "sludge/fonttext.h" #include "sludge/newfatal.h" -#include "sludge/stringy.h" #include "sludge/moreio.h" namespace Sludge { @@ -70,7 +69,6 @@ void killAllSpeech() { while (speech->allSpeech) { killMe = speech->allSpeech; speech->allSpeech = speech->allSpeech->next; - delete[] killMe->textLine; delete killMe; } } @@ -79,7 +77,7 @@ inline void setObjFontColour(objectType *t) { setFontColour(speech->talkCol, t->r, t->g, t->b); } -void addSpeechLine(char *theLine, int x, int &offset) { +void addSpeechLine(const Common::String &theLine, int x, int &offset) { int halfWidth = (stringWidth(theLine) >> 1) / cameraZoom; int xx1 = x - (halfWidth); int xx2 = x + (halfWidth); @@ -87,7 +85,8 @@ void addSpeechLine(char *theLine, int x, int &offset) { checkNew(newLine); newLine->next = speech->allSpeech; - newLine->textLine = copyString(theLine); + newLine->textLine.clear(); + newLine->textLine = theLine; newLine->x = xx1; speech->allSpeech = newLine; if ((xx1 < 5) && (offset < (5 - xx1))) { @@ -102,12 +101,12 @@ int isThereAnySpeechGoingOn() { return speech->allSpeech ? speech->lookWhosTalking : -1; } -int wrapSpeechXY(char *theText, int x, int y, int wrap, int sampleFile) { +int wrapSpeechXY(const Common::String &theText, int x, int y, int wrap, int sampleFile) { int a, offset = 0; killAllSpeech(); - int speechTime = (strlen(theText) + 20) * speechSpeed; + int speechTime = (theText.size() + 20) * speechSpeed; if (speechTime < 1) speechTime = 1; if (sampleFile != -1) { @@ -123,23 +122,26 @@ int wrapSpeechXY(char *theText, int x, int y, int wrap, int sampleFile) { } speech->speechY = y; - while (strlen(theText) > wrap) { + char *tmp, *txt; + tmp = txt = createCString(theText); + while ((int)strlen(txt) > wrap) { a = wrap; - while (theText[a] != ' ') { + while (txt[a] != ' ') { a--; if (a == 0) { a = wrap; break; } } - theText[a] = 0; - addSpeechLine(theText, x, offset); - theText[a] = ' '; - theText += a + 1; + txt[a] = 0; + addSpeechLine(txt, x, offset); + txt[a] = ' '; + txt += a + 1; y -= fontHeight / cameraZoom; } - addSpeechLine(theText, x, offset); + addSpeechLine(txt, x, offset); y -= fontHeight / cameraZoom; + delete []tmp; if (y < 0) speech->speechY -= y; @@ -158,7 +160,7 @@ int wrapSpeechXY(char *theText, int x, int y, int wrap, int sampleFile) { return speechTime; } -int wrapSpeechPerson(char *theText, onScreenPerson &thePerson, int sampleFile, +int wrapSpeechPerson(const Common::String &theText, onScreenPerson &thePerson, int sampleFile, bool animPerson) { int i = wrapSpeechXY(theText, thePerson.x - cameraX, thePerson.y - cameraY @@ -172,7 +174,7 @@ int wrapSpeechPerson(char *theText, onScreenPerson &thePerson, int sampleFile, return i; } -int wrapSpeech(char *theText, int objT, int sampleFile, bool animPerson) { +int wrapSpeech(const Common::String &theText, int objT, int sampleFile, bool animPerson) { int i; speech->lookWhosTalking = objT; diff --git a/engines/sludge/talk.h b/engines/sludge/talk.h index 4f344fb064..fe966cac27 100644 --- a/engines/sludge/talk.h +++ b/engines/sludge/talk.h @@ -27,7 +27,7 @@ namespace Sludge { struct speechLine { - char *textLine; + Common::String textLine; speechLine *next; int x; }; @@ -39,7 +39,7 @@ struct speechStruct { spritePalette talkCol; }; -int wrapSpeech(char *theText, int objT, int sampleFile, bool); +int wrapSpeech(const Common::String &theText, int objT, int sampleFile, bool); void viewSpeech(); void killAllSpeech(); int isThereAnySpeechGoingOn(); diff --git a/engines/sludge/thumbnail.cpp b/engines/sludge/thumbnail.cpp index f28035b795..8044ee645f 100644 --- a/engines/sludge/thumbnail.cpp +++ b/engines/sludge/thumbnail.cpp @@ -129,7 +129,7 @@ bool saveThumbnail(Common::WriteStream *stream) { return true; } -void showThumbnail(char *filename, int atX, int atY) { +void showThumbnail(const Common::String &filename, int atX, int atY) { #if 0 GLubyte *thumbnailTexture = NULL; GLuint thumbnailTextureName = 0; diff --git a/engines/sludge/thumbnail.h b/engines/sludge/thumbnail.h index 4452dcae13..5b58ecb65d 100644 --- a/engines/sludge/thumbnail.h +++ b/engines/sludge/thumbnail.h @@ -27,7 +27,7 @@ namespace Sludge { bool saveThumbnail(Common::WriteStream *stream); bool skipThumbnail(Common::SeekableReadStream *stream); -void showThumbnail(char *filename, int x, int y); +void showThumbnail(const Common::String &filename, int x, int y); } // End of namespace Sludge diff --git a/engines/sludge/variable.cpp b/engines/sludge/variable.cpp index eee9d07e1a..7d4ab0cdc0 100644 --- a/engines/sludge/variable.cpp +++ b/engines/sludge/variable.cpp @@ -26,7 +26,6 @@ #include "sludge/variable.h" #include "sludge/moreio.h" #include "sludge/newfatal.h" -#include "sludge/stringy.h" #include "sludge/objtypes.h" #include "sludge/people.h" #include "sludge/fileset.h" @@ -43,7 +42,7 @@ extern char *outputDir; void unlinkVar(variable &thisVar) { switch (thisVar.varType) { case SVT_STRING: - delete[] thisVar.varData.theString; + delete []thisVar.varData.theString; thisVar.varData.theString = NULL; break; @@ -143,10 +142,9 @@ int stackSize(const stackHandler *me) { return r; } -bool getSavedGamesStack(stackHandler *sH, char *ext) { - char *pattern = joinStrings("*", ext); - if (!pattern) - return false; +bool getSavedGamesStack(stackHandler *sH, const Common::String &ext) { + Common::String pattern = "*"; + pattern += ext; variable newName; newName.varType = SVT_NULL; @@ -173,8 +171,6 @@ bool getSavedGamesStack(stackHandler *sH, char *ext) { closedir(dir); #endif - delete[] pattern; - pattern = NULL; return true; } @@ -238,14 +234,12 @@ void addVariablesInSecond(variable &var1, variable &var2) { if (var1.varType == SVT_INT && var2.varType == SVT_INT) { var2.varData.intValue += var1.varData.intValue; } else { - char *string1 = getTextFromAnyVar(var1); - char *string2 = getTextFromAnyVar(var2); + Common::String string1 = getTextFromAnyVar(var1); + Common::String string2 = getTextFromAnyVar(var2); unlinkVar(var2); - var2.varData.theString = joinStrings(string1, string2); + var2.varData.theString = createCString(string1 + string2); var2.varType = SVT_STRING; - delete[] string1; - delete[] string2; } } @@ -285,65 +279,59 @@ void compareVariablesInSecond(const variable &var1, variable &var2) { setVariable(var2, SVT_INT, compareVars(var1, var2)); } -void makeTextVar(variable &thisVar, const char *txt) { +char *createCString(const Common::String &s) { + uint n = s.size() + 1; + char *res = new char[n]; + if (!checkNew(res)) { + fatal("createCString : Unable to copy String"); + return NULL; + } + memcpy(res, s.c_str(), n); + return res; +} + +void makeTextVar(variable &thisVar, const Common::String &txt) { unlinkVar(thisVar); thisVar.varType = SVT_STRING; - thisVar.varData.theString = copyString(txt); + thisVar.varData.theString = createCString(txt); } bool loadStringToVar(variable &thisVar, int value) { - makeTextVar(thisVar, getNumberedString(value)); return (bool)(thisVar.varData.theString != NULL); } -char *getTextFromAnyVar(const variable &from) { +Common::String getTextFromAnyVar(const variable &from) { switch (from.varType) { case SVT_STRING: - return copyString(from.varData.theString); + return from.varData.theString; case SVT_FASTARRAY: { - char *builder = copyString("FAST:"); - char *builder2; - char *grabText; + Common::String builder = "FAST:"; + Common::String builder2 = ""; + Common::String grabText = ""; for (int i = 0; i < from.varData.fastArray->size; i++) { - builder2 = joinStrings(builder, " "); - if (!builder2) - return NULL; - delete builder; + builder2 = builder + " "; grabText = getTextFromAnyVar(from.varData.fastArray->fastVariables[i]); - builder = joinStrings(builder2, grabText); - if (!builder) - return NULL; - delete grabText; - grabText = NULL; - delete builder2; - builder2 = NULL; + builder.clear(); + builder = builder2 + grabText; } return builder; } case SVT_STACK: { - char *builder = copyString("ARRAY:"); - char *builder2; - char *grabText; + Common::String builder = "ARRAY:"; + Common::String builder2 = ""; + Common::String grabText = ""; variableStack *stacky = from.varData.theStack->first; while (stacky) { - builder2 = joinStrings(builder, " "); - if (!builder2) - return NULL; - delete[] builder; + builder2 = builder + " "; grabText = getTextFromAnyVar(stacky->thisVar); - builder = joinStrings(builder2, grabText); - if (!builder) - return NULL; - delete[] grabText; - grabText = NULL; - delete[] builder2; - builder2 = NULL; + builder.clear(); + builder = builder2 + grabText; stacky = stacky->next; } return builder; @@ -351,17 +339,14 @@ char *getTextFromAnyVar(const variable &from) { case SVT_INT: { char *buff = new char[10]; - if (!checkNew(buff)) - return NULL; sprintf(buff, "%i", from.varData.intValue); - return buff; + Common::String res = buff; + delete []buff; + return res; } case SVT_FILE: { -// char * buff = new char[15]; -// if (! checkNew (buff)) return NULL; -// sprintf (buff, "FILE %i", from.varData.intValue); - return joinStrings("", resourceNameFromNum(from.varData.intValue)); + return resourceNameFromNum(from.varData.intValue); } /* case SVT_ANIM: @@ -375,14 +360,15 @@ char *getTextFromAnyVar(const variable &from) { case SVT_OBJTYPE: { objectType *thisType = findObjectType(from.varData.intValue); if (thisType) - return copyString(thisType->screenName); + return thisType->screenName; + break; } default: break; } - return copyString(typeName[from.varType]); + return typeName[from.varType]; } bool getBoolean(const variable &from) { @@ -425,7 +411,7 @@ bool copyMain(const variable &from, variable &to) { return true; case SVT_STRING: - to.varData.theString = copyString(from.varData.theString); + to.varData.theString = createCString(from.varData.theString); return to.varData.theString ? true : false; case SVT_STACK: diff --git a/engines/sludge/variable.h b/engines/sludge/variable.h index f36e4e75ec..3278b8e38f 100644 --- a/engines/sludge/variable.h +++ b/engines/sludge/variable.h @@ -56,7 +56,7 @@ struct stackHandler { union variableData { signed int intValue; - char *theString; + const char *theString; stackHandler *theStack; struct personaAnimation *animHandler; struct persona *costumeHandler; @@ -84,15 +84,16 @@ bool copyVariable(const variable &from, variable &to); bool loadStringToVar(variable &thisVar, int value); void newAnimationVariable(variable &thisVar, struct personaAnimation *i); void newCostumeVariable(variable &thisVar, struct persona *i); -void makeTextVar(variable &thisVar, const char *txt); +void makeTextVar(variable &thisVar, const Common::String &txt); void addVariablesInSecond(variable &var1, variable &var2); void compareVariablesInSecond(const variable &var1, variable &var2); +char *createCString(const Common::String &s); // Misc. void unlinkVar(variable &thisVar); -char *getNumberedString(int value); -char *getTextFromAnyVar(const variable &from); +Common::String getNumberedString(int value); +Common::String getTextFromAnyVar(const variable &from); struct persona *getCostumeFromVar(variable &thisVar); struct personaAnimation *getAnimationFromVar(variable &thisVar); bool getBoolean(const variable &from); @@ -110,7 +111,7 @@ bool copyStack(const variable &from, variable &to); int stackSize(const stackHandler *me); bool stackSetByIndex(variableStack *, uint, const variable &); variable *stackGetByIndex(variableStack *, uint); -bool getSavedGamesStack(stackHandler *sH, char *ext); +bool getSavedGamesStack(stackHandler *sH, const Common::String &ext); bool makeFastArrayFromStack(variable &to, const stackHandler *stacky); bool makeFastArraySize(variable &to, int size); -- cgit v1.2.3