Make libauto3 better liked by MSVC 2008.

Originally committed to SVN as r2816.
This commit is contained in:
Niels Martin Hansen 2009-04-17 20:51:58 +00:00
parent e977f3c991
commit 2d64f5079f
2 changed files with 23 additions and 24 deletions

View file

@ -137,12 +137,12 @@ static int Auto3LuaLoad(lua_State *L, filename_t filename, const char *prettynam
fclose(script_reader.f); fclose(script_reader.f);
if (res) { if (res) {
*error = strdup(lua_tostring(L, -1)); *error = Auto3Strdup(lua_tostring(L, -1));
return res; return res;
} }
if (script_reader.isfirst == -1) { if (script_reader.isfirst == -1) {
// Signals we got a bad UTF // Signals we got a bad UTF
*error = strdup(script_reader.databuf); *error = Auto3Strdup(script_reader.databuf);
return -1; return -1;
} }
@ -222,7 +222,7 @@ static int Auto3ParseConfigData(lua_State *L, struct Auto3Interpreter *script, c
lua_pushstring(L, "name"); lua_pushstring(L, "name");
lua_gettable(L, -2); lua_gettable(L, -2);
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
opt->name = strdup(lua_tostring(L, -1)); opt->name = Auto3Strdup(lua_tostring(L, -1));
} else { } else {
// name is required to be valid // name is required to be valid
opt->kind = COK_INVALID; opt->kind = COK_INVALID;
@ -233,7 +233,7 @@ static int Auto3ParseConfigData(lua_State *L, struct Auto3Interpreter *script, c
lua_pushstring(L, "label"); lua_pushstring(L, "label");
lua_gettable(L, -2); lua_gettable(L, -2);
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
opt->label = strdup(lua_tostring(L, -1)); opt->label = Auto3Strdup(lua_tostring(L, -1));
} else { } else {
// label is also required // label is also required
opt->kind = COK_INVALID; opt->kind = COK_INVALID;
@ -244,9 +244,9 @@ static int Auto3ParseConfigData(lua_State *L, struct Auto3Interpreter *script, c
lua_pushstring(L, "hint"); lua_pushstring(L, "hint");
lua_gettable(L, -2); lua_gettable(L, -2);
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
opt->hint = strdup(lua_tostring(L, -1)); opt->hint = Auto3Strdup(lua_tostring(L, -1));
} else { } else {
opt->hint = strdup(""); opt->hint = Auto3Strdup("");
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -286,8 +286,8 @@ static int Auto3ParseConfigData(lua_State *L, struct Auto3Interpreter *script, c
case COK_COLOUR: case COK_COLOUR:
// expect it to be a string // expect it to be a string
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
opt->default_val.stringval = strdup(lua_tostring(L, -1)); opt->default_val.stringval = Auto3Strdup(lua_tostring(L, -1));
opt->value.stringval = strdup(opt->default_val.stringval); opt->value.stringval = Auto3Strdup(opt->default_val.stringval);
} else { } else {
// not a string, baaaad scripter // not a string, baaaad scripter
opt->kind = COK_INVALID; opt->kind = COK_INVALID;
@ -435,7 +435,7 @@ AUTO3_API struct Auto3Interpreter *CreateAuto3Script(const filename_t filename,
// Execute the script // Execute the script
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
*error = strdup(lua_tostring(L, -1)); *error = Auto3Strdup(lua_tostring(L, -1));
goto faillua; goto faillua;
} }
@ -443,28 +443,28 @@ AUTO3_API struct Auto3Interpreter *CreateAuto3Script(const filename_t filename,
// Script has been run, stuff exists in the global environment // Script has been run, stuff exists in the global environment
lua_getglobal(L, "version"); lua_getglobal(L, "version");
if (!lua_isnumber(L, -1)) { if (!lua_isnumber(L, -1)) {
*error = strdup("'version' value not found or not a number"); *error = Auto3Strdup("'version' value not found or not a number");
goto faillua; goto faillua;
} }
if ((int)lua_tonumber(L, -1) != 3) { if ((int)lua_tonumber(L, -1) != 3) {
// invalid version // invalid version
*error = strdup("'version' must be 3 for Automation 3 scripts"); *error = Auto3Strdup("'version' must be 3 for Automation 3 scripts");
goto faillua; goto faillua;
} }
// skip 'kind', it's useless // skip 'kind', it's useless
// name // name
lua_getglobal(L, "name"); lua_getglobal(L, "name");
if (!lua_isstring(L, -1)) { if (!lua_isstring(L, -1)) {
script->name = strdup(prettyname); script->name = Auto3Strdup(prettyname);
} else { } else {
script->name = strdup(lua_tostring(L, -1)); script->name = Auto3Strdup(lua_tostring(L, -1));
} }
// description (optional) // description (optional)
lua_getglobal(L, "description"); lua_getglobal(L, "description");
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
script->description = strdup(lua_tostring(L, -1)); script->description = Auto3Strdup(lua_tostring(L, -1));
} else { } else {
script->description = strdup(""); script->description = Auto3Strdup("");
} }
lua_pop(L, 3); lua_pop(L, 3);
@ -528,7 +528,12 @@ AUTO3_API void *Auto3Malloc(size_t amount)
// Convenience function, use this for duplicating strings this lib should own // Convenience function, use this for duplicating strings this lib should own
AUTO3_API char *Auto3Strdup(const char *str) AUTO3_API char *Auto3Strdup(const char *str)
{ {
#if _MSC_VER >= 1400
// MS Visual C++ 2004 or newer
return _strdup(str);
#else
return strdup(str); return strdup(str);
#endif
} }
// Our "free" function, free generated error messages with this // Our "free" function, free generated error messages with this

View file

@ -38,15 +38,9 @@
#include <stdlib.h> #include <stdlib.h>
#ifdef AUTO3LIB #ifdef AUTO3LIB
#ifdef __WINDOWS__ #include <lua.h>
#include "lua/include/lua.h" #include <lualib.h>
#include "lua/include/lualib.h" #include <lauxlib.h>
#include "lua/include/lauxlib.h"
#else
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#endif
#endif #endif