1
0

A globally-accessible OS-independent GetDirectoryContents() function for listing all objects in a folder as an AStringList

git-svn-id: http://mc-server.googlecode.com/svn/trunk@433 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-03-25 14:24:51 +00:00
parent 534e221316
commit ba5b6ca751
6 changed files with 81 additions and 85 deletions

View File

@ -1,6 +1,6 @@
/* /*
** Lua binding: AllToLua ** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 03/11/12 17:55:41. ** Generated automatically by tolua++-1.0.92 on 03/25/12 16:23:48.
*/ */
#ifndef __cplusplus #ifndef __cplusplus
@ -17574,6 +17574,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_constant(tolua_S,"E_ENT_LOOK",E_ENT_LOOK); tolua_constant(tolua_S,"E_ENT_LOOK",E_ENT_LOOK);
tolua_constant(tolua_S,"E_REL_ENT_MOVE_LOOK",E_REL_ENT_MOVE_LOOK); tolua_constant(tolua_S,"E_REL_ENT_MOVE_LOOK",E_REL_ENT_MOVE_LOOK);
tolua_constant(tolua_S,"E_ENT_TELEPORT",E_ENT_TELEPORT); tolua_constant(tolua_S,"E_ENT_TELEPORT",E_ENT_TELEPORT);
tolua_constant(tolua_S,"E_ENT_HEAD_LOOK",E_ENT_HEAD_LOOK);
tolua_constant(tolua_S,"E_ENT_STATUS",E_ENT_STATUS); tolua_constant(tolua_S,"E_ENT_STATUS",E_ENT_STATUS);
tolua_constant(tolua_S,"E_METADATA",E_METADATA); tolua_constant(tolua_S,"E_METADATA",E_METADATA);
tolua_constant(tolua_S,"E_PRE_CHUNK",E_PRE_CHUNK); tolua_constant(tolua_S,"E_PRE_CHUNK",E_PRE_CHUNK);
@ -17594,6 +17595,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_constant(tolua_S,"E_CREATIVE_INVENTORY_ACTION",E_CREATIVE_INVENTORY_ACTION); tolua_constant(tolua_S,"E_CREATIVE_INVENTORY_ACTION",E_CREATIVE_INVENTORY_ACTION);
tolua_constant(tolua_S,"E_UPDATE_SIGN",E_UPDATE_SIGN); tolua_constant(tolua_S,"E_UPDATE_SIGN",E_UPDATE_SIGN);
tolua_constant(tolua_S,"E_PLAYER_LIST_ITEM",E_PLAYER_LIST_ITEM); tolua_constant(tolua_S,"E_PLAYER_LIST_ITEM",E_PLAYER_LIST_ITEM);
tolua_constant(tolua_S,"E_PLAYER_ABILITIES",E_PLAYER_ABILITIES);
tolua_constant(tolua_S,"E_PING",E_PING); tolua_constant(tolua_S,"E_PING",E_PING);
tolua_constant(tolua_S,"E_DISCONNECT",E_DISCONNECT); tolua_constant(tolua_S,"E_DISCONNECT",E_DISCONNECT);
tolua_array(tolua_S,"g_BlockLightValue",tolua_get_AllToLua_g_BlockLightValue,tolua_set_AllToLua_g_BlockLightValue); tolua_array(tolua_S,"g_BlockLightValue",tolua_get_AllToLua_g_BlockLightValue,tolua_set_AllToLua_g_BlockLightValue);

View File

@ -1,6 +1,6 @@
/* /*
** Lua binding: AllToLua ** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 03/11/12 17:55:42. ** Generated automatically by tolua++-1.0.92 on 03/25/12 16:23:48.
*/ */
/* Exported function */ /* Exported function */

View File

