Make parse prop file a util function

This commit is contained in:
topjohnwu
2019-03-05 20:27:09 -05:00
parent b278d07b05
commit 04ef1e6405
8 changed files with 35 additions and 59 deletions

View File

@@ -367,8 +367,8 @@ void write_zero(int fd, size_t size) {
}
}
void file_readline(const char *filename, const function<bool (string_view&)> &fn, bool trim) {
FILE *fp = xfopen(filename, "re");
void file_readline(const char *file, const function<bool (string_view)> &fn, bool trim) {
FILE *fp = xfopen(file, "re");
if (fp == nullptr)
return;
size_t len = 1024;
@@ -383,10 +383,22 @@ void file_readline(const char *filename, const function<bool (string_view&)> &fn
while (*start == ' ')
++start;
}
string_view s(start);
if (!fn(s))
if (!fn(start))
break;
}
fclose(fp);
free(buf);
}
void parse_prop_file(const char *file, const function<bool (string_view, string_view)> &fn) {
file_readline(file, [&](string_view line_view) -> bool {
char *line = (char *) line_view.data();
if (line[0] == '#')
return true;
char *eql = strchr(line, '=');
if (eql == nullptr || eql == line)
return true;
*eql = '\0';
return fn(line, eql + 1);
}, true);
}

View File

@@ -172,7 +172,9 @@ private:
// file.cpp
void file_readline(const char *filename, const std::function<bool (std::string_view&)> &fn, bool trim = false);
void file_readline(const char *file, const std::function<bool (std::string_view)> &fn, bool trim = false);
void parse_prop_file(const char *file, const std::function
<bool(std::string_view, std::string_view)> &fn);
void *__mmap(const char *filename, size_t *size, bool rw);
template <typename B>