My Project
Static Public Member Functions | List of all members
NPL::NPLHelper Class Reference

a collection of helper functions. More...

#include <NPLHelper.h>

Static Public Member Functions

static void DevideString (const string &input, string &str1, string &str2, char separator=';')
 
static bool IsSCodePureData (const char *sCode, int nCodeSize=-1)
 verify the script code. More...
 
static bool IsPureData (const char *sCode, int nCodeSize=-1)
 it will return true if input string is "false", "true", NUMBER, STRING, and {table} More...
 
static bool IsPureTable (const char *sCode, int nCodeSize=-1)
 it will return true if input string is a {table} containing only "false", "true", NUMBER, STRING, and other such {table} More...
 
template<typename StringType >
static bool SerializeToSCode (const char *sStorageVar, const luabind::object &input, StringType &sCode, int nCodeOffset=0, STableStack *pRecursionTable=NULL, bool sort=false)
 serialize a luabind object into sCode. More...
 
template<typename StringType >
static bool SerializeToJson (const luabind::object &input, StringType &sCode, int nCodeOffset=0, STableStack *pRecursionTable=NULL, bool bUseEmptyArray=false)
 
static bool isControlCharacter (char ch)
 
static bool containsControlCharacter (const char *str)
 
static const char * LuaObjectToString (const luabind::object &input, int *pSize=NULL)
 safe convert the lua object to string. More...
 
static bool LuaObjectToString (const luabind::object &input, string &output)
 safe convert the lua object to string. More...
 
static int LuaObjectToInt (const luabind::object &input, int default_value=0)
 safe convert the lua object to int
 
static float LuaObjectToFloat (const luabind::object &input, float default_value=0.f)
 safe convert the lua object to float
 
static bool LuaObjectIsTable (const luabind::object &input)
 
static double LuaObjectToDouble (const luabind::object &input, double default_value=0.)
 safe convert the lua object to double
 
static bool LuaObjectToBool (const luabind::object &input, bool default_value=false)
 safe convert the lua object to boolean
 
static DWORD LuaObjectToColor (const luabind::object &pen, DWORD default_value=0)
 safe convert the lua object to color
 
static bool StringToLuaObject (const char *input, int nLen, luabind::object &output, lua_State *pState=NULL)
 Currently it will only convert pure data table from string to object. More...
 
static bool StringToAnyObject (const char *input, int nLen, luabind::object &output, lua_State *pState=NULL)
 
static bool MsgStringToLuaObject (const char *input, int nLen, lua_State *pState)
 the input must be a string that begins with "msg={", msg={name=value, ...} it will replace the global msg table with a new table.
 
static NPLObjectProxy StringToNPLTable (const char *input, int nLen=-1)
 converting string to NPL table object More...
 
static NPLObjectProxy MsgStringToNPLTable (const char *input, int nLen=-1)
 same as StringToNPLTable(), except that it begins with "msg={...}" More...
 
static bool LuaObjectToNPLObject (const luabind::object &input, NPLObjectProxy &out)
 lua object to npl object. More...
 
template<typename StringType >
static bool SerializeNPLTableToString (const char *sStorageVar, NPLObjectProxy &input, StringType &sCode, int nCodeOffset=0)
 safe convert the lua object to string. More...
 
static bool NPLTableToString (const char *sStorageVar, NPLObjectProxy &input, std::string &sCode, int nCodeOffset=0)
 same as SerializeNPLTableToString. More...
 
template<typename StringType >
static void EncodeStringInQuotation (StringType &output, int nOutputOffset, const char *input, int nInputSize)
 this is something like string.format("%q") in NPL. More...
 
template<typename StringType >
static void EncodeStringInQuotation (StringType &output, int nOutputOffset, const std::string &input)
 
template<typename StringType >
static void EncodeStringInQuotation (StringType &output, int nOutputOffset, const char *input)
 
template<typename StringType >
static void EncodeJsonStringInQuotation (StringType &output, int nOutputOffset, const char *input, int nInputSize)
 
template<typename StringType >
static void EncodeJsonStringInQuotation (StringType &output, int nOutputOffset, const std::string &input)
 
template<typename StringType >
static void EncodeJsonStringInQuotation (StringType &output, int nOutputOffset, const char *input)
 
static bool CanEncodeStringInDoubleBrackets (const char *buffer, int nLength)
 this function is thread-safe. More...
 

Detailed Description

a collection of helper functions.

Member Function Documentation

§ CanEncodeStringInDoubleBrackets()

bool NPL::NPLHelper::CanEncodeStringInDoubleBrackets ( const char *  buffer,
int  nLength 
)
static

this function is thread-safe.

Check if the given string can be encoded using lua long string, i.e. [[]] json string usually can not be encoded in double brackets.

§ EncodeStringInQuotation()

template<typename StringType >
void NPL::NPLHelper::EncodeStringInQuotation ( StringType &  output,
int  nOutputOffset,
const char *  input,
int  nInputSize 
)
static

