Cleaned up love.filesystem.read/write code

This commit is contained in:
Alex Szpakowski
2013-03-17 03:30:36 -03:00
parent 8d4f35b86b
commit 96229bcb5d
3 changed files with 79 additions and 141 deletions
+13 -106
View File
@@ -309,118 +309,25 @@ bool Filesystem::remove(const char *file)
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)
@@ -607,8 +514,8 @@ int Filesystem::getLastModified(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;
}
+9 -10
View File
@@ -232,20 +232,19 @@ public:
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.
@@ -199,26 +199,67 @@ int w_remove(lua_State *L)
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)
@@ -279,27 +320,18 @@ int w_getSize(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)