wrap_File.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2006-2011 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. namespace love
  24. {
  25. namespace filesystem
  26. {
  27. namespace physfs
  28. {
  29. File * luax_checkfile(lua_State * L, int idx)
  30. {
  31. return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
  32. }
  33. int w_File_getSize(lua_State * L)
  34. {
  35. File * t = luax_checkfile(L, 1);
  36. try
  37. {
  38. lua_pushinteger(L, t->getSize());
  39. }
  40. catch (Exception & e)
  41. {
  42. return luaL_error(L, e.what());
  43. }
  44. return 1;
  45. }
  46. int w_File_open(lua_State * L)
  47. {
  48. File * file = luax_checkfile(L, 1);
  49. File::Mode mode;
  50. if(!File::getConstant(luaL_checkstring(L, 2), mode))
  51. return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
  52. try
  53. {
  54. lua_pushboolean(L, file->open(mode) ? 1 : 0);
  55. }
  56. catch(Exception e)
  57. {
  58. return luaL_error(L, e.what());
  59. }
  60. return 1;
  61. }
  62. int w_File_close(lua_State * L)
  63. {
  64. File * file = luax_checkfile(L, 1);
  65. lua_pushboolean(L, file->close() ? 1 : 0);
  66. return 1;
  67. }
  68. int w_File_read(lua_State * L)
  69. {
  70. File * file = luax_checkfile(L, 1);
  71. Data * d = 0;
  72. try
  73. {
  74. d = file->read(luaL_optint(L, 2, file->getSize()));
  75. }
  76. catch(Exception e)
  77. {
  78. return luaL_error(L, e.what());
  79. }
  80. lua_pushlstring(L, (const char*) d->getData(), d->getSize());
  81. lua_pushnumber(L, d->getSize());
  82. d->release();
  83. return 2;
  84. }
  85. int w_File_write(lua_State * L)
  86. {
  87. File * file = luax_checkfile(L, 1);
  88. bool result;
  89. if ( file->getMode() == File::CLOSED )
  90. return luaL_error(L, "File is not open.");
  91. if ( lua_isstring(L, 2) )
  92. {
  93. try
  94. {
  95. result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
  96. }
  97. catch(Exception e)
  98. {
  99. return luaL_error(L, e.what());
  100. }
  101. }
  102. else if( luax_istype(L, 2, DATA_T)) {
  103. try
  104. {
  105. love::Data * data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
  106. result = file->write(data, luaL_optint(L, 3, data->getSize()));
  107. } catch(Exception e)
  108. {
  109. return luaL_error(L, e.what());
  110. }
  111. }
  112. else
  113. {
  114. return luaL_error(L, "String or data expected.");
  115. }
  116. lua_pushboolean(L, result);
  117. return 1;
  118. }
  119. int w_File_eof(lua_State * L)
  120. {
  121. File * file = luax_checkfile(L, 1);
  122. lua_pushboolean(L, file->eof() ? 1 : 0);
  123. return 1;
  124. }
  125. int w_File_tell(lua_State * L)
  126. {
  127. File * file = luax_checkfile(L, 1);
  128. lua_pushinteger(L, file->tell());
  129. return 1;
  130. }
  131. int w_File_seek(lua_State * L)
  132. {
  133. File * file = luax_checkfile(L, 1);
  134. int pos = luaL_checkinteger(L, 2);
  135. lua_pushboolean(L, file->seek(pos) ? 1 : 0);
  136. return 1;
  137. }
  138. //yes, the following two are copy-pasted and slightly edited
  139. int w_File_lines(lua_State * L)
  140. {
  141. File * file;
  142. if(luax_istype(L, 1, FILESYSTEM_FILE_T))
  143. {
  144. file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
  145. lua_pushnumber(L, 0); // 0 = do not close.
  146. }
  147. else
  148. return luaL_error(L, "Expected file handle.");
  149. // Reset the file position.
  150. if(!file->seek(0))
  151. return luaL_error(L, "File does not appear to be open.\n");
  152. lua_pushcclosure(L, Filesystem::lines_i, 2);
  153. return 1;
  154. }
  155. static const luaL_Reg functions[] = {
  156. { "getSize", w_File_getSize },
  157. { "open", w_File_open },
  158. { "close", w_File_close },
  159. { "read", w_File_read },
  160. { "write", w_File_write },
  161. { "eof", w_File_eof },
  162. { "tell", w_File_tell },
  163. { "seek", w_File_seek },
  164. { "lines", w_File_lines },
  165. { 0, 0 }
  166. };
  167. int luaopen_file(lua_State * L)
  168. {
  169. return luax_register_type(L, "File", functions);
  170. }
  171. } // physfs
  172. } // filesystem
  173. } // love