diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 17:30:21 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 17:31:47 (EDT) |
commit | 4945cedb57edd76f4a53d57437d486daa7c26662 (patch) | |
tree | 08abda1f8193237230445ad0e94921cf9013d426 | |
parent | 55ad889515de695f3cdc19e0ccbd76a3acc9fe73 (diff) | |
download | dodge-balls-4945cedb57edd76f4a53d57437d486daa7c26662.zip dodge-balls-4945cedb57edd76f4a53d57437d486daa7c26662.tar.gz dodge-balls-4945cedb57edd76f4a53d57437d486daa7c26662.tar.bz2 |
db_games_find(): Parse game name and description
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/game.c | 54 | ||||
-rw-r--r-- | src/main.c | 3 |
3 files changed, 54 insertions, 7 deletions
diff --git a/configure.ac b/configure.ac index ecc933a..d98283d 100644 --- a/configure.ac +++ b/configure.ac @@ -36,8 +36,8 @@ test -d "${srcdir}/.git" || CFLAGS="${save_CFLAGS}" funcs_missing=false AC_CHECK_FUNCS( [\ - alphasort calloc dirname free malloc printf putchar realpath \ - scandir sscanf strcmp strdup vprintf + alphasort calloc dirname free isspace malloc printf putchar \ + realpath scandir sscanf strcmp strdup strndup vprintf ], [], [funcs_missing=true]) @@ -30,6 +30,10 @@ struct db_game { char *id; char *name; char *desc; + enum { + DB_GAME_XML_PARSING_NAME, + DB_GAME_XML_PARSING_DESCRIPTION, + } xml_parsing; }; static int @@ -65,6 +69,46 @@ _db_game_xml_invalid_start(void *pv, const char *name, } static void XMLCALL +_db_game_xml_cdata(void *pv, const char *s, int len) +{ + XML_Parser p; + struct db_game *game; + char **data; + char *s_z; + char *s_z_trimmed; + char *s_z_trimmed_end; + + db_dbg( "[CDATA]"); + + p = (XML_Parser) pv; + game = db_xml_node_peek(p); + if (game->xml_parsing == DB_GAME_XML_PARSING_NAME) { + data = &game->name; + } else { + data = &game->desc; + } + + s_z = s_z_trimmed = strndup(s, len); + + while(isspace(*s_z_trimmed)) { + ++s_z_trimmed; + --len; + } + if (*s_z_trimmed == '\0') { + return; + } + s_z_trimmed_end = s_z_trimmed + len - 1; + while (s_z_trimmed_end > s_z_trimmed && isspace(*s_z_trimmed_end)) { + --s_z_trimmed_end; + } + *(s_z_trimmed_end + 1) = '\0'; + + *data = strdup(s_z_trimmed); + + free(s_z); +} + +static void XMLCALL _db_game_xml_name_end(void *pv, const char *name) { XML_Parser p; @@ -74,7 +118,6 @@ _db_game_xml_name_end(void *pv, const char *name) p = (XML_Parser) pv; if (db_xml_check_tag(name, "name")) { - /* TODO */ db_xml_node_pop(p); } else { db_xml_unexpected_end_tag(p, name, "name"); @@ -91,7 +134,6 @@ _db_game_xml_description_end(void *pv, const char *name) p = (XML_Parser) pv; if (db_xml_check_tag(name, "description")) { - /* TODO */ db_xml_node_pop(p); } else { db_xml_unexpected_end_tag(p, name, "description"); @@ -177,10 +219,12 @@ _db_game_xml_game_el_start(void *pv, const char *name, const char **attr) return; } db_dbg(" lang=\"%s\"", lang); + game->xml_parsing = DB_GAME_XML_PARSING_NAME; /* TODO */ free(lang); db_xml_node_push(p, game, _db_game_xml_invalid_start, - _db_game_xml_name_end, NULL /* TODO */); + _db_game_xml_name_end, + _db_game_xml_cdata); } else if (db_xml_check_tag(name, "description")) { lang = NULL; db_xml_get_string_attr(p, attr, "lang", &lang, 1); @@ -188,10 +232,12 @@ _db_game_xml_game_el_start(void *pv, const char *name, const char **attr) return; } db_dbg(" lang=\"%s\"", lang); + game->xml_parsing = DB_GAME_XML_PARSING_DESCRIPTION; /* TODO */ free(lang); db_xml_node_push(p, game, _db_game_xml_invalid_start, - _db_game_xml_description_end, NULL /* TODO */); + _db_game_xml_description_end, + _db_game_xml_cdata); } else if (db_xml_check_tag(name, "levels")) { db_xml_node_push(p, game, _db_game_xml_levels_el_start, _db_game_xml_levels_end, NULL); @@ -26,7 +26,8 @@ main(int argc, char *argv[]) if (n > 0) { printf("%d games:\n", n); for (i = 0; i < n; ++i) { - printf("\t%s\n", db_game_get_name(games[i])); + printf("\t%s - %s\n", db_game_get_name(games[i]), + db_game_get_desc(games[i])); db_game_free(games[i]); } free(games); |