From ce2b2032514420cfead2fdacb588d9179af49319 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Tue, 1 May 2007 21:51:49 +0000 Subject: [PATCH] Split out the LuaScriptReader class, though it might be futile. Originally committed to SVN as r1154. --- aegisub/auto4_auto3.cpp | 2 +- aegisub/auto4_lua.cpp | 67 +----------------- aegisub/auto4_lua.h | 11 --- aegisub/auto4_lua_scriptreader.cpp | 105 +++++++++++++++++++++++++++++ aegisub/auto4_lua_scriptreader.h | 54 +++++++++++++++ 5 files changed, 161 insertions(+), 78 deletions(-) create mode 100644 aegisub/auto4_lua_scriptreader.cpp create mode 100644 aegisub/auto4_lua_scriptreader.h diff --git a/aegisub/auto4_auto3.cpp b/aegisub/auto4_auto3.cpp index 857120b70..243535168 100644 --- a/aegisub/auto4_auto3.cpp +++ b/aegisub/auto4_auto3.cpp @@ -34,7 +34,7 @@ // #include "auto4_auto3.h" -#include "auto4_lua.h" +#include "auto4_lua_scriptreader.h" #include "../lua51/src/lualib.h" #include "../lua51/src/lauxlib.h" #include "options.h" diff --git a/aegisub/auto4_lua.cpp b/aegisub/auto4_lua.cpp index 83c3135ea..fb9ac8bc1 100644 --- a/aegisub/auto4_lua.cpp +++ b/aegisub/auto4_lua.cpp @@ -34,6 +34,7 @@ // #include "auto4_lua.h" +#include "auto4_lua_scriptreader.h" #include "auto4_auto3.h" #include "ass_dialogue.h" #include "ass_style.h" @@ -98,72 +99,6 @@ namespace Automation4 { #endif - // LuaScriptReader - LuaScriptReader::LuaScriptReader(const wxString &filename) - { -#ifdef WIN32 - f = _tfopen(filename.c_str(), _T("rb")); -#else - f = fopen(filename.fn_str(), "rb"); -#endif - first = true; - databuf = new char[bufsize]; - } - - LuaScriptReader::~LuaScriptReader() - { - if (databuf) - delete databuf; - fclose(f); - } - - const char* LuaScriptReader::reader_func(lua_State *L, void *data, size_t *size) - { - LuaScriptReader *self = (LuaScriptReader*)(data); - unsigned char *b = (unsigned char *)self->databuf; - FILE *f = self->f; - - if (feof(f)) { - *size = 0; - return 0; - } - - if (self->first) { - // check if file is sensible and maybe skip bom - if ((*size = fread(b, 1, 4, f)) == 4) { - if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { - // got an utf8 file with bom - // nothing further to do, already skipped the bom - fseek(f, -1, SEEK_CUR); - } else { - // oops, not utf8 with bom - // check if there is some other BOM in place and complain if there is... - if ((b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00) || // utf32be - (b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF) || // utf32le - (b[0] == 0xFF && b[1] == 0xFE) || // utf16be - (b[0] == 0xFE && b[1] == 0xFF) || // utf16le - (b[0] == 0x2B && b[1] == 0x2F && b[2] == 0x76) || // utf7 - (b[0] == 0x00 && b[2] == 0x00) || // looks like utf16be - (b[1] == 0x00 && b[3] == 0x00)) { // looks like utf16le - throw _T("The script file uses an unsupported character set. Only UTF-8 is supported."); - } - // assume utf8 without bom, and rewind file - fseek(f, 0, SEEK_SET); - } - } else { - // hmm, rather short file this... - // doesn't have a bom, assume it's just ascii/utf8 without bom - return self->databuf; // *size is already set - } - self->first = false; - } - - *size = fread(b, 1, bufsize, f); - - return self->databuf; - } - - // LuaScript LuaScript::LuaScript(const wxString &filename) diff --git a/aegisub/auto4_lua.h b/aegisub/auto4_lua.h index 191d6a899..7832710e3 100644 --- a/aegisub/auto4_lua.h +++ b/aegisub/auto4_lua.h @@ -48,17 +48,6 @@ class wxWindow; namespace Automation4 { - // Manage reading in a Lua script file - struct LuaScriptReader { - FILE *f; - bool first; - char *databuf; - static const size_t bufsize = 512; - LuaScriptReader(const wxString &filename); - ~LuaScriptReader(); - static const char* reader_func(lua_State *L, void *data, size_t *size); - }; - // Provides access to an AssFile object (and all lines contained) for a Lua script class LuaAssFile { private: diff --git a/aegisub/auto4_lua_scriptreader.cpp b/aegisub/auto4_lua_scriptreader.cpp new file mode 100644 index 000000000..3dd8a95bc --- /dev/null +++ b/aegisub/auto4_lua_scriptreader.cpp @@ -0,0 +1,105 @@ +// Copyright (c) 2006, 2007, Niels Martin Hansen +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:jiifurusu@gmail.com +// + +#include "auto4_lua_scriptreader.h" + +namespace Automation4 { + + // LuaScriptReader + LuaScriptReader::LuaScriptReader(const wxString &filename) + { +#ifdef WIN32 + f = _tfopen(filename.c_str(), _T("rb")); +#else + f = fopen(filename.fn_str(), "rb"); +#endif + first = true; + databuf = new char[bufsize]; + } + + LuaScriptReader::~LuaScriptReader() + { + if (databuf) + delete databuf; + fclose(f); + } + + const char* LuaScriptReader::reader_func(lua_State *L, void *data, size_t *size) + { + LuaScriptReader *self = (LuaScriptReader*)(data); + unsigned char *b = (unsigned char *)self->databuf; + FILE *f = self->f; + + if (feof(f)) { + *size = 0; + return 0; + } + + if (self->first) { + // check if file is sensible and maybe skip bom + if ((*size = fread(b, 1, 4, f)) == 4) { + if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { + // got an utf8 file with bom + // nothing further to do, already skipped the bom + fseek(f, -1, SEEK_CUR); + } else { + // oops, not utf8 with bom + // check if there is some other BOM in place and complain if there is... + if ((b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00) || // utf32be + (b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF) || // utf32le + (b[0] == 0xFF && b[1] == 0xFE) || // utf16be + (b[0] == 0xFE && b[1] == 0xFF) || // utf16le + (b[0] == 0x2B && b[1] == 0x2F && b[2] == 0x76) || // utf7 + (b[0] == 0x00 && b[2] == 0x00) || // looks like utf16be + (b[1] == 0x00 && b[3] == 0x00)) { // looks like utf16le + throw _T("The script file uses an unsupported character set. Only UTF-8 is supported."); + } + // assume utf8 without bom, and rewind file + fseek(f, 0, SEEK_SET); + } + } else { + // hmm, rather short file this... + // doesn't have a bom, assume it's just ascii/utf8 without bom + return self->databuf; // *size is already set + } + self->first = false; + } + + *size = fread(b, 1, bufsize, f); + + return self->databuf; + } + +}; diff --git a/aegisub/auto4_lua_scriptreader.h b/aegisub/auto4_lua_scriptreader.h new file mode 100644 index 000000000..b580bd32d --- /dev/null +++ b/aegisub/auto4_lua_scriptreader.h @@ -0,0 +1,54 @@ +// Copyright (c) 2006, 2007, Niels Martin Hansen +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:jiifurusu@gmail.com +// + +#pragma once + +#include +#include "../lua51/src/lua.h" + +namespace Automation4 { + + // Manage reading in a Lua script file + struct LuaScriptReader { + FILE *f; + bool first; + char *databuf; + static const size_t bufsize = 512; + LuaScriptReader(const wxString &filename); + ~LuaScriptReader(); + static const char* reader_func(lua_State *L, void *data, size_t *size); + }; + +}; \ No newline at end of file