namespace.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 a term!");
  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. if (nsm) LSUP_nsmap_free (nsm);
  23. return 0;
  24. }
  25. static int l_nsmap_add (lua_State *L)
  26. {
  27. LSUP_NSMap *nsm = check_nsm (L);
  28. const char
  29. *pfx = luaL_checkstring (L, 2),
  30. *nsstr = luaL_checkstring (L, 3);
  31. LUA_PCHECK (
  32. LSUP_nsmap_add (nsm, pfx, nsstr),
  33. "Error adding member to NS map");
  34. return 0;
  35. }
  36. static int l_nsmap_remove (lua_State *L)
  37. {
  38. LSUP_NSMap *nsm = check_nsm (L);
  39. const char *pfx = luaL_checkstring (L, 2);
  40. LUA_PCHECK (
  41. LSUP_nsmap_remove (nsm, pfx),
  42. "Error removing member to NS map");
  43. return 0;
  44. }
  45. static int l_nsmap_get_ns (lua_State *L)
  46. {
  47. LSUP_NSMap *nsm = check_nsm (L);
  48. const char *pfx = luaL_checkstring (L, 2);
  49. const char *ns = LSUP_nsmap_get_ns (nsm, pfx);
  50. if (ns) lua_pushstring (L, ns);
  51. else return 0;
  52. return 1;
  53. }
  54. static int l_nsmap_get_pfx (lua_State *L)
  55. {
  56. LSUP_NSMap *nsm = check_nsm (L);
  57. const char *ns = luaL_checkstring (L, 2);
  58. const char *pfx = LSUP_nsmap_get_pfx (nsm, ns);
  59. if (pfx) lua_pushstring (L, pfx);
  60. else lua_pushnil (L);
  61. return 1;
  62. }
  63. static int l_nsmap_normalize_uri (lua_State *L)
  64. {
  65. LSUP_NSMap *nsm = check_nsm (L);
  66. const char *pfx_uri = luaL_checkstring (L, 2);
  67. char *fq_uri;
  68. LUA_PCHECK (
  69. LSUP_nsmap_normalize_uri (nsm, pfx_uri, &fq_uri),
  70. "Error normalizing URI");
  71. if (fq_uri) lua_pushstring (L, fq_uri);
  72. else return 0;
  73. free (fq_uri);
  74. return 1;
  75. }
  76. static int l_nsmap_denormalize_uri (lua_State *L)
  77. {
  78. LSUP_NSMap *nsm = check_nsm (L);
  79. const char *fq_uri = luaL_checkstring (L, 2);
  80. char *pfx_uri;
  81. LUA_PCHECK (
  82. LSUP_nsmap_denormalize_uri (nsm, fq_uri, &pfx_uri),
  83. "Error denormalizing URI");
  84. if (pfx_uri) lua_pushstring (L, pfx_uri);
  85. else return 0;
  86. free (pfx_uri);
  87. return 1;
  88. }
  89. static int l_nsmap_iter (lua_State *L)
  90. {
  91. LSUP_NSMap *nsm = check_nsm (L);
  92. // TODO
  93. return 0;
  94. }
  95. /*
  96. * Library setup.
  97. */
  98. static const luaL_Reg nsmap_lib_fn [] = {
  99. {"new", l_nsmap_new},
  100. {NULL}
  101. };
  102. static const luaL_Reg nsmap_lib_meth [] = {
  103. {"__gc", l_nsmap_gc},
  104. {"add", l_nsmap_add},
  105. {"remove", l_nsmap_remove},
  106. {"get_ns", l_nsmap_get_ns},
  107. {"get_pfx", l_nsmap_get_pfx},
  108. {"normalize_uri", l_nsmap_normalize_uri},
  109. {"denormalize_uri", l_nsmap_denormalize_uri},
  110. //{"iter", l_nsmap_iter},
  111. {NULL}
  112. };
  113. int luaopen_lsup_namespace (lua_State *L)
  114. {
  115. luaL_newmetatable (L, "LSUP.NSMap");
  116. lua_pushvalue (L, -1);
  117. lua_setfield (L, -2, "__index");
  118. luaL_setfuncs (L, nsmap_lib_meth, 0);
  119. luaL_newlib (L, nsmap_lib_fn);
  120. return 1;
  121. }