Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Stop using deprecated hunspell API
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqr committed Sep 8, 2019
1 parent 2b93d33 commit 7e253d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ if (HUNSPELL_FOUND)
target_link_libraries (Aegisub ${HUNSPELL_LIBRARIES})
add_definitions("-DWITH_HUNSPELL")
target_sources(Aegisub PRIVATE src/spellchecker_hunspell.cpp)
if (HUNSPELL_HAS_STRING_API)
target_compile_definitions(Aegisub PRIVATE "HUNSPELL_HAS_STRING_API")
endif(HUNSPELL_HAS_STRING_API)
endif (HUNSPELL_FOUND)

find_package(PulseAudio)
Expand Down
25 changes: 17 additions & 8 deletions cmake/FindHunspell.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
# HUNSPELL_FOUND - system has Hunspell
# HUNSPELL_INCLUDE_DIR - the Hunspell include directory
# HUNSPELL_LIBRARIES - Link these to use Hunspell
# HUNSPELL_HAS_STRING_API - Hunspell has vector<string> api (>=1.5.1)
#
# Redistribution and use of this file is allowed according to the terms of the
# MIT license. For details see the file COPYING-CMAKE-MODULES.


if ( HUNSPELL_INCLUDE_DIR AND HUNSPELL_LIBRARIES )
# in cache already
SET(Hunspell_FIND_QUIETLY TRUE)
endif ( HUNSPELL_INCLUDE_DIR AND HUNSPELL_LIBRARIES )

# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
if( NOT WIN32 )
Expand Down Expand Up @@ -45,13 +41,26 @@ FIND_LIBRARY(HUNSPELL_LIBRARIES NAMES hunspell-1.7 hunspell-1.6 hunspell-1.5 hun
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Hunspell DEFAULT_MSG HUNSPELL_LIBRARIES HUNSPELL_INCLUDE_DIR )

# show the HUNSPELL_INCLUDE_DIR and HUNSPELL_LIBRARIES variables only in the advanced view
MARK_AS_ADVANCED(HUNSPELL_INCLUDE_DIR HUNSPELL_LIBRARIES )

add_library(hunspell UNKNOWN IMPORTED)
set_target_properties(hunspell PROPERTIES IMPORTED_LOCATION ${HUNSPELL_LIBRARIES} INTERFACE_INCLUDE_DIRECTORIES ${HUNSPELL_INCLUDE_DIR})
if (NOT BUILD_SHARED_LIBS)
# At least statically compiled hunspell 1.7.0 requires HUNSPELL_STATIC
# For other versions, it should not hurt
set_target_properties(hunspell PROPERTIES INTERFACE_COMPILE_DEFINITIONS HUNSPELL_STATIC)
endif ()

if (HUNSPELL_FOUND)
try_compile(HUNSPELL_HAS_STRING_API "${CMAKE_BINARY_DIR}/hunspell_string_api"
"${CMAKE_CURRENT_LIST_DIR}/hunspell_string_api.cpp"
LINK_LIBRARIES ${HUNSPELL_LIBRARIES}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${HUNSPELL_INCLUDE_DIR}" "-DLINK_LIBRARIES=${HUNSPELL_LIBRARIES}"
OUTPUT_VARIABLE debuggggg)
if (HUNSPELL_HAS_STRING_API)
message(STATUS "Hunspell has string API")
else(HUNSPELL_HAS_STRING_API)
message(STATUS "Hunspell does not have string API")
endif(HUNSPELL_HAS_STRING_API)
endif(HUNSPELL_FOUND)

# show the HUNSPELL_INCLUDE_DIR and HUNSPELL_LIBRARIES variables only in the advanced view
MARK_AS_ADVANCED(HUNSPELL_INCLUDE_DIR HUNSPELL_LIBRARIES HUNSPELL_HAS_STRING_API)
10 changes: 10 additions & 0 deletions cmake/hunspell_string_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <hunspell/hunspell.hxx>
#include <string>

int main() {
Hunspell hunspell(NULL, NULL);
std::string word = "";
hunspell.suggest(word);
hunspell.spell(word);
return 0;
}
27 changes: 25 additions & 2 deletions src/spellchecker_hunspell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ bool HunspellSpellChecker::CanAddWord(std::string const& word) {
}

bool HunspellSpellChecker::CanRemoveWord(std::string const& word) {
return !!customWords.count(word);
return customWords.count(word) != 0;
}

void HunspellSpellChecker::AddWord(std::string const& word) {
if (!hunspell) return;

// Add it to the in-memory dictionary
#ifdef HUNSPELL_HAS_STRING_API
hunspell->add(conv->Convert(word));
#else
hunspell->add(conv->Convert(word).c_str());
#endif

// Add the word
if (customWords.insert(word).second)
Expand All @@ -73,7 +77,11 @@ void HunspellSpellChecker::RemoveWord(std::string const& word) {
if (!hunspell) return;

// Remove it from the in-memory dictionary
#ifdef HUNSPELL_HAS_STRING_API
hunspell->remove(conv->Convert(word));
#else
hunspell->remove(conv->Convert(word).c_str());
#endif

auto word_iter = customWords.find(word);
if (word_iter != customWords.end()) {
Expand Down Expand Up @@ -120,7 +128,11 @@ void HunspellSpellChecker::WriteUserDictionary() {
bool HunspellSpellChecker::CheckWord(std::string const& word) {
if (!hunspell) return true;
try {
return hunspell->spell(conv->Convert(word).c_str()) == 1;
#ifdef HUNSPELL_HAS_STRING_API
return hunspell->spell(conv->Convert(word));
#else
return hunspell->spell(conv->Convert(word).c_str()) != 0;
#endif
}
catch (agi::charset::ConvError const&) {
return false;
Expand All @@ -131,6 +143,12 @@ std::vector<std::string> HunspellSpellChecker::GetSuggestions(std::string const&
std::vector<std::string> suggestions;
if (!hunspell) return suggestions;

#ifdef HUNSPELL_HAS_STRING_API
std::vector<std::string> suggestions_dic_encoding = hunspell->suggest(conv->Convert(word));
suggestions.reserve(suggestions_dic_encoding.size());
for (std::string& result : suggestions_dic_encoding)
suggestions.push_back(rconv->Convert(result));
#else
char **results;
int n = hunspell->suggest(&results, conv->Convert(word).c_str());

Expand All @@ -147,6 +165,7 @@ std::vector<std::string> HunspellSpellChecker::GetSuggestions(std::string const&
}

free(results);
#endif

return suggestions;
}
Expand Down Expand Up @@ -212,7 +231,11 @@ void HunspellSpellChecker::OnLanguageChanged() {

for (auto const& word : customWords) {
try {
#ifdef HUNSPELL_HAS_STRING_API
hunspell->add(conv->Convert(word));
#else
hunspell->add(conv->Convert(word).c_str());
#endif
}
catch (agi::charset::ConvError const&) {
// Normally this shouldn't happen, but some versions of Aegisub
Expand Down

0 comments on commit 7e253d5

Please sign in to comment.