00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "lua_config.h"
00010
00011
00012 #include <string.h>
00013 #include <stdio.h>
00014
00015
00016
00017 int get_boolean( lua_State* L, const char* name, const int mDefault )
00018 {
00019 int i;
00020
00021 lua_getglobal( L, name );
00022 if( !lua_isboolean(L, -1) )
00023 i=mDefault;
00024 else
00025 i=(int)lua_toboolean( L, -1 );
00026 lua_pop( L, 1 );
00027
00028 return i;
00029 }
00030
00031
00032
00033
00034 double get_number( lua_State* L, const char* name, const double mDefault )
00035 {
00036 double f;
00037
00038 lua_getglobal( L, name );
00039 if( !lua_isnumber(L, -1) )
00040 f=mDefault;
00041 else
00042 f=(double)lua_tonumber( L, -1 );
00043 lua_pop( L,1 );
00044
00045 return f;
00046 }
00047
00048
00049
00050
00051 void get_string( lua_State* L, const char* name, char* buffer, size_t bufferSize, const char* mDefault )
00052 {
00053 lua_getglobal( L, name );
00054 if( !lua_isstring(L, -1) )
00055 snprintf( buffer, bufferSize, "%s", mDefault );
00056 else
00057 snprintf( buffer, bufferSize, "%s", lua_tostring(L, -1) );
00058 lua_pop( L, 1 );
00059 }
00060
00061
00062
00063
00064 double get_number_from_table( lua_State* L, const char* table, const char* name, const double mDefault )
00065 {
00066 double f;
00067
00068 lua_getglobal( L, table );
00069 if( !lua_istable(L, -1) )
00070 f=mDefault;
00071 else {
00072 lua_pushstring( L, name );
00073 lua_gettable( L, -2 );
00074 if( !lua_isnumber(L, -1) )
00075 f=mDefault;
00076 else
00077 f=(double)lua_tonumber( L, -1 );
00078
00079 lua_pop( L, 1 );
00080 }
00081 lua_pop( L, 1 );
00082
00083 return f;
00084 }
00085
00086
00087
00088
00089 int get_boolean_from_table( lua_State* L, const char* table, const char* name, const int mDefault )
00090 {
00091 int i;
00092
00093 lua_getglobal( L, table );
00094 if( !lua_istable(L, -1) )
00095 i=mDefault;
00096 else {
00097 lua_pushstring( L, name );
00098 lua_gettable( L, -2 );
00099 if( !lua_isboolean(L, -1) )
00100 i=mDefault;
00101 else
00102 i=(int)lua_toboolean( L, -1 );
00103
00104 lua_pop( L, 1 );
00105 }
00106 lua_pop( L, 1 );
00107
00108 return i;
00109 }
00110
00111
00112
00113
00114 void get_string_from_table( lua_State* L, const char* table, const char* name, char* buffer, const size_t bufferSize, const char* mDefault )
00115 {
00116 lua_getglobal( L, table );
00117 if( !lua_istable(L, -1) )
00118 snprintf( buffer, bufferSize, "%s", mDefault );
00119 else {
00120 lua_pushstring( L, name );
00121 lua_gettable( L, -2 );
00122 if( !lua_isstring(L, -1) )
00123 snprintf( buffer, bufferSize, "%s", mDefault );
00124 else
00125 snprintf( buffer, bufferSize, "%s", lua_tostring(L, -1) );
00126 lua_pop( L, 1 );
00127 }
00128 lua_pop( L, 1 );
00129 }
00130
00131
00132
00133
00134
00135
00136 void call_lua_function( lua_State* L, const char *func, const char *sig, ... )
00137 {
00138 va_list vl;
00139 int narg, nres;
00140
00141 va_start(vl, sig);
00142 lua_getglobal( L, func );
00143
00144
00145 narg = 0;
00146 while (*sig) {
00147 switch (*sig++) {
00148
00149 case 'd':
00150 lua_pushnumber(L, va_arg(vl, double));
00151 break;
00152
00153 case 'i':
00154 lua_pushnumber(L, va_arg(vl, int));
00155 break;
00156
00157 case 's':
00158 lua_pushstring(L, va_arg(vl, char *));
00159 break;
00160
00161 case '>':
00162 goto endwhile;
00163
00164 default:
00165 printf( "invalid option (%c)", *(sig - 1) );
00166 }
00167 narg++;
00168 luaL_checkstack(L, 1, "too many arguments");
00169 } endwhile:
00170
00171
00172 nres = strlen(sig);
00173 if (lua_pcall(L, narg, nres, 0) != 0)
00174 printf( "error running function `%s': %s\n",
00175 func, lua_tostring(L, -1) );
00176
00177
00178 nres = -nres;
00179 while (*sig) {
00180 switch (*sig++) {
00181
00182 case 'd':
00183 if (!lua_isnumber(L, nres))
00184 printf( "wrong result type" );
00185 *va_arg(vl, double *) = lua_tonumber(L, nres);
00186 break;
00187
00188 case 'i':
00189 if (!lua_isnumber(L, nres))
00190 printf( "wrong result type" );
00191 *va_arg(vl, int *) = (int)lua_tonumber(L, nres);
00192 break;
00193
00194 case 's':
00195 if (!lua_isstring(L, nres))
00196 printf( "wrong result type" );
00197 *va_arg(vl, const char **) = lua_tostring(L, nres);
00198 break;
00199
00200 default:
00201 printf("invalid option (%c)", *(sig - 1));
00202 }
00203 nres++;
00204 }
00205 va_end(vl);
00206 }