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