Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

LuaState Class Reference

#include <LuaState.h>

Inheritance diagram for LuaState::

LuaStateOwner List of all members.

Public Types

enum  { NOREF = LUA_NOREF }
enum  { REFNIL = LUA_REFNIL }

Public Methods

 LuaState (lua_State *state)
 ~LuaState ()
LuaObject GetMethods (int index)
LuaObject GetDefaultMethods (int type)
void SetMethods (int index)
int GetTop ()
void SetTop (int index)
void PushValue (int index)
void PushValue (LuaObject object)
void RemoveStack (int index)
void InsertStack (int index)
int StackSpace ()
const lua_charType (int index)
int IsNumber (int index)
int IsString (int index)
int IsUString (int index)
int IsCFunction (int index)
bool IsFunction (int index)
bool IsTable (int index)
bool IsUserData (int index)
bool IsNil (int index)
bool IsNull (int index)
bool IsPointer (int index) const
int Tag (int index)
int RawTag (int index)
int Equal (int index1, int index2)
int LessThan (int index1, int index2)
lua_Number ToNumber (int index)
int ToInteger (int index)
const lua_charToString (int index)
const wchar_t * ToUString (int index)
size_t StrLen (int index)
lua_CFunction ToCFunction (int index)
void * ToUserData (int index)
const void * ToPointer (int index)
const void * ToLuaPointer (int index)
void PushBool (bool value)
void PushNil ()
void PushNumber (double n)
void PushLString (const char *s, size_t len)
void PushString (const char *s)
void PushLUString (const wchar_t *s, size_t len)
void PushUString (const wchar_t *s)
void PushCClosure (lua_CFunction fn, int n)
void PushCClosure (LuaStateCFunction fn, int n)
void PushPointer (const void *p)
LuaObject GetGlobal (const char *name)
void GetTable (int index)
void RawGet (int index)
void RawGetI (int index, int n)
LuaObject GetGlobals ()
void GetTagMethod (int tag, const char *event)
LuaObject GetRef (int ref)
LuaObject NewTable (int size=0)
void GetRegistry ()
void GetWeakRegistry ()
void SetGlobal (const char *name)
void SetTable (int index)
void RawSet (int index)
void RawSetI (int index, int n)
void SetGlobals ()
void SetTagMethod (int tag, const char *event)
int Ref (int lock)
int Call (int nargs, int nresults)
void RawCall (int nargs, int nresults)
int LoadFile (const lua_char *filename)
int DoFile (const char *filename)
int DoString (const char *str)
int LoadBuffer (const lua_char *buff, size_t size, const lua_char *name)
int DoBuffer (const char *buff, size_t size, const char *name)
int GetGCThreshold ()
int GetGCCount ()
void SetGCThreshold (int newthreshold)
int NewType (const lua_char *name, int basictype)
int CopyTagMethods (int tagto, int tagfrom)
void SetTag (int tag)
int Name2Tag (const lua_char *name)
const lua_charTag2Name (int tag)
void Error (const lua_char *errorStr)
void Unref (int ref)
int Next (int index)
int GetN (int index)
void Concat (int n)
void PushUserData (void *u)
void * NewUserData (size_t size)
void NewUserDataBox (void *u)
void SetWeakMode (int mode)
int GetWeakMode (int index)
void Pop ()
void Pop (int amount)
void Register (const char *funcName, lua_CFunction function)
void Register (const char *funcName, LuaStateCFunction function)
void Unregister (const char *funcName)
void Register (const LuaFunctionList *functionList)
void Unregister (const LuaFunctionList *functionList)
void PushCFunction (lua_CFunction f)
void PushCFunction (LuaStateCFunction f)
int CloneTag (int t)
void CollectGarbage ()
void SetNilGCMethod (LuaStateCFunction f)
int ConfigGetInteger (const char *section, const char *entry, int defaultValue=0)
float ConfigGetReal (const char *section, const char *entry, double defaultValue=0.0)
const char * ConfigGetString (const char *section, const char *entry, const char *defaultValue="")
void ConfigSetInteger (const char *section, const char *entry, int value)
void ConfigSetReal (const char *section, const char *entry, double value)
void ConfigSetString (const char *section, const char *entry, const char *value)
void WriteLuaGlobalsFile (const char *filename, bool writeAll=false, bool alphabetical=false, bool writeTablePointers=false, unsigned int maxIndentLevel=0xffffffff)
void WriteLuaFile (const char *filename, const char *name, LuaObject value, int indentLevel, bool writeAll=false, bool alphabetical=false, bool writeTablePointers=false, unsigned int maxIndentLevel=0xffffffff)
bool WriteLuaObject (LuaStateOutFile &file, const char *name, LuaObject value, int indentLevel, bool writeAll=false, bool alphabetical=false, bool writeTablePointers=false, unsigned int maxIndentLevel=0xffffffff)
bool WriteLuaObject (FILE *file, const char *name, LuaObject value, int indentLevel, bool writeAll=false, bool alphabetical=false, bool writeTablePointers=false, unsigned int maxIndentLevel=0xffffffff)
 operator lua_State * ()

