Use read_file_mapping for reading automation scripts

This commit is contained in:
Thomas Goyne 2014-03-21 07:22:31 -07:00
parent 95008b7dc1
commit ad193f70a9

View file

@ -23,11 +23,9 @@
#include "auto4_lua_utils.h" #include "auto4_lua_utils.h"
#include <libaegisub/io.h> #include <libaegisub/file_mapping.h>
#include <fstream>
#include <lua.h> #include <lua.h>
#include <memory>
namespace Automation4 { namespace Automation4 {
bool LoadFile(lua_State *L, agi::fs::path const& raw_filename) { bool LoadFile(lua_State *L, agi::fs::path const& raw_filename) {
@ -39,26 +37,18 @@ namespace Automation4 {
LOG_E("auto4/lua") << "Error canonicalizing path: " << e.GetChainedMessage(); LOG_E("auto4/lua") << "Error canonicalizing path: " << e.GetChainedMessage();
} }
std::unique_ptr<std::istream> file(agi::io::Open(filename, true)); agi::read_file_mapping file(filename);
file->seekg(0, std::ios::end); char *buff = file.read(0, file.size());
size_t size = file->tellg(); size_t size = file.size();
file->seekg(0, std::ios::beg);
std::string buff;
buff.resize(size);
// Discard the BOM if present // Discard the BOM if present
file->read(&buff[0], 3); if (size >= 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) {
size_t start = file->gcount(); buff += 3;
if (start == 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) { size -= 3;
buff.resize(size - 3);
start = 0;
} }
file->read(&buff[start], size - start);
if (!agi::fs::HasExtension(filename, "moon")) if (!agi::fs::HasExtension(filename, "moon"))
return luaL_loadbuffer(L, &buff[0], buff.size(), filename.string().c_str()) == 0; return luaL_loadbuffer(L, buff, size, filename.string().c_str()) == 0;
// Save the text we'll be loading for the line number rewriting in the // Save the text we'll be loading for the line number rewriting in the
// error handling // error handling
@ -70,7 +60,7 @@ namespace Automation4 {
// MoonScript to Lua // MoonScript to Lua
if (luaL_dostring(L, "return require('moonscript').loadstring")) if (luaL_dostring(L, "return require('moonscript').loadstring"))
return false; // Leaves error message on stack return false; // Leaves error message on stack
lua_pushlstring(L, &buff[0], buff.size()); lua_pushlstring(L, buff, size);
lua_pushstring(L, filename.string().c_str()); lua_pushstring(L, filename.string().c_str());
if (lua_pcall(L, 2, 2, 0)) if (lua_pcall(L, 2, 2, 0))
return false; // Leaves error message on stack return false; // Leaves error message on stack