Use this class to read configuration settings. More...
#include <XCongo.h>
Public Member Functions | |||||
| XCongo () | |||||
| Call Init method to to make initalize object. | |||||
| virtual | ~XCongo () | ||||
| bool | Init () | ||||
| Error prone initialization. | |||||
| bool | SizeOf (const char *rep_name, size_t *val) | ||||
| Size of an resource array. | |||||
| bool | Load (const char *filename, const char *app_name) | ||||
| Load configuration settings from file. | |||||
| XCongo () | |||||
| Call Init method to to make initalize object. | |||||
| virtual | ~XCongo () | ||||
| bool | Init () | ||||
| Error prone initialization. | |||||
| bool | SizeOf (const char *rep_name, size_t *val) | ||||
| Size of an resource array. | |||||
| bool | Load (const char *filename, const char *app_name) | ||||
| Load configuration settings from file. | |||||
Global Variables Accessors All methods in this group has the same first argument and return value
| |||||
| bool | IntValue (const char *name, int *val) | ||||
| bool | DoubleValue (const char *name, double *val) | ||||
| bool | StringValue (const char *name, char **val) | ||||
Resource oriented methods: These methods are very similar to Global Variables Accessors methods, except thay add resource name and id. These methods are used to read an array of objects in simplified form: {.cpp}
IntValue("Quads", 3, "mColor.mRed", value)
will have the same effects as {.cpp}
IntValue("Quads[3].mColor.mRed", value)
Another way to access resources and hide parent path is to use XRes class.
| |||||
| bool | IntValue (const char *rep_name, int res_id, const char *name, int *val) | ||||
| bool | StringValue (const char *rep_name, int res_id, const char *name, char **val) | ||||
| bool | DoubleValue (const char *rep_name, int res_id, const char *name, double *val) | ||||
Global Variables Accessors All methods in this group has the same first argument and return value
| |||||
| bool | IntValue (const char *name, int *val) | ||||
| bool | DoubleValue (const char *name, double *val) | ||||
| bool | StringValue (const char *name, char **val) | ||||
Resource oriented methods: These methods are very similar to Global Variables Accessors methods, except thay add resource name and id. These methods are used to read an array of objects in simplified form: {.cpp}
IntValue("Quads", 3, "mColor.mRed", value)
will have the same effects as {.cpp}
IntValue("Quads[3].mColor.mRed", value)
Another way to access resources and hide parent path is to use XRes class.
| |||||
| bool | IntValue (const char *rep_name, int res_id, const char *name, int *val) | ||||
| bool | StringValue (const char *rep_name, int res_id, const char *name, char **val) | ||||
| bool | DoubleValue (const char *rep_name, int res_id, const char *name, double *val) | ||||
Static Public Attributes | |||||
| static const int | ID_STATIC = -1 | ||||
| Set this to ignore resource id in resource oriented methods. | |||||
Private Member Functions | |||||
| bool | AssignToTmp (const char *value) | ||||
| Helper function. Assign value to _tmp global lua variable. | |||||
| bool | AssignToTmp (const char *value) | ||||
| Helper function. Assign value to _tmp global lua variable. | |||||
Private Attributes | |||||
| lua_State * | mpLuaState | ||||
| Lua context. | |||||
Use this class to read configuration settings.
Currently it supports only reading from LUA configuration files. All reading methods use NAME, VALUE and optional RESOURCE ID.
NAME is global name of variable to read. Examples:
VALUE can be of type int, double and char*. In the case of char* memory for that value is allocated with malloc and user should take care for cleanup.
RESOURCE ID is integer from 0 to SizeOf(REP_NAME) including ID_STATIC. Resource ID with SizeOf function can be used as method to read array of objects from configuration files. If you want to read red component color of 3th quad you could use:
IntValue("Quads", 3, "mColor.mRed")
It is is equivalent to
IntValue("Quads[3].mColor.mRed")
| XCongo::XCongo | ( | ) |
Call Init method to to make initalize object.
Definition at line 60 of file XCongo.cpp.
: mpLuaState(0) { }
| XCongo::~XCongo | ( | ) | [virtual] |
Definition at line 65 of file XCongo.cpp.
References mpLuaState.
{
if (mpLuaState)
lua_close(mpLuaState);
}
| XCongo::XCongo | ( | ) |
Call Init method to to make initalize object.
| virtual XCongo::~XCongo | ( | ) | [virtual] |
| bool XCongo::AssignToTmp | ( | const char * | value | ) | [private] |
Helper function. Assign value to _tmp global lua variable.
| value | Value to be assigned. |
Definition at line 95 of file XCongo.cpp.
References mpLuaState.
Referenced by DoubleValue(), IntValue(), and StringValue().
{
int error;
char *cmd_buf = new char[5 + strlen(value) + 1];
sprintf(cmd_buf, "_tmp=%s", value);
error = luaL_loadbuffer(mpLuaState, cmd_buf, strlen(cmd_buf), "assignment") ||
lua_pcall(mpLuaState, 0, 0, 0);
delete[] cmd_buf;
if (error)
{
fprintf(stderr, "%s\n", lua_tostring(mpLuaState, -1));
lua_pop(mpLuaState, 1);
return false;
}
return true;
}
| bool XCongo::AssignToTmp | ( | const char * | value | ) | [private] |
Helper function. Assign value to _tmp global lua variable.
| value | Value to be assigned. |
| bool XCongo::DoubleValue | ( | const char * | name, |
| double * | val | ||
| ) |
Definition at line 130 of file XCongo.cpp.
References AssignToTmp(), and mpLuaState.
Referenced by XRes::DoubleValue(), and DoubleValue().
{
if (!AssignToTmp(name) )
return false;
lua_getglobal(mpLuaState, "_tmp");
if (!lua_isnumber(mpLuaState, -1))
{
lua_pop(mpLuaState, 1);
return false;
}
*val = lua_tonumber(mpLuaState, -1);
return true;
}
| bool XCongo::DoubleValue | ( | const char * | name, |
| double * | val | ||
| ) |
| bool XCongo::DoubleValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| double * | val | ||
| ) |
| bool XCongo::DoubleValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| double * | val | ||
| ) |
Definition at line 166 of file XCongo.cpp.
References DoubleValue(), and ID_STATIC.
{
bool ret_val;
char *cmd_buf;
if (res_id != ID_STATIC)
{
cmd_buf = new char[strlen(rep_name) + 10 + 3 + strlen(name) + 2];
sprintf(cmd_buf, "%s[%d].%s", rep_name, res_id, name);
}
else
{
cmd_buf = new char[strlen(rep_name) + 1 + strlen(name) + 2];
sprintf(cmd_buf, "%s.%s", rep_name, name);
}
ret_val = DoubleValue(cmd_buf, val);
delete[] cmd_buf;
return ret_val;
}
| bool XCongo::Init | ( | ) |
Error prone initialization.
| bool XCongo::Init | ( | ) |
Error prone initialization.
Read needed lua libraries into context.
Definition at line 71 of file XCongo.cpp.
References mpLuaState.
{
mpLuaState = lua_open();
luaopen_base(mpLuaState);
luaopen_math(mpLuaState);
return true;
}
| bool XCongo::IntValue | ( | const char * | name, |
| int * | val | ||
| ) |
Definition at line 114 of file XCongo.cpp.
References AssignToTmp(), and mpLuaState.
Referenced by XRes::IntValue(), IntValue(), and SizeOf().
{
if (!AssignToTmp(name) )
return false;
lua_getglobal(mpLuaState, "_tmp");
if (!lua_isnumber(mpLuaState, -1))
{
lua_pop(mpLuaState, 1);
return false;
}
*val = (int)lua_tonumber(mpLuaState, -1);
return true;
}
| bool XCongo::IntValue | ( | const char * | name, |
| int * | val | ||
| ) |
| bool XCongo::IntValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| int * | val | ||
| ) |
Definition at line 145 of file XCongo.cpp.
References ID_STATIC, and IntValue().
{
bool ret_val;
char *cmd_buf;
if (res_id != ID_STATIC)
{
cmd_buf = new char[strlen(rep_name) + 10 + 3 + strlen(name) + 2];
sprintf(cmd_buf, "%s[%d].%s", rep_name, res_id, name);
}
else
{
cmd_buf = new char[strlen(rep_name) + 1 + strlen(name) + 2];
sprintf(cmd_buf, "%s.%s", rep_name, name);
}
ret_val = IntValue(cmd_buf, val);
delete[] cmd_buf;
return ret_val;
}
| bool XCongo::IntValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| int * | val | ||
| ) |
| bool XCongo::Load | ( | const char * | filename, |
| const char * | app_name | ||
| ) |
Load configuration settings from file.
Currently only lua files are supported.
| filename | lua file name |
| app_name | used to create full path to data directory |
Definition at line 82 of file XCongo.cpp.
References mluaL_loadfile(), and mpLuaState.
{
if (mluaL_loadfile(mpLuaState, app_name, fileName) || lua_pcall(mpLuaState, 0, 0, 0))
{
fprintf(stderr, "error: loading %s: %s\n",
fileName,
lua_tostring(mpLuaState, -1));
return false;
}
return true;
}
| bool XCongo::Load | ( | const char * | filename, |
| const char * | app_name | ||
| ) |
Load configuration settings from file.
Currently only lua files are supported.
| filename | lua file name |
| app_name | used to create full path to data directory |
| bool XCongo::SizeOf | ( | const char * | rep_name, |
| size_t * | val | ||
| ) |
Size of an resource array.
Use this function to iterate throug resource array in cooperation with Resource oriented methods: IntValue, StringValue and DoubleValue
| rep_name | Resource array path |
| val | size of array to be set |
Definition at line 206 of file XCongo.cpp.
References IntValue().
{
bool ret_val;
int ival;
char *cmd_buf = new char[1 + strlen(rep_name) + 1];
sprintf(cmd_buf, "#%s", rep_name);
ret_val = IntValue(cmd_buf, &ival);
*val = ival;
delete[] cmd_buf;
return ret_val;
}
| bool XCongo::SizeOf | ( | const char * | rep_name, |
| size_t * | val | ||
| ) |
Size of an resource array.
Use this function to iterate throug resource array in cooperation with Resource oriented methods: IntValue, StringValue and DoubleValue
| rep_name | Resource array path |
| val | size of array to be set |
| bool XCongo::StringValue | ( | const char * | name, |
| char ** | val | ||
| ) |
| bool XCongo::StringValue | ( | const char * | name, |
| char ** | val | ||
| ) |
Definition at line 220 of file XCongo.cpp.
References AssignToTmp(), mpLuaState, and s.
Referenced by XRes::StringValue(), and StringValue().
{
if (!AssignToTmp(name) )
return false;
lua_getglobal(mpLuaState, "_tmp");
if (!lua_isstring(mpLuaState, -1) )
{
lua_pop(mpLuaState, 1);
return false;
}
const char *s = lua_tostring(mpLuaState, -1);
size_t str_len = strlen(s);
*val = new char[str_len + 1];
memcpy(*val, s, str_len + 1);
return true;
}
| bool XCongo::StringValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| char ** | val | ||
| ) |
| bool XCongo::StringValue | ( | const char * | rep_name, |
| int | res_id, | ||
| const char * | name, | ||
| char ** | val | ||
| ) |
Definition at line 186 of file XCongo.cpp.
References ID_STATIC, and StringValue().
{
bool ret_val;
char *cmd_buf;
if (res_id != ID_STATIC)
{
cmd_buf = new char[strlen(rep_name) + 10 + 3 + strlen(name) + 2];
sprintf(cmd_buf, "%s[%d].%s", rep_name, res_id, name);
}
else
{
cmd_buf = new char[strlen(rep_name) + 1 + strlen(name) + 2];
sprintf(cmd_buf, "%s.%s", rep_name, name);
}
ret_val = StringValue(cmd_buf, val);
delete[] cmd_buf;
return ret_val;
}
static const int XCongo::ID_STATIC = -1 [static] |
Set this to ignore resource id in resource oriented methods.
Definition at line 47 of file XCongo.h.
Referenced by DoubleValue(), IntValue(), and StringValue().
lua_State * XCongo::mpLuaState [private] |
Lua context.
Definition at line 167 of file XCongo.h.
Referenced by AssignToTmp(), DoubleValue(), Init(), IntValue(), Load(), StringValue(), and ~XCongo().
1.8.0