wrap_File.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright (c) 2006-2014 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. #include "wrap_File.h"
  21. #include "physfs/Filesystem.h"
  22. #include "common/Data.h"
  23. #include "common/Exception.h"
  24. #include "common/int.h"
  25. namespace love
  26. {
  27. namespace filesystem
  28. {
  29. int luax_ioError(lua_State *L, const char *fmt, ...)
  30. {
  31. va_list args;
  32. va_start(args, fmt);
  33. lua_pushnil(L);
  34. lua_pushvfstring(L, fmt, args);
  35. va_end(args);
  36. return 2;
  37. }
  38. File *luax_checkfile(lua_State *L, int idx)
  39. {
  40. return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
  41. }
  42. int w_File_getSize(lua_State *L)
  43. {
  44. File *t = luax_checkfile(L, 1);
  45. int64 size = -1;
  46. try
  47. {
  48. size = t->getSize();
  49. }
  50. catch (love::Exception &e)
  51. {
  52. return luax_ioError(L, "%s", e.what());
  53. }
  54. // Push nil on failure or if size does not fit into a double precision floating-point number.
  55. if (size == -1)
  56. return luax_ioError(L, "Could not determine file size.");
  57. else if (size >= 0x20000000000000LL)
  58. return luax_ioError(L, "Size is too large.");
  59. lua_pushnumber(L, (lua_Number) size);
  60. return 1;
  61. }
  62. int w_File_open(lua_State *L)
  63. {
  64. File *file = luax_checkfile(L, 1);
  65. const char *str = luaL_checkstring(L, 2);
  66. File::Mode mode;
  67. if (!File::getConstant(str, mode))
  68. return luaL_error(L, "Incorrect file open mode: %s", str);
  69. try
  70. {
  71. luax_pushboolean(L, file->open(mode));
  72. }
  73. catch (love::Exception &e)
  74. {
  75. return luax_ioError(L, "%s", e.what());
  76. }
  77. return 1;
  78. }
  79. int w_File_close(lua_State *L)
  80. {
  81. File *file = luax_checkfile(L, 1);
  82. luax_pushboolean(L, file->close());
  83. return 1;
  84. }
  85. int w_File_isOpen(lua_State *L)
  86. {
  87. File *file = luax_checkfile(L, 1);
  88. luax_pushboolean(L, file->isOpen());
  89. return 1;
  90. }
  91. int w_File_read(lua_State *L)
  92. {
  93. File *file = luax_checkfile(L, 1);
  94. Data *d = 0;
  95. int64 size = (int64)luaL_optnumber(L, 2, File::ALL);
  96. try
  97. {
  98. d = file->read(size);
  99. }
  100. catch (love::Exception &e)
  101. {
  102. return luax_ioError(L, "%s", e.what());
  103. }
  104. lua_pushlstring(L, (const char *) d->getData(), d->getSize());
  105. lua_pushnumber(L, d->getSize());
  106. d->release();
  107. return 2;
  108. }
  109. int w_File_write(lua_State *L)
  110. {
  111. File *file = luax_checkfile(L, 1);
  112. bool result = false;
  113. if (lua_isstring(L, 2))
  114. {
  115. try
  116. {
  117. size_t datasize = 0;
  118. const char *data = lua_tolstring(L, 2, &datasize);
  119. if (!lua_isnoneornil(L, 3))
  120. datasize = luaL_checkinteger(L, 3);
  121. result = file->write(data, datasize);
  122. }
  123. catch (love::Exception &e)
  124. {
  125. return luax_ioError(L, "%s", e.what());
  126. }
  127. }
  128. else if (luax_istype(L, 2, DATA_T))
  129. {
  130. try
  131. {
  132. love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
  133. result = file->write(data, luaL_optinteger(L, 3, data->getSize()));
  134. }
  135. catch (love::Exception &e)
  136. {
  137. return luax_ioError(L, "%s", e.what());
  138. }
  139. }
  140. else
  141. {
  142. return luaL_argerror(L, 2, "string or data expected");
  143. }
  144. luax_pushboolean(L, result);
  145. return 1;
  146. }
  147. int w_File_flush(lua_State *L)
  148. {
  149. File *file = luax_checkfile(L, 1);
  150. bool success = false;
  151. try
  152. {
  153. success = file->flush();
  154. }
  155. catch (love::Exception &e)
  156. {
  157. return luax_ioError(L, "%s", e.what());
  158. }
  159. luax_pushboolean(L, success);
  160. return 1;
  161. }
  162. int w_File_eof(lua_State *L)
  163. {
  164. File *file = luax_checkfile(L, 1);
  165. luax_pushboolean(L, file->eof());
  166. return 1;
  167. }
  168. int w_File_tell(lua_State *L)
  169. {
  170. File *file = luax_checkfile(L, 1);
  171. int64 pos = file->tell();
  172. // Push nil on failure or if pos does not fit into a double precision floating-point number.
  173. if (pos == -1)
  174. return luax_ioError(L, "Invalid position.");
  175. else if (pos >= 0x20000000000000LL)
  176. return luax_ioError(L, "Number is too large.");
  177. else
  178. lua_pushnumber(L, (lua_Number)pos);
  179. return 1;
  180. }
  181. int w_File_seek(lua_State *L)
  182. {
  183. File *file = luax_checkfile(L, 1);
  184. lua_Number pos = luaL_checknumber(L, 2);
  185. // Push false on negative and precision-problematic numbers.
  186. // Better fail than seek to an unknown position.
  187. if (pos < 0.0 || pos >= 9007199254740992.0)
  188. luax_pushboolean(L, false);
  189. else
  190. luax_pushboolean(L, file->seek((uint64)pos));
  191. return 1;
  192. }
  193. int w_File_lines(lua_State *L)
  194. {
  195. File *file = luax_checkfile(L, 1);
  196. lua_pushnumber(L, 0); // File position.
  197. luax_pushboolean(L, file->getMode() != File::CLOSED); // Save current file mode.
  198. if (file->getMode() != File::READ)
  199. {
  200. if (file->getMode() != File::CLOSED)
  201. file->close();
  202. bool success = false;
  203. EXCEPT_GUARD(success = file->open(File::READ);)
  204. if (!success)
  205. return luaL_error(L, "Could not open file.");
  206. }
  207. lua_pushcclosure(L, physfs::Filesystem::lines_i, 3);
  208. return 1;
  209. }
  210. int w_File_setBuffer(lua_State *L)
  211. {
  212. File *file = luax_checkfile(L, 1);
  213. const char *str = luaL_checkstring(L, 2);
  214. int64 size = (int64) luaL_optnumber(L, 3, 0.0);
  215. File::BufferMode bufmode;
  216. if (!File::getConstant(str, bufmode))
  217. return luaL_error(L, "Incorrect file buffer mode: %s", str);
  218. bool success = false;
  219. try
  220. {
  221. success = file->setBuffer(bufmode, size);
  222. }
  223. catch (love::Exception &e)
  224. {
  225. return luax_ioError(L, "%s", e.what());
  226. }
  227. luax_pushboolean(L, success);
  228. return 1;
  229. }
  230. int w_File_getBuffer(lua_State *L)
  231. {
  232. File *file = luax_checkfile(L, 1);
  233. int64 size = 0;
  234. File::BufferMode bufmode = file->getBuffer(size);
  235. const char *str = 0;
  236. if (!File::getConstant(bufmode, str))
  237. return luax_ioError(L, "Unknown file buffer mode.");
  238. lua_pushstring(L, str);
  239. lua_pushnumber(L, (lua_Number) size);
  240. return 2;
  241. }
  242. int w_File_getMode(lua_State *L)
  243. {
  244. File *file = luax_checkfile(L, 1);
  245. File::Mode mode = file->getMode();
  246. const char *str = 0;
  247. if (!File::getConstant(mode, str))
  248. return luax_ioError(L, "Unknown file mode.");
  249. lua_pushstring(L, str);
  250. return 1;
  251. }
  252. static const luaL_Reg functions[] =
  253. {
  254. { "getSize", w_File_getSize },
  255. { "open", w_File_open },
  256. { "close", w_File_close },
  257. { "isOpen", w_File_isOpen },
  258. { "read", w_File_read },
  259. { "write", w_File_write },
  260. { "flush", w_File_flush },
  261. { "eof", w_File_eof },
  262. { "tell", w_File_tell },
  263. { "seek", w_File_seek },
  264. { "lines", w_File_lines },
  265. { "setBuffer", w_File_setBuffer },
  266. { "getBuffer", w_File_getBuffer },
  267. { "getMode", w_File_getMode },
  268. { 0, 0 }
  269. };
  270. extern "C" int luaopen_file(lua_State *L)
  271. {
  272. return luax_register_type(L, "File", functions);
  273. }
  274. } // filesystem
  275. } // love