Skip to content

Commit

Permalink
use boost::json to parse name
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Apr 20, 2024
1 parent de0894a commit 8c1feb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if (ENABLE_GUI)
endif()
endif()

find_package(Boost 1.61 REQUIRED COMPONENTS iostreams)
find_package(Boost 1.75 REQUIRED COMPONENTS iostreams)
find_package(LibIMEPinyin 1.1.7 REQUIRED)
find_package(LibIMETable 1.1.4 REQUIRED)

Expand Down
33 changes: 24 additions & 9 deletions modules/chttrans/chttrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <fcntl.h>
#ifdef ENABLE_OPENCC
#include "chttrans-opencc.h"
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/json/src.hpp>
#endif
#include "chttrans-native.h"
#include <fcitx/inputcontext.h>
Expand Down Expand Up @@ -215,15 +218,27 @@ void Chttrans::save() {

const Configuration *Chttrans::getConfig() const {
#ifdef ENABLE_OPENCC
std::vector<std::string> profiles;
StandardPath::global().scanFiles(
StandardPath::Type::Data, "opencc",
[&profiles](const std::string &path, const std::string &, bool) {
if (stringutils::endsWith(path, ".json")) {
profiles.emplace_back(path);
}
return true;
});
std::vector<std::pair<std::string, std::string>> profiles{{"", ""}};
auto files = StandardPath::global().multiOpen(
StandardPath::Type::Data, "opencc", O_RDONLY, filter::Suffix(".json"));
for (const auto &file : files) {
try {
boost::iostreams::stream_buffer<
boost::iostreams::file_descriptor_source>
buffer(file.second.fd(),
boost::iostreams::file_descriptor_flags::
never_close_handle);
std::istream in(&buffer);
std::string strBuf(std::istreambuf_iterator<char>(in), {});
auto jv = boost::json::parse(strBuf);
const auto &obj = jv.as_object();
auto name = boost::json::value_to<std::string>(obj.at("name"));
profiles.emplace_back(file.first, std::move(name));
} catch (const std::exception &e) {
FCITX_WARN() << "Failed to parse " << file.first;
profiles.emplace_back(file.first, file.first);
}
}
config_.openCCS2TProfile.annotation().setProfiles(profiles);
config_.openCCT2SProfile.annotation().setProfiles(std::move(profiles));
#endif
Expand Down
10 changes: 6 additions & 4 deletions modules/chttrans/chttrans.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@

#ifdef ENABLE_OPENCC
struct OpenCCAnnotation : public fcitx::EnumAnnotation {
void setProfiles(std::vector<std::string> profiles) {
void
setProfiles(std::vector<std::pair<std::string, std::string>> profiles) {
profiles_ = std::move(profiles);
}
void dumpDescription(fcitx::RawConfig &config) const {
fcitx::EnumAnnotation::dumpDescription(config);
for (size_t i = 0; i < profiles_.size(); i++) {
config.setValueByPath("Enum/" + std::to_string(i), profiles_[i]);
config.setValueByPath("Enum/" + std::to_string(i),
profiles_[i].first);
config.setValueByPath("EnumI18n/" + std::to_string(i),
profiles_[i]);
profiles_[i].second);
}
}

private:
std::vector<std::string> profiles_;
std::vector<std::pair<std::string, std::string>> profiles_;
};
#endif

Expand Down

0 comments on commit 8c1feb3

Please sign in to comment.