SLUDGE: replace char* by Common::String for error messages

This commit is contained in:
yinsimei 2017-07-06 18:49:50 +02:00 committed by Eugene Sandulenko
parent 8152793d09
commit fe773c1beb
4 changed files with 32 additions and 78 deletions

View file

@ -2569,8 +2569,7 @@ builtIn(setThumbnailSize) {
return BR_ERROR; return BR_ERROR;
trimStack(fun->stack); trimStack(fun->stack);
if (thumbWidth < 0 || thumbHeight < 0 || thumbWidth > winWidth || thumbHeight > winHeight) { if (thumbWidth < 0 || thumbHeight < 0 || thumbWidth > winWidth || thumbHeight > winHeight) {
char buff[50]; Common::String buff = thumbWidth + " x " + thumbHeight;
sprintf(buff, "%d x %d", thumbWidth, thumbHeight);
fatal("Invalid thumbnail size", buff); fatal("Invalid thumbnail size", buff);
return BR_ERROR; return BR_ERROR;
} }

View file

@ -19,12 +19,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* *
*/ */
#if 0
#include <SDL/SDL.h>
#include <string.h>
#include <stdlib.h>
#endif
#include "common/debug.h" #include "common/debug.h"
@ -41,8 +35,8 @@ namespace Sludge {
const char emergencyMemoryMessage[] = "Out of memory displaying error message!"; const char emergencyMemoryMessage[] = "Out of memory displaying error message!";
static char *fatalMessage = NULL; static Common::String fatalMessage;
static char *fatalInfo = joinStrings("Initialisation error! Something went wrong before we even got started!", ""); static Common::String fatalInfo = "Initialisation error! Something went wrong before we even got started!";
extern int numResourceNames /* = 0*/; extern int numResourceNames /* = 0*/;
extern char * *allResourceNames /*= NULL*/; extern char * *allResourceNames /*= NULL*/;
@ -60,20 +54,20 @@ const char *resourceNameFromNum(int i) {
} }
bool hasFatal() { bool hasFatal() {
if (fatalMessage) if (!fatalMessage.empty())
return true; return true;
return false; return false;
} }
void displayFatal() { void displayFatal() {
if (fatalMessage) { if (!fatalMessage.empty()) {
#if 0 #if 0
msgBox("SLUDGE v" TEXT_VERSION " fatal error!", fatalMessage); msgBox("SLUDGE v" TEXT_VERSION " fatal error!", fatalMessage);
#endif #endif
} }
} }
void warning(const char *l) { void warning(const Common::String &l) {
#if 0 #if 0
setGraphicsWindow(false); setGraphicsWindow(false);
msgBox("SLUDGE v" TEXT_VERSION " non-fatal indigestion report", l); msgBox("SLUDGE v" TEXT_VERSION " non-fatal indigestion report", l);
@ -81,40 +75,12 @@ void warning(const char *l) {
} }
void registerWindowForFatal() { void registerWindowForFatal() {
delete[] fatalInfo; fatalInfo = "There's an error with this SLUDGE game! If you're designing this game, please turn on verbose error messages in the project manager and recompile. If not, please contact the author saying where and how this problem occured.";
fatalInfo =
joinStrings("There's an error with this SLUDGE game! If you're designing this game, please turn on verbose error messages in the project manager and recompile. If not, please contact the author saying where and how this problem occured.", "");
} }
#if 0 int inFatal(const Common::String &str) {
extern SDL_Event quit_event;
#endif
int inFatal(const char *str) {
error(str);
delete []str;
#if 0
FILE *fatFile = fopen("fatal.txt", "wt");
if (fatFile) {
fprintf(fatFile, "FATAL:\n%s\n", str);
fclose(fatFile);
}
fatalMessage = copyString(str);
if (fatalMessage == NULL)
fatalMessage = copyString("Out of memory");
killSoundStuff(); killSoundStuff();
error(str.c_str());
#if defined(HAVE_GLES2)
EGL_Close();
#endif
SDL_Quit();
atexit(displayFatal);
exit(1);
#endif
return true; return true;
} }
@ -126,45 +92,30 @@ int checkNew(const void *mem) {
return 1; return 1;
} }
void setFatalInfo(const char *userFunc, const char *BIF) { void setFatalInfo(const Common::String &userFunc, const Common::String &BIF) {
delete[] fatalInfo; fatalInfo = "Currently in this sub: " + userFunc + "\nCalling: " + BIF;
fatalInfo = new char[strlen(userFunc) + strlen(BIF) + 38]; debug(kSludgeDebugFatal, "%s", fatalInfo.c_str());
if (fatalInfo)
sprintf(fatalInfo, "Currently in this sub: %s\nCalling: %s", userFunc, BIF);
debug(kSludgeDebugFatal, "%s", fatalInfo);
} }
void setResourceForFatal(int n) { void setResourceForFatal(int n) {
resourceForFatal = n; resourceForFatal = n;
} }
int fatal(const char *str1) { int fatal(const Common::String &str1) {
if (numResourceNames && resourceForFatal != -1) { if (numResourceNames && resourceForFatal != -1) {
const char *r = resourceNameFromNum(resourceForFatal); Common::String r = resourceNameFromNum(resourceForFatal);
char *newStr = new char[strlen(str1) + strlen(r) + strlen(fatalInfo) + 14]; Common::String newStr = fatalInfo + "\nResource: " + r + "\n\n" + str1;
if (checkNew(newStr)) { inFatal(newStr);
sprintf(newStr, "%s\nResource: %s\n\n%s", fatalInfo, r, str1);
inFatal(newStr);
} else
fatal(emergencyMemoryMessage);
} else { } else {
char *newStr = new char[strlen(str1) + strlen(fatalInfo) + 3]; Common::String newStr = fatalInfo + "\n\n" + str1;
if (checkNew(newStr)) { inFatal(newStr);
sprintf(newStr, "%s\n\n%s", fatalInfo, str1);
inFatal(newStr);
} else
fatal(emergencyMemoryMessage);
} }
return 0; return 0;
} }
int fatal(const char *str1, const char *str2) { int fatal(const Common::String &str1, const Common::String &str2) {
char *newStr = new char[strlen(str1) + strlen(str2) + 2]; Common::String newStr = str1 + " " + str2;
if (checkNew(newStr)) { fatal(newStr);
sprintf(newStr, "%s %s", str1, str2);
fatal(newStr);
} else
fatal(emergencyMemoryMessage);
return 0; return 0;
} }