Public Attributes

lua_Statem_state

Protected Methods

 LuaState ()

Detailed Description

A lightweight lua_State wrapper.

#include "LuaState.h"

static int LS_PrintNumber(LuaState state)
{
    // Retrieve the number passed on the stack.
    LuaObject numberObj(state, 1);

    // Verify it is a number and print it.
    if (numberObj.IsNumber())
        printf("%f\n", numberObj.GetNumber());

    // No return values.
    return 0;
}


static int LS_Add(lua_State* L)
{
    // Using a special LuaState constructor, we can wrap the lua_State pointer
    // very conveniently.
    LuaState state(L);

    // Retrieve the numbers passed on the stack.
    LuaObject number1Obj(state, 1);
    LuaObject number2Obj(state, 2);

    // Verify it is a number and print it.
    if (number1Obj.IsNumber()  &&  number2Obj.IsNumber())
    {
        state.PushNumber(number1Obj.GetNumber() + number2Obj.GetNumber());
    }
    else
    {
        state.PushNumber(0.0);
    }

    // 1 return value.
    return 1;
}


void DoScriptIniWriteTest()
{
    LuaStateOwner state(false);

    state.ConfigSetReal("Environment", "WindSpeed", 55.0);
    state.ConfigSetString("Environment", "TypeStr", "Overcast");
    state.ConfigSetInteger("AnotherGroup", "IntegerValue", 1);

    state.WriteLuaGlobalsFile("ScriptIniTest.dmp");
}


void DoScriptIniReadTest()
{
    LuaStateOwner state;

    state.DoFile("ScriptIniTest.dmp");

    float windSpeed = state.ConfigGetReal("Environment", "WindSpeed", 0.0);
    const char* typeStr = state.ConfigGetString("Environment", "TypeStr", "Clear");
    int integerValue = state.ConfigGetInteger("AnotherGroup", "IntegerValue");
}


void DoScriptCallbackTest()
{
    LuaStateOwner state;

    state.Register("PrintNumber", LS_PrintNumber);
    state.Register("Add", LS_Add);

    state.DoFile("ScriptCallbackTest.lua");
}


void DoScriptSaveTest()
{
    LuaStateOwner state;

    state.DoFile("ScriptSaveTest.lua");
    state.WriteLuaGlobalsFile("ScriptSaveTest.dmp");
}


void DoScriptArrayTest()
{
    LuaStateOwner state;
    state.DoFile("ScriptArrayTest.lua");
    LuaObject testTableObj = state.GetGlobals().GetByName("TestArray");
    // or                           state.GetGlobal("TestArray");
    for (int i = 1; ; ++i)
    {
        LuaAutoBlock block(state);  // Automatic stack fixups.
        LuaObject entryObj = testTableObj.GetByIndex(i);
        if (entryObj.IsNil())
            break;
        if (entryObj.IsNumber())        // IsNumber() must ALWAYS come first.
            printf("%f\n", entryObj.GetNumber());
        else if (entryObj.IsString())   // IsString() returns true for a number or string.
            printf("%s\n", entryObj.GetString());
    }
}


static int LS_PointerCall(LuaState state)
{
    bool isPointer = state.IsPointer(1);
    const void* ptr = state.ToPointer(1);
    return 0;
}


void TestPointer()
{
    LuaStateOwner state;
    state.Register("PointerCall", LS_PointerCall);
    state.GetGlobal("PointerCall");
    state.PushPointer((const void*)0xfedcba98);
    state.Call(1, 0);
}


int __cdecl main(int argc, char* argv[])
{
    DoScriptIniWriteTest();
    DoScriptIniReadTest();
    DoScriptCallbackTest();
    DoScriptSaveTest();
    DoScriptArrayTest();
    TestPointer();

    return 0;
}

Definition at line 1049 of file LuaState.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
NOREF 

Definition at line 1053 of file LuaState.h.

anonymous enum
 

Enumeration values:
REFNIL 

Definition at line 1054 of file LuaState.h.


Constructor & Destructor Documentation

LuaState::LuaState lua_State   state [inline]
 

Constructor.

Definition at line 1336 of file LuaState.h.

LuaState::~LuaState   [inline]
 

Definition at line 1057 of file LuaState.h.

LuaState::LuaState   [inline, protected]
 

Definition at line 1264 of file LuaState.h.


