summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/convert.c
blob: 0f09e83094df36841a56aeb63e04c31e7f89aa10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stc/cstr.h>

#define i_key_str
#define i_val_str
#include <stc/cmap.h>

#define i_val_str
#include <stc/cvec.h>

#define i_val_str
#define i_extern // define _clist_mergesort() once
#include <stc/clist.h>

int main()
{
    cmap_str map, mclone;
    cvec_str keys = {0}, values = {0};
    clist_str list = {0};

    c_defer(
        cmap_str_drop(&map),
        cmap_str_drop(&mclone),
        cvec_str_drop(&keys),
        cvec_str_drop(&values),
        clist_str_drop(&list)
    ){
        map = c_make(cmap_str, {
            {"green", "#00ff00"},
            {"blue", "#0000ff"},
            {"yellow", "#ffff00"},
        });

        puts("MAP:");
        c_foreach (i, cmap_str, map)
            printf("  %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second));

        puts("\nCLONE MAP:");
        mclone = cmap_str_clone(map);
        c_foreach (i, cmap_str, mclone)
            printf("  %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second));

        puts("\nCOPY MAP TO VECS:");
        c_foreach (i, cmap_str, mclone) {
            cvec_str_emplace_back(&keys, cstr_str(&i.ref->first));
            cvec_str_emplace_back(&values, cstr_str(&i.ref->second));
        }
        c_forrange (i, cvec_str_size(&keys))
            printf("  %s: %s\n", cstr_str(keys.data + i), cstr_str(values.data + i));

        puts("\nCOPY VEC TO LIST:");
        c_foreach (i, cvec_str, keys)
            clist_str_emplace_back(&list, cstr_str(i.ref));

        c_foreach (i, clist_str, list)
            printf("  %s\n", cstr_str(i.ref));
    }
}