Drop support for non-UTF-8 lua scripts

This commit is contained in:
Thomas Goyne 2013-04-13 08:12:16 -07:00
parent 68662211a1
commit 8bdb7d32ad
2 changed files with 14 additions and 26 deletions

View file

@ -1,4 +1,4 @@
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -21,40 +21,29 @@
#include "auto4_lua_scriptreader.h" #include "auto4_lua_scriptreader.h"
#include "charset_detect.h"
#include <libaegisub/io.h> #include <libaegisub/io.h>
#include <libaegisub/charset_conv.h>
#include <fstream> #include <fstream>
namespace Automation4 { namespace Automation4 {
LuaScriptReader::LuaScriptReader(agi::fs::path const& filename) LuaScriptReader::LuaScriptReader(agi::fs::path const& filename)
: conv(new agi::charset::IconvWrapper(CharSetDetect::GetEncoding(filename).c_str(), "utf-8", false)) : file(agi::io::Open(filename))
, file(agi::io::Open(filename)) , first(true)
{ {
} }
LuaScriptReader::~LuaScriptReader() { } LuaScriptReader::~LuaScriptReader() { }
const char *LuaScriptReader::Read(size_t *bytes_read) { const char *LuaScriptReader::Read(size_t *bytes_read) {
char in_buf[512]; file->read(buf, sizeof(buf));
file->read(in_buf, sizeof(in_buf)); *bytes_read = file->gcount();
if (first) {
const char *in = in_buf; first = false;
char *out = buf; // Skip the bom
size_t in_bytes = file->gcount(); if (*bytes_read >= 3 && buf[0] == -17 && buf[1] == -69 && buf[2] == -65) {
size_t out_bytes = sizeof(buf); *bytes_read -= 3;
return buf + 3;
conv->Convert(&in, &in_bytes, &out, &out_bytes); }
if (in_bytes > 0 && in != in_buf)
file->seekg(-(std::streamoff)in_bytes, std::ios_base::cur);
*bytes_read = out - buf;
// Skip the bom
if (*bytes_read >= 3 && buf[0] == -17 && buf[1] == -69 && buf[2] == -65) {
*bytes_read -= 3;
return buf + 3;
} }
return buf; return buf;
} }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -24,12 +24,11 @@
#include <string> #include <string>
struct lua_State; struct lua_State;
namespace agi { namespace charset { class IconvWrapper; } }
namespace Automation4 { namespace Automation4 {
class LuaScriptReader { class LuaScriptReader {
std::unique_ptr<agi::charset::IconvWrapper> conv;
std::unique_ptr<std::istream> file; std::unique_ptr<std::istream> file;
bool first;
char buf[512]; char buf[512];
const char *Read(size_t *bytes_read); const char *Read(size_t *bytes_read);