[Scummvm-cvs-logs] scummvm master -> fdf12c5c09e1c644001079a9dce5335e9ae2707d
fingolfin
max at quendi.de
Wed May 25 13:03:04 CEST 2011
This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
0aa6aca420 SCUMM: Switch some code to use String::format
fdf12c5c09 SWORD25: Remove more unused loadlib code + use of fopen
Commit: 0aa6aca4201df105276455d550d00495fdaa067b
https://github.com/scummvm/scummvm/commit/0aa6aca4201df105276455d550d00495fdaa067b
Author: Max Horn (max at quendi.de)
Date: 2011-05-25T04:01:05-07:00
Commit Message:
SCUMM: Switch some code to use String::format
Changed paths:
engines/scumm/detection.cpp
diff --git a/engines/scumm/detection.cpp b/engines/scumm/detection.cpp
index bba2696..aecd13d 100644
--- a/engines/scumm/detection.cpp
+++ b/engines/scumm/detection.cpp
@@ -69,26 +69,26 @@ static const MD5Table *findInMD5Table(const char *md5) {
Common::String ScummEngine::generateFilename(const int room) const {
const int diskNumber = (room > 0) ? _res->_types[rtRoom][room]._roomno : 0;
- char buf[128];
+ Common::String result;
if (_game.version == 4) {
if (room == 0 || room >= 900) {
- snprintf(buf, sizeof(buf), "%03d.lfl", room);
+ result = Common::String::format("%03d.lfl", room);
} else {
- snprintf(buf, sizeof(buf), "disk%02d.lec", diskNumber);
+ result = Common::String::format("disk%02d.lec", diskNumber);
}
} else {
switch (_filenamePattern.genMethod) {
case kGenDiskNum:
- snprintf(buf, sizeof(buf), _filenamePattern.pattern, diskNumber);
+ result = Common::String::format(_filenamePattern.pattern, diskNumber);
break;
case kGenRoomNum:
- snprintf(buf, sizeof(buf), _filenamePattern.pattern, room);
+ result = Common::String::format(_filenamePattern.pattern, room);
break;
case kGenUnchanged:
- strncpy(buf, _filenamePattern.pattern, sizeof(buf));
+ result = _filenamePattern.pattern;
break;
default:
@@ -96,11 +96,11 @@ Common::String ScummEngine::generateFilename(const int room) const {
}
}
- return buf;
+ return result;
}
Common::String ScummEngine_v60he::generateFilename(const int room) const {
- char buf[128];
+ Common::String result;
char id = 0;
switch (_filenamePattern.genMethod) {
@@ -115,16 +115,16 @@ Common::String ScummEngine_v60he::generateFilename(const int room) const {
}
if (_filenamePattern.genMethod == kGenHEPC) {
- snprintf(buf, sizeof(buf), "%s.he%c", _filenamePattern.pattern, id);
+ result = Common::String::format("%s.he%c", _filenamePattern.pattern, id);
} else {
if (id == '3') { // special case for cursors
// For mac they're stored in game binary
- strncpy(buf, _filenamePattern.pattern, sizeof(buf));
+ result = _filenamePattern.pattern;
} else {
if (_filenamePattern.genMethod == kGenHEMac)
- snprintf(buf, sizeof(buf), "%s (%c)", _filenamePattern.pattern, id);
+ result = Common::String::format("%s (%c)", _filenamePattern.pattern, id);
else
- snprintf(buf, sizeof(buf), "%s %c", _filenamePattern.pattern, id);
+ result = Common::String::format("%s %c", _filenamePattern.pattern, id);
}
}
@@ -135,11 +135,11 @@ Common::String ScummEngine_v60he::generateFilename(const int room) const {
return ScummEngine::generateFilename(room);
}
- return buf;
+ return result;
}
Common::String ScummEngine_v70he::generateFilename(const int room) const {
- char buf[128];
+ Common::String result;
char id = 0;
switch (_filenamePattern.genMethod) {
@@ -156,19 +156,19 @@ Common::String ScummEngine_v70he::generateFilename(const int room) const {
id = 'b';
// Special cases for Blue's games, which share common (b) files
if (_game.id == GID_BIRTHDAY && !(_game.features & GF_DEMO))
- strcpy(buf, "Blue'sBirthday.(b)");
+ result = "Blue'sBirthday.(b)";
else if (_game.id == GID_TREASUREHUNT)
- strcpy(buf, "Blue'sTreasureHunt.(b)");
+ result = "Blue'sTreasureHunt.(b)";
else
- snprintf(buf, sizeof(buf), "%s.(b)", _filenamePattern.pattern);
+ result = Common::String::format("%s.(b)", _filenamePattern.pattern);
break;
case 1:
id = 'a';
- snprintf(buf, sizeof(buf), "%s.(a)", _filenamePattern.pattern);
+ result = Common::String::format("%s.(a)", _filenamePattern.pattern);
break;
default:
id = '0';
- snprintf(buf, sizeof(buf), "%s.he0", _filenamePattern.pattern);
+ result = Common::String::format("%s.he0", _filenamePattern.pattern);
}
} else if (room < 0) {
id = '0' - room;
@@ -179,16 +179,16 @@ Common::String ScummEngine_v70he::generateFilename(const int room) const {
if (_filenamePattern.genMethod == kGenHEPC) {
// For HE >= 98, we already called snprintf above.
if (_game.heversion < 98 || room < 0)
- snprintf(buf, sizeof(buf), "%s.he%c", _filenamePattern.pattern, id);
+ result = Common::String::format("%s.he%c", _filenamePattern.pattern, id);
} else {
if (id == '3') { // special case for cursors
// For mac they're stored in game binary
- strncpy(buf, _filenamePattern.pattern, sizeof(buf));
+ result = _filenamePattern.pattern;
} else {
if (_filenamePattern.genMethod == kGenHEMac)
- snprintf(buf, sizeof(buf), "%s (%c)", _filenamePattern.pattern, id);
+ result = Common::String::format("%s (%c)", _filenamePattern.pattern, id);
else
- snprintf(buf, sizeof(buf), "%s %c", _filenamePattern.pattern, id);
+ result = Common::String::format("%s %c", _filenamePattern.pattern, id);
}
}
@@ -199,40 +199,39 @@ Common::String ScummEngine_v70he::generateFilename(const int room) const {
return ScummEngine_v60he::generateFilename(room);
}
- return buf;
+ return result;
}
static Common::String generateFilenameForDetection(const char *pattern, FilenameGenMethod genMethod) {
- char buf[128];
+ Common::String result;
switch (genMethod) {
case kGenDiskNum:
case kGenRoomNum:
- snprintf(buf, sizeof(buf), pattern, 0);
+ result = Common::String::format(pattern, 0);
break;
case kGenHEPC:
- snprintf(buf, sizeof(buf), "%s.he0", pattern);
+ result = Common::String::format("%s.he0", pattern);
break;
case kGenHEMac:
- snprintf(buf, sizeof(buf), "%s (0)", pattern);
+ result = Common::String::format("%s (0)", pattern);
break;
case kGenHEMacNoParens:
- snprintf(buf, sizeof(buf), "%s 0", pattern);
+ result = Common::String::format("%s 0", pattern);
break;
case kGenUnchanged:
- strncpy(buf, pattern, sizeof(buf));
+ result = pattern;
break;
default:
error("generateFilenameForDetection: Unsupported genMethod");
}
- buf[sizeof(buf) - 1] = 0;
- return buf;
+ return result;
}
struct DetectorDesc {
Commit: fdf12c5c09e1c644001079a9dce5335e9ae2707d
https://github.com/scummvm/scummvm/commit/fdf12c5c09e1c644001079a9dce5335e9ae2707d
Author: Max Horn (max at quendi.de)
Date: 2011-05-25T04:01:06-07:00
Commit Message:
SWORD25: Remove more unused loadlib code + use of fopen
The functions loader_C and loader_Croot provably did not do anything
besides returning an error, so removing them was safe.
For loader_Lua, this was using luaL_loadfile. But that in turn now
uses Sword25FileProxy, which only supports loading config.lua,
and config.lua is not used via the loadlib mechanism.
Therefore, I deemed it safe to also remove this third loader.
This leaves loader_preload as only remaining loader. It is probably
unused, too, but I did not both to investigate further, as I already
achieved my primary goal (getting rid of fopen/fclose use).
All other removed functions were unused due to the removal of the
first three loader_* functions.
Changed paths:
engines/sword25/util/lua/loadlib.cpp
diff --git a/engines/sword25/util/lua/loadlib.cpp b/engines/sword25/util/lua/loadlib.cpp
index 2fa831a..9795a57 100644
--- a/engines/sword25/util/lua/loadlib.cpp
+++ b/engines/sword25/util/lua/loadlib.cpp
@@ -8,19 +8,6 @@
** implementation for Windows, and a stub for other systems.
*/
-// FIXME: Avoid using these APIs.
-// Actually, this could be achieved by removing 80% of the remaining
-// code in this file. Most of it is an elaborate way of expressing
-// something like "return ERROR;" anyway, as we don't support loading
-// dynamic libs
-#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
-#define FORBIDDEN_SYMBOL_EXCEPTION_fopen
-#define FORBIDDEN_SYMBOL_EXCEPTION_fclose
-
-
-#include <stdlib.h>
-#include <string.h>
-
#define loadlib_c
#define LUA_LIB
@@ -44,11 +31,6 @@
#define LIB_FAIL "open"
-/* error codes for ll_loadfunc */
-#define ERRLIB 1
-#define ERRFUNC 2
-
-
/*
** {======================================================
** Fallback for other systems
@@ -97,29 +79,28 @@ static int gctm (lua_State *L) {
}
-static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
- void **reg = ll_register(L, path);
- if (*reg == NULL) {
- lua_pushliteral(L, DLMSG); // loading not supported, just push an error msg
- return ERRLIB; /* unable to load library */
- } else {
- return ERRFUNC; /* unable to find function */
- }
-}
+/* error codes for ll_loadfunc */
+#define ERRLIB 1
+#define ERRFUNC 2
static int ll_loadlib (lua_State *L) {
const char *path = luaL_checkstring(L, 1);
- const char *init = luaL_checkstring(L, 2);
- int stat = ll_loadfunc(L, path, init);
- if (stat == 0) /* no errors? */
- return 1; /* return the loaded function */
- else { /* error; error message is on stack top */
- lua_pushnil(L);
- lua_insert(L, -2);
- lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
- return 3; /* return nil, error message, and where */
+// const char *init = luaL_checkstring(L, 2);
+ int stat;
+ void **reg = ll_register(L, path);
+ if (*reg == NULL) {
+ stat = ERRLIB; /* unable to load library */
+ } else {
+ stat = ERRFUNC; /* unable to find function */
}
+ lua_pushliteral(L, DLMSG);
+
+ /* error; error message is on stack top */
+ lua_pushnil(L);
+ lua_insert(L, -2);
+ lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
+ return 3; /* return nil, error message, and where */
}
@@ -131,109 +112,6 @@ static int ll_loadlib (lua_State *L) {
*/
-static int readable (const char *filename) {
- FILE *f = fopen(filename, "r"); /* try to open file */
- if (f == NULL) return 0; /* open failed */
- fclose(f);
- return 1;
-}
-
-
-static const char *pushnexttemplate (lua_State *L, const char *path) {
- const char *l;
- while (*path == *LUA_PATHSEP) path++; /* skip separators */
- if (*path == '\0') return NULL; /* no more templates */
- l = strchr(path, *LUA_PATHSEP); /* find next separator */
- if (l == NULL) l = path + strlen(path);
- lua_pushlstring(L, path, l - path); /* template */
- return l;
-}
-
-
-static const char *findfile (lua_State *L, const char *name,
- const char *pname) {
- const char *path;
- name = luaL_gsub(L, name, ".", LUA_DIRSEP);
- lua_getfield(L, LUA_ENVIRONINDEX, pname);
- path = lua_tostring(L, -1);
- if (path == NULL)
- luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
- lua_pushliteral(L, ""); /* error accumulator */
- while ((path = pushnexttemplate(L, path)) != NULL) {
- const char *filename;
- filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
- lua_remove(L, -2); /* remove path template */
- if (readable(filename)) /* does file exist and is readable? */
- return filename; /* return that file name */
- lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
- lua_remove(L, -2); /* remove file name */
- lua_concat(L, 2); /* add entry to possible error message */
- }
- return NULL; /* not found */
-}
-
-
-static void loaderror (lua_State *L, const char *filename) {
- luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
- lua_tostring(L, 1), filename, lua_tostring(L, -1));
-}
-
-
-static int loader_Lua (lua_State *L) {
- const char *filename;
- const char *name = luaL_checkstring(L, 1);
- filename = findfile(L, name, "path");
- if (filename == NULL) return 1; /* library not found in this path */
- if (luaL_loadfile(L, filename) != 0)
- loaderror(L, filename);
- return 1; /* library loaded successfully */
-}
-
-
-static const char *mkfuncname (lua_State *L, const char *modname) {
- const char *funcname;
- const char *mark = strchr(modname, *LUA_IGMARK);
- if (mark) modname = mark + 1;
- funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
- funcname = lua_pushfstring(L, POF"%s", funcname);
- lua_remove(L, -2); /* remove 'gsub' result */
- return funcname;
-}
-
-
-static int loader_C (lua_State *L) {
- const char *funcname;
- const char *name = luaL_checkstring(L, 1);
- const char *filename = findfile(L, name, "cpath");
- if (filename == NULL) return 1; /* library not found in this path */
- funcname = mkfuncname(L, name);
- if (ll_loadfunc(L, filename, funcname) != 0)
- loaderror(L, filename);
- return 1; /* library loaded successfully */
-}
-
-
-static int loader_Croot (lua_State *L) {
- const char *funcname;
- const char *filename;
- const char *name = luaL_checkstring(L, 1);
- const char *p = strchr(name, '.');
- int stat;
- if (p == NULL) return 0; /* is root */
- lua_pushlstring(L, name, p - name);
- filename = findfile(L, lua_tostring(L, -1), "cpath");
- if (filename == NULL) return 1; /* root not found */
- funcname = mkfuncname(L, name);
- if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
- if (stat != ERRFUNC) loaderror(L, filename); /* real error */
- lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
- name, filename);
- return 1; /* function not found */
- }
- return 1;
-}
-
-
static int loader_preload (lua_State *L) {
const char *name = luaL_checkstring(L, 1);
lua_getfield(L, LUA_ENVIRONINDEX, "preload");
@@ -409,7 +287,7 @@ static const luaL_Reg ll_funcs[] = {
static const lua_CFunction loaders[] =
- {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
+ {loader_preload, NULL};
LUALIB_API int luaopen_package (lua_State *L) {
More information about the Scummvm-git-logs
mailing list