Skip to content

Commit 50d3e73

Browse files
committed
reorg.
1 parent d0a25bd commit 50d3e73

File tree

15 files changed

+663
-547
lines changed

15 files changed

+663
-547
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# facil.io 0.8.x for C - now with an integrated C STL (Server Toolbox library)
1+
# facil.io 0.8.x for C - now with an integrated C STL
2+
3+
## The Web microFramework and Server Toolbox library
24

35
[![POSIX C/C++ CI](https://github.com/facil-io/cstl/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/facil-io/cstl/actions/workflows/c-cpp.yml) [![Windows C/C++ CI](https://github.com/facil-io/cstl/actions/workflows/windows.yml/badge.svg)](https://github.com/facil-io/cstl/actions/workflows/windows.yml)
46

@@ -12,7 +14,7 @@ In other words, some of the most common building blocks one would need in any C
1214

1315
Simply copy the `fio-stl.h` file to your project's folder (using a single header file). Done.
1416

15-
Or... copy the `fio-stl` folder to your project's folder (using `"fio-stl/include.h"`). Done.
17+
**Or**... copy the `fio-stl` folder to your project's folder (using `"fio-stl/include.h"`). Done.
1618

1719
Include the file as many times as required and enjoy.
1820

examples/array.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* *****************************************************************************
2+
Easily construct dynamic Array types for any type, including structs and unions.
3+
***************************************************************************** */
4+
5+
typedef struct {
6+
int i;
7+
float f;
8+
} foo_s;
9+
10+
#define FIO_ARRAY_NAME foo_ary
11+
#define FIO_ARRAY_TYPE foo_s
12+
#define FIO_ARRAY_TYPE_CMP(a, b) (a.i == b.i && a.f == b.f)
13+
#include "fio-stl/include.h" /* or "fio-stl.h" */
14+
15+
int main(void) {
16+
foo_ary_s a = FIO_ARRAY_INIT;
17+
foo_ary_push(&a, (foo_s){.i = 42});
18+
foo_ary_push(&a, (foo_s){.i = -42});
19+
FIO_ARRAY_EACH(foo_ary, &a, pos) { // pos will be a pointer to the element
20+
printf("* [%zu]: %p : %d\n",
21+
(size_t)(pos - foo_ary2ptr(&a)),
22+
(void *)pos,
23+
pos->i);
24+
}
25+
foo_ary_destroy(&a);
26+
}

examples/bstr.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* *****************************************************************************
2+
3+
Using binary strings with `fio_bstr`
4+
5+
The fecil.io C STL provides a builtin solution similar in approach to the
6+
Simple Dynamic Strings library (https://github.com/antirez/sds)...
7+
... and with similar cons / pros:
8+
9+
***************************************************************************** */
10+
#define FIO_LEAK_COUNTER 1
11+
12+
/* include Core String functionality */
13+
#define FIO_STR
14+
#include "fio-stl/include.h" /* or "fio-stl.h" */
15+
16+
int main(void) {
17+
/* note that the `bstr` pointer might be updated!
18+
* not updating the pointer after a `write` is a bug. */
19+
char *org = fio_bstr_write(NULL, "Hello World", 11);
20+
char *copy = fio_bstr_copy(org);
21+
printf("Since we use copy on write: %p == %p\n", (void *)copy, (void *)org);
22+
/* we could also use fio_bstr_printf, but `write2` should be faster. */
23+
copy = fio_bstr_write2(copy,
24+
FIO_STRING_WRITE_STR1(". The answer is: "),
25+
FIO_STRING_WRITE_UNUM(42));
26+
printf("Original string: %s\n", org);
27+
printf("Copied string: %s\n", copy);
28+
fio_bstr_free(org);
29+
fio_bstr_free(copy);
30+
}

tests/chat.c examples/chat.c

File renamed without changes.

tests/client.c examples/client.c

File renamed without changes.

examples/fiobj.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* *****************************************************************************
2+
Using JSON with FIOBJ (facil.io soft types)
3+
***************************************************************************** */
4+
#define FIO_FIOBJ
5+
#include "fio-stl/include.h"
6+
7+
int main(void) {
8+
/* we will create a hash map to contain all our key-value pairs */
9+
FIOBJ map = fiobj_hash_new();
10+
11+
/* note that the ownership of `array` is placed inside the hash map. */
12+
FIOBJ array = fiobj_hash_set3(map, "array", 5, fiobj_array_new());
13+
for (size_t i = 0; i < 5; ++i) {
14+
/* add numerals to array */
15+
fiobj_array_unshift(array, fiobj_num_new(0 - (intptr_t)i));
16+
fiobj_array_push(array, fiobj_num_new((intptr_t)i + 1));
17+
}
18+
19+
/* add a string to the array - note that the array will own the new String */
20+
fiobj_array_push(array, fiobj_str_new_cstr("done", 4));
21+
22+
/* output data as a new JSON String */
23+
FIOBJ json_output = fiobj2json(FIOBJ_INVALID, map, 1);
24+
printf("JSON output of our hash:\n%s\n", fiobj_str_ptr(json_output));
25+
26+
/* free the Hash Map - will free also all values (array, strings) and keys */
27+
fiobj_free(map);
28+
29+
/* rebuild the map object from the JSON String */
30+
FIOBJ json_data = fiobj_json_parse(fiobj2cstr(json_output), NULL);
31+
32+
/* free the JSON String */
33+
fiobj_free(json_output);
34+
35+
/* seek an object in a complex object using a JSON style lookup string. */
36+
printf(
37+
"The 7th object in the \"array\" key is: %s\n",
38+
fiobj2cstr(fiobj_json_find(json_data, FIO_STR_INFO2("array[6]", 8))).buf);
39+
40+
/* free the rebuilt map */
41+
fiobj_free(json_data);
42+
}

examples/map.c

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* *****************************************************************************
2+
3+
Hash Maps
4+
5+
6+
Easily define key-value String Hash Map, also sometimes called a "dictionary",
7+
using different smart defaults for short keys `FIO_MAP_KEY_KSTR` vs longer keys
8+
(or when expecting a sparsely populated map) `FIO_MAP_KEY_BSTR`.
9+
10+
***************************************************************************** */
11+
12+
/* *****************************************************************************
13+
(2) Hash Map: String Dictionary (automatic setup)
14+
***************************************************************************** */
15+
16+
/* Set the properties for the key-value Unordered Map type called `dict_s` */
17+
#define FIO_UMAP_NAME dict
18+
#define FIO_MAP_KEY_KSTR /* pre-defined macro for using fio_keystr_s keys. */
19+
#define FIO_MAP_VALUE_BSTR /* pre-defined macro for using String values. */
20+
#define FIO_MAP_HASH_FN(str) \
21+
fio_risky_hash(str.buf, str.len, (uint64_t)&fio_risky_hash)
22+
#include "fio-stl/include.h" /* or "fio-stl.h" */
23+
24+
void easy_dict_example(void) {
25+
dict_s dictionary = FIO_MAP_INIT;
26+
/* insertion using dict_set */
27+
dict_set(&dictionary,
28+
FIO_STR_INFO1("Hello"),
29+
FIO_STR_INFO1("Hello World!"),
30+
NULL);
31+
dict_set(&dictionary,
32+
FIO_STR_INFO1("42"),
33+
FIO_STR_INFO1("Meaning of life..."),
34+
NULL);
35+
/* access using dict_get */
36+
printf("- Printing each key: value\n");
37+
printf("Hello: %s\n", dict_get(&dictionary, FIO_STR_INFO1("Hello")).buf);
38+
printf("42: %s\n", dict_get(&dictionary, FIO_STR_INFO1("42")).buf);
39+
/* update using dict_set */
40+
printf("- Updating value for 42 and reprinting\n");
41+
dict_set(&dictionary,
42+
FIO_STR_INFO1("42"),
43+
FIO_STR_INFO1("What was the question?"),
44+
NULL);
45+
/* map iteration - this is an unordered map, order is incidental. */
46+
FIO_MAP_EACH(dict, &dictionary, i) {
47+
printf("%-8s: %s\n", i.key.buf, i.value.buf);
48+
}
49+
/* removal using dict_remove */
50+
dict_remove(&dictionary, FIO_STR_INFO1("42"), NULL);
51+
/* Since the "42" key was removed, its `buf` value will point to NULL. */
52+
printf("Did we remove 42 ... ? - %s\n",
53+
dict_get(&dictionary, FIO_STR_INFO1("42")).buf
54+
? dict_get(&dictionary, FIO_STR_INFO1("42")).buf
55+
: "removed");
56+
/* Cleanup. */
57+
dict_destroy(&dictionary);
58+
}
59+
60+
/* *****************************************************************************
61+
(2) Hash Map: String Dictionary (manual setup)
62+
***************************************************************************** */
63+
64+
/* Create a binary safe String type for Strings that aren't mutated often */
65+
#define FIO_STR_SMALL str
66+
#include "fio-stl/include.h" /* or "fio-stl.h" */
67+
68+
/* Defines a key-value Unordered Map type called `dictionary_s` */
69+
#define FIO_MAP_NAME dictionary
70+
#define FIO_MAP_ORDERED 0
71+
#define FIO_MAP_VALUE str_s
72+
#define FIO_MAP_VALUE_COPY(dest, src) str_init_copy2(&(dest), &(src))
73+
#define FIO_MAP_VALUE_DESTROY(k) str_destroy(&k)
74+
#define FIO_MAP_VALUE_CMP(a, b) str_is_eq(&(a), &(b))
75+
#define FIO_MAP_KEY FIO_MAP_VALUE
76+
#define FIO_MAP_KEY_COPY FIO_MAP_VALUE_COPY
77+
#define FIO_MAP_KEY_DESTROY FIO_MAP_VALUE_DESTROY
78+
#define FIO_MAP_KEY_CMP FIO_MAP_VALUE_CMP
79+
#include "fio-stl/include.h" /* or "fio-stl.h" */
80+
/** set helper for consistent hash values */
81+
FIO_IFUNC str_s dictionary_set2(dictionary_s *m, str_s key, str_s obj) {
82+
return dictionary_set(m, str_hash(&key, (uint64_t)m), key, obj, NULL);
83+
}
84+
/** get helper for consistent hash values */
85+
FIO_IFUNC str_s *dictionary_get2(dictionary_s *m, str_s key) {
86+
return &(dictionary_get_ptr(m, str_hash(&key, (uint64_t)m), key)->value);
87+
}
88+
89+
void dictionary_example(void) {
90+
dictionary_s dictionary = FIO_MAP_INIT;
91+
str_s key, val;
92+
str_init_const(&key, "hello", 5);
93+
str_init_const(&val, "Hello World!", 12);
94+
dictionary_set2(&dictionary, key, val);
95+
printf("%s\n", str_ptr(dictionary_get2(&dictionary, key)));
96+
dictionary_destroy(&dictionary);
97+
}
98+
99+
/* *****************************************************************************
100+
(3) Hash Map: Strings to Numbers
101+
***************************************************************************** */
102+
103+
/* map words to numbers. */
104+
#define FIO_UMAP_NAME umap
105+
#define FIO_MAP_KEY_KSTR
106+
#define FIO_MAP_VALUE uintptr_t
107+
#define FIO_MAP_HASH_FN(k) \
108+
fio_risky_hash((k).buf, (k).len, (uint64_t)(uintptr_t)&umap_destroy)
109+
#include "fio-stl/include.h" /* or "fio-stl.h" */
110+
111+
/* example adding strings to map and printing data. */
112+
void map_keystr_example(void) {
113+
umap_s map = FIO_MAP_INIT;
114+
/* FIO_KEYSTR_CONST prevents copying of longer constant strings */
115+
umap_set(&map, FIO_STR_INFO3("One", 3, FIO_KEYSTR_CONST), 1, NULL);
116+
umap_set(&map, FIO_STR_INFO3("Two", 3, FIO_KEYSTR_CONST), 2, NULL);
117+
umap_set(&map, FIO_STR_INFO3("Three", 5, FIO_KEYSTR_CONST), 3, NULL);
118+
FIO_MAP_EACH(umap, &map, pos) {
119+
uintptr_t value = pos.value;
120+
printf("%.*s: %llu\n",
121+
(int)pos.key.len,
122+
pos.key.buf,
123+
(unsigned long long)value);
124+
}
125+
umap_destroy(&map);
126+
}
127+
128+
/* *****************************************************************************
129+
main
130+
***************************************************************************** */
131+
132+
int main(void) {
133+
printf("=====================================\n");
134+
easy_dict_example();
135+
printf("=====================================\n");
136+
dictionary_example();
137+
printf("=====================================\n");
138+
map_keystr_example();
139+
printf("=====================================\n");
140+
}

0 commit comments

Comments
 (0)