Alex Szpakowski avatar Alex Szpakowski committed 4031a4e

Cleaned up love.filesystem.read/write code

Comments (0)

Files changed (3)

src/modules/filesystem/physfs/Filesystem.cpp

 	return true;
 }
 
-int Filesystem::read(lua_State *L)
+Data *Filesystem::read(const char *filename, int64 size) const
 {
-	// The file to read from. The file must either be created
-	// on-the-fly, or passed as a parameter.
-	File *file;
+	File file(filename);
 
-	if (lua_isstring(L, 1))
-	{
-		// Create the file.
-		file = newFile(lua_tostring(L, 1));
-	}
-	else
-		return luaL_error(L, "Expected filename.");
+	file.open(File::READ);
 
-	// Optionally, the caller can specify whether to read
-	// the whole file, or just a part of it.
-	int count = luaL_optint(L, 2, (lua_Integer)file->getSize()); // FIXME
-
-	// Read the data.
-	Data *data = file->read(count);
-
-	// Error check.
-	if (data == 0)
-		return luaL_error(L, "File could not be read.");
-
-	// Close and delete the file, if we created it.
-	// (I.e. if the first parameter is a string).
-	if (lua_isstring(L, 1))
-		file->release();
-
-	// Push the string.
-	lua_pushlstring(L, (char *)data->getData(), data->getSize());
-
-	// Push the size.
-	lua_pushinteger(L, data->getSize());
-
-	// Lua has a copy now, so we can free it.
-	data->release();
-
-	return 2;
+	// close() is called in the File destructor.
+	return file.read(size);
 }
 
-int Filesystem::write(lua_State *L)
+void Filesystem::write(const char *filename, const void *data, int64 size)
 {
-	// The file to write to. The file must either be created
-	// on-the-fly, or passed as a parameter.
-	File *file;
+	File file(filename);
 
-	// We know for sure that we need a second parameter, so
-	// let's check that first.
-	if (lua_isnoneornil(L, 2))
-		return luaL_error(L, "Second argument needed.");
+	file.open(File::WRITE);
 
-	if (lua_isstring(L, 1))
-	{
-		// Create the file.
-		file = newFile(lua_tostring(L, 1));
-	}
-	else
-		return luaL_error(L, "Expected filename.");
-
-	// Get the current mode of the file.
-	File::Mode mode = file->getMode();
-
-	if (mode == File::CLOSED)
-	{
-		// It should be possible to use append mode, but
-		// normal File::Mode::Write is the default.
-		int mode = luaL_optint(L, 4, File::WRITE);
-
-		// Open the file.
-		if (!file->open((File::Mode)mode))
-			return luaL_error(L, "Could not open file.");
-	}
-
-	size_t length = 0;
-	const char *input;
-	if (lua_isstring(L, 2))
-	{
-		input = lua_tolstring(L, 2, &length);
-	}
-	else if (luax_istype(L, 2, DATA_T))
-	{
-		love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
-		length = data->getSize();
-		input = (char *)data->getData();
-	}
-	else
-	{
-		return luaL_error(L, "Expected string or data for argument #2.");
-	}
-
-	// Get how much we should write. Length of string default.
-	length = luaL_optint(L, 3, length);
-
-	// Write the data.
-	bool success = file->write(input, length);
-
-	// Close and delete the file, if we created
-	// it in this function.
-	if (lua_isstring(L, 1))
-	{
-		// Kill the file if "we" created it.
-		file->close();
-		file->release();
-	}
-
-	if (!success)
-		return luaL_error(L, "Data could not be written.");
-
-	lua_pushboolean(L, success);
-	return 1;
+	// close() is called in the File destructor.
+	if (!file.write(data, size))
+		throw love::Exception("Data could not be written.");
 }
 
 int Filesystem::enumerate(lua_State *L)
 
 int64 Filesystem::getSize(const char *filename)
 {
-	File f(filename);
-	int64 size = f.getSize();
+	File file(filename);
+	int64 size = file.getSize();
 	return size;
 }
 

