Introducing StringUtils - the place to be if you are a generic string routine :) No more sprintf()!

git-svn-id: http://mc-server.googlecode.com/svn/trunk@209 0a769ca7-a7f5-676a-18bf-c427514a06d6
master
madmaxoft@gmail.com 2012-02-01 09:12:54 +00:00
parent 3364ecc284
commit bb49f0e021
19 changed files with 519 additions and 241 deletions

View File

@ -197,18 +197,6 @@
RelativePath="..\source\cAuthenticator.h"
>
</File>
<File
RelativePath="..\source\cBlockEntity.h"
>
</File>
<File
RelativePath="..\source\cBlockToPickup.cpp"
>
</File>
<File
RelativePath="..\source\cBlockToPickup.h"
>
</File>
<File
RelativePath="..\source\cChatColor.cpp"
>
@ -277,18 +265,6 @@
RelativePath="..\source\cCuboid.h"
>
</File>
<File
RelativePath="..\source\cDoors.h"
>
</File>
<File
RelativePath="..\source\cEntity.cpp"
>
</File>
<File
RelativePath="..\source\cEntity.h"
>
</File>
<File
RelativePath="..\source\cFileFormatUpdater.cpp"
>
@ -297,14 +273,6 @@
RelativePath="..\source\cFileFormatUpdater.h"
>
</File>
<File
RelativePath="..\source\cFurnaceEntity.cpp"
>
</File>
<File
RelativePath="..\source\cFurnaceEntity.h"
>
</File>
<File
RelativePath="..\source\cFurnaceRecipe.cpp"
>
@ -353,18 +321,6 @@
RelativePath="..\source\cInventory.h"
>
</File>
<File
RelativePath="..\source\cItem.cpp"
>
</File>
<File
RelativePath="..\source\cItem.h"
>
</File>
<File
RelativePath="..\source\cLadder.h"
>
</File>
<File
RelativePath="..\source\cLog.cpp"
>
@ -373,14 +329,6 @@
RelativePath="..\source\cLog.h"
>
</File>
<File
RelativePath="..\source\cMakeDir.cpp"
>
</File>
<File
RelativePath="..\source\cMakeDir.h"
>
</File>
<File
RelativePath="..\source\cMCLogger.cpp"
>
@ -431,38 +379,6 @@
RelativePath="..\source\cNoise.inc"
>
</File>
<File
RelativePath="..\source\cPawn.cpp"
>
</File>
<File
RelativePath="..\source\cPawn.h"
>
</File>
<File
RelativePath="..\source\cPickup.cpp"
>
</File>
<File
RelativePath="..\source\cPickup.h"
>
</File>
<File
RelativePath="..\source\cPiston.cpp"
>
</File>
<File
RelativePath="..\source\cPiston.h"
>
</File>
<File
RelativePath="..\source\cPlayer.cpp"
>
</File>
<File
RelativePath="..\source\cPlayer.h"
>
</File>
<File
RelativePath="..\source\cPlugin.cpp"
>
@ -523,14 +439,6 @@
RelativePath="..\source\cSign.h"
>
</File>
<File
RelativePath="..\source\cSignEntity.cpp"
>
</File>
<File
RelativePath="..\source\cSignEntity.h"
>
</File>
<File
RelativePath="..\source\cSimulator.cpp"
>
@ -555,18 +463,6 @@
RelativePath="..\source\cSocket.h"
>
</File>
<File
RelativePath="..\source\cStairs.h"
>
</File>
<File
RelativePath="..\source\cStringMap.cpp"
>
</File>
<File
RelativePath="..\source\cStringMap.h"
>
</File>
<File
RelativePath="..\source\cTCPLink.cpp"
>
@ -575,14 +471,6 @@
RelativePath="..\source\cTCPLink.h"
>
</File>
<File
RelativePath="..\source\cThread.cpp"
>
</File>
<File
RelativePath="..\source\cThread.h"
>
</File>
<File
RelativePath="..\source\cTimer.cpp"
>
@ -591,10 +479,6 @@
RelativePath="..\source\cTimer.h"
>
</File>
<File
RelativePath="..\source\cTorch.h"
>
</File>
<File
RelativePath="..\source\cTracer.cpp"
>
@ -715,6 +599,14 @@
RelativePath="..\source\ptr_cChunk.h"
>
</File>
<File
RelativePath="..\source\StringUtils.cpp"
>
</File>
<File
RelativePath="..\source\StringUtils.h"
>
</File>
<File
RelativePath="..\source\Vector3d.cpp"
>
@ -1320,8 +1212,100 @@
</File>
</Filter>
<Filter
Name="Items"
Name="Entities"
>
<File
RelativePath="..\source\cBlockEntity.h"
>
</File>
<File
RelativePath="..\source\cBlockToPickup.cpp"
>
</File>
<File
RelativePath="..\source\cBlockToPickup.h"
>
</File>
<File
RelativePath="..\source\cDoors.h"
>
</File>
<File
RelativePath="..\source\cEntity.cpp"
>
</File>
<File
RelativePath="..\source\cEntity.h"
>
</File>
<File
RelativePath="..\source\cFurnaceEntity.cpp"
>
</File>
<File
RelativePath="..\source\cFurnaceEntity.h"
>
</File>
<File
RelativePath="..\source\cItem.cpp"
>
</File>
<File
RelativePath="..\source\cItem.h"
>
</File>
<File
RelativePath="..\source\cLadder.h"
>
</File>
<File
RelativePath="..\source\cPawn.cpp"
>
</File>
<File
RelativePath="..\source\cPawn.h"
>
</File>
<File
RelativePath="..\source\cPickup.cpp"
>
</File>
<File
RelativePath="..\source\cPickup.h"
>
</File>
<File
RelativePath="..\source\cPiston.cpp"
>
</File>
<File
RelativePath="..\source\cPiston.h"
>
</File>
<File
RelativePath="..\source\cPlayer.cpp"
>
</File>
<File
RelativePath="..\source\cPlayer.h"
>
</File>
<File
RelativePath="..\source\cSignEntity.cpp"
>
</File>
<File
RelativePath="..\source\cSignEntity.h"
>
</File>
<File
RelativePath="..\source\cStairs.h"
>
</File>
<File
RelativePath="..\source\cTorch.h"
>
</File>
</Filter>
<Filter
Name="UI"
@ -1450,6 +1434,14 @@
RelativePath="..\source\cFile.h"
>
</File>
<File
RelativePath="..\source\cMakeDir.cpp"
>
</File>
<File
RelativePath="..\source\cMakeDir.h"
>
</File>
<File
RelativePath="..\source\cSemaphore.cpp"
>
@ -1466,6 +1458,14 @@
RelativePath="..\source\cSleep.h"
>
</File>
<File
RelativePath="..\source\cThread.cpp"
>
</File>
<File
RelativePath="..\source\cThread.h"
>
</File>
</Filter>
<Filter
Name="Bindings"
@ -1502,6 +1502,14 @@
RelativePath="..\source\cPlugin_NewLua.h"
>
</File>
<File
RelativePath="..\source\cStringMap.cpp"
>
</File>
<File
RelativePath="..\source\cStringMap.h"
>
</File>
<File
RelativePath="..\source\cWebPlugin_Lua.cpp"
>

