Skip to content

Commit

Permalink
Slight format changes to parse + refactor for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed May 20, 2024
1 parent 9fc9cd6 commit 5239f72
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/dictpopup_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ static s8 json_get_string_(json_stream *json) {
size_t slen = 0;
s8 r = {0};
r.s = (u8 *)json_get_string(json, &slen);
r.len = slen > 0 ? (size)(slen - 1) : 0; // API includes terminating 0 in
// length
r.len = slen > 0 ? (size)(slen - 1) : 0; // API includes terminating 0 in length
return r;
}

Expand Down Expand Up @@ -109,7 +108,7 @@ static s8 parse_liststyletype(s8 lst) {
else if (s8equals(lst, S("square")))
return S("▪");
else if (s8equals(lst, S("none")))
return S(" ");
return S(" ");
else
return s8dup(lst); // そのまま
}
Expand Down Expand Up @@ -221,6 +220,9 @@ static void append_definition(json_stream s[static 1], stringbuilder_s sb[static
else
dbg("Encountered unknown type '%s' within definition.", json_typename[type]);

if (json_get_depth(s) == 3)
sb_append_char(sb, '\n');

type = json_next(s);
}

Expand All @@ -240,6 +242,16 @@ static void freedictentry_(dictentry de[static 1]) {
frees8(&de->definition);
}

static s8 parse_definition(json_stream *s) {
stringbuilder_s sb = sb_init(100);

append_definition(s, &sb, (s8){0}, 0);

s8 def = sb_steals8(sb);
strip_trailing_whitespace(&def);
return def;
}

static dictentry parse_dictionary_entry(json_stream *s) {
enum json_type type;
dictentry de = {0};
Expand Down Expand Up @@ -275,10 +287,7 @@ static dictentry parse_dictionary_entry(json_stream *s) {
/* ----------- */

/* sixth entry */
stringbuilder_s sb = sb_init(100);
append_definition(s, &sb, (s8){0}, 0);
de.definition = sb_steals8(sb);
strip_trailing_whitespace(&(de.definition));
de.definition = parse_definition(s);
/* ----------- */

/* seventh entry */
Expand Down Expand Up @@ -319,6 +328,8 @@ static void add_dictionary(database_t *db, s8 buffer, s8 dictname) {
add_dictent_to_db(db, de);

freedictentry_(&de);
} else {
dbg("Encountered unknown entry while parsing dictionary");
}
}
}
Expand Down Expand Up @@ -541,8 +552,6 @@ int main(int argc, char *argv[]) {
} else
createdir((char *)cfg.dbpath.s);

_drop_(db_close) database_t *db = db_open((char *)dbdir.s, false);
_drop_(closedir) DIR *dir = opendir(".");
_drop_(db_close) database_t *db = db_open((char *)cfg.dbpath.s, false);
_drop_(closedir) DIR *dir = opendir((char *)cfg.dictspath.s);
die_on(!dir, "Error opening current directory: %s", strerror(errno));
Expand Down

0 comments on commit 5239f72

Please sign in to comment.