From 236a140b445e1f9cc1b1d2df27bf03abd0aa425b Mon Sep 17 00:00:00 2001 From: Weng Xuetian Date: Mon, 18 Nov 2024 10:08:50 -0800 Subject: [PATCH] Clean up --- CMakeLists.txt | 11 +++-------- tools/CMakeLists.txt | 11 +++++++++-- tools/filesystem_helper.cpp | 26 ++++++++++++++++++++++++++ tools/filesystem_helper.h | 19 +++++++++++++++++++ tools/libime_migrate_fcitx4_pinyin.cpp | 20 +++----------------- tools/libime_migrate_fcitx4_table.cpp | 13 ++----------- 6 files changed, 62 insertions(+), 38 deletions(-) create mode 100644 tools/filesystem_helper.cpp create mode 100644 tools/filesystem_helper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cec340d..79877f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,16 +43,11 @@ int main() { } " HAS_STD_FILESYSTEM) -if (HAS_STD_FILESYSTEM) - set(BOOST_FILESYSTEM) - set(BOOST_FILESYSTEM_LIB) -else() - add_definitions(-DUSE_BOOST_FILESYSTEM) - set(BOOST_FILESYSTEM filesystem) - set(BOOST_FILESYSTEM_LIB Boost::filesystem) +find_package(Boost 1.61 REQUIRED COMPONENTS iostreams) +if (NOT HAS_STD_FILESYSTEM) + find_package(Boost REQUIRED COMPONENTS filesystem) endif() -find_package(Boost 1.61 REQUIRED COMPONENTS iostreams ${BOOST_FILESYSTEM}) set(LIBIME_INSTALL_PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/libime") set(LIBIME_INSTALL_LIBDATADIR "${CMAKE_INSTALL_FULL_LIBDIR}/libime") diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 08a0189..e5ecf4b 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -24,12 +24,19 @@ target_link_libraries(libime_tabledict LibIME::Table) install(TARGETS libime_tabledict DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::tabledict ALIAS libime_tabledict) +add_library(filesystem_helper filesystem_helper.cpp) +if (HAS_STD_FILESYSTEM) + target_compile_definitions(filesystem_helper PRIVATE -DHAS_STD_FILESYSTEM) +else() + target_link_libraries(filesystem_helper PUBLIC Boost::filesystem) +endif() + add_executable(libime_migrate_fcitx4_table libime_migrate_fcitx4_table.cpp) -target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams ${BOOST_FILESYSTEM_LIB}) +target_link_libraries(libime_migrate_fcitx4_table LibIME::Table Boost::iostreams filesystem_helper) install(TARGETS libime_migrate_fcitx4_table DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_table ALIAS libime_migrate_fcitx4_table) add_executable(libime_migrate_fcitx4_pinyin libime_migrate_fcitx4_pinyin.cpp) -target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams ${BOOST_FILESYSTEM_LIB}) +target_link_libraries(libime_migrate_fcitx4_pinyin LibIME::Pinyin Boost::iostreams filesystem_helper) install(TARGETS libime_migrate_fcitx4_pinyin DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools) add_executable(LibIME::migrate_fcitx4_pinyin ALIAS libime_migrate_fcitx4_pinyin) diff --git a/tools/filesystem_helper.cpp b/tools/filesystem_helper.cpp new file mode 100644 index 0000000..4a3d9b9 --- /dev/null +++ b/tools/filesystem_helper.cpp @@ -0,0 +1,26 @@ +/* + * SPDX-FileCopyrightText: 2024~2024 CSSlayer + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include +#include + +#ifdef HAS_STD_FILESYSTEM +#include +#else +#include +#endif + +namespace libime { + +std::string absolutePath(const std::string &path) { +#ifdef HAS_STD_FILESYSTEM + return std::filesystem::absolute(path); +#else + return boost::filesystem::absolute(path).string(); +#endif +} + +} // namespace libime diff --git a/tools/filesystem_helper.h b/tools/filesystem_helper.h new file mode 100644 index 0000000..fa02206 --- /dev/null +++ b/tools/filesystem_helper.h @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2024~2024 CSSlayer + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#ifndef _TOOLS_FILESYSTEM_HELPER_H_ +#define _TOOLS_FILESYSTEM_HELPER_H_ + +#include +#include + +namespace libime { + +std::string absolutePath(const std::string &path); + +} + +#endif diff --git a/tools/libime_migrate_fcitx4_pinyin.cpp b/tools/libime_migrate_fcitx4_pinyin.cpp index 35f9a2c..ebc3197 100644 --- a/tools/libime_migrate_fcitx4_pinyin.cpp +++ b/tools/libime_migrate_fcitx4_pinyin.cpp @@ -5,6 +5,7 @@ * */ +#include "filesystem_helper.h" #include "libime/core/historybigram.h" #include "libime/core/utils.h" #include "libime/core/utils_p.h" @@ -18,12 +19,6 @@ #include #include -#ifdef USE_BOOST_FILESYSTEM -#include -#else -#include -#endif - static const std::array PYFA = { "AA", "AB", "AC", "AD", "AE", "AF", "AH", "AI", "AJ", "AU", "AV", "AW", "AX", "AY", "AZ", "Aa", "Ac", "Ad", "Ae", "BA", "BB", "BC", "BD", "BE", @@ -347,11 +342,7 @@ int main(int argc, char *argv[]) { if (dictFile[0] == '/') { outputDictFile = dictFile; } else { -#ifdef USE_BOOST_FILESYSTEM - outputDictFile = boost::filesystem::absolute(dictFile).string(); -#else - outputDictFile = std::filesystem::absolute(dictFile); -#endif + outputDictFile = absolutePath(dictFile); } } StandardPath::global().safeSave( @@ -373,12 +364,7 @@ int main(int argc, char *argv[]) { if (historyFile[0] == '/') { outputHistoryFile = historyFile; } else { -#ifdef USE_BOOST_FILESYSTEM - outputHistoryFile = - boost::filesystem::absolute(historyFile).string(); -#else - outputHistoryFile = std::filesystem::absolute(historyFile); -#endif + outputHistoryFile = absolutePath(historyFile); } } StandardPath::global().safeSave( diff --git a/tools/libime_migrate_fcitx4_table.cpp b/tools/libime_migrate_fcitx4_table.cpp index ff5ae21..11c6286 100644 --- a/tools/libime_migrate_fcitx4_table.cpp +++ b/tools/libime_migrate_fcitx4_table.cpp @@ -6,6 +6,7 @@ * */ #include "config.h" +#include "filesystem_helper.h" #include "libime/core/historybigram.h" #include "libime/core/utils.h" #include "libime/core/utils_p.h" @@ -17,12 +18,6 @@ #include #include -#ifdef USE_BOOST_FILESYSTEM -#include -#else -#include -#endif - using namespace libime; using namespace fcitx; @@ -82,11 +77,7 @@ struct MigrationCommonOption { return stringutils::joinPath("table", path); } -#ifdef USE_BOOST_FILESYSTEM - return boost::filesystem::absolute(path).string(); -#else - return std::filesystem::absolute(path); -#endif + return absolutePath(path); } return path; }