View File

@ -463,6 +463,7 @@
<ClCompile Include="..\source\packets\cPacket_WindowClose.cpp" />
<ClCompile Include="..\source\packets\cPacket_WindowOpen.cpp" />
<ClCompile Include="..\source\SquirrelBindings.cpp" />
<ClCompile Include="..\source\StringUtils.cpp" />
<ClCompile Include="..\source\Vector3d.cpp" />
<ClCompile Include="..\source\Vector3f.cpp" />
<ClCompile Include="..\source\Vector3i.cpp" />
@ -632,6 +633,7 @@
<ClInclude Include="..\source\packets\cPacket_WindowOpen.h" />
<ClInclude Include="..\source\ptr_cChunk.h" />
<ClInclude Include="..\source\SquirrelBindings.h" />
<ClInclude Include="..\source\StringUtils.h" />
<ClInclude Include="..\source\Vector3d.h" />
<ClInclude Include="..\source\Vector3f.h" />
<ClInclude Include="..\source\Vector3i.h" />

View File

@ -909,6 +909,7 @@
</ClCompile>
<ClCompile Include="..\source\Globals.cpp" />
<ClCompile Include="..\source\cFile.cpp" />
<ClCompile Include="..\source\StringUtils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\source\cServer.h">
@ -1399,6 +1400,7 @@
</ClInclude>
<ClInclude Include="..\source\Globals.h" />
<ClInclude Include="..\source\cFile.h" />
<ClInclude Include="..\source\StringUtils.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\source\AllToLua.pkg">

