Welcome to little lamb

Code » limb » commit ccc5fd5

doc: Complete cdb.h

author Olivier Brunel
2023-06-29 12:47:18 UTC
committer Olivier Brunel
2023-07-24 10:16:41 UTC
parent bb68f8a2d652b9634a5a586cf5a5b623ecf14acf

doc: Complete cdb.h

src/doc/cdb.h.0.md +87 -4
src/doc/cdb.h/cdb_find.3.md +49 -0
src/doc/cdb.h/cdb_find_next.3.md +1 -0
src/doc/cdb.h/cdb_find_start.3.md +1 -0
src/doc/cdb.h/cdb_free.3.md +26 -0
src/doc/cdb.h/cdb_init.3.md +54 -0
src/doc/cdb.h/cdb_init_fromfd.3.md +1 -0
src/doc/cdb.h/cdb_init_frommem.3.md +5 -1
src/doc/cdb.h/cdb_initat.3.md +1 -0
src/doc/cdb.h/cdb_traverse_init.3.md +40 -0
src/doc/cdb.h/cdb_traverse_next.3.md +1 -0
src/liblimb/include/limb/cdb.h +7 -0

diff --git a/src/doc/cdb.h.0.md b/src/doc/cdb.h.0.md
index b6e7274..efa00bf 100644
--- a/src/doc/cdb.h.0.md
+++ b/src/doc/cdb.h.0.md
@@ -16,13 +16,96 @@ cdb.h - accessing constant databases
 The header defines the needed functions to access constant databases, known as
 .cdb files.
 
-! INFO: skalibs
-! This header is a complement to skalibs' own [skalibs/cdb.h](0) and thusly
-! includes said header.
+<inc skalibs.md>
+
+## Structures
+
+The following structures are defined :
+
+: *struct cdb_data_s*
+:: A structure to hold information about an element in a cdb, defined as such :
+
+    struct cdb_data_s {
+        const char *s;
+        u32 len;
+    }
+
+:: Member `s` points to the data, of length `len` bytes. (cdb database must be
+:: under 4 GiB, therefore any length can fit within a 32bit value.)
+
+## Types
+
+The following types are defined :
+
+: *cdb*
+:: An opaque structure representing a cdb.
+
+: *cdb_data*
+:: A *struct cdb_data_s*, usually given to a cdb-reading function for it to be
+:: filled with information about the wanted element. Pointers set in such a
+:: *cdb_data* are read-only (hence the `const`) and remain valid until the *cdb*
+:: is closed.
+
+: *cdb_find_state*
+:: An opaque structure used when searching inside a cdb, notably to find all
+:: elements stored with the same key.
+
+: *cdb_traverse*
+:: An opaque structure used when iterating over a cdb.
+
+## Macros
+
+The following macros are defined :
+
+: *CDB_ZERO*
+:: Value to initialize a stack-allocated *cdb*.
+
+: *CDB_FIND_STATE_ZERO*
+:: Value to initialize a stack-allocated *cdb_find_state*.
+
+: *CDB_TRAVERSE_ZERO*
+:: Value to initialize a stack-allocated *cdb_traverse*.
+
+## Objects
+
+The following objects are defined :
+
+: *cdb_zero*
+:: An empty *cdb*, which can be used for (re-)initialization.
 
 ## Functions
 
 The following functions are defined :
 
+: [cdb_find_start](3)
+:: Initialize a *cdb_find_state*.
+
+: [cdb_find_next](3)
+:: Similar to [cdb_find](3) except that it takes a *cdb_find_state* argument,
+:: allowing to find all elements with the same key.
+
+: [cdb_find](3)
+:: Find the first element for the given key in a *cdb*.
+
+: [cdb_free](3)
+:: Free memory associated with a *cdb* from a file (descriptor).
+
+: [cdb_init](3)
+:: Initialize a cdb from a file.
+
+: [cdb_initat](3)
+:: Similar to [cdb_init](3) except for relative path, relative to the given file
+:: descriptor.
+
+: [cdb_init_fromfd](3)
+:: Similar to [cdb_init](3) but taking a file descriptor instead of a file name.
+
 : [cdb_init_frommem](3)
