Quad Pang
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes
XCongo Class Reference

Use this class to read configuration settings. More...

#include <XCongo.h>

List of all members.

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

Parameters:
nameFull qualified name of the resource
valValue to be read and returned. StringValue function will allocate buffer with new[]. Call delete[] on this variable when value is longer needed for cleanup.
Returns:
true on success, false on failure
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.

Parameters:
rep_nameResource array path
res_idResource index. If set to XCongo::ID_STATIC index will be ignored and name will be made in form <rep_name>.<name>.
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

Parameters:
nameFull qualified name of the resource
valValue to be read and returned. StringValue function will allocate buffer with new[]. Call delete[] on this variable when value is longer needed for cleanup.
Returns:
true on success, false on failure
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.

Parameters:
rep_nameResource array path
res_idResource index. If set to XCongo::ID_STATIC index will be ignored and name will be made in form <rep_name>.<name>.
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.

Detailed Description

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")

See also:
XRes for convinent way of reading object properties.
XCongo.cpp for implementation details.

Definition at line 39 of file XCongo.h.


Constructor & Destructor Documentation

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);
}

Call Init method to to make initalize object.

virtual XCongo::~XCongo ( ) [virtual]

Member Function Documentation

bool XCongo::AssignToTmp ( const char *  value) [private]

Helper function. Assign value to _tmp global lua variable.

Parameters:
valueValue to be assigned.
Returns:
true if ok, false on error.

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.

Parameters:
valueValue to be assigned.
Returns:
true if ok, false on error.
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.

Returns:
true if ok false on error.
bool XCongo::Init ( )

Error prone initialization.

Returns:
true if ok false on error.

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.

Parameters:
filenamelua file name
app_nameused to create full path to data directory
Returns:
true if OK, false if error

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.

Parameters:
filenamelua file name
app_nameused to create full path to data directory
Returns:
true if OK, false if error
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

Parameters:
rep_nameResource array path
valsize of array to be set
Returns:
true on sucess, false on failure

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

Parameters:
rep_nameResource array path
valsize of array to be set
Returns:
true on sucess, false on failure
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;
}

Member Data Documentation

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().


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