this is something like string.format("%q") in NPL.

[thread-safe] replace quotation mark in string. e.g. "\" will be converted to "\"

Parameters
outputinto which buffer to write the output
inputthe input string to be encoded
nOutputOffsetthe output will be written to output[nOutputOffset]. default to 0,which is the beginning
nInputSizeif -1, strlen is used on buffer

§ IsPureData()

bool NPLHelper::IsPureData ( const char *  sCode,
int  nCodeSize = -1 
)
static

it will return true if input string is "false", "true", NUMBER, STRING, and {table}

Parameters
sCodethe input string
nCodeSizenumber of bytes in string. if -1, strlen() is used to determine the size.

§ IsPureTable()

bool NPL::NPLHelper::IsPureTable ( const char *  sCode,
int  nCodeSize = -1 
)
static

it will return true if input string is a {table} containing only "false", "true", NUMBER, STRING, and other such {table}

Parameters
sCodethe input string
nCodeSizenumber of bytes in string. if -1, strlen() is used to determine the size.

§ IsSCodePureData()

bool NPLHelper::IsSCodePureData ( const char *  sCode,
int  nCodeSize = -1 
)
static

verify the script code.

it returns true if the script code contains pure msg data or table. this function is used to verify scode received from the network. So that the execution of a pure data in the local runtime is harmless.

Parameters
sCodethe input string
nCodeSizenumber of bytes in string. if -1, strlen() is used to determine the size.

§ LuaObjectToNPLObject()

bool NPL::NPLHelper::LuaObjectToNPLObject ( const luabind::object input,
NPLObjectProxy out 
)
static

lua object to npl object.

§ LuaObjectToString() [1/2]

const char * NPLHelper::LuaObjectToString ( const luabind::object input,
int *  pSize = NULL 
)
static

safe convert the lua object to string.

if the input is nil, NULL is returned. please note that the returned const char* has the same lifetime as the input object

§ LuaObjectToString() [2/2]

bool NPLHelper::LuaObjectToString ( const luabind::object input,
string &  output 
)
static

safe convert the lua object to string.

if the input is nil, output is not assigned. return true if input is a string object and value is written to output.

§ MsgStringToNPLTable()

NPLObjectProxy NPL::NPLHelper::MsgStringToNPLTable ( const char *  input,
int  nLen = -1 
)
static

same as StringToNPLTable(), except that it begins with "msg={...}"

Parameters
inputsuch as "msg={nid=10, name=\"value", tab={name1="value1"}}"

§ NPLTableToString()

bool NPLHelper::NPLTableToString ( const char *  sStorageVar,
NPLObjectProxy input,
std::string &  sCode,
int  nCodeOffset = 0 
)
static

same as SerializeNPLTableToString.

Except that it is used via DLL interface.

§ SerializeNPLTableToString()

template<typename StringType >
template bool NPL::NPLHelper::SerializeNPLTableToString ( const char *  sStorageVar,
NPLObjectProxy input,
StringType &  sCode,
int  nCodeOffset = 0 
)
static

safe convert the lua object to string.

if the input is nil, output is not assigned. return true if input is a string object and value is written to output.

Parameters
bUseMsgif true, the table will be inside msg={...}, it default to false.

§ SerializeToSCode()

template<typename StringType >
template bool NPL::NPLHelper::SerializeToSCode ( const char *  sStorageVar,
const luabind::object input,
StringType &  sCode,
int  nCodeOffset = 0,
STableStack pRecursionTable = NULL,
bool  sort = false 
)
static

serialize a luabind object into sCode.

The object could be a value, string or a table without nested tables. [thread safe] e.g. log(NPL.SerializeToSCode("var", {name = [[lxz" "lxz]], age = 24}));

Parameters
sStorageVarif this is NULL or "", the scode contains only the object. otherwise an assignment is made, by prefixing the scode with "sStorageVar = "
inputinput luabind object
sCodethe output scode. It can be StringBuilder or std::string. StringBuilder is the prefered way.
nCodeOffsetoffset in byte to begin writing to sCode[nCodeOffset]. Default to 0, which will write from the beginning of the sCode.
Returns
true if successful.

§ StringToLuaObject()

bool NPL::NPLHelper::StringToLuaObject ( const char *  input,
int  nLen,
luabind::object output,
lua_State pState = NULL 
)
static

Currently it will only convert pure data table from string to object.

Parameters
inputinput string such as {name=value,}
nLennumber of char in input.
outputthe table object . it needs to be an empty table object.
pStateif NULL, output.interpreter() is used.
Returns
true if succeed.

§ StringToNPLTable()

NPLObjectProxy NPL::NPLHelper::StringToNPLTable ( const char *  input,
int  nLen = -1 
)
static

converting string to NPL table object

Parameters
inputsuch as "{nid=10, name=\"value", tab={name1="value1"}}"

The documentation for this class was generated from the following files: