wrap_Filesystem.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 w_init(lua_State * L)
  36. {
  37. const char * arg0 = luaL_checkstring(L, 1);
  38. try
  39. {
  40. instance->init(arg0);
  41. }
  42. catch(Exception & e)
  43. {
  44. return luaL_error(L, e.what());
  45. }
  46. return 0;
  47. }
  48. int w_setIdentity(lua_State * L)
  49. {
  50. const char * arg = luaL_checkstring(L, 1);
  51. if(!instance->setIdentity(arg))
  52. return luaL_error(L, "Could not set write directory.");
  53. return 0;
  54. }
  55. int w_setSource(lua_State * L)
  56. {
  57. const char * arg = luaL_checkstring(L, 1);
  58. if(!instance->setSource(arg))
  59. return luaL_error(L, "Could not set source.");
  60. return 0;
  61. }
  62. int w_newFile(lua_State * L)
  63. {
  64. const char * filename = luaL_checkstring(L, 1);
  65. File * t;
  66. try
  67. {
  68. t = instance->newFile(filename);
  69. }
  70. catch(Exception e)
  71. {
  72. return luaL_error(L, e.what());
  73. }
  74. luax_newtype(L, "File", FILESYSTEM_FILE_T, (void*)t);
  75. return 1;
  76. }
  77. int w_newFileData(lua_State * L)
  78. {
  79. if(!lua_isstring(L, 1))
  80. return luaL_error(L, "String expected.");
  81. if(!lua_isstring(L, 2))
  82. return luaL_error(L, "String expected.");
  83. size_t length = 0;
  84. const char * str = lua_tolstring(L, 1, &length);
  85. const char * filename = lua_tostring(L, 2);
  86. FileData * t = instance->newFileData((void*)str, (int)length, filename);
  87. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t);
  88. return 1;
  89. }
  90. int w_getWorkingDirectory(lua_State * L)
  91. {
  92. lua_pushstring(L, instance->getWorkingDirectory());
  93. return 1;
  94. }
  95. int w_getUserDirectory(lua_State * L)
  96. {
  97. lua_pushstring(L, instance->getUserDirectory());
  98. return 1;
  99. }
  100. int w_getAppdataDirectory(lua_State * L)
  101. {
  102. lua_pushstring(L, instance->getAppdataDirectory());
  103. return 1;
  104. }
  105. int w_getSaveDirectory(lua_State * L)
  106. {
  107. lua_pushstring(L, instance->getSaveDirectory());
  108. return 1;
  109. }
  110. int w_exists(lua_State * L)
  111. {
  112. const char * arg = luaL_checkstring(L, 1);
  113. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  114. return 1;
  115. }
  116. int w_isDirectory(lua_State * L)
  117. {
  118. const char * arg = luaL_checkstring(L, 1);
  119. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  120. return 1;
  121. }
  122. int w_isFile(lua_State * L)
  123. {
  124. const char * arg = luaL_checkstring(L, 1);
  125. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  126. return 1;
  127. }
  128. int w_mkdir(lua_State * L)
  129. {
  130. const char * arg = luaL_checkstring(L, 1);
  131. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  132. return 1;
  133. }
  134. int w_remove(lua_State * L)
  135. {
  136. const char * arg = luaL_checkstring(L, 1);
  137. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  138. return 1;
  139. }
  140. int w_read(lua_State * L)
  141. {
  142. return instance->read(L);
  143. }
  144. int w_write(lua_State * L)
  145. {
  146. return instance->write(L);
  147. }
  148. int w_enumerate(lua_State * L)
  149. {
  150. return instance->enumerate(L);
  151. }
  152. int w_lines(lua_State * L)
  153. {
  154. return instance->lines(L);
  155. }
  156. int w_load(lua_State * L)
  157. {
  158. return instance->load(L);
  159. }
  160. int loader(lua_State * L)
  161. {
  162. const char * filename = lua_tostring(L, -1);
  163. std::string tmp(filename);
  164. int size = tmp.size();
  165. if(size <= 4 || strcmp(filename + (size-4), ".lua") != 0)
  166. tmp.append(".lua");
  167. for(int i=0;i<size-4;i++)
  168. {
  169. if(tmp[i] == '.')
  170. {
  171. tmp[i] = '/';
  172. }
  173. }
  174. // Check whether file exists.
  175. if(!instance->exists(tmp.c_str()))
  176. {
  177. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", tmp.c_str());
  178. return 1;
  179. }
  180. lua_pop(L, 1);
  181. lua_pushstring(L, tmp.c_str());
  182. // Ok, load it.
  183. return instance->load(L);
  184. }
  185. // List of functions to wrap.
  186. static const luaL_Reg functions[] = {
  187. { "init", w_init },
  188. { "setIdentity", w_setIdentity },
  189. { "setSource", w_setSource },
  190. { "newFile", w_newFile },
  191. { "getWorkingDirectory", w_getWorkingDirectory },
  192. { "getUserDirectory", w_getUserDirectory },
  193. { "getAppdataDirectory", w_getAppdataDirectory },
  194. { "getSaveDirectory", w_getSaveDirectory },
  195. { "exists", w_exists },
  196. { "isDirectory", w_isDirectory },
  197. { "isFile", w_isFile },
  198. { "mkdir", w_mkdir },
  199. { "remove", w_remove },
  200. { "read", w_read },
  201. { "write", w_write },
  202. { "enumerate", w_enumerate },
  203. { "lines", w_lines },
  204. { "load", w_load },
  205. { 0, 0 }
  206. };
  207. static const lua_CFunction types[] = {
  208. luaopen_file,
  209. luaopen_filedata,
  210. 0
  211. };
  212. // List of constants.
  213. static const LuaConstant constants[] = {
  214. { "file_closed", File::CLOSED },
  215. { "file_read", File::READ },
  216. { "file_write", File::WRITE },
  217. { "file_append", File::APPEND },
  218. { 0, 0 }
  219. };
  220. int luaopen_love_filesystem(lua_State * L)
  221. {
  222. if(instance == 0)
  223. {
  224. try
  225. {
  226. instance = new Filesystem();
  227. love::luax_register_searcher(L, loader);
  228. }
  229. catch(Exception & e)
  230. {
  231. return luaL_error(L, e.what());
  232. }
  233. }
  234. luax_register_gc(L, instance);
  235. return luax_register_module(L, functions, types, constants, "filesystem");
  236. }
  237. } // physfs
  238. } // filesystem
  239. } // love