Skip to content

Commit

Permalink
Get rid of C getline usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Feb 7, 2025
1 parent 206c3ce commit 98fac6a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.6)

project(fcitx-m17n VERSION 5.1.3)

set(REQUIRED_FCITX_VERSION 5.1.12)
set(REQUIRED_FCITX_VERSION 5.1.13)
find_package(ECM REQUIRED 1.0.0)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
include(FeatureSummary)
Expand Down
7 changes: 2 additions & 5 deletions im/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,8 @@ M17NEngine::M17NEngine(Instance *instance)

auto file = StandardPath::global().open(StandardPath::Type::PkgData,
"m17n/default", O_RDONLY);
FILE *fp = fdopen(file.fd(), "r");
if (fp) {
file.release();
list_ = ParseDefaultSettings(fp);
fclose(fp);
if (file.isValid()) {
list_ = ParseDefaultSettings(file.fd());
}

instance_->inputContextManager().registerProperty("m17nState", &factory_);
Expand Down
17 changes: 10 additions & 7 deletions im/overrideparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fcitx-utils/fdstreambuf.h>
#include <fcitx-utils/stringutils.h>
#include <istream>
#include <string>
#include <vector>

using namespace fcitx;

std::vector<OverrideItem> ParseDefaultSettings(FILE *fp) {
char *buf = nullptr;
size_t bufsize = 0;
std::vector<OverrideItem> ParseDefaultSettings(int fd) {
std::vector<OverrideItem> list;
while (getline(&buf, &bufsize, fp) != -1) {

IFDStreamBuf buf(fd);
std::istream in(&buf);
std::string line;
while (std::getline(in, line)) {
/* ignore comments */
if (!buf || buf[0] == '#') {
if (!line.empty() || line[0] == '#') {
continue;
}
auto trimmed = stringutils::trim(buf);
const auto trimmed = stringutils::trimView(line);
auto strList = stringutils::split(trimmed, ":");

do {
Expand Down Expand Up @@ -53,7 +57,6 @@ std::vector<OverrideItem> ParseDefaultSettings(FILE *fp) {
}
} while (0);
}
free(buf);

std::stable_sort(list.begin(), list.end(),
[](const auto &lhs, const auto &rhs) {
Expand Down
2 changes: 1 addition & 1 deletion im/overrideparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct OverrideItem {
int wildcardCount;
};

std::vector<OverrideItem> ParseDefaultSettings(FILE *fp);
std::vector<OverrideItem> ParseDefaultSettings(int fd);
const OverrideItem *MatchDefaultSettings(const std::vector<OverrideItem> &list,
const std::string &lang,
const std::string &name);
Expand Down

0 comments on commit 98fac6a

Please sign in to comment.