Browse Source

Add variants of File:read and love.filesystem.read which take an enum to determine whether they return a FileData or string.

Alex Szpakowski 7 years ago
parent
commit
0b7cb4a353

+ 1 - 1
src/modules/data/wrap_DataModule.cpp

@@ -321,7 +321,7 @@ int w_hash(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
-static int w_pack(lua_State *L)
+int w_pack(lua_State *L)
 {
 {
 	ContainerType ctype = luax_checkcontainertype(L, 1);
 	ContainerType ctype = luax_checkcontainertype(L, 1);
 	const char *fmt = luaL_checkstring(L, 2);
 	const char *fmt = luaL_checkstring(L, 2);

+ 28 - 5
src/modules/filesystem/wrap_File.cpp

@@ -24,6 +24,8 @@
 #include "common/Exception.h"
 #include "common/Exception.h"
 #include "common/int.h"
 #include "common/int.h"
 
 
+#include "data/wrap_DataModule.h"
+
 namespace love
 namespace love
 {
 {
 namespace filesystem
 namespace filesystem
@@ -108,9 +110,18 @@ int w_File_isOpen(lua_State *L)
 int w_File_read(lua_State *L)
 int w_File_read(lua_State *L)
 {
 {
 	File *file = luax_checkfile(L, 1);
 	File *file = luax_checkfile(L, 1);
-	StrongRef<Data> d = nullptr;
+	StrongRef<FileData> d = nullptr;
+
+	love::data::ContainerType ctype = love::data::CONTAINER_STRING;
 
 
-	int64 size = (int64) luaL_optnumber(L, 2, (lua_Number) File::ALL);
+	int startidx = 2;
+	if (lua_type(L, 2) == LUA_TSTRING)
+	{
+		ctype = love::data::luax_checkcontainertype(L, 2);
+		startidx = 3;
+	}
+
+	int64 size = (int64) luaL_optnumber(L, startidx, (lua_Number) File::ALL);
 
 
 	try
 	try
 	{
 	{
@@ -121,9 +132,21 @@ int w_File_read(lua_State *L)
 		return luax_ioError(L, "%s", e.what());
 		return luax_ioError(L, "%s", e.what());
 	}
 	}
 
 
-	lua_pushlstring(L, (const char *) d->getData(), d->getSize());
-	lua_pushnumber(L, d->getSize());
-	return 2;
+	int nret = 0;
+
+	if (ctype == love::data::CONTAINER_DATA)
+	{
+		luax_pushtype(L, d.get());
+		nret = 1;
+	}
+	else
+	{
+		lua_pushlstring(L, (const char *) d->getData(), d->getSize());
+		lua_pushinteger(L, d->getSize());
+		nret = 2;
+	}
+
+	return nret;
 }
 }
 
 
 int w_File_write(lua_State *L)
 int w_File_write(lua_State *L)

+ 26 - 8
src/modules/filesystem/wrap_Filesystem.cpp

@@ -25,6 +25,7 @@
 #include "wrap_DroppedFile.h"
 #include "wrap_DroppedFile.h"
 #include "wrap_FileData.h"
 #include "wrap_FileData.h"
 #include "data/wrap_Data.h"
 #include "data/wrap_Data.h"
+#include "data/wrap_DataModule.h"
 
 
 #include "physfs/Filesystem.h"
 #include "physfs/Filesystem.h"
 
 
@@ -445,10 +446,19 @@ 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);
+	love::data::ContainerType ctype = love::data::CONTAINER_STRING;
+	int startidx = 1;
 
 
-	Data *data = nullptr;
+	if (lua_type(L, 2) == LUA_TSTRING)
+	{
+		ctype = love::data::luax_checkcontainertype(L, 1);
+		startidx = 2;
+	}
+
+	const char *filename = luaL_checkstring(L, startidx + 0);
+	int64 len = (int64) luaL_optinteger(L, startidx + 1, File::ALL);
+
+	FileData *data = nullptr;
 	try
 	try
 	{
 	{
 		data = instance()->read(filename, len);
 		data = instance()->read(filename, len);
@@ -461,16 +471,24 @@ int w_read(lua_State *L)
 	if (data == nullptr)
 	if (data == nullptr)
 		return luax_ioError(L, "File could not be read.");
 		return luax_ioError(L, "File could not be read.");
 
 
-	// Push the string.
-	lua_pushlstring(L, (const char *) data->getData(), data->getSize());
+	int nret = 0;
 
 
-	// Push the size.
-	lua_pushinteger(L, data->getSize());
+	if (ctype == love::data::CONTAINER_DATA)
+	{
+		luax_pushtype(L, data);
+		nret = 1;
+	}
+	else
+	{
+		lua_pushlstring(L, (const char *) data->getData(), data->getSize());
+		lua_pushinteger(L, data->getSize());
+		nret = 2;
+	}
 
 
 	// Lua has a copy now, so we can free it.
 	// Lua has a copy now, so we can free it.
 	data->release();
 	data->release();
 
 
-	return 2;
+	return nret;
 }
 }
 
 
 static int w_write_or_append(lua_State *L, File::Mode mode)
 static int w_write_or_append(lua_State *L, File::Mode mode)