From 4f1dc2c59dbead933d0febec8d9b781c2de825f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sun, 22 Apr 2007 02:03:40 +0000 Subject: [PATCH] Made hunspell into a pluggable factory. This MIGHT cause issues with mythes if hunspell is not available... Originally committed to SVN as r1108. --- aegisub/dialog_about.cpp | 15 +----- aegisub/dialog_spellchecker.cpp | 2 +- aegisub/frame_main.cpp | 4 -- aegisub/mythes.cxx | 24 +++++----- aegisub/options.cpp | 1 + aegisub/setup.cpp | 11 ----- aegisub/setup0.h | 42 +---------------- aegisub/spellchecker.cpp | 37 ++++++++++----- aegisub/spellchecker.h | 16 ++++++- aegisub/spellchecker_hunspell.cpp | 59 ++++++++++++++++++++---- aegisub/spellchecker_hunspell.h | 76 ------------------------------- aegisub/stdwx.h | 15 ------ aegisub/subs_edit_ctrl.cpp | 2 +- aegisub/text_file_reader.cpp | 3 +- 14 files changed, 108 insertions(+), 199 deletions(-) delete mode 100644 aegisub/spellchecker_hunspell.h diff --git a/aegisub/dialog_about.cpp b/aegisub/dialog_about.cpp index 3fb70b0d0..02916f857 100644 --- a/aegisub/dialog_about.cpp +++ b/aegisub/dialog_about.cpp @@ -40,7 +40,6 @@ #include "dialog_about.h" #include "version.h" #include "options.h" -#include "setup.h" /////////////// @@ -62,21 +61,11 @@ AboutScreen::AboutScreen(wxWindow *parent) libString += _T("wxWidgets - Copyright (c) 1998-2006 Julian Smart, Robert Roebling et al;\n"); libString += _T("wxStyledTextCtrl - Copyright (c) 2004 wxCode;\n"); libString += _T("Lua - Copyright (c) 1994-2006 Lua.org, PUC-Rio;\n"); -#if USE_HUNSPELL == 1 libString += _T("Hunspell - Copyright (c) Kevin Hendricks;\n"); -#endif -#if USE_PORTAUDIO == 1 libString += _T("PortAudio - Copyright (c) 1999-2000 Ross Bencina, Phil Burk;\n"); -#endif -#if USE_LAVC == 1 - libString += _T("FFmpeg - Copyright (c) ??;\n"); -#endif -#if USE_LIBASS == 1 - libString += _T("libass - Copyright (c) 2006, Evgeniy Stepanov;\n"); -#endif -#if USE_ASA == 1 + libString += _T("FFmpeg - Copyright (c) 2001 Fabrice Bellard,;\n"); + libString += _T("libass - Copyright (c) 2006-2007, Evgeniy Stepanov;\n"); libString += _T("asa - Copyright (c) 2004-2007, David Lamparter;\n"); -#endif libString += _T("MyThes - Copyright (c) 2003 Kevin B. Hendricks, Stratford, Ontario, Canada\n"); libString += _T("Matroska Parser and VideoSink - Copyright (c) 2004-2007 Mike Matsnev\n"); diff --git a/aegisub/dialog_spellchecker.cpp b/aegisub/dialog_spellchecker.cpp index 91a2df2e8..adb1345ea 100644 --- a/aegisub/dialog_spellchecker.cpp +++ b/aegisub/dialog_spellchecker.cpp @@ -66,7 +66,7 @@ DialogSpellChecker::DialogSpellChecker(wxFrame *parent) : wxDialog(parent, -1, _("Spell Checker"), wxDefaultPosition, wxDefaultSize) { // Get spell checker - spellchecker = SpellChecker::GetSpellChecker(); + spellchecker = SpellCheckerFactory::GetSpellChecker(); if (!spellchecker) { wxMessageBox(_T("No spellchecker available."),_T("Error"),wxICON_ERROR); Destroy(); diff --git a/aegisub/frame_main.cpp b/aegisub/frame_main.cpp index b14387ffb..1b9afb33c 100644 --- a/aegisub/frame_main.cpp +++ b/aegisub/frame_main.cpp @@ -234,9 +234,7 @@ void FrameMain::InitToolbar () { Toolbar->AddTool(Menu_Tools_Fonts_Collector,_("Fonts Collector"),wxBITMAP(font_collector_button),_("Open Fonts Collector")); Toolbar->AddTool(Menu_Tools_Resample,_("Resample"),wxBITMAP(resample_toolbutton),_("Resample Script Resolution")); Toolbar->AddTool(Menu_Tools_Timing_Processor,_("Timing Post-Processor"),wxBITMAP(timing_processor_toolbutton),_("Open Timing Post-processor dialog")); -#if USE_HUNSPELL == 1 Toolbar->AddTool(Menu_Tools_SpellCheck,_("Spell Checker"),wxBITMAP(spellcheck_toolbutton),_("Open Spell checker")); -#endif Toolbar->AddSeparator(); // Options @@ -332,9 +330,7 @@ void FrameMain::InitMenu() { AppendBitmapMenuItem (subtitlesMenu,Menu_Tools_Translation, _("&Translation Assistant..."),_("Open translation assistant"), wxBITMAP(translation_toolbutton)); AppendBitmapMenuItem (subtitlesMenu,Menu_Tools_Resample,_("Resample resolution..."), _("Changes resolution and modifies subtitles to conform to change"), wxBITMAP(resample_toolbutton)); AppendBitmapMenuItem (subtitlesMenu,Menu_Tools_Fonts_Collector, _("&Fonts Collector..."),_("Open fonts collector"), wxBITMAP(font_collector_button)); -#if USE_HUNSPELL == 1 AppendBitmapMenuItem (subtitlesMenu,Menu_Tools_SpellCheck, _("Spe&ll Checker..."),_("Open spell checker"), wxBITMAP(spellcheck_toolbutton)); -#endif MenuBar->Append(subtitlesMenu, _("&Subtitles")); // Create timing menu diff --git a/aegisub/mythes.cxx b/aegisub/mythes.cxx index a1654c34b..461636c9c 100644 --- a/aegisub/mythes.cxx +++ b/aegisub/mythes.cxx @@ -38,12 +38,11 @@ #include #include #include -#include "setup.h" #include "mythes.hxx" // some basic utility routines -#if USE_HUNSPELL == 0 +#if 0 // string duplication routine char * mystrdup(const char * p) { @@ -56,7 +55,17 @@ char * mystrdup(const char * p) } return NULL; } + +// remove cross-platform text line end characters +void mychomp(char * s) +{ + int k = strlen(s); + if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0'; + if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0'; +} #endif +char * mystrdup(const char * p); +void mychomp(char * s); // return index of char in string @@ -68,17 +77,6 @@ int mystr_indexOfChar(const char * d, int c) } -// remove cross-platform text line end characters -#if USE_HUNSPELL == 0 -void mychomp(char * s) -{ - int k = strlen(s); - if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0'; - if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0'; -} -#endif - - MyThes::MyThes(const char* idxpath, const char * datpath) { nw = 0; diff --git a/aegisub/options.cpp b/aegisub/options.cpp index 5c5ef4706..993eceec3 100644 --- a/aegisub/options.cpp +++ b/aegisub/options.cpp @@ -108,6 +108,7 @@ void OptionsManager::LoadDefaults(bool onlyDefaults) { // Edit Box SetText(_T("Dictionaries path"),_T("dictionaries")); + SetText(_T("Spell Checker"),_T("hunspell")); SetBool(_T("Link time boxes commit"),true); SetModificationType(MOD_EDIT_BOX); SetBool(_T("Call tips enabled"),true); diff --git a/aegisub/setup.cpp b/aegisub/setup.cpp index d0a65f36b..57ccf4d6f 100644 --- a/aegisub/setup.cpp +++ b/aegisub/setup.cpp @@ -93,15 +93,4 @@ #pragma comment(lib, "wsock32.lib") -//////////// -// Hunspell -#if USE_HUNSPELL == 1 -#ifdef __WXDEBUG__ -#pragma comment(lib,"hunspelld.lib") -#else -#pragma comment(lib,"hunspell.lib") -#endif -#endif - - #endif // VisualC diff --git a/aegisub/setup0.h b/aegisub/setup0.h index 3444bb6d1..e8de428c0 100644 --- a/aegisub/setup0.h +++ b/aegisub/setup0.h @@ -42,36 +42,6 @@ // - -//////////////////////////////////// -// Enable DirectShow Video Provider -// Requires: Win32, DirectX SDK -#define USE_DIRECTSHOW 0 - - -/////////////////////////////////// -// Enable DirectSound Audio Player -// Requires: Win32, DirectX SDK -#define USE_DIRECTSOUND 1 - - -///////////////////////////////// -// Enable PortAudio Audio Player -// Requires: PortAudio library -#define USE_PORTAUDIO 0 - - -//////////////////////////////// -// Enable Hunspell spellchecker -#define USE_HUNSPELL 0 - - -////////////////////////////// -// Enable LAVC video provider -// Requires: FFMPEG library -#define USE_LAVC 0 - - //////////////////////// // Enable PRS Exporting // Requires: wxPNG library @@ -84,17 +54,7 @@ #define USE_FEXTRACKER 1 -// The following two are Linux-specific, so it would involve changing the makefiles -// Therefore, I haven't changed the code to make them work, yet - - ///////////////// // Enable LibASS -// Requires: libass library, GNU? +// Requires: libass library #define USE_LIBASS 0 - - -////////////// -// Enable ASA -// Requires: asa library -#define USE_ASA 0 diff --git a/aegisub/spellchecker.cpp b/aegisub/spellchecker.cpp index 6295acf53..f0b5de4d3 100644 --- a/aegisub/spellchecker.cpp +++ b/aegisub/spellchecker.cpp @@ -38,22 +38,35 @@ // Headers #include "setup.h" #include "spellchecker.h" -#if USE_HUNSPELL == 1 -#include "spellchecker_hunspell.h" -#endif +#include "options.h" ///////////////////// // Get spell checker -SpellChecker *SpellChecker::GetSpellChecker() { - // Initialize - SpellChecker *check = NULL; +SpellChecker *SpellCheckerFactory::GetSpellChecker() { + // List of providers + wxArrayString list = GetFactoryList(Options.AsText(_T("Spell Checker"))); - // Try hunspell - #if USE_HUNSPELL == 1 - check = new HunspellSpellChecker(); - #endif + // None available + if (list.Count() == 0) throw _T("No spell checkers are available."); - // Return - return check; + // Get provider + wxString error; + for (unsigned int i=0;iCreateSpellChecker(); + if (checker) return checker; + } + catch (wxString err) { error += list[i] + _T(" factory: ") + err + _T("\n"); } + catch (const wxChar *err) { error += list[i] + _T(" factory: ") + wxString(err) + _T("\n"); } + catch (...) { error += list[i] + _T(" factory: Unknown error\n"); } + } + + // Failed + throw error; } + + +////////// +// Static +template std::map* AegisubFactory::factories=NULL; diff --git a/aegisub/spellchecker.h b/aegisub/spellchecker.h index 7b8ebaf62..71fa86acb 100644 --- a/aegisub/spellchecker.h +++ b/aegisub/spellchecker.h @@ -40,14 +40,13 @@ /////////// // Headers #include +#include "factory.h" /////////////////////////// // Spellchecking interface class SpellChecker { public: - static SpellChecker *GetSpellChecker(); - SpellChecker() {} virtual ~SpellChecker() {} @@ -60,3 +59,16 @@ public: virtual wxArrayString GetLanguageList()=0; virtual void SetLanguage(wxString language)=0; }; + + +/////////// +// Factory +class SpellCheckerFactory : public AegisubFactory { +protected: + virtual SpellChecker *CreateSpellChecker()=0; + SpellCheckerFactory(wxString name) { RegisterFactory(name); } + +public: + virtual ~SpellCheckerFactory() {} + static SpellChecker *GetSpellChecker(); +}; diff --git a/aegisub/spellchecker_hunspell.cpp b/aegisub/spellchecker_hunspell.cpp index 097d52d67..5d75e4680 100644 --- a/aegisub/spellchecker_hunspell.cpp +++ b/aegisub/spellchecker_hunspell.cpp @@ -36,18 +36,63 @@ /////////// // Headers -#include "setup.h" -#if USE_HUNSPELL == 1 +#include "spellchecker.h" +#include "main.h" +#include "utils.h" +#include "options.h" #include #include +#include #include #include #include #include -#include "spellchecker_hunspell.h" -#include "main.h" -#include "utils.h" -#include "options.h" + + +///////////// +// Libraries +#if __VISUALC__ >= 1200 +#ifdef __WXDEBUG__ +#pragma comment(lib,"hunspelld.lib") +#else +#pragma comment(lib,"hunspell.lib") +#endif +#endif + + +////////////////// +// Hunspell class +class HunspellSpellChecker : public SpellChecker { +private: + Hunspell *hunspell; + wxCSConv *conv; + wxString affpath; + wxString dicpath; + + void Reset(); + +public: + HunspellSpellChecker(); + ~HunspellSpellChecker(); + + void AddWord(wxString word); + bool CanAddWord(wxString word); + + bool CheckWord(wxString word); + wxArrayString GetSuggestions(wxString word); + + wxArrayString GetLanguageList(); + void SetLanguage(wxString language); +}; + + +/////////// +// Factory +class HunspellSpellCheckerFactory : public SpellCheckerFactory { +public: + SpellChecker *CreateSpellChecker() { return new HunspellSpellChecker(); } + HunspellSpellCheckerFactory() : SpellCheckerFactory(_T("hunspell")) {} +} registerHunspell; /////////////// @@ -234,5 +279,3 @@ void HunspellSpellChecker::SetLanguage(wxString language) { conv = NULL; if (hunspell) conv = new wxCSConv(wxString(hunspell->get_dic_encoding(),wxConvUTF8)); } - -#endif diff --git a/aegisub/spellchecker_hunspell.h b/aegisub/spellchecker_hunspell.h deleted file mode 100644 index d2a737ada..000000000 --- a/aegisub/spellchecker_hunspell.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2006, Rodrigo Braz Monteiro -// 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:zeratul@cellosoft.com -// - - -/////////// -// Headers -#include "setup.h" -#if USE_HUNSPELL == 1 -#include "spellchecker.h" -#include - - -////////////// -// Prototypes -class Hunspell; - - -////////////////// -// Hunspell class -class HunspellSpellChecker : public SpellChecker { -private: - Hunspell *hunspell; - wxCSConv *conv; - wxString affpath; - wxString dicpath; - - void Reset(); - -public: - HunspellSpellChecker(); - ~HunspellSpellChecker(); - - void AddWord(wxString word); - bool CanAddWord(wxString word); - - bool CheckWord(wxString word); - wxArrayString GetSuggestions(wxString word); - - wxArrayString GetLanguageList(); - void SetLanguage(wxString language); -}; - - -#endif diff --git a/aegisub/stdwx.h b/aegisub/stdwx.h index a9b992ccd..bfbeccd66 100644 --- a/aegisub/stdwx.h +++ b/aegisub/stdwx.h @@ -79,7 +79,6 @@ #include #include #include -//#include #include #include #include @@ -92,18 +91,4 @@ #include -/////////////// -// DirectSound -#if USE_DIRECTSOUND == 1 -//#include -#endif - - -//////////// -// Hunspell -#if USE_HUNSPELL == 1 -#include -#endif - - #endif // C++ diff --git a/aegisub/subs_edit_ctrl.cpp b/aegisub/subs_edit_ctrl.cpp index 24b4ba555..8284f3b73 100644 --- a/aegisub/subs_edit_ctrl.cpp +++ b/aegisub/subs_edit_ctrl.cpp @@ -69,7 +69,7 @@ SubsTextEditCtrl::SubsTextEditCtrl(wxWindow* parent, wxWindowID id, const wxStri CmdKeyClear('U',wxSTC_SCMOD_CTRL); // Set spellchecker - spellchecker = SpellChecker::GetSpellChecker(); + spellchecker = SpellCheckerFactory::GetSpellChecker(); // Set thesaurus thesaurus = Thesaurus::GetThesaurus(); diff --git a/aegisub/text_file_reader.cpp b/aegisub/text_file_reader.cpp index 7a68991db..4bbffecea 100644 --- a/aegisub/text_file_reader.cpp +++ b/aegisub/text_file_reader.cpp @@ -185,7 +185,7 @@ void TextFileReader::SetEncodingConfiguration() { wxString TextFileReader::ReadLineFromFile() { Open(); wxString wxbuffer; - int bufAlloc = 1024; + size_t bufAlloc = 1024; wxbuffer.Alloc(bufAlloc); #ifdef TEXT_READER_USE_STDIO char buffer[512]; @@ -199,7 +199,6 @@ wxString TextFileReader::ReadLineFromFile() { char charbuffer[3]; charbuffer[2] = 0; wchar_t ch = 0; - int n = 0; size_t len = 0; #ifdef TEXT_READER_USE_STDIO while (ch != L'\n' && !feof(file)) {