summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-10-16 00:01:40 +0200
committerTyge Løvset <[email protected]>2021-10-16 00:01:40 +0200
commit36d2e0960ad33ca6d194ca41a17f0834526173d0 (patch)
treeba051c18d9fca105c54d90b14ce276d9f64ff21e /examples
parent959eab1e1f590ba4e5b521f106ae48ff2e493421 (diff)
downloadSTC-modified-36d2e0960ad33ca6d194ca41a17f0834526173d0.tar.gz
STC-modified-36d2e0960ad33ca6d194ca41a17f0834526173d0.zip
Changed c_hash_default() function. Was bad on long strings. Updated mapmap.c example.
Diffstat (limited to 'examples')
-rw-r--r--examples/mapmap.c61
1 files changed, 34 insertions, 27 deletions
diff --git a/examples/mapmap.c b/examples/mapmap.c
index 44cf7da5..184a7a7d 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -1,39 +1,46 @@
-
-#include <stdio.h>
-#include <stc/cstr.h>
-
// unordered_map<string, unordered_map<string, string>>:
#define i_key_str
#define i_val_str
-#include <stc/cmap.h>
+#define i_tag sect
+#include <stc/csmap.h>
#define i_key_str
-#define i_val cmap_str
-#define i_valdel cmap_str_del
-#define i_tag cfg
-#include <stc/cmap.h>
+#define i_val csmap_sect
+#define i_valdel csmap_sect_del
+#define i_tag conf
+#include <stc/csmap.h>
-int main(void)
+void add(csmap_conf* map, const char* section, const char* entry, const char* value)
{
- c_auto (cmap_cfg, cfg)
- {
- cmap_cfg_insert(&cfg, cstr_from("user"), cmap_str_init());
- cmap_cfg_insert(&cfg, cstr_from("group"), cmap_str_init());
- cmap_cfg_insert(&cfg, cstr_from("admin"), cmap_str_init());
-
- cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "name", "Joe");
- cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "groups", "proj1,proj3");
- cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj1", "Energy");
- cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj2", "Windy");
- cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj3", "Oil");
- cmap_str_emplace(cmap_cfg_at(&cfg, "admin"), "employees", "2302");
+ csmap_sect *smap = &csmap_conf_insert(map, cstr_from(section), csmap_sect_init()).ref->second;
+ csmap_sect_emplace_or_assign(smap, entry, value);
+}
- cmap_str_emplace_or_assign(cmap_cfg_at(&cfg, "group"), "proj2", "Wind"); // Update
+bool contains(csmap_conf* map, const char* section, const char* entry)
+{
+ csmap_conf_value_t *val = csmap_conf_get(map, section);
+ return val && csmap_sect_get(&val->second, entry);
+}
- c_foreach (i, cmap_cfg, cfg)
- c_foreach (j, cmap_str, i.ref->second)
- printf("%s: %s - %s (%u)\n", i.ref->first.str, j.ref->first.str, j.ref->second.str,
- i.ref->second.bucket_count);
+int main(void)
+{
+ c_auto (csmap_conf, map)
+ {
+ add(&map, "user", "name", "Joe");
+ add(&map, "user", "groups", "proj1,proj3");
+ add(&map, "group", "proj1", "Energy");
+ add(&map, "group", "proj2", "Windy");
+ add(&map, "group", "proj3", "Oil");
+ add(&map, "admin", "employees", "2302");
+ add(&map, "group", "proj2", "Wind"); // Update
+
+ printf("contains: %d\n", contains(&map, "group", "employees"));
+ printf("contains: %d\n", contains(&map, "admin", "name"));
+ printf("contains: %d\n", contains(&map, "admin", "employees"));
+
+ c_foreach (i, csmap_conf, map)
+ c_foreach (j, csmap_sect, i.ref->second)
+ printf("%s: %s - %s\n", i.ref->first.str, j.ref->first.str, j.ref->second.str);
}
} \ No newline at end of file