lua_namespace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "lua_lsup.h"
  2. #define check_nsm(L) \
  3. *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
  4. /*
  5. * Factory methods.
  6. */
  7. int l_nsmap_new (lua_State *L)
  8. {
  9. LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
  10. luaL_getmetatable (L, "LSUP.NSMap");
  11. lua_setmetatable (L, -2);
  12. *nsm_p = LSUP_nsmap_new ();
  13. if (!*nsm_p) return luaL_error (L, "Error while creating namespace map.");
  14. return 1;
  15. }
  16. /*
  17. * Class methods.
  18. */
  19. static int l_nsmap_gc (lua_State *L)
  20. {
  21. LSUP_NSMap *nsm = check_nsm (L);
  22. LOG_DEBUG ("Garbage collecting NSM @%p", nsm);
  23. if (nsm) LSUP_nsmap_free (nsm);
  24. return 0;
  25. }
  26. static int l_nsmap_add (lua_State *L)
  27. {
  28. LSUP_NSMap *nsm = check_nsm (L);
  29. const char
  30. *pfx = luaL_checkstring (L, 2),
  31. *nsstr = luaL_checkstring (L, 3);
  32. LUA_PCHECK (
  33. LSUP_nsmap_add (nsm, pfx, nsstr),
  34. "Error adding member to NS map");
  35. return 0;
  36. }
  37. static int l_nsmap_remove (lua_State *L)
  38. {
  39. LSUP_NSMap *nsm = check_nsm (L);
  40. const char *pfx = luaL_checkstring (L, 2);
  41. LUA_PCHECK (
  42. LSUP_nsmap_remove (nsm, pfx),
  43. "Error removing member to NS map");
  44. return 0;
  45. }
  46. static int l_nsmap_get_ns (lua_State *L)
  47. {
  48. LSUP_NSMap *nsm = check_nsm (L);
  49. const char *pfx = luaL_checkstring (L, 2);
  50. const char *ns = LSUP_nsmap_get_ns (nsm, pfx);
  51. if (ns) lua_pushstring (L, ns);
  52. else return 0;
  53. return 1;
  54. }
  55. static int l_nsmap_get_pfx (lua_State *L)
  56. {
  57. LSUP_NSMap *nsm = check_nsm (L);
  58. const char *ns = luaL_checkstring (L, 2);
  59. const char *pfx = LSUP_nsmap_get_pfx (nsm, ns);
  60. if (pfx) lua_pushstring (L, pfx);
  61. else lua_pushnil (L);
  62. return 1;
  63. }
  64. static int l_nsmap_normalize_uri (lua_State *L)
  65. {
  66. LSUP_NSMap *nsm = check_nsm (L);
  67. const char *pfx_uri = luaL_checkstring (L, 2);
  68. char *fq_uri;
  69. LUA_PCHECK (
  70. LSUP_nsmap_normalize_uri (nsm, pfx_uri, &fq_uri),
  71. "Error normalizing URI");
  72. if (fq_uri) lua_pushstring (L, fq_uri);
  73. else return 0;
  74. free (fq_uri);
  75. return 1;
  76. }
  77. static int l_nsmap_denormalize_uri (lua_State *L)
  78. {
  79. LSUP_NSMap *nsm = check_nsm (L);
  80. const char *fq_uri = luaL_checkstring (L, 2);
  81. char *pfx_uri;
  82. LUA_PCHECK (
  83. LSUP_nsmap_denormalize_uri (nsm, fq_uri, &pfx_uri),
  84. "Error denormalizing URI");
  85. if (pfx_uri) lua_pushstring (L, pfx_uri);
  86. else return 0;
  87. free (pfx_uri);
  88. return 1;
  89. }
  90. /*
  91. static int l_nsmap_iter_next (lua_State *L)
  92. {
  93. // TODO
  94. return 0;
  95. }
  96. */
  97. static int nsmap_iter_next (lua_State *L)
  98. {
  99. const char ****data_p = lua_touserdata (L, lua_upvalueindex (1));
  100. const char ***data = *data_p;
  101. size_t *i = lua_touserdata (L, lua_upvalueindex (2));
  102. if ((data)[*i]) {
  103. lua_pushstring (L, data[*i][0]);
  104. lua_pushstring (L, data[*i][1]);
  105. (*i)++;
  106. return 2;
  107. } else {
  108. free (data);
  109. *data_p = NULL;
  110. free (i);
  111. return 0;
  112. }
  113. }
  114. static int l_nsmap_iter (lua_State *L)
  115. {
  116. LSUP_NSMap *nsm = check_nsm (L);
  117. const char ****data_p = lua_newuserdata (L, sizeof (*data_p));
  118. //luaL_getmetatable (L, "LSUP.String2DIterator");
  119. //lua_setmetatable (L, -2);
  120. *data_p = LSUP_nsmap_dump (nsm);
  121. LUA_NLCHECK (*data_p, "Error creating NS map iterator.");
  122. size_t *i = malloc (sizeof (*i));
  123. LUA_NLCHECK (i, "Memory error in NS map iterator.");
  124. *i = 0;
  125. lua_pushlightuserdata (L, i);
  126. lua_pushcclosure (L, nsmap_iter_next, 2);
  127. return 1;
  128. }
  129. /*
  130. * Library setup.
  131. */
  132. static const luaL_Reg nsmap_lib_fn [] = {
  133. {"new", l_nsmap_new},
  134. {NULL}
  135. };
  136. static const luaL_Reg nsmap_lib_meth [] = {
  137. {"__gc", l_nsmap_gc},
  138. {"__pairs", l_nsmap_iter},
  139. {"add", l_nsmap_add},
  140. {"remove", l_nsmap_remove},
  141. {"get_ns", l_nsmap_get_ns},
  142. {"get_pfx", l_nsmap_get_pfx},
  143. {"normalize_uri", l_nsmap_normalize_uri},
  144. {"denormalize_uri", l_nsmap_denormalize_uri},
  145. //{"iter", l_nsmap_iter},
  146. {NULL}
  147. };
  148. int luaopen_lsup_namespace (lua_State *L)
  149. {
  150. luaL_newmetatable (L, "LSUP.NSMap");
  151. lua_pushvalue (L, -1);
  152. lua_setfield (L, -2, "__index");
  153. luaL_setfuncs (L, nsmap_lib_meth, 0);
  154. luaL_newlib (L, nsmap_lib_fn);
  155. //luaL_newmetatable (L, "LSUP.String2DIterator");
  156. return 1;
  157. }