Commits

Alex Szpakowski  committed 486d48a

Cleaned up love.filesystem.load and love.filesystem.getLastModified code

  • Participants
  • Parent commits d8a56ce

Comments (0)

Files changed (3)

File src/modules/filesystem/physfs/Filesystem.cpp

 	return 0;
 }
 
-int Filesystem::load(lua_State *L)
+int64 Filesystem::getLastModified(const char *filename) const
 {
-	// Need only one arg.
-	luax_assert_argc(L, 1, 1);
+	PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
 
-	// Must be string.
-	if (!lua_isstring(L, -1))
-		return luaL_error(L, "The argument must be a string.");
+	if (time == -1)
+		throw love::Exception("Could not determine file modification date.");
 
-	std::string filename = lua_tostring(L, -1);
-
-	// The file must exist.
-	if (!exists(filename.c_str()))
-		return luaL_error(L, "File %s does not exist.", filename.c_str());
-
-	// Create the file.
-	File *file = newFile(filename.c_str());
-
-	// Get the data from the file.
-	Data *data = file->read();
-
-	int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
-
-	data->release();
-	file->release();
-
-	// Load the chunk, but don't run it.
-	switch (status)
-	{
-	case LUA_ERRMEM:
-		return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
-	case LUA_ERRSYNTAX:
-		return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
-	default: // success
-		return 1;
-	}
-}
-
-int Filesystem::getLastModified(lua_State *L)
-{
-	const char *filename = luaL_checkstring(L, 1);
-	PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
-	if (time == -1)
-	{
-		lua_pushnil(L);
-		lua_pushstring(L, "Could not determine file modification date.");
-		return 2;
-	}
-	lua_pushnumber(L, static_cast<lua_Number>(time));
-	return 1;
+	return time;
 }
 
 int64 Filesystem::getSize(const char *filename) const

File src/modules/filesystem/physfs/Filesystem.h

 	 * @param filename The name of the file to read from.
 	 * @param size The size in bytes of the data to read.
 	 **/
-	Data *read(const char *filename, int64 size) const;
+	Data *read(const char *filename, int64 size = File::ALL) const;
 
 	/**
 	 * Write data to a file.
 	int enumerate(lua_State *L);
 
 	/**
-	 * Loads a file without running it. The loaded
-	 * chunk is returned as a function.
-	 * @param filename The filename of the file to load.
-	 * @return A function.
+	 * Gets the last modification time of a file, in seconds
+	 * since the Unix epoch.
+	 * @param filename The name of the file.
 	 **/
-	int load(lua_State *L);
-
-	int getLastModified(lua_State *L);
+	int64 getLastModified(const char *filename) const;
 
 	/**
 	 * Gets the size of a file in bytes.

File src/modules/filesystem/physfs/wrap_Filesystem.cpp

 
 int w_load(lua_State *L)
 {
+	std::string filename = std::string(luaL_checkstring(L, 1));
+
+	Data *data = 0;
 	try
 	{
-		return instance->load(L);
+		data = instance->read(filename.c_str());
 	}
-	catch(love::Exception &e)
+	catch (love::Exception &e)
 	{
-		return luaL_error(L, e.what());
+		return luaL_error(L, "%s", e.what());
+	}
+
+	int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
+
+	data->release();
+
+	// Load the chunk, but don't run it.
+	switch (status)
+	{
+	case LUA_ERRMEM:
+		return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
+	case LUA_ERRSYNTAX:
+		return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
+	default: // success
+		return 1;
 	}
 }
 
 int w_getLastModified(lua_State *L)
 {
-	return instance->getLastModified(L);
+	const char *filename = luaL_checkstring(L, 1);
+
+	int64 time = 0;
+	try
+	{
+		time = instance->getLastModified(filename);
+	}
+	catch (love::Exception &e)
+	{
+		lua_pushnil(L);
+		lua_pushstring(L, e.what());
+		return 2;
+	}
+
+	lua_pushnumber(L, static_cast<lua_Number>(time));
+
+	return 1;
 }
 
 int w_getSize(lua_State *L)
 		lua_pop(L, 1);
 		lua_pushstring(L, tmp.c_str());
 		// Ok, load it.
-		return instance->load(L);
+		return w_load(L);
 	}
 
 	tmp = filename;
 			lua_pop(L, 1);
 			lua_pushstring(L, tmp.c_str());
 			// Ok, load it.
-			return instance->load(L);
+			return w_load(L);
 		}
 	}