lua-users home
lua-l archive

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


Hi

On Thu, Apr 03, 2008 at 12:46:30PM +0200, Michael Ring wrote:
> Your solution is 99% what I want, on my point of view the code would be 
> cleaner if I were able to construct the additional array in my C-code. 
> This also opens the possibility to access the data by name instead of 
> the index. Do you have an idea how to do that?

for an example check the implemenation of "attributes" in LHF's lfs.c:

static int file_info (lua_State *L) {
  struct stat info;
  const char *file = luaL_checkstring (L, 1);

  if (stat(file, &info)) {
    lua_pushnil (L);
    lua_pushfstring (L, "cannot obtain information from file `%s'", file);
    return 2;
  }
  lua_newtable (L);
  /* device inode resides on */
  lua_pushliteral (L, "dev");
  lua_pushnumber (L, (lua_Number)info.st_dev);
  lua_rawset (L, -3);
  /* inode's number */
  lua_pushliteral (L, "ino");
  lua_pushnumber (L, (lua_Number)info.st_ino);
  lua_rawset (L, -3);
  /* inode protection mode */
  lua_pushliteral (L, "mode");
  lua_pushstring (L, mode2string (info.st_mode));
  lua_rawset (L, -3);

...