More efficient xml parsing

This commit is contained in:
topjohnwu 2019-03-09 04:27:04 -05:00
parent 081074ad9d
commit f24a5dfd45
2 changed files with 19 additions and 16 deletions

View File

@ -99,27 +99,29 @@ static bool parse_packages_xml(string_view s) {
return true; return true;
/* <package key1="value1" key2="value2"....> */ /* <package key1="value1" key2="value2"....> */
char *start = (char *) s.data(); char *start = (char *) s.data();
start[s.length() - 2] = '\0'; /* Remove trailing '>' */ start[s.length() - 1] = '\0'; /* Remove trailing '>' */
start += 9; /* Skip '<package ' */ start += 9; /* Skip '<package ' */
char key[32], value[1024]; string_view pkg;
const char *pkg = nullptr; for (char *tok = start; *tok;) {
char *eql = strchr(tok, '=');
char *tok; *eql = '\0'; /* Terminate '=' */
while ((tok = strtok_r(nullptr, " ", &start))) { string_view key(tok, eql - tok);
sscanf(tok, "%[^=]=\"%[^\"]", key, value); eql += 2; /* Skip '="' */
string_view key_view(key); tok = strchr(eql, '\"'); /* Find closing '"' */
string_view value_view(value); *tok = '\0';
if (key_view == "name") { string_view value(eql, tok - eql);
tok += 2;
if (key == "name") {
for (auto &hide : hide_set) { for (auto &hide : hide_set) {
if (hide.first == value_view) { if (hide.first == value) {
pkg = hide.first.data(); pkg = hide.first;
break; break;
} }
} }
if (!pkg) if (pkg.empty())
return true; return true;
} else if (key_view == "userId" || key_view == "sharedUserId") { } else if (key == "userId" || key == "sharedUserId") {
int uid = parse_int(value); int uid = parse_int(value);
for (auto &hide : hide_set) { for (auto &hide : hide_set) {
if (hide.first == pkg) if (hide.first == pkg)

View File

@ -378,8 +378,9 @@ void file_readline(const char *file, const function<bool (string_view)> &fn, boo
while ((read = getline(&buf, &len, fp)) >= 0) { while ((read = getline(&buf, &len, fp)) >= 0) {
start = buf; start = buf;
if (trim) { if (trim) {
while (buf[read - 1] == '\n' || buf[read - 1] == ' ') while (read && (buf[read - 1] == '\n' || buf[read - 1] == ' '))
buf[read-- - 1] = '\0'; --read;
buf[read] = '\0';
while (*start == ' ') while (*start == ' ')
++start; ++start;
} }