diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 10:13:58 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 10:13:58 (EDT) |
commit | 71e445c1ffd01b371c9b3060a57c866dc85e3e00 (patch) | |
tree | acb798330759368e861b35fd114216a0cd9718b0 | |
parent | 60abe24450a8d02b0a0f85c40190f26472fb5ab8 (diff) | |
download | dodge-balls-71e445c1ffd01b371c9b3060a57c866dc85e3e00.zip dodge-balls-71e445c1ffd01b371c9b3060a57c866dc85e3e00.tar.gz dodge-balls-71e445c1ffd01b371c9b3060a57c866dc85e3e00.tar.bz2 |
db_games_find(): Initialize game structures
-rw-r--r-- | src/game.c | 46 | ||||
-rw-r--r-- | src/game.h | 9 | ||||
-rw-r--r-- | src/main.c | 14 |
3 files changed, 59 insertions, 10 deletions
@@ -23,6 +23,12 @@ #include "game.h" #include "output.h" +struct db_game { + char *id; + char *name; + char *desc; +}; + static int _db_game_is_dir(const struct dirent *entry) { @@ -30,7 +36,7 @@ _db_game_is_dir(const struct dirent *entry) } int -db_games_find(const char *games_dir, char ***games) +db_games_find(const char *games_dir, struct db_game ***games) { struct dirent **entries; int n; @@ -51,7 +57,16 @@ db_games_find(const char *games_dir, char ***games) } for (i = 0; i < n; ++i) { - (*games)[i] = strdup(entries[i]->d_name); + (*games)[i] = calloc(1, sizeof(***games)); + if ((*games)[i] == NULL) { + db_err("Failed to allocate memory"); + while (--i >= 0) { + db_game_free((*games)[i]); + } + free(*games); + return -1; + } + (*games)[i]->id = strdup(entries[i]->d_name); free(entries[i]); } @@ -59,3 +74,30 @@ db_games_find(const char *games_dir, char ***games) return n; } + +const char * +db_game_get_name(struct db_game *game) +{ + return game->name; +} + +const char * +db_game_get_desc(struct db_game *game) +{ + return game->desc; +} + +void +db_game_free(struct db_game *game) +{ + if (game->id) { + free(game->id); + } + if (game->name) { + free(game->name); + } + if (game->desc) { + free(game->desc); + } + free(game); +} @@ -20,6 +20,13 @@ #ifndef DB_GAME_H_ #define DB_GAME_H_ -int db_games_find(const char *games_dir, char ***games); +struct db_game; + +int db_games_find(const char *games_dir, struct db_game ***games); +const char *db_game_get_name(struct db_game *game) + __attribute__((__pure__)); +const char *db_game_get_desc(struct db_game *game) + __attribute__((__pure__)); +void db_game_free(struct db_game *game); #endif /* DB_GAME_H_ */ @@ -7,11 +7,11 @@ int main(int argc, char *argv[]) { - char *program_dir; - const char *games_dir; - char **games; - int n; - int i; + char *program_dir; + const char *games_dir; + struct db_game **games; + int n; + int i; program_dir = realpath(dirname(argv[0]), NULL); if (strcmp(program_dir, ABS_BUILDDIR) == 0) { @@ -26,8 +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", games[i]); - free(games[i]); + printf("\t%s\n", db_game_get_name(games[i])); + db_game_free(games[i]); } free(games); } |