Mercurial > molko
diff libcore/core/save.h @ 172:6250883b81f0
core: improve save module, closes #2507
- Databases can now be opened in read-only or in read-write modes,
- Module is fully reentrant,
- SQL commands are in separate files,
- Improve tests,
- API to get/set property is using a dedicated structure.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 21 Oct 2020 12:43:25 +0200 |
parents | 789b23e01f52 |
children | dd7c8d4321a3 |
line wrap: on
line diff
--- a/libcore/core/save.h Wed Oct 21 11:47:18 2020 +0200 +++ b/libcore/core/save.h Wed Oct 21 12:43:25 2020 +0200 @@ -22,14 +22,60 @@ /** * \file save.h * \brief Save functions. + * + * This module provides several functions to save the game data into a database. + * + * Database can be opened in read only mode (\ref SAVE_MODE_READ) which will + * return an error if not present or write mode (\ref SAVE_MODE_WRITE) which + * will create and initialize a database if not present on disk. */ #include <stdbool.h> +#include <time.h> + +#include "plat.h" + +/** + * \brief Max property key. + */ +#define SAVE_PROPERTY_KEY_MAX (64) /** * \brief Max property value. */ -#define SAVE_PROPERTY_VALUE_MAX 1024 +#define SAVE_PROPERTY_VALUE_MAX (1024) + +/** + * \brief Save database handle. + */ +struct save { + time_t created; /*!< (-) Date when the save was created. */ + time_t updated; /*!< (-) Date when it was last updated. */ + void *handle; /*!< (*) Private handle. */ +}; + +/** + * \brief Open mode. + */ +enum save_mode { + SAVE_MODE_READ, /*!< Only try to read (no creation). */ + SAVE_MODE_WRITE /*!< Open for both reading and writing */ +}; + +/** + * \brief Mapping for the property table. + */ +struct save_property { + /** + * (+) Property key to set. + */ + char key[SAVE_PROPERTY_KEY_MAX + 1]; + + /** + * (+) Property value to set. + */ + char value[SAVE_PROPERTY_VALUE_MAX + 1]; +}; /** * Open a database by index. @@ -37,59 +83,74 @@ * This function use the preferred path to store local files under the user * home directory. The parameter idx specifies the save slot to use. * + * \pre db != NULL + * \param db the database to initialize * \param idx the save slot - * \return false on error + * \param mode the mode + * \return False on error. */ bool -save_open(unsigned int idx); +save_open(struct save *db, unsigned int idx, enum save_mode mode) PLAT_NODISCARD; /** * Open the save slot specified by path. * + * \pre db != NULL * \pre path != NULL + * \param db the database to initialize * \param path the path to the save slot - * \return false on error + * \param mode the mode + * \return False on error. */ bool -save_open_path(const char *path); +save_open_path(struct save *db, const char *path, enum save_mode mode) PLAT_NODISCARD; /** * Sets an arbitrary property. * * If the property already exists, replace it. * - * \pre key != NULL - * \pre value != NULL && strlen(value) <= SAVE_PROPERTY_VALUE_MAX - * \param key the property key - * \param value the property value + * \pre db != NULL + * \pre prop != NULL + * \param db the database + * \param prop the property to set + * \return False on error. */ bool -save_set_property(const char *key, const char *value); +save_set_property(struct save *db, const struct save_property *prop); /** * Get a property. * - * \pre key != NULL - * \param key the property key - * \return the key or NULL if not found + * Call this function by setting prop->key to the desired key to get. + * + * \pre db != NULL + * \param db the database + * \param prop the property to retrieve + * \return False on error and prop->value is left untouched */ -const char * -save_get_property(const char *key); +bool +save_get_property(struct save *db, struct save_property *prop) PLAT_NODISCARD; /** * Remove a property. * - * \pre key != NULL - * \param key the property key + * \pre db != NULL + * \pre prop != NULL + * \param db the database + * \param prop the property to remove * \return false on error */ bool -save_remove_property(const char *key); +save_remove_property(struct save *db, const struct save_property *prop); /** * Close the save slot. + * + * \pre db != NULL + * \param db the database to close */ void -save_finish(void); +save_finish(struct save *db); #endif /* !MOLKO_SAVE_H */