1 /**
2 * Copyright (c) 2006-2012 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 
21 #include "wrap_File.h"
22 
23 #include <common/Data.h>
24 #include <common/Exception.h>
25 #include <common/int.h>
26 
27 namespace love
28 {
29 namespace filesystem
30 {
31 namespace physfs
32 {
luax_checkfile(lua_State * L,int idx)33 	File * luax_checkfile(lua_State * L, int idx)
34 	{
35 		return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
36 	}
37 
w_File_getSize(lua_State * L)38 	int w_File_getSize(lua_State * L)
39 	{
40 		File * t = luax_checkfile(L, 1);
41 		int64 size = t->getSize();
42 
43 		// Push nil on failure or if size does not fit into a double precision floating-point number.
44 		if (size == -1 || size >= 0x20000000000000LL)
45 			lua_pushnil(L);
46 		else
47 			lua_pushnumber(L, (lua_Number)size);
48 
49 		return 1;
50 	}
51 
w_File_open(lua_State * L)52 	int w_File_open(lua_State * L)
53 	{
54 		File * file = luax_checkfile(L, 1);
55 		File::Mode mode;
56 
57 		if (!File::getConstant(luaL_checkstring(L, 2), mode))
58 			return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
59 
60 		try
61 		{
62 			lua_pushboolean(L, file->open(mode) ? 1 : 0);
63 		}
64 		catch (Exception e)
65 		{
66 			return luaL_error(L, e.what());
67 		}
68 
69 		return 1;
70 	}
71 
w_File_close(lua_State * L)72 	int w_File_close(lua_State * L)
73 	{
74 		File * file = luax_checkfile(L, 1);
75 		lua_pushboolean(L, file->close() ? 1 : 0);
76 		return 1;
77 	}
78 
w_File_read(lua_State * L)79 	int w_File_read(lua_State * L)
80 	{
81 		File * file = luax_checkfile(L, 1);
82 		Data * d = 0;
83 
84 		int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
85 
86 		try
87 		{
88 			d = file->read(size);
89 		}
90 		catch (Exception e)
91 		{
92 			return luaL_error(L, e.what());
93 		}
94 
95 		lua_pushlstring(L, (const char*) d->getData(), d->getSize());
96 		lua_pushnumber(L, d->getSize());
97 		d->release();
98 		return 2;
99 	}
100 
w_File_write(lua_State * L)101 	int w_File_write(lua_State * L)
102 	{
103 		File * file = luax_checkfile(L, 1);
104 		bool result;
105 		if ( file->getMode() == File::CLOSED )
106 			return luaL_error(L, "File is not open.");
107 		if ( lua_isstring(L, 2) )
108 		{
109 			try
110 			{
111 				result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
112 			}
113 			catch (Exception e)
114 			{
115 				return luaL_error(L, e.what());
116 			}
117 
118 		}
119 		else if ( luax_istype(L, 2, DATA_T))
120 		{
121 			try
122 			{
123 				love::Data * data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
124 				result = file->write(data, luaL_optint(L, 3, data->getSize()));
125 			}
126 			catch (Exception e)
127 			{
128 				return luaL_error(L, e.what());
129 			}
130 		}
131 		else
132 		{
133 			return luaL_error(L, "String or data expected.");
134 		}
135 		lua_pushboolean(L, result);
136 		return 1;
137 	}
138 
w_File_eof(lua_State * L)139 	int w_File_eof(lua_State * L)
140 	{
141 		File * file = luax_checkfile(L, 1);
142 		luax_pushboolean(L, file->eof());
143 		return 1;
144 	}
145 
w_File_tell(lua_State * L)146 	int w_File_tell(lua_State * L)
147 	{
148 		File * file = luax_checkfile(L, 1);
149 		int64 pos = file->tell();
150 		// Push nil on failure or if pos does not fit into a double precision floating-point number.
151 		if (pos == -1 || pos >= 0x20000000000000LL)
152 			lua_pushnil(L);
153 		else
154 			lua_pushnumber(L, (lua_Number)pos);
155 		return 1;
156 	}
157 
w_File_seek(lua_State * L)158 	int w_File_seek(lua_State * L)
159 	{
160 		File * file = luax_checkfile(L, 1);
161 		lua_Number pos = luaL_checknumber(L, 2);
162 
163 		// Push false on negative and precision-problematic numbers.
164 		// Better fail than seek to an unknown position.
165 		if (pos < 0.0 || pos >= 9007199254740992.0)
166 			luax_pushboolean(L, false);
167 		else
168 			luax_pushboolean(L, file->seek((uint64)pos));
169 		return 1;
170 	}
171 
w_File_lines(lua_State * L)172 	int w_File_lines(lua_State * L)
173 	{
174 		File * file;
175 
176 		if (luax_istype(L, 1, FILESYSTEM_FILE_T))
177 		{
178 			file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
179 			lua_pushnumber(L, 0); // File position.
180 			luax_pushboolean(L, file->getMode() != File::CLOSED); // Save current file mode.
181 		}
182 		else
183 			return luaL_error(L, "Expected File.");
184 
185 		if (file->getMode() != File::READ)
186 		{
187 			if (file->getMode() != File::CLOSED)
188 				file->close();
189 
190 			try
191 			{
192 				if (!file->open(File::READ))
193 					return luaL_error(L, "Could not open file.");
194 			}
195 			catch (love::Exception & e)
196 			{
197 				return luaL_error(L, "%s", e.what());
198 			}
199 		}
200 
201 		lua_pushcclosure(L, Filesystem::lines_i, 3);
202 		return 1;
203 	}
204 
205 	static const luaL_Reg functions[] = {
206 		{ "getSize", w_File_getSize },
207 		{ "open", w_File_open },
208 		{ "close", w_File_close },
209 		{ "read", w_File_read },
210 		{ "write", w_File_write },
211 		{ "eof", w_File_eof },
212 		{ "tell", w_File_tell },
213 		{ "seek", w_File_seek },
214 		{ "lines", w_File_lines },
215 		{ 0, 0 }
216 	};
217 
luaopen_file(lua_State * L)218 	extern "C" int luaopen_file(lua_State * L)
219 	{
220 		return luax_register_type(L, "File", functions);
221 	}
222 
223 } // physfs
224 } // filesystem
225 } // love
226