lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Nov 17, 2011 at 9:58 AM, Natanael Copa wrote:

> I created a lua-file-magic project on code.google.com:
> http://code.google.com/p/lua-file-magic/

Sweet!

I'm getting a couple of warnings:

magic.c: In function 'Pload':
magic.c:118:19: warning: initialization discards 'const' qualifier
from pointer target type [enabled by default]
magic.c:119:2: warning: passing argument 2 of 'lua_pushstring' makes
pointer from integer without a cast [enabled by default]
/usr/include/lua.h:164:16: note: expected 'const char *' but argument
is of type 'int'


The attached patch fixes the the warning, now load() will return true
on success;
on failure it returns nil plus an error message.

The patch also adds a primitive implementation for setflags()

 - Jeff
diff --git a/magic.c b/magic.c
index 97fe4ca..43433ec 100644
--- a/magic.c
+++ b/magic.c
@@ -115,9 +115,29 @@ static int Perror(lua_State *L)
 static int Pload(lua_State *L)
 {
 	magic_t m = Pmagic_checkarg(L, 1);
-	char *filename = luaL_optstring(L, 2, NULL);
-	lua_pushstring(L, magic_load(m, filename));
-	return 1;
+	const char *filename = luaL_optstring(L, 2, NULL);
+	if (magic_load(m, filename)==0) {
+		lua_pushboolean(L,1);
+		return 1;
+	} else {
+		lua_pushnil(L);
+		lua_pushstring(L,  magic_error(m));
+		return 2;
+	}
+}
+
+static int Psetflags(lua_State *L)
+{
+	magic_t m = Pmagic_checkarg(L, 1);	
+    int flags = luaL_checknumber(L, 2);
+	if (magic_setflags(m, flags)) {
+		lua_pushboolean(L,1);
+		return 1;
+	} else {
+		lua_pushnil(L);
+		lua_pushstring(L, magic_error(m));
+		return 2;
+	}
 }
 
 static const luaL_reg Pmagic_methods[] = {
@@ -128,7 +148,7 @@ static const luaL_reg Pmagic_methods[] = {
 	{"descriptor",	Ptodo},
 	{"buffer",	Ptodo},
 	{"error",	Perror},
-	{"setflags",	Ptodo},
+	{"setflags",	Psetflags},
 	{"load",	Pload},
 	{"compile",	Ptodo},
 	{"check",	Ptodo},