Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Nov 18, 2024
1 parent 4cebf58 commit 236a140
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
11 changes: 3 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
11 changes: 9 additions & 2 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 26 additions & 0 deletions tools/filesystem_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include <string>
#include <string_view>

#ifdef HAS_STD_FILESYSTEM
#include <filesystem>
#else
#include <boost/filesystem.hpp>
#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
19 changes: 19 additions & 0 deletions tools/filesystem_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2024~2024 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/

#ifndef _TOOLS_FILESYSTEM_HELPER_H_
#define _TOOLS_FILESYSTEM_HELPER_H_

#include <string>
#include <string_view>

namespace libime {

std::string absolutePath(const std::string &path);

}

#endif
20 changes: 3 additions & 17 deletions tools/libime_migrate_fcitx4_pinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/

#include "filesystem_helper.h"
#include "libime/core/historybigram.h"
#include "libime/core/utils.h"
#include "libime/core/utils_p.h"
Expand All @@ -18,12 +19,6 @@
#include <string_view>
#include <unordered_map>

#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
#else
#include <filesystem>
#endif

static const std::array<std::string, 412> 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",
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
13 changes: 2 additions & 11 deletions tools/libime_migrate_fcitx4_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,12 +18,6 @@
#include <fcntl.h>
#include <sstream>

#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
#else
#include <filesystem>
#endif

using namespace libime;
using namespace fcitx;

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 236a140

Please sign in to comment.