View file

@ -22,21 +22,23 @@
#ifndef SLUDGE_NEWFATAL_H #ifndef SLUDGE_NEWFATAL_H
#define SLUDGE_NEWFATAL_H #define SLUDGE_NEWFATAL_H
#include "common/str.h"
#include "sludge/errors.h" #include "sludge/errors.h"
namespace Sludge { namespace Sludge {
bool hasFatal(); bool hasFatal();
int fatal(const char *str); int fatal(const Common::String &str);
int fatal(const char *str1, const char *str2); int fatal(const Common::String &str1, const Common::String &str2);
int checkNew(const void *mem); int checkNew(const void *mem);
void displayFatal(); void displayFatal();
void registerWindowForFatal(); void registerWindowForFatal();
void setFatalInfo(const char *userFunc, const char *BIF); void setFatalInfo(const Common::String &userFunc, const Common::String &BIF);
void warning(const char *l); void warning(const Common::String &l);
void setResourceForFatal(int n); void setResourceForFatal(int n);
char *resourceNameFromNum(int i); const char *resourceNameFromNum(int i);
} // End of namespace Sludge } // End of namespace Sludge

View file

@ -591,8 +591,10 @@ variableStack *stackFindLast(variableStack *hunt) {
bool getValueType(int &toHere, variableType vT, const variable &v) { bool getValueType(int &toHere, variableType vT, const variable &v) {
//if (! v) return false; //if (! v) return false;
if (v.varType != vT) { if (v.varType != vT) {
char *e1 = joinStrings("Can only perform specified operation on a value which is of type ", typeName[vT]); Common::String e1 = "Can only perform specified operation on a value which is of type ";
char *e2 = joinStrings("... value supplied was of type ", typeName[v.varType]); e1 += typeName[vT];
Common::String e2 = "... value supplied was of type ";
e2 += typeName[v.varType];
fatal(e1, e2); fatal(e1, e2);
return false; return false;