2008-10-31 18:38:05 +00:00
|
|
|
/**
|
|
|
|
* \file os_match.c
|
|
|
|
* \brief Match files and directories.
|
2014-04-24 14:49:06 +00:00
|
|
|
* \author Copyright (c) 2002-2014 Jason Perkins and the Premake project
|
2008-10-31 18:38:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "premake.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if PLATFORM_WINDOWS
|
|
|
|
|
|
|
|
typedef struct struct_MatchInfo
|
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
int is_first;
|
|
|
|
WIN32_FIND_DATA entry;
|
|
|
|
} MatchInfo;
|
|
|
|
|
|
|
|
int os_matchstart(lua_State* L)
|
|
|
|
{
|
|
|
|
const char* mask = luaL_checkstring(L, 1);
|
|
|
|
MatchInfo* m = (MatchInfo*)malloc(sizeof(MatchInfo));
|
|
|
|
m->handle = FindFirstFile(mask, &m->entry);
|
|
|
|
m->is_first = 1;
|
|
|
|
lua_pushlightuserdata(L, m);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchdone(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
if (m->handle != INVALID_HANDLE_VALUE)
|
|
|
|
FindClose(m->handle);
|
|
|
|
free(m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchname(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
lua_pushstring(L, m->entry.cFileName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchisfile(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
lua_pushboolean(L, (m->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchnext(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
2014-04-24 14:49:06 +00:00
|
|
|
if (m->handle == INVALID_HANDLE_VALUE) {
|
2008-10-31 18:38:05 +00:00
|
|
|
return 0;
|
2014-04-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2008-12-30 17:10:01 +00:00
|
|
|
while (m) /* loop forever */
|
2008-10-31 18:38:05 +00:00
|
|
|
{
|
|
|
|
if (!m->is_first)
|
|
|
|
{
|
|
|
|
if (!FindNextFile(m->handle, &m->entry))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->is_first = 0;
|
2014-04-24 14:49:06 +00:00
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
return 1;
|
2008-10-31 18:38:05 +00:00
|
|
|
}
|
2008-12-30 17:10:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2008-10-31 18:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
typedef struct struct_MatchInfo
|
|
|
|
{
|
|
|
|
DIR* handle;
|
|
|
|
struct dirent* entry;
|
|
|
|
char* path;
|
|
|
|
char* mask;
|
|
|
|
} MatchInfo;
|
|
|
|
|
|
|
|
int os_matchstart(lua_State* L)
|
|
|
|
{
|
|
|
|
const char* split;
|
|
|
|
const char* mask = luaL_checkstring(L, 1);
|
|
|
|
MatchInfo* m = (MatchInfo*)malloc(sizeof(MatchInfo));
|
|
|
|
|
|
|
|
/* split the mask into path and filename components */
|
|
|
|
split = strrchr(mask, '/');
|
|
|
|
if (split)
|
|
|
|
{
|
|
|
|
m->path = (char*)malloc(split - mask + 1);
|
|
|
|
strncpy(m->path, mask, split - mask);
|
|
|
|
m->path[split - mask] = '\0';
|
2009-09-29 19:27:33 +00:00
|
|
|
m->mask = (char*)malloc(mask + strlen(mask) - split);
|
|
|
|
strcpy(m->mask, split + 1);
|
2008-10-31 18:38:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m->path = (char*)malloc(2);
|
|
|
|
strcpy(m->path, ".");
|
2009-09-29 19:27:33 +00:00
|
|
|
m->mask = (char*)malloc(strlen(mask)+1);
|
|
|
|
strcpy(m->mask, mask);
|
2008-10-31 18:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m->handle = opendir(m->path);
|
|
|
|
lua_pushlightuserdata(L, m);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchdone(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
if (m->handle != NULL)
|
|
|
|
closedir(m->handle);
|
|
|
|
free(m->path);
|
2009-09-29 19:27:33 +00:00
|
|
|
free(m->mask);
|
2008-10-31 18:38:05 +00:00
|
|
|
free(m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchname(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
lua_pushstring(L, m->entry->d_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchisfile(lua_State* L)
|
|
|
|
{
|
|
|
|
const char* fname;
|
|
|
|
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
|
|
|
lua_pushfstring(L, "%s/%s", m->path, m->entry->d_name);
|
|
|
|
fname = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2014-07-10 20:08:19 +00:00
|
|
|
lua_pushboolean(L, do_isfile(fname));
|
|
|
|
return 1;
|
2008-10-31 18:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_matchnext(lua_State* L)
|
|
|
|
{
|
|
|
|
MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1);
|
2014-04-24 14:49:06 +00:00
|
|
|
if (m->handle == NULL) {
|
2008-10-31 18:38:05 +00:00
|
|
|
return 0;
|
2014-04-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2008-10-31 18:38:05 +00:00
|
|
|
m->entry = readdir(m->handle);
|
|
|
|
while (m->entry != NULL)
|
|
|
|
{
|
|
|
|
const char* name = m->entry->d_name;
|
2014-04-24 14:49:06 +00:00
|
|
|
if (fnmatch(m->mask, name, 0) == 0)
|
2008-10-31 18:38:05 +00:00
|
|
|
{
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
m->entry = readdir(m->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|