// tbl.h: an abstract API for tables with string keys.//// Written by Tony Finch <dot@dotat.at>// You may do anything with this. It has no warranty.// <http://creativecommons.org/publicdomain/zero/1.0/>#pragma once// A table is represented by a pointer to this incomplete struct type.// You initialize an empty table by setting the pointer to NULL.//typedef struct Tbl Tbl;#if defined(__plan9__)#pragma incomplete Tbl#endif// Get the value associated with a key.// Returns NULL if the key is not in the Table.//void *Tgetl(Tbl *tbl, const char *key, usize klen) sl_nonnull(2) sl_rd(2, 3);void *Tget(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);// Returns false if the key is not found, otherwise returns true and// sets *rkey and *rval to the table's key and value pointers.//bool Tgetkv(Tbl *tbl, const char *key, usize klen, const char **rkey, void **rval) sl_nonnull(2, 4, 5) sl_rd(2, 3) sl_wr(4) sl_wr(5);// Associate a key with a value in a table. Returns a new pointer to// the modified table. If there is an error it sets errno and returns// NULL. To delete a key, set its value to NULL. When the last key is// deleted, Tset() returns NULL without setting errno. The key and// value are borrowed not copied.//// Errors:// EINVAL - value pointer is not word-aligned// ENOMEM - allocation failed//Tbl *Tsetl(Tbl *tbl, const char *key, usize klen, void *value) sl_nonnull(2) sl_rd(2, 3);Tbl *Tset(Tbl *tbl, const char *key, void *value) sl_nonnull(2) sl_cstr(2);Tbl *Tdell(Tbl *tbl, const char *key, usize klen) sl_nonnull(2) sl_rd(2, 3);Tbl *Tdel(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);// Deletes an entry from the table as above, and sets *rkey and *rval// to the removed key and value pointers.//Tbl *Tdelkv(Tbl *tbl, const char *key, usize klen, const char **rkey, void **rval) sl_nonnull(2, 4, 5) sl_rd(2, 3) sl_wr(4) sl_wr(5);// Find the next item in the table. The p... arguments are in/out// parameters. To find the first key, pass *pkey=NULL and *pklen=0.// For subsequent keys, *pkey must be present in the table and is// updated to the lexicographically following key. Returns false or// NULL when there are no more keys.//bool Tnextl(Tbl *tbl, const char **pkey, usize *pklen, void **pvalue) sl_nonnull(2, 3, 4);bool Tnext(Tbl *tbl, const char **pkey, void **pvalue) sl_nonnull(2, 3);const char *Tnxt(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);
// tbl.h: an abstract API for tables with string keys.
//
// Written by Tony Finch <dot@dotat.at>
// You may do anything with this. It has no warranty.
// <http://creativecommons.org/publicdomain/zero/1.0/>
#pragma once
// A table is represented by a pointer to this incomplete struct type.
// You initialize an empty table by setting the pointer to NULL.
//
typedef struct Tbl Tbl;
#if defined(__plan9__)
#pragma incomplete Tbl
#endif
// Get the value associated with a key.
// Returns NULL if the key is not in the Table.
//
void *Tgetl(Tbl *tbl, const char *key, usize klen) sl_nonnull(2) sl_rd(2, 3);
void *Tget(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);
// Returns false if the key is not found, otherwise returns true and
// sets *rkey and *rval to the table's key and value pointers.
//
bool Tgetkv(Tbl *tbl, const char *key, usize klen, const char **rkey, void **rval) sl_nonnull(2, 4, 5) sl_rd(2, 3) sl_wr(4) sl_wr(5);
// Associate a key with a value in a table. Returns a new pointer to
// the modified table. If there is an error it sets errno and returns
// NULL. To delete a key, set its value to NULL. When the last key is
// deleted, Tset() returns NULL without setting errno. The key and
// value are borrowed not copied.
//
// Errors:
// EINVAL - value pointer is not word-aligned
// ENOMEM - allocation failed
//
Tbl *Tsetl(Tbl *tbl, const char *key, usize klen, void *value) sl_nonnull(2) sl_rd(2, 3);
Tbl *Tset(Tbl *tbl, const char *key, void *value) sl_nonnull(2) sl_cstr(2);
Tbl *Tdell(Tbl *tbl, const char *key, usize klen) sl_nonnull(2) sl_rd(2, 3);
Tbl *Tdel(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);
// Deletes an entry from the table as above, and sets *rkey and *rval
// to the removed key and value pointers.
//
Tbl *Tdelkv(Tbl *tbl, const char *key, usize klen, const char **rkey, void **rval) sl_nonnull(2, 4, 5) sl_rd(2, 3) sl_wr(4) sl_wr(5);
// Find the next item in the table. The p... arguments are in/out
// parameters. To find the first key, pass *pkey=NULL and *pklen=0.
// For subsequent keys, *pkey must be present in the table and is
// updated to the lexicographically following key. Returns false or
// NULL when there are no more keys.
//
bool Tnextl(Tbl *tbl, const char **pkey, usize *pklen, void **pvalue) sl_nonnull(2, 3, 4);
bool Tnext(Tbl *tbl, const char **pkey, void **pvalue) sl_nonnull(2, 3);
const char *Tnxt(Tbl *tbl, const char *key) sl_nonnull(2) sl_cstr(2);