最近几天,由于需要一组目录遍历的 函数,但标准库里面并没有提供,所以才有自己写一组:
1、添加一个util.c文件,文件内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#define lutillib_c
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int util_findfirst(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
luaL_Buffer b;
luaL_buffinit(L, &b);
struct _finddata_t fd;
long pos = _findfirst(filename, &fd);
if (pos)
{
lua_Integer f, type;
f = pos;
type = fd.attrib;
lua_pushinteger(L, f);
lua_pushinteger(L, type);
long len = strlen(fd.name);
char *p = luaL_prepbuffer(&b);
memcpy(p, fd.name, len