src/modules/filesystem/physfs/Filesystem.h

 	bool close(File *file);
 
 	/**
-	 * Reads count bytes from an open file.
-	 * The first parameter is either a File or
-	 * a string. An optional second parameter specified the
-	 * max number of bytes to read.
+	 * Reads data from a file.
+	 * @param filename The name of the file to read from.
+	 * @param size The size in bytes of the data to read.
 	 **/
-	int read(lua_State *L);
+	Data *read(const char *filename, int64 size) const;
 
 	/**
-	 * Write the bytes in data to the file. File
-	 * must be opened for write.
-	 * The first parameter is either a File or
-	 * a string.
+	 * Write data to a file.
+	 * @param filename The name of the file to write to.
+	 * @param data The data to write.
+	 * @param size The size in bytes of the data to write.
 	 **/
-	int write(lua_State *L);
+	void write(const char *filename, const void *data, int64 size);
 
 	/**
 	 * Check if end-of-file is reached.

src/modules/filesystem/physfs/wrap_Filesystem.cpp

 
 int w_read(lua_State *L)
 {
+	const char *filename = luaL_checkstring(L, 1);
+	int64 len = (int64) luaL_optinteger(L, 2, File::ALL);
+
+	Data *data = 0;
 	try
 	{
-		return instance->read(L);
+		data = instance->read(filename, len);
 	}
-	catch(Exception e)
+	catch (love::Exception &e)
 	{
-		return luaL_error(L, e.what());
+		return luaL_error(L, "%s", e.what());
 	}
+
+	if (data == 0)
+		return luaL_error(L, "File could not be read.");
+
+	// Push the string.
+	lua_pushlstring(L, (const char *) data->getData(), data->getSize());
+
+	// Push the size.
+	lua_pushinteger(L, data->getSize());
+
+	// Lua has a copy now, so we can free it.
+	data->release();
+
+	return 2;
 }
 
 int w_write(lua_State *L)
 {
+	const char *filename = luaL_checkstring(L, 1);
+
+	const char *input = 0;
+	size_t len = 0;
+
+	if (luax_istype(L, 2, DATA_T))
+	{
+		love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
+		input = (const char *) data->getData();
+		len = data->getSize();
+	}
+	else if (lua_isstring(L, 2))
+		input = lua_tolstring(L, 2, &len);
+	else
+		return luaL_argerror(L, 2, "string or Data expected");
+
+	// Get how much we should write. Length of string default.
+	len = luaL_optinteger(L, 3, len);
+
 	try
 	{
-		return instance->write(L);
+		instance->write(filename, (const void *) input, len);
 	}
-	catch(Exception e)
+	catch (love::Exception &e)
 	{
-		return luaL_error(L, e.what());
+		return luaL_error(L, "%s", e.what());
 	}
+
+	luax_pushboolean(L, true);
+
+	return 1;
 }
 
 int w_enumerate(lua_State *L)
 	}
 	catch (love::Exception &e)
 	{
-		// Return nil, errorstring
-		lua_pushnil(L);
-		lua_pushstring(L, e.what());
-		return 2;
+		return luaL_error(L, "%s", e.what());
 	}
 
-	// Return nil on failure or if size does not fit into a double precision floating-point number.
-	if (size == -1 || size >= 0x20000000000000LL)
-	{
-		lua_pushnil(L);
-		if (size == -1)
-			lua_pushstring(L, "Could not determine file size.");
-		else
-			lua_pushstring(L, "Size too large to fit into a Lua number!");
-		return 2;
-	}
-	else
-	{
-		lua_pushnumber(L, (lua_Number) size);
-		return 1;
-	}
+	// Error on failure or if size does not fit into a double precision floating-point number.
+	if (size == -1)
+		return luaL_error(L, "Could not determine file size.");
+	else if (size >= 0x20000000000000LL)
+		return luaL_error(L, "Size too large to fit into a Lua number!");
+
+
+	lua_pushnumber(L, (lua_Number) size);
+	return 1;
 }
 
 int loader(lua_State *L)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.