Member Function Documentation

int LuaState::Call int    nargs,
int    nresults
[inline]
 

Definition at line 1134 of file LuaState.h.

int LuaState::CloneTag int    t [inline]
 

Definition at line 1224 of file LuaState.h.

void LuaState::CollectGarbage   [inline]
 

Definition at line 1226 of file LuaState.h.

void LuaState::Concat int    n [inline]
 

Definition at line 1168 of file LuaState.h.

int LuaState::ConfigGetInteger const char *    section,
const char *    entry,
int    defaultValue = 0
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

Definition at line 231 of file LuaState.cpp.

00235 {

float LuaState::ConfigGetReal const char *    section,
const char *    entry,
double    defaultValue = 0.0
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

Definition at line 242 of file LuaState.cpp.

00252 {
00253     LuaAutoBlock block(*this);
00254 

const char * LuaState::ConfigGetString const char *    section,
const char *    entry,
const char *    defaultValue = ""
 

Returns:
Retrieves the value at [section].[entry]. If either [section] or [entry] doesn't exist, [defaultValue] is returned.

Definition at line 261 of file LuaState.cpp.

00261     {
00262         sectionTable = GetGlobals().CreateTable(section);
00263     }
00264 
00265     sectionTable.SetNumber(entry, value);
00266 }
00267 
00268 
00272 void LuaState::ConfigSetReal(const char* section, const char* entry, double value)
00273 {

void LuaState::ConfigSetInteger const char *    section,
const char *    entry,
int    value
 

Assigns [value] to [section].[entry].

Definition at line 279 of file LuaState.cpp.

00282     {
00283         sectionTable = GetGlobals().CreateTable(section);
00284     }
00285 
00286     sectionTable.SetNumber(entry, value);
00287 }
00288 
00289 
00293 void LuaState::ConfigSetString(const char* section, const char* entry, const char* value)
00294 {

void LuaState::ConfigSetReal const char *    section,
const char *    entry,
double    value
 

Assigns [value] to [section].[entry].

Definition at line 300 of file LuaState.cpp.

00303     {
00304         sectionTable = GetGlobals().CreateTable(section);
00305     }
00306 
00307     sectionTable.SetString(entry, value);
00308 }
00309 
00310 
00314 static void IndentFile(LuaStateOutFile& file, unsigned int indentLevel)
00315 {

void LuaState::ConfigSetString const char *    section,
const char *    entry,
const char *    value
 

Assigns [value] to [section].[entry].

Definition at line 321 of file LuaState.cpp.

00327 {
00328     LuaStateOutFile file;
00329     file.Assign(stdioFile);
00330     WriteLuaObject(file, name, value, indentLevel, writeAll, alphabetical,
00331             writeTablePointers, maxIndentLevel);
00332     return false;
00333 }   
00334 
00335 
00336 static void luaI_addquotedbinary (LuaStateOutFile& file, lua_State *L, const char* s, int l)

int LuaState::CopyTagMethods int    tagto,
int    tagfrom
[inline]
 

Definition at line 1155 of file LuaState.h.

int LuaState::DoBuffer const char *    buff,
size_t    size,
const char *    name
[inline]
 

Definition at line 1143 of file LuaState.h.

int LuaState::DoFile const char *    filename [inline]
 

Definition at line 1137 of file LuaState.h.

int LuaState::DoString const char *    str [inline]
 

Definition at line 1138 of file LuaState.h.

int LuaState::Equal int    index1,
int    index2
[inline]
 

Definition at line 1087 of file LuaState.h.

void LuaState::Error const lua_char   errorStr [inline]
 

Definition at line 1161 of file LuaState.h.

LuaObject LuaState::GetDefaultMethods int    type [inline]
 

Definition at line 1060 of file LuaState.h.

int LuaState::GetGCCount   [inline]
 

Definition at line 1150 of file LuaState.h.

int LuaState::GetGCThreshold   [inline]
 

Definition at line 1149 of file LuaState.h.

LuaObject LuaState::GetGlobal const char *    name [inline]
 

Definition at line 1113 of file LuaState.h.

LuaObject LuaState::GetGlobals   [inline]
 

Definition at line 1117 of file LuaState.h.

Referenced by ConfigGetString(), ConfigSetInteger(), and ConfigSetReal().

LuaObject LuaState::GetMethods int    index [inline]
 

Definition at line 1059 of file LuaState.h.

int LuaState::GetN int    index [inline]
 

Definition at line 1166 of file LuaState.h.

LuaObject LuaState::GetRef int    ref [inline]
 

Definition at line 1119 of file LuaState.h.

void LuaState::GetRegistry   [inline]
 

Definition at line 1121 of file LuaState.h.

void LuaState::GetTable int    index [inline]
 

Definition at line 1114 of file LuaState.h.

void LuaState::GetTagMethod int    tag,
const char *    event
[inline]
 

Definition at line 1118 of file LuaState.h.

int LuaState::GetTop   [inline]
 

Definition at line 1064 of file LuaState.h.

int LuaState::GetWeakMode int    index [inline]
 

Definition at line 1175 of file LuaState.h.

void LuaState::GetWeakRegistry   [inline]
 

Definition at line 1122 of file LuaState.h.

void LuaState::InsertStack int    index [inline]
 

Definition at line 1069 of file LuaState.h.

int LuaState::IsCFunction int    index [inline]
 

Definition at line 1077 of file LuaState.h.

bool LuaState::IsFunction int    index [inline]
 

Definition at line 1078 of file LuaState.h.

bool LuaState::IsNil int    index [inline]
 

Definition at line 1081 of file LuaState.h.

bool LuaState::IsNull int    index [inline]
 

Definition at line 1082 of file LuaState.h.

int LuaState::IsNumber int    index [inline]
 

Definition at line 1074 of file LuaState.h.

bool LuaState::IsPointer int    index const [inline]
 

Definition at line 1083 of file LuaState.h.

int LuaState::IsString int    index [inline]
 

Definition at line 1075 of file LuaState.h.

bool LuaState::IsTable int    index [inline]
 

Definition at line 1079 of file LuaState.h.

int LuaState::IsUString int    index [inline]
 

Definition at line 1076 of file LuaState.h.

bool LuaState::IsUserData int    index [inline]
 

Definition at line 1080 of file LuaState.h.

int LuaState::LessThan int    index1,
int    index2
[inline]
 

Definition at line 1088 of file LuaState.h.

int LuaState::LoadBuffer const lua_char   buff,
size_t    size,
const lua_char   name
[inline]
 

Definition at line 1139 of file LuaState.h.

int LuaState::LoadFile const lua_char   filename [inline]
 

Definition at line 1136 of file LuaState.h.

int LuaState::Name2Tag const lua_char   name [inline]
 

Definition at line 1158 of file LuaState.h.

LuaObject LuaState::NewTable int    size = 0 [inline]
 

Definition at line 1120 of file LuaState.h.

int LuaState::NewType const lua_char   name,
int    basictype
[inline]
 

Definition at line 1154 of file LuaState.h.

void* LuaState::NewUserData size_t    size [inline]
 

Definition at line 1171 of file LuaState.h.

void LuaState::NewUserDataBox void *    u [inline]
 

Definition at line 1172 of file LuaState.h.

int LuaState::Next int    index [inline]
 

Definition at line 1165 of file LuaState.h.

void LuaState::Pop int    amount [inline]
 

Definition at line 1180 of file LuaState.h.

void LuaState::Pop   [inline]
 

Definition at line 1179 of file LuaState.h.

void LuaState::PushBool bool    value [inline]
 

Definition at line 1101 of file LuaState.h.

Referenced by LS_WriteLuaFile(), and LS_WriteLuaObject().

void LuaState::PushCClosure LuaStateCFunction    fn,
int    n
[inline]
 

Definition at line 1109 of file LuaState.h.

void LuaState::PushCClosure lua_CFunction    fn,
int    n
[inline]
 

Definition at line 1108 of file LuaState.h.

void LuaState::PushCFunction LuaStateCFunction    f [inline]
 

Definition at line 1223 of file LuaState.h.

void LuaState::PushCFunction lua_CFunction    f [inline]
 

Definition at line 1222 of file LuaState.h.

void LuaState::PushLString const char *    s,
size_t    len
[inline]
 

Definition at line 1104 of file LuaState.h.

void LuaState::PushLUString const wchar_t *    s,
size_t    len
[inline]
 

Definition at line 1106 of file LuaState.h.

void LuaState::PushNil   [inline]
 

Definition at line 1102 of file LuaState.h.

void LuaState::PushNumber double    n [inline]
 

Definition at line 1103 of file LuaState.h.

void LuaState::PushPointer const void *    p [inline]
 

Definition at line 1110 of file LuaState.h.

void LuaState::PushString const char *    s [inline]
 

Definition at line 1105 of file LuaState.h.

void LuaState::PushUString const wchar_t *    s [inline]
 

Definition at line 1107 of file LuaState.h.

void LuaState::PushUserData void *    u
 

void LuaState::PushValue LuaObject    object [inline]
 

Definition at line 1067 of file LuaState.h.

void LuaState::PushValue int    index [inline]
 

Definition at line 1066 of file LuaState.h.

void LuaState::RawCall int    nargs,
int    nresults
[inline]
 

Definition at line 1135 of file LuaState.h.

void LuaState::RawGet int    index [inline]
 

Definition at line 1115 of file LuaState.h.

void LuaState::RawGetI int    index,
int    n
[inline]
 

Definition at line 1116 of file LuaState.h.

void LuaState::RawSet int    index [inline]
 

Definition at line 1127 of file LuaState.h.

void LuaState::RawSetI int    index,
int    n
[inline]
 

Definition at line 1128 of file LuaState.h.

int LuaState::RawTag int    index [inline]
 

Definition at line 1085 of file LuaState.h.

int LuaState::Ref int    lock [inline]
 

Definition at line 1131 of file LuaState.h.

void LuaState::Register const LuaFunctionList   functionList [inline]
 

Definition at line 1197 of file LuaState.h.

void LuaState::Register const char *    funcName,
LuaStateCFunction    function
[inline]
 

Definition at line 1183 of file LuaState.h.

void LuaState::Register const char *    funcName,
lua_CFunction    function
[inline]
 

Definition at line 1182 of file LuaState.h.

void LuaState::RemoveStack int    index [inline]
 

Definition at line 1068 of file LuaState.h.

void LuaState::SetGCThreshold int    newthreshold [inline]
 

Definition at line 1151 of file LuaState.h.

void LuaState::SetGlobal const char *    name [inline]
 

Definition at line 1125 of file LuaState.h.

void LuaState::SetGlobals   [inline]
 

Definition at line 1129 of file LuaState.h.

void LuaState::SetMethods int    index [inline]
 

Definition at line 1061 of file LuaState.h.

void LuaState::SetNilGCMethod LuaStateCFunction    f [inline]
 

Definition at line 1228 of file LuaState.h.

void LuaState::SetTable int    index [inline]
 

Definition at line 1126 of file LuaState.h.

void LuaState::SetTag int    tag [inline]
 

Definition at line 1156 of file LuaState.h.

void LuaState::SetTagMethod int    tag,
const char *    event
[inline]
 

Definition at line 1130 of file LuaState.h.

void LuaState::SetTop int    index [inline]
 

Definition at line 1065 of file LuaState.h.

void LuaState::SetWeakMode int    mode [inline]
 

Definition at line 1174 of file LuaState.h.

int LuaState::StackSpace   [inline]
 

Definition at line 1070 of file LuaState.h.

size_t LuaState::StrLen int    index [inline]
 

Definition at line 1094 of file LuaState.h.

int LuaState::Tag int    index [inline]
 

Definition at line 1084 of file LuaState.h.

const lua_char* LuaState::Tag2Name int    tag [inline]
 

Definition at line 1159 of file LuaState.h.

lua_CFunction LuaState::ToCFunction int    index [inline]
 

Definition at line 1095 of file LuaState.h.

int LuaState::ToInteger int    index [inline]
 

Definition at line 1091 of file LuaState.h.

const void* LuaState::ToLuaPointer int    index [inline]
 

Definition at line 1098 of file LuaState.h.

lua_Number LuaState::ToNumber int    index [inline]
 

Definition at line 1090 of file LuaState.h.

const void* LuaState::ToPointer int    index [inline]
 

Definition at line 1097 of file LuaState.h.

const lua_char* LuaState::ToString int    index [inline]
 

Definition at line 1092 of file LuaState.h.

Referenced by LS_WriteLuaFile().

const wchar_t* LuaState::ToUString int    index [inline]
 

Definition at line 1093 of file LuaState.h.

void* LuaState::ToUserData int    index [inline]
 

Definition at line 1096 of file LuaState.h.

const lua_char* LuaState::Type int    index [inline]
 

Definition at line 1073 of file LuaState.h.

void LuaState::Unref int    ref [inline]
 

Definition at line 1163 of file LuaState.h.

void LuaState::Unregister const LuaFunctionList   functionList [inline]
 

Definition at line 1210 of file LuaState.h.

void LuaState::Unregister const char *    funcName [inline]
 

Definition at line 1189 of file LuaState.h.

void LuaState::WriteLuaFile const char *    filename,
const char *    name,
LuaObject    value,
int    indentLevel,
bool    writeAll = false,
bool    alphabetical = false,
bool    writeTablePointers = false,
unsigned int    maxIndentLevel = 0xffffffff
 

Save the complete script state.

Definition at line 850 of file LuaState.cpp.

void LuaState::WriteLuaGlobalsFile const char *    filename,
bool    writeAll = false,
bool    alphabetical = false,
bool    writeTablePointers = false,
unsigned int    maxIndentLevel = 0xffffffff
 

Save the complete script state.

Definition at line 835 of file LuaState.cpp.

Referenced by LS_WriteLuaFile().

bool LuaState::WriteLuaObject FILE *    stdioFile,
const char *    name,
LuaObject    value,
int    indentLevel,
bool    writeAll = false,
bool    alphabetical = false,
bool    writeTablePointers = false,
unsigned int    maxIndentLevel = 0xffffffff
 

Definition at line 352 of file LuaState.cpp.

00352                           :     file.Print("\\v");      break;
00353             default:
00354                 if (isprint((BYTE)*s))
00355                     file.Print("%c", *s);
00356                 else
00357                 {
00358                     file.Print("\\%03d", (BYTE)*s);
00359                 }
00360         }
00361         s++;

bool LuaState::WriteLuaObject LuaStateOutFile   file,
const char *    name,
LuaObject    value,
int    indentLevel,
bool    writeAll = false,
bool    alphabetical = false,
bool    writeTablePointers = false,
unsigned int    maxIndentLevel = 0xffffffff
 

Writes a Lua object to a text file.

Definition at line 432 of file LuaState.cpp.

00438                                                              : indentLevel) * INDENT_SIZE;
00439     IndentFile(file, indentSpaces);
00440     
00441     // If the object has a name, write it out.
00442     if (name)
00443         file.Print("%s = ", name);
00444 
00445     // If the object's value is a number, write it as a number.
00446     if (value.IsNumber())
00447         file.Print("%.16g", value.GetNumber());
00448 
00449     // Or if the object's value is a string, write it as a quoted string.
00450     else if (value.IsString())
00451     {
00452         luaI_addquotedbinary(file, m_state, value.GetString(), value.StrLen());
00453     }
00454 
00455     // Or if the object's value is a string, write it as a quoted string.
00456     else if (value.IsUString())
00457     {
00458         luaI_addquotedwidebinary(file, m_state, value.GetUString(), value.StrLen());
00459     }
00460 
00461     // Otherwise, see if the object's value is a table.
00462     else if (value.IsTable())
00463     {
00464         // Write the table header.
00465         if (indentLevel != -1)
00466         {
00467             if (indentLevel < maxIndentLevel)
00468             {
00469                 file.Print("\n");
00470                 IndentFile(file, indentSpaces);
00471             }
00472             if (writeTablePointers)
00473                 file.Print("{ --%8x\n", lua_topointer(m_state, value));
00474             else
00475                 file.Print("{");
00476             if (indentLevel < maxIndentLevel)
00477             {
00478                 file.Print("\n");
00479             }
00480         }
00481 
00482         // Rename, just for ease of reading.
00483         LuaObject table = value;
00484 
00485         // upperIndex is the upper index value of a sequential numerical array
00486         // items.
00487         int upperIndex = 1;
00488         bool wroteSemi = false;
00489         bool hasSequential = false;
00490 
00491         // Block to search for array items.
00492         {
00493             // Pop the stack state when done.
00494             LuaAutoBlock block(*this);
00495 
00496             // Grab index 1 and index 2 of the table.
00497             LuaObject value1 = table.GetByIndex(1);
00498             LuaObject value2 = table.GetByIndex(2);
00499 
00500             // If they both exist, then there is a sequential list.
00501             if (!value1.IsNil()  &&  !value2.IsNil())
00502             {
00503                 // Cycle through the list.
00504                 bool firstSequential = true;
00505                 for (; ; ++upperIndex)
00506                 {
00507                     // Restore the stack state each iteration.
00508                     LuaAutoBlock block(*this);
00509 
00510                     // Try retrieving the table entry at upperIndex.
00511                     LuaObject value = table.GetByIndex(upperIndex);
00512 
00513                     // If it doesn't exist, then exit the loop.
00514                     if (value.IsNil())
00515                         break;
00516 
00517                     // Only add the comma and return if not on the first item.
00518                     if (!firstSequential  &&  indentLevel != -1)
00519                     {
00520                         file.Print(",");
00521                         if (indentLevel < maxIndentLevel)
00522                         {
00523                             file.Print("\n");
00524                         }
00525                     }
00526                     
00527                     // Write the object as an unnamed entry.
00528                     WriteLuaObject(file, NULL, value, indentLevel + 1, writeAll, alphabetical, writeTablePointers, maxIndentLevel);
00529 
00530                     // We've definitely passed the first item now.
00531                     firstSequential = false;
00532                 }
00533             }
00534         }
00535 
00536         // Did we find any sequential table values?
00537         if (upperIndex > 1)
00538         {
00539             hasSequential = true;
00540         }
00541         
00542         if (alphabetical)
00543         {
00544             std::list<std::string> keyNames;
00545 
00546             // Cycle through the table.
00547             int i;
00548             PushNil();
00549             while ((i = Next(table)) != 0)
00550             {
00551                 char keyName[255];
00552 
00553                 // Retrieve the table entry's key and value.
00554                 LuaObject key(this, GetTop() - 1);
00555 
00556                 // Is the key a number?
00557                 if (key.IsNumber())
00558                 {
00559                     // Yes, were there sequential array items in this table?
00560                     if (hasSequential)
00561                     {
00562                         // Is the array item's key an integer?
00563                         float realNum = key.GetNumber();
00564                         int intNum = (int)realNum;
00565                         if (realNum == (float)intNum)
00566                         {
00567                             // Yes.  Is it between 1 and upperIndex?
00568                             if (intNum >= 1  &&  intNum < upperIndex)
00569                             {
00570                                 // We already wrote it as part of the sequential
00571                                 // list.
00572                                 Pop();
00573                                 continue;
00574                             }
00575                         }
00576                     }
00577 
00578                     // Build the table entry name for the number.
00579                     sprintf(keyName, "[%.16g]", key.GetNumber());
00580                 }
00581                 else
00582                 {
00583                     // Build the table entry name for the string key name.
00584                     const char* ptr = key.GetString();
00585                     bool isAlphaNumeric = true;
00586                     while (*ptr)
00587                     {
00588                         if (!isalnum(*ptr)  &&  *ptr != '_')
00589                         {
00590                             isAlphaNumeric = false;
00591                             break;
00592                         }
00593                         ptr++;
00594                     }
00595                     if (isAlphaNumeric)
00596                         strcpy(keyName, key.GetString());
00597                     else
00598                         sprintf(keyName, "[\"%s\"]", key.GetString());
00599                 }
00600 
00601                 keyNames.push_back(keyName);
00602 
00603                 // Go to the next item.
00604                 Pop();
00605             }
00606 
00607             keyNames.sort();
00608 
00609             if (keyNames.size() > 0)
00610             {
00611                 // If we wrote a sequential list, the value we're about to write
00612                 // is not nil, and we haven't written the semicolon to separate
00613                 // the sequential table entries from the keyed table entries...
00614                 if (hasSequential  &&  indentLevel != -1)
00615                 {
00616                     // Then add a comma (for good measure) and the semicolon.
00617                     file.Print(", ;");
00618                     if (indentLevel < maxIndentLevel)
00619                     {
00620                         file.Print("\n");
00621                     }
00622                     wroteSemi = true;
00623                 }
00624             }
00625 
00626             for(std::list<std::string>::iterator it = keyNames.begin(); it != keyNames.end(); ++it)
00627             {
00628                 LuaAutoBlock autoBlock(*this);
00629                 const std::string& keyFullName = (*it);
00630 
00631                 LuaObject value = table;        // Temp
00632                 if (keyFullName[0] == '[')
00633                 {
00634                     if (keyFullName[1] == '"')
00635                     {
00636                         std::string keyName = keyFullName.substr(2, keyFullName.size() - 4);
00637                         value = table.GetByName(keyName.c_str());
00638                     }
00639                     else
00640                     {
00641                         std::string keyName = keyFullName.substr(1, keyFullName.size() - 2);
00642                         int number = atoi(keyName.c_str());
00643                         value = table.GetByIndex(number);
00644                     }
00645                 }
00646                 else
00647                 {
00648                     value = table.GetByName(keyFullName.c_str());
00649                 }
00650 
00651                 // Write the table entry.
00652                 bool ret = WriteLuaObject(file, keyFullName.c_str(), value, indentLevel + 1, writeAll, alphabetical, writeTablePointers, maxIndentLevel);
00653 
00654                 // Add a comma after the table entry.
00655                 if (indentLevel != -1  &&  ret)
00656                 {
00657                     file.Print(",");
00658                     if (indentLevel < maxIndentLevel)
00659                     {
00660                         file.Print("\n");
00661                     }
00662                 }
00663             }
00664         }
00665         else
00666         {
00667             // Cycle through the table.
00668             int i;
00669             PushNil();
00670             while ((i = Next(table)) != 0)
00671             {
00672                 char keyName[255];
00673 
00674                 // Retrieve the table entry's key and value.
00675                 LuaObject key(this, GetTop() - 1);
00676                 LuaObject value(this, GetTop());
00677 
00678                 // Is the key a number?
00679                 if (key.IsNumber())
00680                 {
00681                     // Yes, were there sequential array items in this table?
00682                     if (hasSequential)
00683                     {
00684                         // Is the array item's key an integer?
00685                         float realNum = key.GetNumber();
00686                         int intNum = (int)realNum;
00687                         if (realNum == (float)intNum)
00688                         {
00689                             // Yes.  Is it between 1 and upperIndex?
00690                             if (intNum >= 1  &&  intNum < upperIndex)
00691                             {
00692                                 // We already wrote it as part of the sequential
00693                                 // list.
00694                                 Pop();
00695                                 continue;
00696                             }
00697                         }
00698                     }
00699 
00700                     // Build the table entry name for the number.
00701                     sprintf(keyName, "[%.16g]", key.GetNumber());
00702                 }
00703                 else
00704                 {
00705                     // Build the table entry name for the string key name.
00706                     const char* ptr = key.GetString();
00707                     bool isAlphaNumeric = true;
00708                     while (*ptr)
00709                     {
00710                         if (!isalnum(*ptr)  &&  *ptr != '_')
00711                         {
00712                             isAlphaNumeric = false;
00713                             break;
00714                         }
00715                         ptr++;
00716                     }
00717                     if (isAlphaNumeric)
00718                         strcpy(keyName, key.GetString());
00719                     else
00720                         sprintf(keyName, "[\"%s\"]", key.GetString());
00721                 }
00722 
00723                 // If we wrote a sequential list, the value we're about to write
00724                 // is not nil, and we haven't written the semicolon to separate
00725                 // the sequential table entries from the keyed table entries...
00726                 if (hasSequential  &&  !value.IsNil()  &&  !wroteSemi)
00727                 {
00728                     // Then add a comma (for good measure) and the semicolon.
00729                     if (indentLevel != -1)
00730                     {
00731                         file.Print(", ;");
00732                         if (indentLevel < maxIndentLevel)
00733                         {
00734                             file.Print("\n");
00735                         }
00736                     }
00737                     wroteSemi = true;
00738                 }
00739 
00740                 // Write the table entry.
00741                 bool ret = WriteLuaObject(file, keyName, value, indentLevel + 1, writeAll, alphabetical, writeTablePointers, maxIndentLevel);
00742 
00743                 // Add a comma after the table entry.
00744                 if (ret  &&  indentLevel != -1)
00745                 {
00746                     file.Print(",");
00747                     if (indentLevel < maxIndentLevel)
00748                     {
00749                         file.Print("\n");
00750                     }
00751                 }
00752 
00753                 // Go to the next item.
00754                 Pop();
00755             }
00756         }
00757 
00758         // If we wrote a sequential list and haven't written a semicolon, then
00759         // there were no keyed table entries.  Just write the final comma.
00760         if (hasSequential  &&  !wroteSemi  &&  indentLevel != -1)
00761         {
00762             file.Print(",");
00763             if (indentLevel < maxIndentLevel)
00764             {
00765                 file.Print("\n");
00766             }
00767         }
00768         
00769         // Indent, with the intent of closing up the table.
00770         IndentFile(file, indentSpaces);
00771 
00772         // If the indentation level is 0, then we're at the root position.
00773         if (indentLevel == 0)
00774         {
00775             // Add a couple extra returns for readability's sake.
00776             file.Print("}");
00777             if (indentLevel < maxIndentLevel)
00778             {
00779                 file.Print("\n\n", file);
00780             }
00781         }
00782         else if (indentLevel > 0)
00783         {
00784             // Close the table.  The comma is written when WriteObject()
00785             // returns from the recursive call.
00786             file.Print("}");
00787         }
00788     }
00789 
00790     // If the indentation level is at the root, then add a return to separate
00791     // the lines.
00792     if (indentLevel == 0)
00793     {
00794         if (indentLevel < maxIndentLevel)
00795         {
00796             file.Print("\n");
00797         }
00798     }
00799 
00800     return true;
00801 }
00802 
00803 
00807 void LuaState::WriteLuaGlobalsFile(const char* filename, bool writeAll,
00808                                    bool alphabetical, bool writeTablePointers,
00809                                    unsigned int maxIndentLevel)
00810 {
00811     // For safety, just in case we leave something behind on the script stack.
00812     LuaAutoBlock block(*this);
00813 
00814     WriteLuaFile(filename, NULL, GetGlobals(), -1, writeAll, alphabetical,
00815             writeTablePointers, maxIndentLevel);
00816 }
00817 
00818 
00822 void LuaState::WriteLuaFile(const char* filename, const char* name, LuaObject value,
00823                          int indentLevel, bool writeAll, bool alphabetical,
00824                          bool writeTablePointers, unsigned int maxIndentLevel)
00825 {
00826     // Open the text file to write the script state to.
00827     LuaStateOutFile file;
00828     file.Open(filename);
00829 

LuaState::operator lua_State *   [inline]
 

Definition at line 1258 of file LuaState.h.


Member Data Documentation

lua_State* LuaState::m_state
 

Definition at line 1261 of file LuaState.h.


The documentation for this class was generated from the following files:
Generated on Wed Nov 7 21:15:17 2001 for LuaState(LuaWrapper) by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001