wrap_Filesystem.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Filesystem.h"
  22. namespace love
  23. {
  24. namespace filesystem
  25. {
  26. namespace physfs
  27. {
  28. static Filesystem * instance = 0;
  29. bool hack_setupWriteDirectory()
  30. {
  31. if(instance != 0)
  32. return instance->setupWriteDirectory();
  33. return false;
  34. }
  35. int _wrap_setIdentity(lua_State * L)
  36. {
  37. const char * arg = luaL_checkstring(L, 1);
  38. if(!instance->setIdentity(arg))
  39. return luaL_error(L, "Could not set write directory.");
  40. return 0;
  41. }
  42. int _wrap_setSource(lua_State * L)
  43. {
  44. const char * arg = luaL_checkstring(L, 1);
  45. if(!instance->setSource(arg))
  46. return luaL_error(L, "Could not set source.");
  47. return 0;
  48. }
  49. int _wrap_newFile(lua_State * L)
  50. {
  51. const char * filename = luaL_checkstring(L, 1);
  52. File * t = instance->newFile(filename);
  53. luax_newtype(L, "File", LOVE_FILESYSTEM_FILE_BITS, (void*)t);
  54. return 1;
  55. }
  56. int _wrap_newFileData(lua_State * L)
  57. {
  58. if(!lua_isstring(L, 1))
  59. return luaL_error(L, "String expected.");
  60. if(!lua_isstring(L, 2))
  61. return luaL_error(L, "String expected.");
  62. size_t length = 0;
  63. const char * str = lua_tolstring(L, 1, &length);
  64. const char * filename = lua_tostring(L, 2);
  65. FileData * t = instance->newFileData((void*)str, (int)length, filename);
  66. luax_newtype(L, "FileData", LOVE_FILESYSTEM_FILE_DATA_BITS, (void*)t);
  67. return 1;
  68. }
  69. int _wrap_getWorkingDirectory(lua_State * L)
  70. {
  71. lua_pushstring(L, instance->getWorkingDirectory());
  72. return 1;
  73. }
  74. int _wrap_getUserDirectory(lua_State * L)
  75. {
  76. lua_pushstring(L, instance->getUserDirectory());
  77. return 1;
  78. }
  79. int _wrap_getAppdataDirectory(lua_State * L)
  80. {
  81. lua_pushstring(L, instance->getAppdataDirectory());
  82. return 1;
  83. }
  84. int _wrap_getSaveDirectory(lua_State * L)
  85. {
  86. lua_pushstring(L, instance->getSaveDirectory());
  87. return 1;
  88. }
  89. int _wrap_exists(lua_State * L)
  90. {
  91. const char * arg = luaL_checkstring(L, 1);
  92. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  93. return 1;
  94. }
  95. int _wrap_isDirectory(lua_State * L)
  96. {
  97. const char * arg = luaL_checkstring(L, 1);
  98. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  99. return 1;
  100. }
  101. int _wrap_isFile(lua_State * L)
  102. {
  103. const char * arg = luaL_checkstring(L, 1);
  104. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  105. return 1;
  106. }
  107. int _wrap_mkdir(lua_State * L)
  108. {
  109. const char * arg = luaL_checkstring(L, 1);
  110. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  111. return 1;
  112. }
  113. int _wrap_remove(lua_State * L)
  114. {
  115. const char * arg = luaL_checkstring(L, 1);
  116. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  117. return 1;
  118. }
  119. int _wrap_read(lua_State * L)
  120. {
  121. return instance->read(L);
  122. }
  123. int _wrap_write(lua_State * L)
  124. {
  125. return instance->write(L);
  126. }
  127. int _wrap_enumerate(lua_State * L)
  128. {
  129. return instance->enumerate(L);
  130. }
  131. int _wrap_lines(lua_State * L)
  132. {
  133. return instance->lines(L);
  134. }
  135. int _wrap_load(lua_State * L)
  136. {
  137. return instance->load(L);
  138. }
  139. int loader(lua_State * L)
  140. {
  141. const char * filename = lua_tostring(L, -1);
  142. std::string tmp(filename);
  143. int size = tmp.size();
  144. if(size <= 4 || strcmp(filename + (size-4), ".lua") != 0)
  145. tmp.append(".lua");
  146. for(int i=0;i<size-4;i++)
  147. {
  148. if(tmp[i] == '.')
  149. {
  150. tmp[i] = '/';
  151. }
  152. }
  153. // Check whether file exists.
  154. if(!instance->exists(tmp.c_str()))
  155. {
  156. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", tmp.c_str());
  157. return 1;
  158. }
  159. lua_pop(L, 1);
  160. lua_pushstring(L, tmp.c_str());
  161. // Ok, load it.
  162. return instance->load(L);
  163. }
  164. // List of functions to wrap.
  165. const luaL_Reg wrap_Filesystem_functions[] = {
  166. { "setIdentity", _wrap_setIdentity },
  167. { "setSource", _wrap_setSource },
  168. { "newFile", _wrap_newFile },
  169. { "getWorkingDirectory", _wrap_getWorkingDirectory },
  170. { "getUserDirectory", _wrap_getUserDirectory },
  171. { "getAppdataDirectory", _wrap_getAppdataDirectory },
  172. { "getSaveDirectory", _wrap_getSaveDirectory },
  173. { "exists", _wrap_exists },
  174. { "isDirectory", _wrap_isDirectory },
  175. { "isFile", _wrap_isFile },
  176. { "mkdir", _wrap_mkdir },
  177. { "remove", _wrap_remove },
  178. { "read", _wrap_read },
  179. { "write", _wrap_write },
  180. { "enumerate", _wrap_enumerate },
  181. { "lines", _wrap_lines },
  182. { "load", _wrap_load },
  183. { 0, 0 }
  184. };
  185. const lua_CFunction wrap_Filesystem_types[] = {
  186. wrap_File_open,
  187. wrap_FileData_open,
  188. 0
  189. };
  190. int wrap_Filesystem_open(lua_State * L)
  191. {
  192. if(instance == 0)
  193. {
  194. try
  195. {
  196. instance = new Filesystem();
  197. love::luax_register_searcher(L, loader);
  198. }
  199. catch(Exception & e)
  200. {
  201. return luaL_error(L, e.what());
  202. }
  203. }
  204. luax_register_gc(L, "love.filesystem", instance);
  205. return luax_register_module(L, wrap_Filesystem_functions, wrap_Filesystem_types, "filesystem");
  206. }
  207. } // physfs
  208. } // filesystem
  209. } // love