lua_namespace.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "lua_volksdata.h"
  2. /*
  3. * Class methods.
  4. */
  5. static int l_nsmap_add (lua_State *L)
  6. {
  7. const char
  8. *pfx = luaL_checkstring (L, 1),
  9. *nsstr = luaL_checkstring (L, 2);
  10. LUA_PCHECK (
  11. VOLK_nsmap_add (pfx, nsstr),
  12. "Error adding member to NS map");
  13. return 0;
  14. }
  15. static int l_nsmap_remove (lua_State *L)
  16. {
  17. const char *pfx = luaL_checkstring (L, 1);
  18. LUA_PCHECK (
  19. VOLK_nsmap_remove (pfx),
  20. "Error removing member to NS map");
  21. return 0;
  22. }
  23. static int l_nsmap_get_ns (lua_State *L)
  24. {
  25. const char *pfx = luaL_checkstring (L, 1);
  26. const char *ns = VOLK_nsmap_get_ns (pfx);
  27. if (ns) lua_pushstring (L, ns);
  28. else return 0;
  29. return 1;
  30. }
  31. static int l_nsmap_get_pfx (lua_State *L)
  32. {
  33. const char *ns = luaL_checkstring (L, 1);
  34. const char *pfx = VOLK_nsmap_get_pfx (ns);
  35. if (pfx) lua_pushstring (L, pfx);
  36. else lua_pushnil (L);
  37. return 1;
  38. }
  39. static int l_nsmap_normalize_uri (lua_State *L)
  40. {
  41. const char *pfx_uri = luaL_checkstring (L, 1);
  42. char *fq_uri;
  43. LUA_PCHECK (
  44. VOLK_nsmap_normalize_uri (pfx_uri, &fq_uri),
  45. "Error normalizing URI");
  46. if (fq_uri) lua_pushstring (L, fq_uri);
  47. else return 0;
  48. free (fq_uri);
  49. return 1;
  50. }
  51. static int l_nsmap_denormalize_uri (lua_State *L)
  52. {
  53. const char *fq_uri = luaL_checkstring (L, 1);
  54. char *pfx_uri;
  55. LUA_PCHECK (
  56. VOLK_nsmap_denormalize_uri (fq_uri, &pfx_uri),
  57. "Error denormalizing URI");
  58. if (pfx_uri) lua_pushstring (L, pfx_uri);
  59. else return 0;
  60. free (pfx_uri);
  61. return 1;
  62. }
  63. static int nsmap_iter_next (lua_State *L)
  64. {
  65. const char ****data_p = lua_touserdata (L, lua_upvalueindex (1));
  66. const char ***data = *data_p;
  67. size_t *i = lua_touserdata (L, lua_upvalueindex (2));
  68. if ((data)[*i]) {
  69. lua_pushstring (L, data[*i][0]);
  70. lua_pushstring (L, data[*i][1]);
  71. (*i)++;
  72. return 2;
  73. } else {
  74. free (data);
  75. *data_p = NULL;
  76. free (i);
  77. return 0;
  78. }
  79. }
  80. static int l_nsmap_iter (lua_State *L)
  81. {
  82. const char ****data_p = lua_newuserdata (L, sizeof (*data_p));
  83. //luaL_getmetatable (L, "VOLK.String2DIterator");
  84. //lua_setmetatable (L, -2);
  85. *data_p = VOLK_nsmap_dump ();
  86. LUA_NLCHECK (*data_p, "Error creating NS map iterator.");
  87. size_t *i = malloc (sizeof (*i));
  88. LUA_NLCHECK (i, "Memory error in NS map iterator.");
  89. *i = 0;
  90. lua_pushlightuserdata (L, i);
  91. lua_pushcclosure (L, nsmap_iter_next, 2);
  92. return 1;
  93. }
  94. /*
  95. * Library setup.
  96. */
  97. static const luaL_Reg nsmap_lib_fn [] = {
  98. {"add", l_nsmap_add},
  99. {"remove", l_nsmap_remove},
  100. {"get_ns", l_nsmap_get_ns},
  101. {"get_pfx", l_nsmap_get_pfx},
  102. {"normalize_uri", l_nsmap_normalize_uri},
  103. {"denormalize_uri", l_nsmap_denormalize_uri},
  104. {"iter", l_nsmap_iter},
  105. {NULL}
  106. };
  107. /*
  108. static const luaL_Reg nsmap_lib_meth [] = {
  109. {NULL}
  110. };
  111. */
  112. int luaopen_volksdata_namespace (lua_State *L)
  113. {
  114. luaL_newmetatable (L, "VOLK.NSMap");
  115. lua_pushvalue (L, -1);
  116. //lua_setfield (L, -2, "__index");
  117. //luaL_setfuncs (L, nsmap_lib_meth, 0);
  118. luaL_newlib (L, nsmap_lib_fn);
  119. //luaL_newmetatable (L, "VOLK.String2DIterator");
  120. return 1;
  121. }