wrap_File.cpp 6.8 KB

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