1 /*
2  * Copyright 2005-2007 Gerald Schmidt.
3  *
4  * This file is part of Xml Copy Editor.
5  *
6  * Xml Copy Editor is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * Xml Copy Editor is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Xml Copy Editor; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include <wx/intl.h>
22 #include <iostream>
23 #include <cstdio>
24 #include <stdexcept>
25 #include "wrapaspell.h"
26 #ifdef USE_ENCHANT
27 #include <enchant++.h>
28 #endif
29 #include "casehandler.h"
30 #include "contexthandler.h"
31 #include "getword.h"
32 
WrapAspell(const wxString & lang,const wxString & aspellDataPathParameter,const wxString & aspellDictPathParameter)33 WrapAspell::WrapAspell (
34                        const wxString& lang
35 #if !defined(USE_ENCHANT) && defined(__WXMSW__)
36                        ,
37                        const wxString& aspellDataPathParameter,
38                        const wxString& aspellDictPathParameter
39 #endif
40 )
41 {
42 #ifdef USE_ENCHANT
43 	spell_broker = enchant::Broker::instance();
44 	spell_checker = spell_broker->request_dict( std::string ( lang.mb_str() ) );
45 #else
46 	spell_config = new_aspell_config();
47 
48 #ifdef __WXMSW__
49        aspell_config_replace ( spell_config, "data-dir", aspellDataPathParameter.mb_str() );//ASPELL_DATA_PATH );
50        aspell_config_replace ( spell_config, "dict-dir", aspellDictPathParameter.mb_str() );//ASPELL_DICT_PATH );
51 #endif
52 
53 	aspell_config_replace ( spell_config, "lang", lang.mb_str() );
54 	aspell_config_replace ( spell_config, "encoding", "utf-8" );
55 	AspellCanHaveError * possible_err = new_aspell_speller ( spell_config );
56 	spell_checker = 0;
57 	if ( aspell_error_number ( possible_err ) != 0)
58 	{
59 		puts ( aspell_error_message ( possible_err ) );
60 		throw;
61 	}
62 	else
63 		spell_checker = to_aspell_speller ( possible_err );
64 #endif
65 }
66 
~WrapAspell()67 WrapAspell::~WrapAspell()
68 {
69 #ifdef USE_ENCHANT
70 	delete spell_checker;
71 #else
72 	delete_aspell_speller ( spell_checker );
73 	delete_aspell_config ( spell_config );
74 #endif
75 }
76 
checkWord(const std::string & s)77 bool WrapAspell::checkWord ( const std::string &s )
78 {
79 #ifdef USE_ENCHANT
80 	return spell_checker->check(s);
81 #else
82 	return checkWord ( (char *) s.c_str(), s.size() );
83 #endif
84 }
85 
getSuggestion(std::string & s)86 std::string WrapAspell::getSuggestion (
87     std::string &s )
88 {
89 #ifdef USE_ENCHANT
90 	std::vector<std::string> out_suggestions;
91 	spell_checker->suggest(s, out_suggestions);
92 	return out_suggestions.empty() ? "----" : out_suggestions[0];
93 #else
94 	const AspellWordList *suggestions = aspell_speller_suggest ( spell_checker, s.c_str(), s.size() );
95 	AspellStringEnumeration *elements = aspell_word_list_elements ( suggestions );
96 	const char *word = aspell_string_enumeration_next ( elements ); // no iteration req'd
97 	return (word) ? word : "----";
98 #endif
99 }
100 
checkString(std::string & s,std::vector<ContextMatch> & v,int contextRange)101 void WrapAspell::checkString (
102     std::string &s,
103     std::vector<ContextMatch> &v,
104     int contextRange )
105 {
106 	std::string suggestion;
107 	size_t len;
108 	char *origin, *iterator, *ptr;
109 	origin = iterator = ( char * ) s.c_str();
110 	while ( ( ptr = GetWord::run ( &iterator, &len, true ) ) != NULL )
111 		if ( !checkWord ( ptr, len ) )
112 		{
113             ContextMatch m = ContextHandler::getContext (
114 			                     ptr,
115 			                     len,
116 			                     origin,
117 			                     contextRange );
118 
119 			// handle suggestion
120 			suggestion = getSuggestion ( m.match );
121 			m.replace.append ( suggestion );
122 			m.elementCount = 0;
123 			m.offset = ptr - origin;
124 			v.push_back ( m );
125 		}
126 }
127 
checkWord(const char * s,size_t len)128 bool WrapAspell::checkWord ( const char *s, size_t len )
129 {
130 #ifdef USE_ENCHANT
131 	return checkWord( std::string(s, len) );
132 #else
133 	return aspell_speller_check ( spell_checker, s, len );
134 #endif
135 }
136