wrap_File.cpp 6.4 KB

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