constmap − fetch matching strings from a hashed data structure in constant time
#include "constmap.h"
int
constmap_init(struct constmap *cm,const char
*string,int len,int flagcolon);
int constmap_init_char(struct constmap
*cm,const char *string,int len,int
flagcolon,char flagchar);
int constmap_index(struct constmap *cm,const
char *string,int len);
char *constmap(struct constmap *cm,const char
*string,int len);
char *constmap_get(struct constmap *cm,int
len);
void constmap_free(struct constmap *cm);
Reading a file perhaps with control_readfile(&cmap,"path/filename",0) constmap_init can be used to convert its content into a constant time search map: constmap. Here, you can specify whether the entries in constmap are plain or key/value structured setting flagcolon to one and assuming the delimiter equals to a colon: :. If the delimiter needs to be particulary tailored, use constmap_init_char.
Given the search string and providing its length len (without the trailing \0) constmap will now retrieve the information in constant time returning a pointer to the search result or 0.
The datastructure can be freed by means of constmap_free.
#include <constmap.h>
struct constmap
cmap;
stralloc result = {0};
char *info;
char *search;
if
(control_readfile(&cmap,"control/conf",0))
constmap_init(&cmap,result.s,result.len,1));
info =
constmap(&cmap,search,str_len(search));
if (!info) return 0;
cdbread(3), cdbmake(3)