Skip to content

Commit 2e818ff

Browse files
committed
Get rid of C getline in code
1 parent bb62714 commit 2e818ff

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

src/lib/fcitx-config/iniparser.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <fcntl.h>
99
#include <cstdio>
1010
#include <functional>
11+
#include <istream>
1112
#include <string>
1213
#include <string_view>
14+
#include "fcitx-utils/fdstreambuf.h"
1315
#include "fcitx-utils/fs.h"
1416
#include "fcitx-utils/macros.h"
1517
#include "fcitx-utils/misc.h"
@@ -21,17 +23,11 @@
2123

2224
namespace fcitx {
2325

24-
void readFromIni(RawConfig &config, int fd) {
25-
if (fd < 0) {
26-
return;
27-
}
28-
// dup it
29-
UnixFD unixFD(fd);
30-
UniqueFilePtr fp = fs::openFD(unixFD, "rb");
26+
void readFromIni(RawConfig &config, FILE *fp) {
3127
if (!fp) {
3228
return;
3329
}
34-
readFromIni(config, fp.get());
30+
readFromIni(config, fileno(fp));
3531
}
3632

3733
bool writeAsIni(const RawConfig &config, int fd) {
@@ -47,25 +43,27 @@ bool writeAsIni(const RawConfig &config, int fd) {
4743
return writeAsIni(config, fp.get());
4844
}
4945

50-
void readFromIni(RawConfig &config, FILE *fin) {
46+
void readFromIni(RawConfig &config, int fd) {
5147
std::string currentGroup;
5248

53-
UniqueCPtr<char> clineBuf;
54-
size_t bufSize = 0;
55-
unsigned int line = 0;
56-
while (getline(clineBuf, &bufSize, fin) >= 0) {
57-
line++;
58-
std::string_view lineBuf = stringutils::trimView(clineBuf.get());
49+
IFDStreamBuf buf(fd);
50+
std::istream in(&buf);
51+
std::string line;
52+
53+
unsigned int lineNumber = 0;
54+
while (std::getline(in, line)) {
55+
lineNumber++;
56+
std::string_view lineBuf = stringutils::trimView(line);
5957
if (lineBuf.empty() || lineBuf.front() == '#') {
6058
continue;
6159
}
6260

6361
if (lineBuf.front() == '[' && lineBuf.back() == ']') {
6462
currentGroup = lineBuf.substr(1, lineBuf.size() - 2);
6563
config.visitItemsOnPath(
66-
[line](RawConfig &config, const std::string &) {
64+
[lineNumber](RawConfig &config, const std::string &) {
6765
if (!config.lineNumber()) {
68-
config.setLineNumber(line);
66+
config.setLineNumber(lineNumber);
6967
}
7068
},
7169
currentGroup);
@@ -90,7 +88,7 @@ void readFromIni(RawConfig &config, FILE *fin) {
9088
subConfig = config.get(std::string(name), true);
9189
}
9290
subConfig->setValue(*value);
93-
subConfig->setLineNumber(line);
91+
subConfig->setLineNumber(lineNumber);
9492
}
9593
}
9694
}

src/modules/quickphrase/quickphraseprovider.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include "quickphraseprovider.h"
88
#include <fcntl.h>
99
#include <cstddef>
10+
#include <istream>
1011
#include <string>
1112
#include <string_view>
1213
#include <utility>
14+
#include "fcitx-utils/fdstreambuf.h"
1315
#include "fcitx-utils/fs.h"
1416
#include "fcitx-utils/macros.h"
1517
#include "fcitx-utils/misc.h"
@@ -45,7 +47,7 @@ void BuiltInQuickPhraseProvider::reloadConfig() {
4547
if (auto file = StandardPath::global().open(
4648
StandardPath::Type::PkgData, "data/QuickPhrase.mb", O_RDONLY);
4749
file.fd() >= 0) {
48-
load(fs::openFD(file, "rb"));
50+
load(file.fd());
4951
}
5052

5153
auto files = StandardPath::global().locate(StandardPath::Type::PkgData,
@@ -59,19 +61,18 @@ void BuiltInQuickPhraseProvider::reloadConfig() {
5961
continue;
6062
}
6163
UnixFD fd = UnixFD::own(open(p.second.c_str(), O_RDONLY));
62-
load(fs::openFD(fd, "rb"));
64+
if (fd.isValid()) {
65+
load(fd.fd());
66+
}
6367
}
6468
}
6569

66-
void BuiltInQuickPhraseProvider::load(UniqueFilePtr fp) {
67-
if (!fp) {
68-
return;
69-
}
70-
71-
UniqueCPtr<char> buf;
72-
size_t len = 0;
73-
while (getline(buf, &len, fp.get()) != -1) {
74-
std::string_view text = stringutils::trimView(buf.get());
70+
void BuiltInQuickPhraseProvider::load(int fd) {
71+
IFDStreamBuf buf(fd);
72+
std::istream in(&buf);
73+
std::string line;
74+
while (std::getline(in, line)) {
75+
std::string_view text = stringutils::trimView(line);
7576
if (text.empty()) {
7677
continue;
7778
}

src/modules/quickphrase/quickphraseprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BuiltInQuickPhraseProvider : public QuickPhraseProvider {
3939
void reloadConfig();
4040

4141
private:
42-
void load(UniqueFilePtr fp);
42+
void load(int fd);
4343
std::multimap<std::string, std::string> map_;
4444
};
4545

0 commit comments

Comments
 (0)