-:: To initializes a CDB from a memory area, which must contain a valid CDB file.
+:: Similar to [cdb_init](3) but taking a pointer to a memory area, which must
+:: contain a valid CDB file.
+
+: [cdb_traverse_init](3)
+:: Initialize a *cdb_traverse*.
+
+: [cdb_traverse_next](3)
+:: Find the next element when iterating over a *cdb*.
diff --git a/src/doc/cdb.h/cdb_find.3.md b/src/doc/cdb.h/cdb_find.3.md
new file mode 100644
index 0000000..6125971
--- /dev/null
+++ b/src/doc/cdb.h/cdb_find.3.md
@@ -0,0 +1,49 @@
+% limb manual
+% cdb_find(3)
+
+# NAME
+
+cdb_find, cdb_find_start, cdb_find_next - finding element in a cdb
+
+# SYNOPSIS
+
+    #include <limb/cdb.h>
+
+```pre hl
+int cdb_find(const cdb *<em>c</em>, cdb_data *<em>data</em>, const char *<em>key</em>, u32 <em>klen</em>);
+
+void cdb_find_start(cdb_find_state *<em>find</em>)
+int cdb_find_next(const cdb *<em>c</em>, cdb_data *<em>data</em>, const char *<em>key</em>, u32 <em>klen</em>, cdb_find_state *<em>find</em>)
+```
+
+# DESCRIPTION
+
+The `cdb_find`() function looks up the key pointed by `key` of length `klen` in
+the *cdb* pointed by `c`. If found, it will set the members `s` and `len` of the
+*cdb_data* pointed by `data` to represent the data associated with the first
+element found by that key.
+
+Only the first element with that key can be found using this function.
+
+The `cdb_find_start`() macro initialize the state pointed by `find` so that the
+next invocation of `cdb_find_next`() using that state will find the first
+element associated with the given key.
+
+The `cdb_find_next`() function is similar to `cdb_find`() when the state pointed
+by `find` has been initialized by `cdb_find_start`().
+However, said state will be updated so that the next call to `cdb_find_next`()
+using the same arguments for `key`, `klen` and `state` return the next element
+associated with the key, and so on for all elements for said key.
+
+Note that it is invalid and undefined what happens if calling `cdb_find_next`()
+with an uninitialized state, or one initialized for a different key.
+
+# RETURN VALUE
+
+The `cdb_find`() and `cdb_find_next`() functions return -1 when the *cdb* pointed
+by `c` is invalid; They return 1 on success, i.e. an element was found and
+information about its associated data was set inside the *cdb_data* pointed by
+`data`.
+
+Otherwise, i.e. no element with that key was found, or - in the case of
+`cdb_find_next`() - there are no more elements with that key, they return 0.
diff --git a/src/doc/cdb.h/cdb_find_next.3.md b/src/doc/cdb.h/cdb_find_next.3.md
new file mode 120000
index 0000000..f468aa1
--- /dev/null
+++ b/src/doc/cdb.h/cdb_find_next.3.md
@@ -0,0 +1 @@
+cdb_find.3.md
\ No newline at end of file
diff --git a/src/doc/cdb.h/cdb_find_start.3.md b/src/doc/cdb.h/cdb_find_start.3.md
new file mode 120000
index 0000000..f468aa1
--- /dev/null
+++ b/src/doc/cdb.h/cdb_find_start.3.md
@@ -0,0 +1 @@
+cdb_find.3.md
\ No newline at end of file
diff --git a/src/doc/cdb.h/cdb_free.3.md b/src/doc/cdb.h/cdb_free.3.md
new file mode 100644
index 0000000..9a4f6ca
--- /dev/null
+++ b/src/doc/cdb.h/cdb_free.3.md
@@ -0,0 +1,26 @@
+% limb manual
+% cdb_free(3)
+
+# NAME
+
+cdb_free - free memory associated with a cdb
+
+# SYNOPSIS
+
+    #include <limb/cdb.h>
+
+```pre hl
+void cdb_free(cdb *<em>c</em>)
+```
+
+# DESCRIPTION
+
+The `cdb_free`() function frees all memory associated with the cdb pointed by
+`c`. Notably, this renders invalid any and all pointers returned via a
+*cdb_data* in relation to the cdb pointed by `c`.
+If you intend to use such data /after/ having closing the database (i.e. after a
+call to `cdb_free`()) then you need to copy it before.
+
+! WARN: Important
+! Any *cdb* that was /not/ initialized from a file, i.e. via
+! [cdb_init_frommem](3), must *not* be freed via `cdb_free`().
diff --git a/src/doc/cdb.h/cdb_init.3.md b/src/doc/cdb.h/cdb_init.3.md
new file mode 100644
index 0000000..ce3b623
--- /dev/null
+++ b/src/doc/cdb.h/cdb_init.3.md
@@ -0,0 +1,54 @@
+% limb manual
+% cdb_init(3)
+
+# NAME
+
+cdb_init, cdb_initat, cdb_init_fromfd - initialize a cdb
+
+# SYNOPSIS
+
+    #include <limb/cdb.h>
+
+```pre hl
+int cdb_init(cdb *<em>c</em>, const char *<em>file</em>)
+int cdb_initat(cdb *<em>c</em>, int <em>dfd</em>, const char *<em>file</em>)
+int cdb_init_fromfd(cdb *<em>c</em>, int <em>fd</em>)
+```
+
+# DESCRIPTION
+
+The `cdb_init`() function initializes the cdb pointed to by `c`, which
+must be `CDB_ZERO` before the call, by mapping the file named `file`, which must
+be a valid cdb.
+
+The `cdb_initat`() function is similar to `cdb_init`() except when `file` refer
+to a relative path, in which case it is relative to the directory described by
+the file descriptor `dfd`.
+
+The `cdb_init_fromfd`() function is similar to `cdb_init`() except that the file
+must already be opened and readable via file descriptor `fd`.
+
+# RETURN VALUE
+
+These functions return 1 on success. Otherwise, they return 0 and set `errno` to
+indicate the error.
+
+# ERRORS
+
+These functions may fail if :
+
+: *EOVERFLOW*
+:: The file is too large. A cdb database must be under 4 GiB.
+
+They may also fail and set `errno` for the errors described for [fstat](3) and
+[mmap](3).
+
+The `cdb_init`() function may also fail and set `errno` for the errors described
+for [openc_read](3).
+
+The `cdb_initat`() function may also fail and set `errno` for the errors
+described for [openc_readat](3).
+
+# SEE ALSO
+
+[cdb_init_frommem](3)
diff --git a/src/doc/cdb.h/cdb_init_fromfd.3.md b/src/doc/cdb.h/cdb_init_fromfd.3.md
new file mode 120000
index 0000000..7efc61a
--- /dev/null
+++ b/src/doc/cdb.h/cdb_init_fromfd.3.md
@@ -0,0 +1 @@
+cdb_init.3.md
\ No newline at end of file
diff --git a/src/doc/cdb.h/cdb_init_frommem.3.md b/src/doc/cdb.h/cdb_init_frommem.3.md
index 6000e7c..d66327a 100644
--- a/src/doc/cdb.h/cdb_init_frommem.3.md
+++ b/src/doc/cdb.h/cdb_init_frommem.3.md
@@ -3,7 +3,7 @@
 
 # NAME
 
