From 4f5b35bb26cd3ab27abbd928d6418a5e5983bb26 Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 15 Apr 2007 00:32:10 +0000 Subject: [PATCH] refactor ex_lock/ex_unlock to a single function --- w32api/ex.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/w32api/ex.c b/w32api/ex.c index 10b6a96..066b867 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -289,7 +289,7 @@ static int ex_dir(lua_State *L) case LUA_TUSERDATA: pd = luaL_checkudata(L, 1, DIR_HANDLE); do d = readdir(*pd); - while (d && isdotfile(d->cFileName)) continue; + while (d && isdotfile(d->cFileName)); if (!d) return push_error(L); new_dirent(L); /* diriter ... entry */ diriter_getpathname(L, 1); /* diriter ... entry dir */ @@ -320,43 +320,43 @@ static int file_lock(lua_State *L, ov.Offset = offset; switch (*mode) { case 'w': - flags = LOCKFILE_EXCLUSIVE_LOCK; - /*FALLTHRU*/ + flags = LOCKFILE_EXCLUSIVE_LOCK; + /*FALLTHRU*/ case 'r': - flags |= LOCKFILE_FAIL_IMMEDIATELY; - ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov); - break; + flags |= LOCKFILE_FAIL_IMMEDIATELY; + ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov); + break; case 'u': - ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov); - break; + ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov); + break; default: - return luaL_error(L, "invalid mode"); + return luaL_error(L, "invalid mode"); } - if (!ret) return push_error(L); + if (!ret) + return push_error(L); /* return the file */ lua_settop(L, 1); return 1; } -/* file mode [offset [length]] -- file/nil error */ -static int ex_lock(lua_State *L) +static const char *opt_mode(lua_State *L, int *pidx) { - FILE *f = check_file(L, 1, NULL); - const char *mode = luaL_checkstring(L, 2); - long offset = luaL_optnumber(L, 3, 0); - long length = luaL_optnumber(L, 4, 0); - return file_lock(L, f, mode, offset, length); + if (lua_type(L, *pidx) != LUA_TSTRING) + return "u"; + return lua_tostring(L, (*pidx)++); } -/* file [offset [length]] -- file/nil error */ -static int ex_unlock(lua_State *L) +/* file [mode] [offset [length]] -- file/nil error */ +static int ex_lock(lua_State *L) { - lua_pushliteral(L, "u"); - lua_insert(L, 2); - return ex_lock(L); + FILE *f = check_file(L, 1, NULL); + int argi = 2; + const char *mode = opt_mode(L, &argi); + long offset = luaL_optnumber(L, argi, 0); + long length = luaL_optnumber(L, argi + 1, 0); + return file_lock(L, f, mode, offset, length); } - /* -- in out/nil error */ static int ex_pipe(lua_State *L) { @@ -491,7 +491,7 @@ int luaopen_ex(lua_State *L) {"pipe", ex_pipe}, #define ex_iofile_methods (ex_iolib + 1) {"lock", ex_lock}, - {"unlock", ex_unlock}, + {"unlock", ex_lock}, {0,0} }; const luaL_reg ex_oslib[] = { /* environment */