wrap_File.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Copyright (c) 2006-2017 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 "common/Data.h"
  22. #include "common/Exception.h"
  23. #include "common/int.h"
  24. namespace love
  25. {
  26. namespace filesystem
  27. {
  28. int luax_ioError(lua_State *L, const char *fmt, ...)
  29. {
  30. va_list args;
  31. va_start(args, fmt);
  32. lua_pushnil(L);
  33. lua_pushvfstring(L, fmt, args);
  34. va_end(args);
  35. return 2;
  36. }
  37. File *luax_checkfile(lua_State *L, int idx)
  38. {
  39. return luax_checktype<File>(L, idx);
  40. }
  41. int w_File_getSize(lua_State *L)
  42. {
  43. File *t = luax_checkfile(L, 1);
  44. int64 size = -1;
  45. try
  46. {
  47. size = t->getSize();
  48. }
  49. catch (love::Exception &e)
  50. {
  51. return luax_ioError(L, "%s", e.what());
  52. }
  53. // Push nil on failure or if size does not fit into a double precision floating-point number.
  54. if (size == -1)
  55. return luax_ioError(L, "Could not determine file size.");
  56. else if (size >= 0x20000000000000LL)
  57. return luax_ioError(L, "Size is too large.");
  58. lua_pushnumber(L, (lua_Number) size);
  59. return 1;
  60. }
  61. int w_File_open(lua_State *L)
  62. {
  63. File *file = luax_checkfile(L, 1);
  64. const char *str = luaL_checkstring(L, 2);
  65. File::Mode mode;
  66. if (!File::getConstant(str, mode))
  67. return luax_enumerror(L, "file open mode", File::getConstants(mode), str);
  68. try
  69. {
  70. luax_pushboolean(L, file->open(mode));
  71. }
  72. catch (love::Exception &e)
  73. {
  74. return luax_ioError(L, "%s", e.what());
  75. }
  76. return 1;
  77. }
  78. int w_File_close(lua_State *L)
  79. {
  80. File *file = luax_checkfile(L, 1);
  81. luax_pushboolean(L, file->close());
  82. return 1;
  83. }
  84. int w_File_isOpen(lua_State *L)
  85. {
  86. File *file = luax_checkfile(L, 1);
  87. luax_pushboolean(L, file->isOpen());
  88. return 1;
  89. }
  90. int w_File_read(lua_State *L)
  91. {
  92. File *file = luax_checkfile(L, 1);
  93. StrongRef<Data> d = nullptr;
  94. int64 size = (int64) luaL_optnumber(L, 2, (lua_Number) File::ALL);
  95. try
  96. {
  97. d.set(file->read(size), Acquire::NORETAIN);
  98. }
  99. catch (love::Exception &e)
  100. {
  101. return luax_ioError(L, "%s", e.what());
  102. }
  103. lua_pushlstring(L, (const char *) d->getData(), d->getSize());
  104. lua_pushnumber(L, d->getSize());
  105. return 2;
  106. }
  107. int w_File_write(lua_State *L)
  108. {
  109. File *file = luax_checkfile(L, 1);
  110. bool result = false;
  111. if (lua_isstring(L, 2))
  112. {
  113. try
  114. {
  115. size_t datasize = 0;
  116. const char *data = lua_tolstring(L, 2, &datasize);
  117. if (!lua_isnoneornil(L, 3))
  118. datasize = luaL_checkinteger(L, 3);
  119. result = file->write(data, datasize);
  120. }
  121. catch (love::Exception &e)
  122. {
  123. return luax_ioError(L, "%s", e.what());
  124. }
  125. }
  126. else if (luax_istype(L, 2, love::Data::type))
  127. {
  128. try
  129. {
  130. love::Data *data = luax_totype<love::Data>(L, 2);
  131. result = file->write(data, luaL_optinteger(L, 3, data->getSize()));
  132. }
  133. catch (love::Exception &e)
  134. {
  135. return luax_ioError(L, "%s", e.what());
  136. }
  137. }
  138. else
  139. {
  140. return luaL_argerror(L, 2, "string or data expected");
  141. }
  142. luax_pushboolean(L, result);
  143. return 1;
  144. }
  145. int w_File_flush(lua_State *L)
  146. {
  147. File *file = luax_checkfile(L, 1);
  148. bool success = false;
  149. try
  150. {
  151. success = file->flush();
  152. }
  153. catch (love::Exception &e)
  154. {
  155. return luax_ioError(L, "%s", e.what());
  156. }
  157. luax_pushboolean(L, success);
  158. return 1;
  159. }
  160. int w_File_isEOF(lua_State *L)
  161. {
  162. File *file = luax_checkfile(L, 1);
  163. luax_pushboolean(L, file->isEOF());
  164. return 1;
  165. }
  166. int w_File_tell(lua_State *L)
  167. {
  168. File *file = luax_checkfile(L, 1);
  169. int64 pos = file->tell();
  170. // Push nil on failure or if pos does not fit into a double precision floating-point number.
  171. if (pos == -1)
  172. return luax_ioError(L, "Invalid position.");
  173. else if (pos >= 0x20000000000000LL)
  174. return luax_ioError(L, "Number is too large.");
  175. else
  176. lua_pushnumber(L, (lua_Number)pos);
  177. return 1;
  178. }
  179. int w_File_seek(lua_State *L)
  180. {
  181. File *file = luax_checkfile(L, 1);
  182. lua_Number pos = luaL_checknumber(L, 2);
  183. // Push false on negative and precision-problematic numbers.
  184. // Better fail than seek to an unknown position.
  185. if (pos < 0.0 || pos >= 9007199254740992.0)
  186. luax_pushboolean(L, false);
  187. else
  188. luax_pushboolean(L, file->seek((uint64)pos));
  189. return 1;
  190. }
  191. int w_File_lines_i(lua_State *L)
  192. {
  193. const int bufsize = 1024;
  194. char buf[bufsize];
  195. int linesize = 0;
  196. bool newline = false;
  197. File *file = luax_checktype<File>(L, lua_upvalueindex(1));
  198. // Only accept read mode at this point.
  199. if (file->getMode() != File::MODE_READ)
  200. return luaL_error(L, "File needs to stay in read mode.");
  201. int64 pos = file->tell();
  202. int64 userpos = -1;
  203. if (lua_isnoneornil(L, lua_upvalueindex(2)) == 0)
  204. {
  205. // User may have changed the file position.
  206. userpos = pos;
  207. pos = (int64) lua_tonumber(L, lua_upvalueindex(2));
  208. if (userpos != pos)
  209. file->seek(pos);
  210. }
  211. while (!newline && !file->isEOF())
  212. {
  213. // This 64-bit to 32-bit integer cast should be safe as it never exceeds bufsize.
  214. int read = (int) file->read(buf, bufsize);
  215. if (read < 0)
  216. return luaL_error(L, "Could not read from file.");
  217. linesize += read;
  218. for (int i = 0; i < read; i++)
  219. {
  220. if (buf[i] == '\n')
  221. {
  222. linesize -= read - i;
  223. newline = true;
  224. break;
  225. }
  226. }
  227. }
  228. if (newline || (file->isEOF() && linesize > 0))
  229. {
  230. if (linesize < bufsize)
  231. {
  232. // We have the line in the buffer on the stack. No 'new' and 'read' needed.
  233. lua_pushlstring(L, buf, linesize > 0 && buf[linesize - 1] == '\r' ? linesize - 1 : linesize);
  234. if (userpos < 0)
  235. file->seek(pos + linesize + 1);
  236. }
  237. else
  238. {
  239. char *str = 0;
  240. try
  241. {
  242. str = new char[linesize + 1];
  243. }
  244. catch(std::bad_alloc &)
  245. {
  246. // Can't lua_error (longjmp) in exception handlers.
  247. }
  248. if (!str)
  249. return luaL_error(L, "Out of memory.");
  250. file->seek(pos);
  251. // Read the \n anyway and save us a call to seek.
  252. if (file->read(str, linesize + 1) == -1)
  253. {
  254. delete [] str;
  255. return luaL_error(L, "Could not read from file.");
  256. }
  257. lua_pushlstring(L, str, str[linesize - 1] == '\r' ? linesize - 1 : linesize);
  258. delete [] str;
  259. }
  260. if (userpos >= 0)
  261. {
  262. // Save new position in upvalue.
  263. lua_pushnumber(L, (lua_Number)(pos + linesize + 1));
  264. lua_replace(L, lua_upvalueindex(2));
  265. file->seek(userpos);
  266. }
  267. return 1;
  268. }
  269. // EOF reached.
  270. if (userpos >= 0 && luax_toboolean(L, lua_upvalueindex(3)))
  271. file->seek(userpos);
  272. else
  273. file->close();
  274. return 0;
  275. }
  276. int w_File_lines(lua_State *L)
  277. {
  278. File *file = luax_checkfile(L, 1);
  279. lua_pushnumber(L, 0); // File position.
  280. luax_pushboolean(L, file->getMode() != File::MODE_CLOSED); // Save current file mode.
  281. if (file->getMode() != File::MODE_READ)
  282. {
  283. if (file->getMode() != File::MODE_CLOSED)
  284. file->close();
  285. bool success = false;
  286. luax_catchexcept(L, [&](){ success = file->open(File::MODE_READ); });
  287. if (!success)
  288. return luaL_error(L, "Could not open file.");
  289. }
  290. lua_pushcclosure(L, w_File_lines_i, 3);
  291. return 1;
  292. }
  293. int w_File_setBuffer(lua_State *L)
  294. {
  295. File *file = luax_checkfile(L, 1);
  296. const char *str = luaL_checkstring(L, 2);
  297. int64 size = (int64) luaL_optnumber(L, 3, 0.0);
  298. File::BufferMode bufmode;
  299. if (!File::getConstant(str, bufmode))
  300. return luax_enumerror(L, "file buffer mode", File::getConstants(bufmode), str);
  301. bool success = false;
  302. try
  303. {
  304. success = file->setBuffer(bufmode, size);
  305. }
  306. catch (love::Exception &e)
  307. {
  308. return luax_ioError(L, "%s", e.what());
  309. }
  310. luax_pushboolean(L, success);
  311. return 1;
  312. }
  313. int w_File_getBuffer(lua_State *L)
  314. {
  315. File *file = luax_checkfile(L, 1);
  316. int64 size = 0;
  317. File::BufferMode bufmode = file->getBuffer(size);
  318. const char *str = 0;
  319. if (!File::getConstant(bufmode, str))
  320. return luax_ioError(L, "Unknown file buffer mode.");
  321. lua_pushstring(L, str);
  322. lua_pushnumber(L, (lua_Number) size);
  323. return 2;
  324. }
  325. int w_File_getMode(lua_State *L)
  326. {
  327. File *file = luax_checkfile(L, 1);
  328. File::Mode mode = file->getMode();
  329. const char *str = 0;
  330. if (!File::getConstant(mode, str))
  331. return luax_ioError(L, "Unknown file mode.");
  332. lua_pushstring(L, str);
  333. return 1;
  334. }
  335. int w_File_getFilename(lua_State *L)
  336. {
  337. File *file = luax_checkfile(L, 1);
  338. luax_pushstring(L, file->getFilename());
  339. return 1;
  340. }
  341. int w_File_getExtension(lua_State *L)
  342. {
  343. File *file = luax_checkfile(L, 1);
  344. luax_pushstring(L, file->getExtension());
  345. return 1;
  346. }
  347. const luaL_Reg w_File_functions[] =
  348. {
  349. { "getSize", w_File_getSize },
  350. { "open", w_File_open },
  351. { "close", w_File_close },
  352. { "isOpen", w_File_isOpen },
  353. { "read", w_File_read },
  354. { "write", w_File_write },
  355. { "flush", w_File_flush },
  356. { "isEOF", w_File_isEOF },
  357. { "tell", w_File_tell },
  358. { "seek", w_File_seek },
  359. { "lines", w_File_lines },
  360. { "setBuffer", w_File_setBuffer },
  361. { "getBuffer", w_File_getBuffer },
  362. { "getMode", w_File_getMode },
  363. { "getFilename", w_File_getFilename },
  364. { "getExtension", w_File_getExtension },
  365. { 0, 0 }
  366. };
  367. extern "C" int luaopen_file(lua_State *L)
  368. {
  369. return luax_register_type(L, &File::type, w_File_functions, nullptr);
  370. }
  371. } // filesystem
  372. } // love