-cdb\_init\_frommem - initialize a cdb from memory area
+cdb_init_frommem - initialize a cdb from memory area
 
 # SYNOPSIS
 
@@ -25,3 +25,7 @@ must hold the content of a valid cdb file, for `len` bytes.
 ! [cdb_free](3) with a cdb initialized from `cdb_init_frommem`().
 ! Instead, it is up to the caller to free the memory pointed to by `mem`
 ! however it needs to, once the cdb isn't needed anymore.
+
+# SEE ALSO
+
+[cdb_init](3)
diff --git a/src/doc/cdb.h/cdb_initat.3.md b/src/doc/cdb.h/cdb_initat.3.md
new file mode 120000
index 0000000..7efc61a
--- /dev/null
+++ b/src/doc/cdb.h/cdb_initat.3.md
@@ -0,0 +1 @@
+cdb_init.3.md
\ No newline at end of file
diff --git a/src/doc/cdb.h/cdb_traverse_init.3.md b/src/doc/cdb.h/cdb_traverse_init.3.md
new file mode 100644
index 0000000..2c66976
--- /dev/null
+++ b/src/doc/cdb.h/cdb_traverse_init.3.md
@@ -0,0 +1,40 @@
+% limb manual
+% cdb_traverse_init(3)
+
+# NAME
+
+cdb_traverse_init, cdb_traverse_next - iterate over a cdb
+
+# SYNOPSIS
+
+    #include <limb/cdb.h>
+
+```pre hl
+void cdb_traverse_init(cdb_traverse *<em>trav</em>)
+int cdb_traverse_next(const cdb *<em>c</em>, cdb_data *<em>key</em>, cdb_data *<em>data</em>, cdb_traverse *<em>trav</em>)
+```
+
+# DESCRIPTION
+
+The `cdb_traverse_init`() macro initialize the state pointed by `trav` so that
+the next invocation of `cdb_traverse_next`() using that state will find the
+first element in the database.
+
+The `cdb_traverse_next`() function find the next element in the *cdb* pointed by
+`c`, as per the state pointed by `trav`.
+When found, information about the key is placed into the *cdb_data* pointed by
+`key` while information about the data associated with the element is placed
+into the *cdb_data* pointed by `data`.
+The state pointed by `trav` is also updated so that the next call using the same
+arguments for `c` and `trav` return the next element in the database, if any,
+until all elements have been found.
+
+# RETURN VALUE
+
+The `cdb_traverse_next`() function returns -1 when either the *cdb* pointed by
+`c` or the state pointed by `trav` is invalid; It returns 1 on success, i.e. an
+element was found and information about its key and associated data were set
+inside the *cdb_data* pointed by `key` and `data`, respectively.
+
+Otherwise, i.e. no more element were found (i.e. the entire database has been
+iterated over), it returns 0.
diff --git a/src/doc/cdb.h/cdb_traverse_next.3.md b/src/doc/cdb.h/cdb_traverse_next.3.md
new file mode 120000
index 0000000..93a6b67
--- /dev/null
+++ b/src/doc/cdb.h/cdb_traverse_next.3.md
@@ -0,0 +1 @@
+cdb_traverse_init.3.md
\ No newline at end of file
diff --git a/src/liblimb/include/limb/cdb.h b/src/liblimb/include/limb/cdb.h
index 5ced9b6..6f1f5e2 100644
--- a/src/liblimb/include/limb/cdb.h
+++ b/src/liblimb/include/limb/cdb.h
@@ -7,6 +7,13 @@
 #include <skalibs/cdb.h>
 #include <limb/int.h>
 
+typedef u32 cdb_traverse;
+#define CDB_TRAVERSE_ZERO   2048
+
+#define cdb_initat      cdb_init_at
+#define cdb_find_start  cdb_findstart
+#define cdb_find_next   cdb_findnext
+
 extern void cdb_init_frommem(cdb *c, const char *mem, u32 len);
 
 #endif /* LIMB_CDB_H */