Skip to content
modkitv0.2

storage.h

#include <modkit/storage.h>18 functions · 1 typedefs

Per-user persistent storage + a simple key/value config.

Writes to the correct per-user, per-app directory (SDL_GetPrefPath under the hood) the right home for savegames and settings, which the asset search paths (read-only game data) are not. Plus a small JSON-backed config store for "remember the player's options" without hand-rolling a parser.

Functions

mk_config_get_boolfunction

bool mk_config_get_bool(const char *key, bool def)

Get bool from the config.

ParameterTypeDescription
keyconst char *Value for key.
defboolValue for def.

Returns True when the operation succeeds.

mk_config_get_floatfunction

float mk_config_get_float(const char *key, float def)

Get float from the config.

ParameterTypeDescription
keyconst char *Value for key.
deffloatValue for def.

Returns The resulting value.

mk_config_get_intfunction

int mk_config_get_int(const char *key, int def)

Get int from the config.

ParameterTypeDescription
keyconst char *Value for key.
defintValue for def.

Returns The resulting value.

mk_config_get_strfunction

const char * mk_config_get_str(const char *key, const char *def)

Get str from the config.

ParameterTypeDescription
keyconst char *Value for key.
defconst char *Value for def.

Returns A borrowed pointer, or NULL when unavailable.

mk_config_loadfunction

bool mk_config_load(const char *name)

Load (or start) a config backed by name in the storage directory.

A missing file starts an empty config; mk_config_save() will create it. The name is remembered for mk_config_save().

ParameterTypeDescription
nameconst char *Stable name.

Returns True when the operation succeeds.

mk_config_savefunction

bool mk_config_save(void)

Persist the in-memory config to the file from the last mk_config_load().

Returns True when the operation succeeds.

mk_config_set_boolfunction

void mk_config_set_bool(const char *key, bool value)

Set bool on the config.

ParameterTypeDescription
keyconst char *Value for key.
valueboolValue to use.

mk_config_set_floatfunction

void mk_config_set_float(const char *key, float value)

Set float on the config.

ParameterTypeDescription
keyconst char *Value for key.
valuefloatValue to use.

mk_config_set_intfunction

void mk_config_set_int(const char *key, int value)

Set int on the config.

ParameterTypeDescription
keyconst char *Value for key.
valueintValue to use.

mk_config_set_strfunction

void mk_config_set_str(const char *key, const char *value)

Set str on the config.

ParameterTypeDescription
keyconst char *Value for key.
valueconst char *Value to use.

mk_storage_deletefunction

bool mk_storage_delete(const char *name)

Delete a named blob.

ParameterTypeDescription
nameconst char *Stable name.

Returns True when the operation succeeds.

mk_storage_dirfunction

const char * mk_storage_dir(void)

Absolute writable directory (with trailing separator), or NULL if uninit.

Returns A borrowed pointer, or NULL when unavailable.

mk_storage_existsfunction

bool mk_storage_exists(const char *name)

True if a named blob exists in the storage directory.

ParameterTypeDescription
nameconst char *Stable name.

Returns True when the operation succeeds.

mk_storage_flushfunction

bool mk_storage_flush(mk_storage_flush_fn callback, void *user_data)

Persist pending writes, then invoke callback (may be NULL).

Native platforms complete before returning and call back synchronously. Web syncs the IDBFS mount to IndexedDB and calls back on a later browser task, so treat the callback as asynchronous everywhere. Returns false if storage is uninitialized or another flush is still in flight.

ParameterTypeDescription
callbackmk_storage_flush_fnCompletion callback.
user_datavoid *Caller-provided context.

Returns True when the operation succeeds.

mk_storage_initfunction

bool mk_storage_init(const char *org, const char *app)

Resolve (and create) the per-user writable directory for org/app and make it the target for subsequent storage and config calls.

ParameterTypeDescription
orgconst char *Value for org.
appconst char *Value for app.

Returns true on success.

mk_storage_is_durablefunction

bool mk_storage_is_durable(void)

True when a completed write is already durable without calling mk_storage_flush().

Native platforms write straight to the filesystem and return true. On web the storage directory is an IDBFS mount whose contents only reach IndexedDB when they are synced, so this returns false and writes survive a reload only after a successful flush.

Returns True when the condition holds.

mk_storage_readfunction

void * mk_storage_read(const char *name, size_t *out_size)

Read a named blob.

Returns a malloc'd buffer the caller must free(); writes the byte count to out_size (may be NULL). Returns NULL if missing.

ParameterTypeDescription
nameconst char *Stable name.
out_sizesize_t *Receives the size.

Returns A borrowed pointer, or NULL when unavailable.

mk_storage_writefunction

bool mk_storage_write(const char *name, const void *data, size_t size)

Write a named blob into the storage directory (overwrites).

name must be a plain filename (no path separators).

ParameterTypeDescription
nameconst char *Stable name.
dataconst void *Data buffer.
sizesize_tSize in bytes.

Returns True when the operation succeeds.

Typedefs

mk_storage_flush_fntypedef

typedef void(*) mk_storage_flush_fn(bool success, void *user_data)

Called when mk_storage_flush() finishes.

success is false if the platform could not persist the pending writes.