luacpp.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // generated by Fast Light User Interface Designer (fluid) version 1.0300
  2. #include "luacpp.h"
  3. namespace lua
  4. {
  5. extern "C"
  6. {
  7. #include <stdarg.h>
  8. #include <string.h>
  9. }
  10. #define DONE_AND_RETURN(x) {ret_val =x; goto done_and_return;}
  11. int Lua::call_va(const char *table, const char *func, const char *sig, ...) {
  12. va_list vl;
  13. int narg, nres; /* number of arguments and results */
  14. int ret_val = 0;
  15. va_start(vl, sig);
  16. if(table){
  17. lua_getglobal(L, table);
  18. lua_getfield(L, -1, func);
  19. } else {
  20. lua_getglobal(L, func); /* get function */
  21. }
  22. if(!lua_isfunction(L,-1)) DONE_AND_RETURN(-1);
  23. /* push arguments */
  24. narg = 0;
  25. while (*sig) { /* push arguments */
  26. switch (*sig++) {
  27. case 'd': /* double argument */
  28. lua_pushnumber(L, va_arg(vl, double));
  29. break;
  30. case 'i': /* int argument */
  31. lua_pushnumber(L, va_arg(vl, int));
  32. break;
  33. case 's': /* string argument */
  34. lua_pushstring(L, va_arg(vl, char *));
  35. break;
  36. case '>':
  37. goto endwhile;
  38. default:
  39. DONE_AND_RETURN(-2);
  40. }
  41. narg++;
  42. luaL_checkstack(L, 1, "too many arguments");
  43. } endwhile:
  44. /* do the call */
  45. nres = strlen(sig); /* number of expected results */
  46. if (lua_pcall(L, narg, nres, 0) != 0) /* do the call */
  47. DONE_AND_RETURN(-3);
  48. /* retrieve results */
  49. nres = -nres; /* stack index of first result */
  50. while (*sig) { /* get results */
  51. switch (*sig++) {
  52. case 'd': /* double result */
  53. if (!lua_isnumber(L, nres))
  54. DONE_AND_RETURN(-10);
  55. *va_arg(vl, double *) = lua_tonumber(L, nres);
  56. break;
  57. case 'i': /* int result */
  58. if (!lua_isnumber(L, nres))
  59. DONE_AND_RETURN(-11);
  60. *va_arg(vl, int *) = (int)lua_tonumber(L, nres);
  61. break;
  62. case 's': /* string result */
  63. if (!lua_isstring(L, nres))
  64. DONE_AND_RETURN(-12);
  65. *va_arg(vl, const char **) = lua_tostring(L, nres);
  66. break;
  67. default:
  68. DONE_AND_RETURN(-5);
  69. }
  70. nres++;
  71. }
  72. done_and_return:
  73. va_end(vl);
  74. return ret_val; //all went ok
  75. }
  76. Lua::Lua() {
  77. show_error_func = NULL;
  78. L = lua_open(); /* opens Lua */
  79. luaL_openlibs(L); /* opens all standard libraries */
  80. show_error_func = NULL;
  81. }
  82. Lua::~Lua() {
  83. lua_close(L);
  84. }
  85. void Lua::set_lua_error(lua_show_error_func ef){
  86. show_error_func = ef;
  87. }
  88. void Lua::show_error(){
  89. if(show_error_func)
  90. (*show_error_func)(lua_tostring(L, -1));
  91. }
  92. char * Lua::gsub(const char* src, const char *re, const char *sub) {
  93. char *ret_value;
  94. int top = lua_gettop(L); //saves the stack top
  95. // if(call_va("string","gsub", "sss>s", src, re, sub, &ret_value)) {
  96. // ret_value = strdup(ret_value); //allocate a new copy
  97. // } else ret_value = NULL;
  98. // lua_settop(L, top); //returns stack to it's orign
  99. // return ret_value;
  100. lua_getglobal(L, "string");
  101. //if(lua_istable(L,-1))
  102. lua_getfield(L, -1, "gsub");
  103. //if(lua_isfunction(L,-1))
  104. lua_pushstring(L, src);
  105. lua_pushstring(L, re);
  106. lua_pushstring(L, sub);
  107. lua_call(L, 3, 1);
  108. if (lua_isstring(L, -1)) ret_value = strdup(lua_tostring(L,-1));
  109. else ret_value = NULL;
  110. lua_settop(L, top); //returns stack to it's orign
  111. return ret_value;
  112. }
  113. int Lua::dostring(const char *script){
  114. int top = lua_gettop(L); //saves the stack top
  115. int ret_value = luaL_dostring(L, script);
  116. lua_settop(L, top); //returns stack to it's orign
  117. return ret_value;
  118. }
  119. int Lua::loadstring(const char *script){
  120. int top = lua_gettop(L); //saves the stack top
  121. int ret_value = luaL_loadstring(L, script);
  122. if(ret_value) show_error();
  123. lua_settop(L, top); //returns stack to it's orign
  124. return ret_value;
  125. }
  126. int Lua::dofile(const char *file_name){
  127. int top = lua_gettop(L); //saves the stack top
  128. int ret_value = luaL_dofile(L, file_name);
  129. lua_settop(L, top); //returns stack to it's orign
  130. return ret_value;
  131. }
  132. int Lua::loadfile(const char *file_name){
  133. int top = lua_gettop(L); //saves the stack top
  134. int ret_value = luaL_loadfile(L, file_name);
  135. lua_settop(L, top); //returns stack to it's orign
  136. return ret_value;
  137. }
  138. int Lua::lua_preprocess_file(const char *file_name){
  139. int top = lua_gettop(L); //saves the stack top
  140. int ret_value = -1;
  141. lua_getglobal(L, "preprocess_file");
  142. if(lua_isfunction(L, -1)){
  143. lua_pushstring(L, file_name);
  144. if(!lua_pcall(L, 1, 1, 0) && lua_isnumber(L, -1))
  145. ret_value = lua_tointeger(L,-1);
  146. }
  147. lua_settop(L, top); //returns stack to it's orign
  148. return ret_value;
  149. }
  150. void Lua::gvar(const char* key, const char *value){
  151. lua_pushstring(L, value);
  152. lua_setglobal(L, key);
  153. }
  154. void Lua::gvar(const char* key, int value){
  155. lua_pushinteger(L, value);
  156. lua_setglobal(L, key);
  157. }
  158. void Lua::gvar(const char* key, double value){
  159. lua_pushnumber(L, value);
  160. lua_setglobal(L, key);
  161. }
  162. char *Lua::gvar_string(const char* key){
  163. int top = lua_gettop(L); //saves the stack top
  164. char *ret_value;
  165. lua_getglobal(L, key);
  166. if(lua_isstring(L,-1)) ret_value = strdup(lua_tostring(L, -1));
  167. else ret_value = NULL;
  168. lua_settop(L, top); //returns stack to it's orign
  169. return ret_value;
  170. }
  171. double Lua::gvar_float(const char* key){
  172. int top = lua_gettop(L); //saves the stack top
  173. double ret_value;
  174. lua_getglobal(L, key);
  175. if(lua_isnumber(L,-1)) ret_value = lua_tonumber(L, -1);
  176. else ret_value = 0;
  177. lua_settop(L, top); //returns stack to it's orign
  178. return ret_value;
  179. }
  180. }