summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/game.c46
-rw-r--r--src/game.h9
-rw-r--r--src/main.c14
3 files changed, 59 insertions, 10 deletions
diff --git a/src/game.c b/src/game.c
index d136005..6989b6e 100644
--- a/src/game.c
+++ b/src/game.c
@@ -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);
+}
diff --git a/src/game.h b/src/game.h
index f49c7e8..8065105 100644
--- a/src/game.h
+++ b/src/game.h
@@ -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_ */
diff --git a/src/main.c b/src/main.c
index 268e42e..6529c88 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);
}