View File

@ -54,6 +54,7 @@
// Common headers:
#include "../source/StringUtils.h"
#include "../source/cCriticalSection.h"
#include "../source/cMCLogger.h"

View File

@ -49,25 +49,6 @@
webserver::request_func webserver::request_func_=0;
static std::vector< std::string > StringSplit(std::string str, std::string delim)
{
std::vector< std::string > results;
size_t cutAt;
while( (cutAt = str.find_first_of(delim)) != str.npos )
{
if(cutAt > 0)
{
results.push_back(str.substr(0,cutAt));
}
str = str.substr(cutAt+1);
}
if(str.length() > 0)
{
results.push_back(str);
}
return results;
}
static std::string EatLine( std::string& a_String )
{
std::string RetVal = "";
@ -140,8 +121,7 @@ void ParseMultipartFormData( webserver::http_request& req, Socket* s)
static const std::string multipart_form_data = "multipart/form-data";
if(req.content_type_.substr(0, multipart_form_data.size()) == multipart_form_data) // Difficult data... :(
{
typedef std::vector< std::string > StringVector;
StringVector ContentTypeData = StringSplit( req.content_type_, "; " );
AStringVector ContentTypeData = StringSplit( req.content_type_, "; " );
std::string boundary;
// Find boundary
@ -208,7 +188,7 @@ void ParseMultipartFormData( webserver::http_request& req, Socket* s)
static const std::string disp_filename = "filename=";
// Parse the disposition
StringVector DispositionData = StringSplit( f_disposition, "; " );
AStringVector DispositionData = StringSplit( f_disposition, "; " );
for( unsigned int i = 0; i < DispositionData.size(); ++i )
{
if( DispositionData[i].substr(0, disp_name.size()) == disp_name )

View File

@ -247,7 +247,8 @@ MCServer : \
build/cFileFormatUpdater.o\
build/cItem.o\
build/cPlugin_NewLua.o\
build/cWebPlugin_Lua.o
build/cWebPlugin_Lua.o\
build/StringUtils.o
$(CC) $(LNK_OPTIONS) \
build/json_reader.o\
build/json_value.o\
@ -458,6 +459,7 @@ MCServer : \
build/cItem.o\
build/cPlugin_NewLua.o\
build/cWebPlugin_Lua.o\
build/StringUtils.o\
-o MCServer
clean :
@ -671,6 +673,7 @@ clean :
build/cItem.o\
build/cPlugin_NewLua.o\
build/cWebPlugin_Lua.o\
build/StringUtils.o\
MCServer
install : MCServer
@ -1528,4 +1531,7 @@ build/cPlugin_NewLua.o : source/cPlugin_NewLua.cpp
build/cWebPlugin_Lua.o : source/cWebPlugin_Lua.cpp
$(CC) $(CC_OPTIONS) source/cWebPlugin_Lua.cpp -c $(INCLUDE) -o build/cWebPlugin_Lua.o
build/StringUtils.o : source/StringUtils.cpp
$(CC) $(CC_OPTIONS) source/StringUtils.cpp -c $(INCLUDE) -o build/StringUtils.o
##### END RUN ####

View File

@ -28,6 +28,7 @@
#include <semaphore.h>
#include <errno.h>
#include <fcntl.h>
#include <memory.h>
#endif
@ -42,6 +43,13 @@
// Compatibility:
#define ASSERT assert
// STL stuff:
#include <vector>
#include <list>
@ -54,6 +62,7 @@
// Common headers:
#include "StringUtils.h"
#include "cSleep.h"
#include "cCriticalSection.h"
#include "cSemaphore.h"

View File

@ -15,7 +15,9 @@
#include "cStringMap.h"
#include "md5/md5.h"
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
/****************************
* Lua bound functions with special return types
@ -26,7 +28,7 @@ static int tolua_StringSplit(lua_State* tolua_S)
std::string str = ((std::string) tolua_tocppstring(tolua_S,1,0));
std::string delim = ((std::string) tolua_tocppstring(tolua_S,2,0));
std::vector<std::string> Split = StringSplit( str, delim );
AStringVector Split = StringSplit( str, delim );
lua_createtable(tolua_S, Split.size(), 0);
int newTable = lua_gettop(tolua_S);
@ -41,6 +43,10 @@ static int tolua_StringSplit(lua_State* tolua_S)
return 1;
}
static int tolua_LOG(lua_State* tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
@ -48,6 +54,10 @@ static int tolua_LOG(lua_State* tolua_S)
return 0;
}
static int tolua_LOGINFO(lua_State* tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
@ -55,6 +65,10 @@ static int tolua_LOGINFO(lua_State* tolua_S)
return 0;
}
static int tolua_LOGWARN(lua_State* tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
@ -62,6 +76,10 @@ static int tolua_LOGWARN(lua_State* tolua_S)
return 0;
}
static int tolua_LOGERROR(lua_State* tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
@ -69,6 +87,10 @@ static int tolua_LOGERROR(lua_State* tolua_S)
return 0;
}
static int tolua_cWorld_GetAllPlayers(lua_State* tolua_S)
{
cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0);
@ -79,6 +101,10 @@ static int tolua_cWorld_GetAllPlayers(lua_State* tolua_S)
return 1;
}
static int tolua_cPlugin_GetCommands(lua_State* tolua_S)
{
cPlugin* self = (cPlugin*) tolua_tousertype(tolua_S,1,0);
@ -100,6 +126,10 @@ static int tolua_cPlugin_GetCommands(lua_State* tolua_S)
return 1;
}
static int tolua_cPluginManager_GetAllPlugins(lua_State* tolua_S)
{
cPluginManager* self = (cPluginManager*) tolua_tousertype(tolua_S,1,0);
@ -121,6 +151,10 @@ static int tolua_cPluginManager_GetAllPlugins(lua_State* tolua_S)
return 1;
}
static int tolua_cPlayer_GetGroups(lua_State* tolua_S)
{
cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0);
@ -142,6 +176,10 @@ static int tolua_cPlayer_GetGroups(lua_State* tolua_S)
return 1;
}
static int tolua_cPlayer_GetResolvedPermissions(lua_State* tolua_S)
{
cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0);
@ -163,6 +201,10 @@ static int tolua_cPlayer_GetResolvedPermissions(lua_State* tolua_S)
return 1;
}
static int tolua_cPlugin_BindCommand(lua_State* tolua_S)
{
cPlugin* self = (cPlugin*) tolua_tousertype(tolua_S,1,0);
@ -217,6 +259,10 @@ static int tolua_cPlugin_BindCommand(lua_State* tolua_S)
return 0;
}
static int tolua_cWebPlugin_Lua_AddTab(lua_State* tolua_S)
{
cWebPlugin_Lua* self = (cWebPlugin_Lua*) tolua_tousertype(tolua_S,1,0);
@ -260,6 +306,10 @@ static int tolua_cWebPlugin_Lua_AddTab(lua_State* tolua_S)
return 0;
}
static int tolua_md5(lua_State* tolua_S)
{
std::string SourceString = tolua_tostring(tolua_S, 1, 0);
@ -268,6 +318,10 @@ static int tolua_md5(lua_State* tolua_S)
return 1;
}
static int tolua_push_StringStringMap(lua_State* tolua_S, std::map< std::string, std::string >& a_StringStringMap )
{
lua_newtable(tolua_S);
@ -285,18 +339,30 @@ static int tolua_push_StringStringMap(lua_State* tolua_S, std::map< std::string,
return 1;
}
static int tolua_get_HTTPRequest_Params(lua_State* tolua_S)
{
HTTPRequest* self = (HTTPRequest*) tolua_tousertype(tolua_S,1,0);
return tolua_push_StringStringMap(tolua_S, self->Params);
}
static int tolua_get_HTTPRequest_PostParams(lua_State* tolua_S)
{
HTTPRequest* self = (HTTPRequest*) tolua_tousertype(tolua_S,1,0);
return tolua_push_StringStringMap(tolua_S, self->PostParams);
}
static int tolua_get_HTTPRequest_FormData(lua_State* tolua_S)
{
HTTPRequest* self = (HTTPRequest*) tolua_tousertype(tolua_S,1,0);
@ -316,6 +382,10 @@ static int tolua_get_HTTPRequest_FormData(lua_State* tolua_S)
return 1;
}
void ManualBindings::Bind( lua_State* tolua_S )
{
tolua_beginmodule(tolua_S,NULL);
@ -357,3 +427,7 @@ void ManualBindings::Bind( lua_State* tolua_S )
tolua_endmodule(tolua_S);
}

129
source/StringUtils.cpp Normal file
View File

@ -0,0 +1,129 @@
// StringUtils.cpp
// Implements the various string helper functions:
#include "Globals.h"
AString & AppendVPrintf(AString & str, const char *format, va_list args)
{
ASSERT(format != NULL);
char buffer[2048];
size_t len;
if ((len = _vsnprintf_s(buffer, ARRAYCOUNT(buffer), _TRUNCATE, format, args)) != -1)
{
str.append(buffer, len);
return str;
}
len = _vscprintf(format, args);
if (len == -1)
{
return str;
}
std::auto_ptr<char> tmp(new char[len + 1]);
ASSERT(tmp.get() != NULL); // Why not alloced? Is the length reasonable?
if (tmp.get() == NULL)
{
throw std::bad_alloc();
}
if ((len = vsprintf_s(tmp.get(), len + 1, format, args)) != -1)
{
str.append(tmp.get(), len);
}
ASSERT(len != -1);
return str;
}
AString & Printf(AString & str, const char *format, ...)
{
str.clear();
va_list args;
va_start(args, format);
std::string &retval = AppendVPrintf(str, format, args);
va_end(args);
return retval;
}
AString & AppendPrintf(AString &str, const char *format, ...)
{
va_list args;
va_start(args, format);
std::string &retval = AppendVPrintf(str, format, args);
va_end(args);
return retval;
}
AStringVector StringSplit(const AString & str, const AString & delim)
{
AStringVector results;
size_t cutAt = 0;
size_t Prev = 0;
while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
{
if (cutAt > 0)
{
results.push_back(str.substr(0, cutAt));
}
Prev = cutAt + delim.length();
}
if (Prev < str.length())
{
results.push_back(str.substr(Prev));
}
return results;
}
AString & StrToUpper(AString & s)
{
AString::iterator i = s.begin();
AString::iterator end = s.end();
while (i != end)
{
*i = (char)toupper(*i);
++i;
}
return s;
}
int NoCaseCompare(const AString & s1, const AString & s2)
{
#ifdef _MSC_VER
// MSVC has stricmp that compares case-insensitive:
return stricmp(s1.c_str(), s2.c_str());
#else
// Do it the hard way:
AString s1Copy(s1);
AString s2Copy(s2);
return StrToUpper(s1Copy).compare(StrToUpper(s2Copy));
#endif // else _MSC_VER
}

51
source/StringUtils.h Normal file
View File

@ -0,0 +1,51 @@
// StringUtils.h
// Interfaces to various string helper functions
#ifndef STRINGUTILS_H_INCLUDED
#define STRINGUTILS_H_INCLUDED
typedef std::string AString;
typedef std::vector<AString> AStringVector;
/// Add the formated string to the existing data in the string
extern AString & AppendVPrintf(AString & str, const char * format, va_list args);
/// Output the formatted text into the string
extern AString & Printf (AString & str, const char * format, ...);
/// Add the formatted string to the existing data in the string
extern AString & AppendPrintf (AString & str, const char * format, ...);
/// Split the string at delimiters, return as a stringvector
extern AStringVector StringSplit(const AString & str, const AString & delim);
/// In-place string conversion to uppercase; returns the same string
extern AString & StrToUpper(AString & s);
/// Case-insensitive string comparison; returns 0 if the strings are the same
extern int NoCaseCompare(const AString & s1, const AString & s2);
// If you have any other string helper functions, declare them here
#endif // STRINGUTILS_H_INCLUDED

View File

@ -7,7 +7,9 @@
#include "cChatColor.h"
#include "cRoot.h"
extern std::vector< std::string > StringSplit( std::string str, std::string delim);
typedef std::map< std::string, cGroup* > GroupMap;
struct cGroupManager::sGroupManagerState
@ -57,7 +59,7 @@ cGroupManager::cGroupManager()
std::string Commands = IniFile.GetValue( KeyName, "Commands", "" );
if( Commands.size() > 0 )
{
std::vector< std::string > Split = StringSplit( Commands, "," );
AStringVector Split = StringSplit( Commands, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->AddCommand( Split[i] );
@ -68,7 +70,7 @@ cGroupManager::cGroupManager()
std::string Permissions = IniFile.GetValue( KeyName, "Permissions", "" );
if( Permissions.size() > 0 )
{
std::vector< std::string > Split = StringSplit( Permissions, "," );
AStringVector Split = StringSplit( Permissions, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->AddPermission( Split[i] );
@ -79,7 +81,7 @@ cGroupManager::cGroupManager()
std::string Groups = IniFile.GetValue( KeyName, "Inherits", "" );
if( Groups.size() > 0 )
{
std::vector< std::string > Split = StringSplit( Groups, "," );
AStringVector Split = StringSplit( Groups, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->InheritFrom( GetGroup( Split[i].c_str() ) );

View File

@ -11,7 +11,6 @@
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
extern bool report_errors(lua_State* lua, int status);
cLuaCommandBinder::cLuaCommandBinder()
@ -58,8 +57,11 @@ bool cLuaCommandBinder::BindCommand( const std::string & a_Command, const std::s
bool cLuaCommandBinder::HandleCommand( const std::string & a_Command, cPlayer* a_Player )
{
std::vector<std::string> Split = StringSplit( a_Command, " ");
if( Split.size() == 0 ) return false;
AStringVector Split = StringSplit(a_Command, " ");
if (Split.size() == 0)
{
return false;
}
CommandMap::iterator FoundCommand = m_BoundCommands.find( Split[0] );
if( FoundCommand != m_BoundCommands.end() )

View File

@ -13,7 +13,9 @@
using namespace std;
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
struct cMonsterConfig::sAttributesStruct
{
@ -61,7 +63,7 @@ void cMonsterConfig::Initialize() {
return;
}
vector<string> SplitList = StringSplit(m_pState->MonsterTypes,",");
AStringVector SplitList = StringSplit(m_pState->MonsterTypes,",");
for(unsigned int i = 0; i < SplitList.size(); ++i) {
if(!SplitList[i].empty()) {
printf("Getting Attributes for: %s \n",SplitList[i].c_str());

View File

@ -1,6 +1,17 @@
#pragma once
#include <cstdio>
// fwd: "cWorld.h"
class cWorld;
class cPiston
{
public:
@ -43,3 +54,7 @@ void ChainMove( int, int, int, char, unsigned short * );
unsigned short FirstPassthroughBlock( int, int, int, char );
};

View File

@ -43,13 +43,11 @@
#include "../iniFile/iniFile.h"
#include <json/json.h>
#ifndef _WIN32 // for mkdir
#include <sys/stat.h>
#include <sys/types.h>
#define sprintf_s(dst, size, format, ...) sprintf(dst, format, __VA_ARGS__ )
#endif
#define float2int(x) ((x)<0 ? ((int)(x))-1 : (int)(x))
extern std::vector< std::string > StringSplit( std::string str, std::string delim);
CLASS_DEFINITION( cPlayer, cPawn );
@ -557,9 +555,13 @@ bool cPlayer::CanUseCommand( const char* a_Command )
return false;
}
bool cPlayer::HasPermission( const char* a_Permission )
{
std::vector< std::string > Split = StringSplit( a_Permission, "." );
AStringVector Split = StringSplit( a_Permission, "." );
PermissionMap Possibilities = m_pState->ResolvedPermissions;
// Now search the namespaces
while( Possibilities.begin() != Possibilities.end() )
@ -567,7 +569,7 @@ bool cPlayer::HasPermission( const char* a_Permission )
PermissionMap::iterator itr = Possibilities.begin();
if( itr->second )
{
std::vector< std::string > OtherSplit = StringSplit( itr->first, "." );
AStringVector OtherSplit = StringSplit( itr->first, "." );
if( OtherSplit.size() <= Split.size() )
{
unsigned int i;
@ -589,6 +591,10 @@ bool cPlayer::HasPermission( const char* a_Permission )
return false;
}
bool cPlayer::IsInGroup( const char* a_Group )
{
for( GroupList::iterator itr = m_pState->ResolvedGroups.begin(); itr != m_pState->ResolvedGroups.end(); ++itr )
@ -743,7 +749,7 @@ void cPlayer::LoadPermissionsFromDisk()
std::string Groups = IniFile.GetValue(m_pState->PlayerName, "Groups", "");
if( Groups.size() > 0 )
{
std::vector< std::string > Split = StringSplit( Groups, "," );
AStringVector Split = StringSplit( Groups, "," );
for( unsigned int i = 0; i < Split.size(); i++ )
{
AddToGroup( Split[i].c_str() );

View File

@ -5,7 +5,6 @@
#include "cPlugin.h"
#include "cPlugin_Lua.h"
#include "cPlugin_NewLua.h"
#include "cMCLogger.h"
#include "cWebAdmin.h"
#include "cItem.h"
#include "cRoot.h"
@ -24,8 +23,6 @@
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
typedef std::list< cPlugin_Lua* > LuaPluginList;
typedef std::map< cPluginManager::PluginHook, cPluginManager::PluginList > HookMap;
@ -96,7 +93,7 @@ void cPluginManager::ReloadPluginsNow()
// allow for comma separated plugin list
// degrades and works fine for the plugin
// per line
std::vector< std::string > split = StringSplit( PluginFile, "," );
AStringVector split = StringSplit( PluginFile, "," );
for (unsigned int j = 0; j < split.size(); j++) {
cPlugin_Lua* Plugin = new cPlugin_Lua( (split[j] + std::string(".lua") ).c_str() );
if( !AddLuaPlugin( Plugin ) )

View File

@ -347,6 +347,10 @@ void ServerTickThread( void * a_Param )
LOG("TICK THREAD STOPPED");
}
void cServer::StartListenThread()
{
m_pState->pListenThread = new cThread( ServerListenThread, this, "cServer::ServerListenThread" );
@ -355,50 +359,24 @@ void cServer::StartListenThread()
m_pState->pTickThread->Start( true );
}
std::vector< std::string > StringSplit(std::string str, std::string delim)
{
std::vector< std::string > results;
size_t cutAt;
while( (cutAt = str.find_first_of(delim)) != str.npos )
{
if(cutAt > 0)
{
results.push_back(str.substr(0,cutAt));
}
str = str.substr(cutAt+1);
}
if(str.length() > 0)
{
results.push_back(str);
}
return results;
}
template <class T>
bool from_string(T& t,
bool from_string(
T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
std::ios_base& (*f)(std::ios_base&)
)
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
std::string & StrToUpper(std::string& s)
{
std::string::iterator i = s.begin();
std::string::iterator end = s.end();
while (i != end) {
*i = (char)toupper(*i);
++i;
}
return s;
}
int NoCaseCompare( std::string s1, std::string s2 )
{
return StrToUpper( s1 ).compare( StrToUpper( s2 ) );
}
bool cServer::Command( cClientHandle & a_Client, const char* a_Cmd )
{
@ -410,7 +388,7 @@ bool cServer::Command( cClientHandle & a_Client, const char* a_Cmd )
if( Command.length() <= 0 ) return false;
if( Command[0] != '/' ) return false;
std::vector< std::string > split = StringSplit( Command, " " );
AStringVector split = StringSplit( Command, " " );
if( split.size() == 0 )
return false;
@ -426,8 +404,8 @@ bool cServer::Command( cClientHandle & a_Client, const char* a_Cmd )
void cServer::ServerCommand( const char* a_Cmd )
{
std::string Command( a_Cmd );
std::vector< std::string > split = StringSplit( Command, " " );
AString Command( a_Cmd );
AStringVector split = StringSplit( Command, " " );
if( split.size() > 0 )
{
if( split[0].compare( "help" ) == 0 )

View File

@ -21,17 +21,18 @@
#include <psapi.h>
#else
#include <sys/resource.h>
#include <memory> // auto_ptr
#endif
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
cWebAdmin * WebAdmin = 0;
cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
: m_Port( a_Port )
, m_bConnected( false )
@ -75,12 +76,16 @@ void cWebAdmin::RemovePlugin( cWebPlugin* a_Plugin )
m_Plugins.remove( a_Plugin );
}
void cWebAdmin::Request_Handler(webserver::http_request* r)
{
if( WebAdmin == 0 ) return;
LOG("Path: %s", r->path_.c_str() );
std::vector< std::string > Split = StringSplit( r->path_, "/" );
AStringVector Split = StringSplit( r->path_, "/" );
if(r->path_ == "/")
{

View File

@ -8,6 +8,15 @@
#include "cWebAdmin.h"
extern bool report_errors(lua_State* lua, int status);
static std::string SafeString( const std::string& a_String )
{
std::string RetVal;
@ -24,8 +33,8 @@ static std::string SafeString( const std::string& a_String )
}
extern bool report_errors(lua_State* lua, int status);
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
struct cWebPlugin_Lua::sWebPluginTab
{
@ -130,7 +139,7 @@ void cWebPlugin_Lua::Initialize()
std::pair< std::string, std::string > cWebPlugin_Lua::GetTabNameForRequest( HTTPRequest* a_Request )
{
std::pair< std::string, std::string > Names;
std::vector<std::string> Split = StringSplit( a_Request->Path, "/" );
AStringVector Split = StringSplit(a_Request->Path, "/");
if( Split.size() > 1 )
{