luacpp.fl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # data file for the Fltk User Interface Designer (fluid)
  2. version 1.0300
  3. header_name {.h}
  4. code_name {.cxx}
  5. block {namespace lua} {open public file_name {} before {\{} after {\}}
  6. } {
  7. block {extern "C"} {open public file_name {} before {\{} after {\}}
  8. } {
  9. decl {\#include <lua.h>} {public global
  10. }
  11. decl {\#include <lauxlib.h>} {public global
  12. }
  13. decl {\#include <lualib.h>} {public global
  14. }
  15. decl {\#include <stdarg.h>} {private local
  16. }
  17. decl {\#include <string.h>} {private local
  18. }
  19. }
  20. Function {call_va(lua_State *L, const char *table, const char *func, const char *sig, ...)} {open private return_type int
  21. } {
  22. code {va_list vl;
  23. int narg, nres; /* number of arguments and results */
  24. va_start(vl, sig);
  25. if(table){
  26. lua_getglobal(L, table);
  27. lua_getfield(L, -1, func);
  28. } else {
  29. lua_getglobal(L, func); /* get function */
  30. }
  31. /* push arguments */
  32. narg = 0;
  33. while (*sig) { /* push arguments */
  34. switch (*sig++) {
  35. case 'd': /* double argument */
  36. lua_pushnumber(L, va_arg(vl, double));
  37. break;
  38. case 'i': /* int argument */
  39. lua_pushnumber(L, va_arg(vl, int));
  40. break;
  41. case 's': /* string argument */
  42. lua_pushstring(L, va_arg(vl, char *));
  43. break;
  44. case '>':
  45. goto endwhile;
  46. default:
  47. return -1;
  48. }
  49. narg++;
  50. luaL_checkstack(L, 1, "too many arguments");
  51. } endwhile:
  52. /* do the call */
  53. nres = strlen(sig); /* number of expected results */
  54. if (lua_pcall(L, narg, nres, 0) != 0) /* do the call */
  55. return -2;
  56. /* retrieve results */
  57. nres = -nres; /* stack index of first result */
  58. while (*sig) { /* get results */
  59. switch (*sig++) {
  60. case 'd': /* double result */
  61. if (!lua_isnumber(L, nres))
  62. return -10;
  63. *va_arg(vl, double *) = lua_tonumber(L, nres);
  64. break;
  65. case 'i': /* int result */
  66. if (!lua_isnumber(L, nres))
  67. return -11;
  68. *va_arg(vl, int *) = (int)lua_tonumber(L, nres);
  69. break;
  70. case 's': /* string result */
  71. if (!lua_isstring(L, nres))
  72. return -12;
  73. *va_arg(vl, const char **) = lua_tostring(L, nres);
  74. break;
  75. default:
  76. return -3;
  77. }
  78. nres++;
  79. }
  80. va_end(vl);
  81. return 1; //all went ok} {}
  82. }
  83. class Lua {
  84. comment {Lua interpreter} open
  85. } {
  86. decl {lua_State *L;} {protected local
  87. }
  88. Function {Lua()} {open
  89. } {
  90. code {L = lua_open(); /* opens Lua */
  91. luaL_openlibs(L); /* opens all standard libraries */} {}
  92. }
  93. Function {~Lua()} {open
  94. } {
  95. code {lua_close(L);} {}
  96. }
  97. Function {gsub(const char* src, const char *re, const char *sub)} {open return_type {char *}
  98. } {
  99. code {char *ret_value;
  100. int top = lua_gettop(L); //saves the stack top
  101. // if(call_va(L, "string","gsub", "sss>s", src, re, sub, &ret_value)) {
  102. // ret_value = strdup(ret_value); //allocate a new copy
  103. // } else ret_value = NULL;
  104. // lua_settop(L, top); //returns stack to it's orign
  105. // return ret_value;
  106. lua_getglobal(L, "string");
  107. //if(lua_istable(L,-1))
  108. lua_getfield(L, -1, "gsub");
  109. //if(lua_isfunction(L,-1))
  110. lua_pushstring(L, src);
  111. lua_pushstring(L, re);
  112. lua_pushstring(L, sub);
  113. lua_call(L, 3, 1);
  114. if (lua_isstring(L, -1)) ret_value = strdup(lua_tostring(L,-1));
  115. else ret_value = NULL;
  116. lua_settop(L, top); //returns stack to it's orign
  117. return ret_value;} {selected
  118. }
  119. }
  120. }
  121. }