Ejemplo n.º 1
0
static int l_filefind_first(lua_State *L) {
	const char* wildcard = luaL_checkstring(L, 1);
	const char* origWildcard = wildcard;

	struct FileFindInfo* info = (struct FileFindInfo*)lua_newuserdata(L, sizeof(struct FileFindInfo));

	char* ptr;
	char* slashPtr;
	char* whichWildcard;

	info->path = malloc(strlen(wildcard) + 256);
	strcpy(info->path, wildcard);
	for (ptr = info->path; *ptr; ++ptr)
		if (*ptr == '\\')
			*ptr = '/';
	slashPtr = strrchr(info->path, '/');
	wildcard = slashPtr ? slashPtr + 1 : info->path;
	info->wildcard = malloc(strlen(wildcard) + 1);
	strcpy(info->wildcard, wildcard);
	if (slashPtr)
		*++slashPtr = 0;
	else
		info->path[0] = 0;
	info->pathEnd = info->path + strlen(info->path);

	info->first = 1;
#if defined(_WIN32)
	whichWildcard = (char*)origWildcard;
	if (whichWildcard[strlen(whichWildcard) - 1] == '/') {
		whichWildcard = info->path;
		info->pathEnd[-1] = 0;
	}
	info->handle = FindFirstFile(whichWildcard, &info->fd);
	if (whichWildcard == info->path) {
		info->pathEnd[-1] = '/';
	}
	if (info->handle != INVALID_HANDLE_VALUE) {
		if (info->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			if (strcmp(info->fd.cFileName, ".") == 0  ||  strcmp(info->fd.cFileName, "..") == 0)
				FileFindNextMatch(info);
		}
	}
#else
	{
		info->dirp = opendir(slashPtr ? info->path : ".");
		if (info->dirp) {
			FileFindNextMatch(info);
		}
	}
#endif

	luaL_getmetatable(L, FILEFIND_METATABLE);
	lua_setmetatable(L, -2);

	return 1;
}
Ejemplo n.º 2
0
static int LS_filefind_matchiter(lua_State* L) {
	struct FileFindInfo* info = (struct FileFindInfo*)(lua_touserdata(L, lua_upvalueindex(1)));

#if defined(WIN32)
	if (info->handle == INVALID_HANDLE_VALUE) {
		info->handle = NULL;
		return 0;
	}
#else
	if (!info->dirp)
		return 0;
#endif

	if (info->first) {
		info->first = 0;
		lua_pushvalue(L, lua_upvalueindex(1));
		return 1;
	}

	if (FileFindNextMatch(info)) {
		lua_pushvalue(L, lua_upvalueindex(1));
		return 1;
	}

	return 0;
}
Ejemplo n.º 3
0
static int filefind_next(lua_State *L) {
	struct FileFindInfo* info = filefind_checkmetatable(L, 1);
	if (FileFindNextMatch(info)) {
		lua_pushvalue(L, 1);
		return 1;
	}
	return 0;
}