diff options
| author | mascarenhas <mascarenhas> | 2009-06-03 20:49:18 +0000 |
|---|---|---|
| committer | mascarenhas <mascarenhas> | 2009-06-03 20:49:18 +0000 |
| commit | 375d009d89eae607b2b95615f42134e4cdd8a2f6 (patch) | |
| tree | 62d0257d079549c4a8cdfe1b794872951770a95b /src | |
| parent | c1eff3de6befe526c17da32210887ce38c0cb78f (diff) | |
| download | luafilesystem-375d009d89eae607b2b95615f42134e4cdd8a2f6.tar.gz luafilesystem-375d009d89eae607b2b95615f42134e4cdd8a2f6.tar.bz2 luafilesystem-375d009d89eae607b2b95615f42134e4cdd8a2f6.zip | |
added explicit next and close methods to second return value of lfs.dir (the directory object), for explicit iteration or explicit closing
Diffstat (limited to 'src')
| -rw-r--r-- | src/lfs.c | 21 |
1 files changed, 15 insertions, 6 deletions
| @@ -17,7 +17,7 @@ | |||
| 17 | ** lfs.touch (filepath [, atime [, mtime]]) | 17 | ** lfs.touch (filepath [, atime [, mtime]]) |
| 18 | ** lfs.unlock (fh) | 18 | ** lfs.unlock (fh) |
| 19 | ** | 19 | ** |
| 20 | ** $Id: lfs.c,v 1.59 2009/04/24 22:24:07 mascarenhas Exp $ | 20 | ** $Id: lfs.c,v 1.60 2009/06/03 20:49:18 mascarenhas Exp $ |
| 21 | */ | 21 | */ |
| 22 | 22 | ||
| 23 | #ifndef _WIN32 | 23 | #ifndef _WIN32 |
| @@ -431,7 +431,7 @@ static int dir_iter (lua_State *L) { | |||
| 431 | #else | 431 | #else |
| 432 | struct dirent *entry; | 432 | struct dirent *entry; |
| 433 | #endif | 433 | #endif |
| 434 | dir_data *d = (dir_data *)lua_touserdata (L, lua_upvalueindex (1)); | 434 | dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); |
| 435 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | 435 | luaL_argcheck (L, !d->closed, 1, "closed directory"); |
| 436 | #ifdef _WIN32 | 436 | #ifdef _WIN32 |
| 437 | if (d->hFile == 0L) { /* first entry */ | 437 | if (d->hFile == 0L) { /* first entry */ |
| @@ -493,7 +493,9 @@ static int dir_close (lua_State *L) { | |||
| 493 | */ | 493 | */ |
| 494 | static int dir_iter_factory (lua_State *L) { | 494 | static int dir_iter_factory (lua_State *L) { |
| 495 | const char *path = luaL_checkstring (L, 1); | 495 | const char *path = luaL_checkstring (L, 1); |
| 496 | dir_data *d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | 496 | dir_data *d; |
| 497 | lua_pushcfunction (L, dir_iter); | ||
| 498 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | ||
| 497 | d->closed = 0; | 499 | d->closed = 0; |
| 498 | #ifdef _WIN32 | 500 | #ifdef _WIN32 |
| 499 | d->hFile = 0L; | 501 | d->hFile = 0L; |
| @@ -510,8 +512,7 @@ static int dir_iter_factory (lua_State *L) { | |||
| 510 | if (d->dir == NULL) | 512 | if (d->dir == NULL) |
| 511 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); | 513 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); |
| 512 | #endif | 514 | #endif |
| 513 | lua_pushcclosure (L, dir_iter, 1); | 515 | return 2; |
| 514 | return 1; | ||
| 515 | } | 516 | } |
| 516 | 517 | ||
| 517 | 518 | ||
| @@ -521,10 +522,18 @@ static int dir_iter_factory (lua_State *L) { | |||
| 521 | static int dir_create_meta (lua_State *L) { | 522 | static int dir_create_meta (lua_State *L) { |
| 522 | luaL_newmetatable (L, DIR_METATABLE); | 523 | luaL_newmetatable (L, DIR_METATABLE); |
| 523 | /* set its __gc field */ | 524 | /* set its __gc field */ |
| 525 | lua_pushstring (L, "__index"); | ||
| 526 | lua_newtable(L); | ||
| 527 | lua_pushstring (L, "next"); | ||
| 528 | lua_pushcfunction (L, dir_iter); | ||
| 529 | lua_settable(L, -3); | ||
| 530 | lua_pushstring (L, "close"); | ||
| 531 | lua_pushcfunction (L, dir_close); | ||
| 532 | lua_settable(L, -3); | ||
| 533 | lua_settable (L, -3); | ||
| 524 | lua_pushstring (L, "__gc"); | 534 | lua_pushstring (L, "__gc"); |
| 525 | lua_pushcfunction (L, dir_close); | 535 | lua_pushcfunction (L, dir_close); |
| 526 | lua_settable (L, -3); | 536 | lua_settable (L, -3); |
| 527 | |||
| 528 | return 1; | 537 | return 1; |
| 529 | } | 538 | } |
| 530 | 539 | ||