@ -159,3 +159,47 @@ void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString &
AStringList GetDirectoryContents(const char * a_Directory)
{
AStringList AllFiles;
#ifdef _WIN32
AString FileFilter = AString(a_Directory) + "*.*";
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
if ((hFind = FindFirstFile(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE)
{
do
{
AllFiles.push_back(FindFileData.cFileName);
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
#else // _WIN32
DIR * dp;
struct dirent *dirp;
if ((dp = opendir(a_Directory)) == NULL)
{
LOGERROR("Error (%i) opening %s\n", errno, a_Directory );
}
else
{
while ((dirp = readdir(dp)) != NULL)
{
AllFiles.push_back(dirp->d_name);
}
closedir(dp);
}
#endif // else _WIN32
return AllFiles;
}

View File

@ -15,6 +15,7 @@
typedef std::string AString; typedef std::string AString;
typedef std::vector<AString> AStringVector; typedef std::vector<AString> AStringVector;
typedef std::list<AString> AStringList;
@ -41,6 +42,10 @@ extern int NoCaseCompare(const AString & s1, const AString & s2);
/// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith /// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith
extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith);
/// Returns the list of all items in the specified directory (files, folders, nix pipes, whatever's there)
extern AStringList GetDirectoryContents(const char * a_Directory);
// If you have any other string helper functions, declare them here // If you have any other string helper functions, declare them here

View File

@ -12,13 +12,6 @@
typedef std::list< std::string > StringList;
StringList GetDirectoryContents( const char* a_Directory );
void cFileFormatUpdater::UpdateFileFormat() void cFileFormatUpdater::UpdateFileFormat()
{ {
UpdatePlayersOfWorld("world"); UpdatePlayersOfWorld("world");
@ -31,13 +24,13 @@ void cFileFormatUpdater::UpdateFileFormat()
// Convert player .bin files to JSON // Convert player .bin files to JSON
void cFileFormatUpdater::UpdatePlayersOfWorld( const char* a_WorldName ) void cFileFormatUpdater::UpdatePlayersOfWorld( const char* a_WorldName )
{ {
std::string PlayerDir = std::string( a_WorldName ) + "/player/"; AString PlayerDir = AString( a_WorldName ) + "/player/";
StringList AllFiles = GetDirectoryContents( PlayerDir.c_str() ); AStringList AllFiles = GetDirectoryContents( PlayerDir.c_str() );
for( StringList::iterator itr = AllFiles.begin(); itr != AllFiles.end(); ++itr ) for (AStringList::iterator itr = AllFiles.begin(); itr != AllFiles.end(); ++itr )
{ {
std::string & FileName = *itr; AString & FileName = *itr;
if( FileName.rfind(".bin") != std::string::npos ) // Get only the files ending in .bin if (FileName.rfind(".bin") != AString::npos) // Get only the files ending in .bin
{ {
PlayerBINtoJSON( (PlayerDir + FileName).c_str() ); PlayerBINtoJSON( (PlayerDir + FileName).c_str() );
} }
@ -148,44 +141,3 @@ void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName )
// Helper function
StringList GetDirectoryContents( const char* a_Directory )
{
StringList AllFiles;
#ifdef _WIN32
std::string FileFilter = std::string( a_Directory ) + "*.*";
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
if( ( hFind = FindFirstFile(FileFilter.c_str(), &FindFileData) ) != INVALID_HANDLE_VALUE)
{
do
{
AllFiles.push_back( FindFileData.cFileName );
} while( FindNextFile(hFind, &FindFileData) );
FindClose(hFind);
}
#else
DIR *dp;
struct dirent *dirp;
if( (dp = opendir( a_Directory ) ) == NULL)
{
LOGERROR("Error (%i) opening %s\n", errno, a_Directory );
}
else
{
while ((dirp = readdir(dp)) != NULL)
{
AllFiles.push_back( dirp->d_name );
}
closedir(dp);
}
#endif
return AllFiles;
}

View File

@ -16,7 +16,7 @@ extern "C"
#include "ManualBindings.h" #include "ManualBindings.h"
#ifdef _WIN32 #ifdef _WIN32
#include "wdirent.h" // #include "wdirent.h"
#else #else
#include <dirent.h> #include <dirent.h>
#endif #endif
@ -59,16 +59,14 @@ bool cPlugin_NewLua::Initialize()
std::string PluginPath = std::string("Plugins/") + m_Directory + "/"; std::string PluginPath = std::string("Plugins/") + m_Directory + "/";
// Load all files for this plugin, and execute them // Load all files for this plugin, and execute them
DIR* dp; AStringList Files = GetDirectoryContents(PluginPath.c_str());
struct dirent *entry; for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
if(dp = opendir( PluginPath.c_str() ))
{ {
while(entry = readdir(dp)) if (itr->rfind(".lua") == AString::npos)
{ {
std::string FileName = entry->d_name; continue;
if( FileName.find(".lua") != std::string::npos ) }
{ AString Path = PluginPath + *itr;
std::string Path = PluginPath + FileName;
int s = luaL_loadfile(m_LuaState, Path.c_str() ); int s = luaL_loadfile(m_LuaState, Path.c_str() );
if( report_errors( m_LuaState, s ) ) if( report_errors( m_LuaState, s ) )
{ {
@ -86,11 +84,7 @@ bool cPlugin_NewLua::Initialize()
m_LuaState = 0; m_LuaState = 0;
return false; return false;
} }
} } // for itr - Files[]
}
closedir( dp );
}
// Call intialize function // Call intialize function
if( !PushFunction("Initialize") ) if( !PushFunction("Initialize") )
@ -102,7 +96,6 @@ bool cPlugin_NewLua::Initialize()
tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua"); tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
if( !CallFunction(1, 1, "Initialize") ) if( !CallFunction(1, 1, "Initialize") )
{ {
lua_close( m_LuaState ); lua_close( m_LuaState );