mirror of
https://github.com/love2d/love.git
synced 2026-08-17 19:23:38 +02:00
Cleaned up love.filesystem.read/write code
This commit is contained in:
@@ -309,118 +309,25 @@ bool Filesystem::remove(const char *file)
|
|||||||
return true;
|
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
|
File file(filename);
|
||||||
// on-the-fly, or passed as a parameter.
|
|
||||||
File *file;
|
|
||||||
|
|
||||||
if (lua_isstring(L, 1))
|
file.open(File::READ);
|
||||||
{
|
|
||||||
// Create the file.
|
|
||||||
file = newFile(lua_tostring(L, 1));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return luaL_error(L, "Expected filename.");
|
|
||||||
|
|
||||||
// Optionally, the caller can specify whether to read
|
// close() is called in the File destructor.
|
||||||
// the whole file, or just a part of it.
|
return file.read(size);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
File file(filename);
|
||||||
// on-the-fly, or passed as a parameter.
|
|
||||||
File *file;
|
|
||||||
|
|
||||||
// We know for sure that we need a second parameter, so
|
file.open(File::WRITE);
|
||||||
// let's check that first.
|
|
||||||
if (lua_isnoneornil(L, 2))
|
|
||||||
return luaL_error(L, "Second argument needed.");
|
|
||||||
|
|
||||||
if (lua_isstring(L, 1))
|
// close() is called in the File destructor.
|
||||||
{
|
if (!file.write(data, size))
|
||||||
// Create the file.
|
throw love::Exception("Data could not be written.");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Filesystem::enumerate(lua_State *L)
|
int Filesystem::enumerate(lua_State *L)
|
||||||
@@ -607,8 +514,8 @@ int Filesystem::getLastModified(lua_State *L)
|
|||||||
|
|
||||||
int64 Filesystem::getSize(const char *filename)
|
int64 Filesystem::getSize(const char *filename)
|
||||||
{
|
{
|
||||||
File f(filename);
|
File file(filename);
|
||||||
int64 size = f.getSize();
|
int64 size = file.getSize();
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -232,20 +232,19 @@ public:
|
|||||||
bool close(File *file);
|
bool close(File *file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads count bytes from an open file.
|
* Reads data from a file.
|
||||||
* The first parameter is either a File or
|
* @param filename The name of the file to read from.
|
||||||
* a string. An optional second parameter specified the
|
* @param size The size in bytes of the data to read.
|
||||||
* max number of bytes to read.
|
|
||||||
**/
|
**/
|
||||||
int read(lua_State *L);
|
Data *read(const char *filename, int64 size) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the bytes in data to the file. File
|
* Write data to a file.
|
||||||
* must be opened for write.
|
* @param filename The name of the file to write to.
|
||||||
* The first parameter is either a File or
|
* @param data The data to write.
|
||||||
* a string.
|
* @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.
|
* Check if end-of-file is reached.
|
||||||
|
|||||||
@@ -199,26 +199,67 @@ int w_remove(lua_State *L)
|
|||||||
|
|
||||||
int w_read(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
|
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)
|
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
|
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)
|
int w_enumerate(lua_State *L)
|
||||||
@@ -279,27 +320,18 @@ int w_getSize(lua_State *L)
|
|||||||
}
|
}
|
||||||
catch (love::Exception &e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
// Return nil, errorstring
|
return luaL_error(L, "%s", e.what());
|
||||||
lua_pushnil(L);
|
|
||||||
lua_pushstring(L, e.what());
|
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return nil on failure or if size does not fit into a double precision floating-point number.
|
// Error on failure or if size does not fit into a double precision floating-point number.
|
||||||
if (size == -1 || size >= 0x20000000000000LL)
|
if (size == -1)
|
||||||
{
|
return luaL_error(L, "Could not determine file size.");
|
||||||
lua_pushnil(L);
|
else if (size >= 0x20000000000000LL)
|
||||||
if (size == -1)
|
return luaL_error(L, "Size too large to fit into a Lua number!");
|
||||||
lua_pushstring(L, "Could not determine file size.");
|
|
||||||
else
|
|
||||||
lua_pushstring(L, "Size too large to fit into a Lua number!");
|
lua_pushnumber(L, (lua_Number) size);
|
||||||
return 2;
|
return 1;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_pushnumber(L, (lua_Number) size);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int loader(lua_State *L)
|
int loader(lua_State *L)
|
||||||
|
|||||||
Reference in New Issue
Block a user