wrap_File.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "wrap_File.h"
  21. #include <common/Exception.h>
  22. namespace love
  23. {
  24. namespace filesystem
  25. {
  26. namespace physfs
  27. {
  28. File * luax_checkfile(lua_State * L, int idx)
  29. {
  30. return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
  31. }
  32. int w_File_getSize(lua_State * L)
  33. {
  34. File * t = luax_checkfile(L, 1);
  35. lua_pushinteger(L, t->getSize());
  36. return 1;
  37. }
  38. int w_File_open(lua_State * L)
  39. {
  40. File * file = luax_checkfile(L, 1);
  41. int mode = luaL_optint(L, 2, File::READ);
  42. try
  43. {
  44. lua_pushboolean(L, file->open((File::Mode)mode) ? 1 : 0);
  45. }
  46. catch(Exception e)
  47. {
  48. return luaL_error(L, e.what());
  49. }
  50. return 1;
  51. }
  52. int w_File_close(lua_State * L)
  53. {
  54. File * file = luax_checkfile(L, 1);
  55. lua_pushboolean(L, file->close() ? 1 : 0);
  56. return 1;
  57. }
  58. int w_File_read(lua_State * L)
  59. {
  60. File * file = luax_checkfile(L, 1);
  61. Data * d = 0;
  62. try
  63. {
  64. d = file->read(luaL_optint(L, 2, file->getSize()));
  65. }
  66. catch(Exception e)
  67. {
  68. return luaL_error(L, e.what());
  69. }
  70. lua_pushlstring(L, (const char*) d->getData(), d->getSize());
  71. lua_pushnumber(L, d->getSize());
  72. d->release();
  73. return 2;
  74. }
  75. int w_File_write(lua_State * L)
  76. {
  77. File * file = luax_checkfile(L, 1);
  78. bool result;
  79. if ( file->getMode() == File::CLOSED )
  80. return luaL_error(L, "File is not open.");
  81. if ( lua_isstring(L, 2) )
  82. {
  83. try
  84. {
  85. result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
  86. }
  87. catch(Exception e)
  88. {
  89. return luaL_error(L, e.what());
  90. }
  91. }
  92. else
  93. {
  94. return luaL_error(L, "String expected.");
  95. }
  96. lua_pushboolean(L, result);
  97. return 1;
  98. }
  99. int w_File_eof(lua_State * L)
  100. {
  101. File * file = luax_checkfile(L, 1);
  102. lua_pushboolean(L, file->eof() ? 1 : 0);
  103. return 1;
  104. }
  105. int w_File_tell(lua_State * L)
  106. {
  107. File * file = luax_checkfile(L, 1);
  108. lua_pushinteger(L, file->tell());
  109. return 1;
  110. }
  111. int w_File_seek(lua_State * L)
  112. {
  113. File * file = luax_checkfile(L, 1);
  114. int pos = luaL_checkinteger(L, 2);
  115. lua_pushboolean(L, file->seek(pos) ? 1 : 0);
  116. return 1;
  117. }
  118. //yes, the following two are copy-pasted and slightly edited
  119. int w_File_lines(lua_State * L)
  120. {
  121. File * file;
  122. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  123. {
  124. file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
  125. lua_pushboolean(L, 0); // 0 = do not close.
  126. }
  127. else
  128. return luaL_error(L, "Expected file handle.");
  129. // Reset the file position.
  130. if(!file->seek(0))
  131. return luaL_error(L, "File does not appear to be open.\n");
  132. lua_pushcclosure(L, lines_i, 2);
  133. return 1;
  134. }
  135. int lines_i(lua_State * L)
  136. {
  137. // We're using a 1k buffer.
  138. const static int bufsize = 1024;
  139. static char buf[bufsize];
  140. File * file = luax_checktype<File>(L, lua_upvalueindex(1), "File", FILESYSTEM_FILE_T);
  141. int close = (int)lua_tointeger(L, lua_upvalueindex(2));
  142. // Find the next newline.
  143. // pos must be at the start of the line we're trying to find.
  144. int pos = file->tell();
  145. int newline = -1;
  146. int totalread = 0;
  147. while(!file->eof())
  148. {
  149. int current = file->tell();
  150. int read = file->read(buf, bufsize);
  151. totalread += read;
  152. if(read < 0)
  153. return luaL_error(L, "Readline failed!");
  154. for(int i = 0;i<read;i++)
  155. {
  156. if(buf[i] == '\n')
  157. {
  158. newline = current+i;
  159. break;
  160. }
  161. }
  162. if(newline > 0)
  163. break;
  164. }
  165. // Special case for the last "line".
  166. if(newline <= 0 && file->eof() && totalread > 0)
  167. newline = pos + totalread;
  168. // We've got a newline.
  169. if(newline > 0)
  170. {
  171. // Ok, we've got a line.
  172. int linesize = (newline-pos);
  173. // Allocate memory for the string.
  174. char * str = new char[linesize];
  175. // Read it.
  176. file->seek(pos);
  177. if(file->read(str, linesize) == -1)
  178. return luaL_error(L, "Read error.");
  179. if(str[linesize-1]=='\r')
  180. linesize -= 1;
  181. lua_pushlstring(L, str, linesize);
  182. // Free the memory. Lua has a copy now.
  183. delete[] str;
  184. // Set the beginning of the next line.
  185. if(!file->eof())
  186. file->seek(newline+1);
  187. return 1;
  188. }
  189. if(close)
  190. {
  191. file->close();
  192. file->release();
  193. }
  194. // else: (newline <= 0)
  195. return 0;
  196. }
  197. static const luaL_Reg functions[] = {
  198. { "getSize", w_File_getSize },
  199. { "open", w_File_open },
  200. { "close", w_File_close },
  201. { "read", w_File_read },
  202. { "write", w_File_write },
  203. { "eof", w_File_eof },
  204. { "tell", w_File_tell },
  205. { "seek", w_File_seek },
  206. { "lines", w_File_lines },
  207. { 0, 0 }
  208. };
  209. int luaopen_file(lua_State * L)
  210. {
  211. return luax_register_type(L, "File", functions);
  212. }
  213. } // physfs
  214. } // filesystem
  215. } // love