Fri, 11 May 2018 17:40:16 +0200
adds ucx_list_prepend_once() and ucx_list_prepend_once_a()
264
24f5484bae97
web doc has now proper titles
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
1 | --- |
24f5484bae97
web doc has now proper titles
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
2 | title: Modules |
24f5484bae97
web doc has now proper titles
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
3 | --- |
259 | 4 | |
5 | UCX provides several modules for data structures and algorithms. | |
6 | You may choose to use specific modules by inclueding the corresponding header | |
7 | file. | |
8 | Please note, that some modules make use of other UCX modules. | |
9 | For instance, the [Allocator](#allocator) module is used by many other modules | |
10 | to allow flexible memory allocation. | |
11 | By default the header files are placed into an `ucx` directory within your | |
282
39e69d78b01d
minor formatting fix in modules.md
Mike Becker <universe@uap-core.de>
parents:
281
diff
changeset
|
12 | systems include directory. In this case you can use a module by including it |
259 | 13 | via `#include <ucx/MODULENAME.h>`. |
14 | Required modules are included automatically. | |
15 | ||
267
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
16 | <div id="modules" align="center"> |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
17 | |
280
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
18 | ----------------------- ---------------------- ---------------------------- ------------------------- |
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
19 | [Allocator](#allocator) [AVL Tree](#avl-tree) [Buffer](#buffer) [List](#list) |
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
20 | [Logging](#logging) [Map](#map) [Memory Pool](#memory-pool) [Properties](#properties) |
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
21 | [Stack](#stack) [String](#string) [Testing](#testing) [Utilities](#utilities) |
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
22 | ----------------------- ---------------------- ---------------------------- ------------------------- |
267
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
23 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
24 | </div> |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
25 | |
259 | 26 | ## Allocator |
27 | ||
28 | *Header file:* [allocator.h](api/allocator_8h.html) | |
29 | *Required modules:* None. | |
30 | ||
31 | A UCX allocator consists of a pointer to the memory area / pool and four | |
32 | function pointers to memory management functions operating on this memory | |
33 | area / pool. These functions shall behave equivalent to the standard libc | |
34 | functions `malloc`, `calloc`, `realloc` and `free`. | |
35 | ||
36 | The signature of the memory management functions is based on the signature | |
37 | of the respective libc function but each of them takes the pointer to the | |
38 | memory area / pool as first argument. | |
39 | ||
40 | As the pointer to the memory area / pool can be arbitrarily chosen, any data | |
41 | can be provided to the memory management functions. One example is the | |
280
6e3c4036a80c
removes artificial anchors from modules.md
Mike Becker <universe@uap-core.de>
parents:
279
diff
changeset
|
42 | [UCX Memory Pool](#memory-pool). |
259 | 43 | |
44 | ## AVL Tree | |
45 | ||
46 | *Header file:* [avl.h](api/avl_8h.html) | |
47 | *Required modules:* [Allocator](#allocator) | |
48 | ||
49 | This binary search tree implementation allows average O(1) insertion and | |
50 | removal of elements (excluding binary search time). | |
51 | All common binary tree operations are implemented. Furthermore, this module | |
52 | provides search functions via lower and upper bounds. | |
53 | ||
287
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
54 | ### Filtering items with a time window |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
55 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
56 | Suppose you have a list of items which contain a `time_t` value and your task |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
57 | is to find all items within a time window `[t_start, t_end]`. |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
58 | With AVL Trees this is easy: |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
59 | ```C |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
60 | /* --------------------- |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
61 | * Somewhere in a header |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
62 | */ |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
63 | typedef struct { |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
64 | time_t ts; |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
65 | // other important data |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
66 | } MyObject; |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
67 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
68 | /* ----------- |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
69 | * Source code |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
70 | */ |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
71 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
72 | UcxAVLTree* tree = ucx_avl_new(ucx_longintcmp); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
73 | // ... populate tree with objects, use '& MyObject.ts' as key ... |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
74 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
75 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
76 | // Now find every item, with 30 <= ts <= 70 |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
77 | time_t ts_start = 30; |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
78 | time_t ts_end = 70; |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
79 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
80 | printf("Values in range:\n"); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
81 | for ( |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
82 | UcxAVLNode* node = ucx_avl_find_node( |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
83 | tree, (intptr_t) &ts_start, |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
84 | ucx_longintdist, UCX_AVL_FIND_LOWER_BOUNDED); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
85 | node && (*(time_t*)node->key) <= ts_end; |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
86 | node = ucx_avl_succ(node) |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
87 | ) { |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
88 | printf(" ts: %ld\n", ((MyObject*)node->value)->ts); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
89 | } |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
90 | |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
91 | ucx_avl_free_content(tree, free); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
92 | ucx_avl_free(tree); |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
93 | ``` |
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
282
diff
changeset
|
94 | |
259 | 95 | ## Buffer |
96 | ||
97 | *Header file:* [buffer.h](api/buffer_8h.html) | |
98 | *Required modules:* None. | |
99 | ||
100 | Instances of this buffer implementation can be used to read from or to write to | |
101 | memory like you would do with a stream. This allows the use of | |
282
39e69d78b01d
minor formatting fix in modules.md
Mike Becker <universe@uap-core.de>
parents:
281
diff
changeset
|
102 | `ucx_stream_copy()` from the [Utilities](#utilities) module to copy contents |
39e69d78b01d
minor formatting fix in modules.md
Mike Becker <universe@uap-core.de>
parents:
281
diff
changeset
|
103 | from one buffer to another or from file or network streams to the buffer and |
259 | 104 | vice-versa. |
105 | ||
106 | More features for convenient use of the buffer can be enabled, like automatic | |
107 | memory management and automatic resizing of the buffer space. | |
108 | See the documentation of the macro constants in the header file for more | |
109 | information. | |
110 | ||
290
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
111 | ### Add line numbers to a file |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
112 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
113 | When reading a file line by line, you have three options: first, you could limit |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
114 | the maximum supported line length. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
115 | Second, you allocate a god buffer large |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
116 | enough for the most lines a text file could have. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
117 | And third, undoubtedly the best option, you start with a small buffer, which |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
118 | adjusts on demand. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
119 | An `UcxBuffer` can be created to do just that for you. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
120 | Just pass the `UCX_BUFFER_AUTOEXTEND` option to the initialization function. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
121 | Here is a full working program, which adds line numbers to a file. |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
122 | ```C |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
123 | #include <stdio.h> |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
124 | #include <ucx/buffer.h> |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
125 | #include <ucx/utils.h> |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
126 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
127 | int main(int argc, char** argv) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
128 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
129 | if (argc != 2) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
130 | fprintf(stderr, "Usage: %s <file>\n", argv[0]); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
131 | return 1; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
132 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
133 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
134 | FILE* input = fopen(argv[1], "r"); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
135 | if (!input) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
136 | perror("Canno read input"); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
137 | return 1; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
138 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
139 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
140 | const size_t chunksize = 256; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
141 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
142 | UcxBuffer* linebuf = |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
143 | ucx_buffer_new( |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
144 | NULL, // the buffer should manage the memory area for us |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
145 | chunksize, // initial buffer size should be the chunk size |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
146 | UCX_BUFFER_AUTOEXTEND); // the buffer will grow when necessary |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
147 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
148 | size_t lineno = 1; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
149 | do { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
150 | // read line chunk |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
151 | size_t read = ucx_stream_ncopy( |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
152 | input, linebuf, fread, ucx_buffer_write, chunksize); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
153 | if (read == 0) break; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
154 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
155 | // handle line endings |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
156 | do { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
157 | sstr_t bufstr = ucx_buffer_to_sstr(linebuf); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
158 | sstr_t nl = sstrchr(bufstr, '\n'); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
159 | if (nl.length == 0) break; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
160 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
161 | size_t linelen = bufstr.length - nl.length; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
162 | sstr_t linestr = sstrsubsl(bufstr, 0, linelen); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
163 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
164 | printf("%zu: %" PRIsstr "\n", lineno++, SFMT(linestr)); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
165 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
166 | // shift the buffer to the next line |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
167 | ucx_buffer_shift_left(linebuf, linelen+1); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
168 | } while(1); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
169 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
170 | } while(1); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
171 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
172 | // print the 'noeol' line, if any |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
173 | sstr_t lastline = ucx_buffer_to_sstr(linebuf); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
174 | if (lastline.length > 0) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
175 | printf("%zu: %" PRIsstr, lineno, SFMT(lastline)); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
176 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
177 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
178 | fclose(input); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
179 | ucx_buffer_free(linebuf); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
180 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
181 | return 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
182 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
183 | ``` |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
184 | |
259 | 185 | ## List |
186 | ||
187 | *Header file:* [list.h](api/list_8h.html) | |
188 | *Required modules:* [Allocator](#allocator) | |
189 | ||
190 | This module provides the data structure and several functions for a doubly | |
191 | linked list. Among the common operations like insert, remove, search and sort, | |
192 | we allow convenient iteration via a special `UCX_FOREACH` macro. | |
193 | ||
194 | ## Logging | |
195 | ||
196 | *Header file:* [logging.h](api/logging_8h.html) | |
197 | *Required modules:* [Map](#map), [String](#string) | |
198 | ||
199 | The logging module comes with some predefined log levels and allows some more | |
200 | customization. You may choose if you want to get timestamps or source file and | |
201 | line number logged automatically when outputting a message. | |
202 | ||
203 | ||
204 | ## Map | |
205 | ||
206 | *Header file:* [map.h](api/map_8h.html) | |
207 | *Required modules:* [Allocator](#allocator), [String](#string) | |
208 | ||
209 | This module provides a hash map implementation using murmur hash 2 and separate | |
210 | chaining with linked lists. Similarly to the list module, we provide a | |
211 | `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs. | |
212 | ||
213 | ## Memory Pool | |
214 | ||
215 | *Header file:* [mempool.h](api/mempool_8h.html) | |
216 | *Required modules:* [Allocator](#allocator) | |
217 | ||
218 | Here we have a concrete allocator implementation in the sense of a memory pool. | |
219 | This pool allows you to register destructor functions for the allocated memory, | |
220 | which are automatically called on the destruction of the pool. | |
221 | But you may also register *independent* destructor functions within a pool in | |
222 | case, some external library allocated memory for you, which you wish to be | |
223 | destroyed together with this pool. | |
224 | ||
225 | ## Properties | |
226 | ||
227 | *Header file:* [properties.h](api/properties_8h.html) | |
228 | *Required modules:* [Map](#map) | |
229 | ||
230 | This module provides load and store function for `*.properties` files. | |
231 | The key/value pairs are stored within an UCX Map. | |
232 | ||
277
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
233 | ### Example: Loading properties from a file |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
234 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
235 | ```C |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
236 | // Open the file as usual |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
237 | FILE* file = fopen("myprops.properties", "r"); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
238 | if (!file) { |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
239 | // error handling |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
240 | return 1; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
241 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
242 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
243 | // Load the properties from the file |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
244 | UcxMap* myprops = ucx_map_new(16); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
245 | if (ucx_properties_load(myprops, file)) { |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
246 | // error handling |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
247 | fclose(file); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
248 | ucx_map_free(myprops); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
249 | return 1; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
250 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
251 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
252 | // Print out the key/value pairs |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
253 | char* propval; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
254 | UcxMapIterator propiter = ucx_map_iterator(myprops); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
255 | UCX_MAP_FOREACH(key, propval, propiter) { |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
256 | printf("%s = %s\n", (char*)key.data, propval); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
257 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
258 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
259 | // Don't forget to free the values before freeing the map |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
260 | ucx_map_free_content(myprops, NULL); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
261 | ucx_map_free(myprops); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
262 | fclose(file); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
263 | ``` |
259 | 264 | ## Stack |
265 | ||
266 | *Header file:* [stack.h](api/stack_8h.html) | |
267 | *Required modules:* [Allocator](#allocator) | |
268 | ||
269 | This concrete implementation of an UCX Allocator allows you to grab some amount | |
270 | of memory which is then handled as a stack. | |
271 | Please note, that the term *stack* only refers to the behavior of this | |
272 | allocator. You may still choose if you want to use stack or heap memory | |
273 | for the underlying space. | |
274 | ||
275 | A typical use case is an algorithm where you need to allocate and free large | |
276 | amounts of memory very frequently. | |
277 | ||
278 | ## String | |
279 | ||
280 | *Header file:* [string.h](api/string_8h.html) | |
281 | *Required modules:* [Allocator](#allocator) | |
282 | ||
283 | This module provides a safe implementation of bounded string. | |
284 | Usually C strings do not carry a length. While for zero-terminated strings you | |
285 | can easily get the length with `strlen`, this is not generally possible for | |
286 | arbitrary strings. | |
287 | The `sstr_t` type of this module always carries the string and its length to | |
288 | reduce the risk of buffer overflows dramatically. | |
289 | ||
267
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
290 | ### Initialization |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
291 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
292 | There are several ways to create an `sstr_t`: |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
293 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
294 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
295 | /* (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
296 | sstr_t a = sstr(cstr); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
297 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
298 | /* (2) cstr does not need to be zero-terminated, if length is specified */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
299 | sstr_t b = sstrn(cstr, len); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
300 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
301 | /* (3) S() macro creates sstr_t from a string using sizeof() and using sstrn(). |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
302 | This version is especially useful for function arguments */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
303 | sstr_t c = S("hello"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
304 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
305 | /* (4) ST() macro creates sstr_t struct literal using sizeof() */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
306 | sstr_t d = ST("hello"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
307 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
308 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
309 | You should not use the `S()` or `ST()` macro with string of unknown origin, |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
310 | since the `sizeof()` call might not coincide with the string length in those |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
311 | cases. If you know what you are doing, it can save you some performance, |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
312 | because you do not need the `strlen()` call. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
313 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
314 | ### Finding the position of a substring |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
315 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
316 | The `sstrstr()` function gives you a new `sstr_t` object starting with the |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
317 | requested substring. Thus determining the position comes down to a simple |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
318 | subtraction. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
319 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
320 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
321 | sstr_t haystack = ST("Here we go!"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
322 | sstr_t needle = ST("we"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
323 | sstr_t result = sstrstr(haystack, needle); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
324 | if (result.ptr) |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
325 | printf("Found at position %zd.\n", haystack.length-result.length); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
326 | else |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
327 | printf("Not found.\n"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
328 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
329 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
330 | ### Spliting a string by a delimiter |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
331 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
332 | The `sstrsplit()` function (and its allocator based version `sstrsplit_a()`) is |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
333 | very powerful and might look a bit nasty at a first glance. But it is indeed |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
334 | very simple to use. It is even more convenient in combination with a memory |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
335 | pool. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
336 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
337 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
338 | sstr_t test = ST("here::are::some::strings"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
339 | sstr_t delim = ST("::"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
340 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
341 | ssize_t count = 0; /* no limit */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
342 | UcxMempool* pool = ucx_mempool_new_default(); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
343 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
344 | sstr_t* result = sstrsplit_a(pool->allocator, test, delim, &count); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
345 | for (ssize_t i = 0 ; i < count ; i++) { |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
346 | /* don't forget to specify the length via the %*s format specifier */ |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
347 | printf("%*s\n", result[i].length, result[i].ptr); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
348 | } |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
349 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
350 | ucx_mempool_destroy(pool); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
351 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
352 | The output is: |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
353 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
354 | here |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
355 | are |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
356 | some |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
357 | strings |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
358 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
359 | The memory pool ensures, that all strings are freed. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
360 | |
259 | 361 | ## Testing |
362 | ||
363 | *Header file:* [test.h](api/test_8h.html) | |
364 | *Required modules:* None. | |
365 | ||
366 | This module provides a testing framework which allows you to execute test cases | |
367 | within test suites. | |
368 | To avoid code duplication within tests, we also provide the possibility to | |
369 | define test subroutines. | |
370 | ||
371 | ## Utilities | |
372 | ||
373 | *Header file:* [utils.h](api/utils_8h.html) | |
374 | *Required modules:* [Allocator](#allocator), [String](#string) | |
375 | ||
376 | In this module we provide very general utility function for copy and compare | |
377 | operations. | |
378 | We also provide several `printf` variants to conveniently print formatted data | |
379 | to streams or strings. | |
380 | ||
279
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
381 | ### A simple copy program |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
382 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
383 | The utilities package provides several stream copy functions. |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
384 | One of them has a very simple interface and can, for instance, be used to copy |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
385 | whole files in a single call. |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
386 | This is a minimal working example: |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
387 | ```C |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
388 | #include <stdio.h> |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
389 | #include <ucx/utils.h> |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
390 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
391 | int main(int argc, char** argv) { |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
392 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
393 | if (argc != 3) { |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
394 | fprintf(stderr, "Use %s <src> <dest>", argv[0]); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
395 | return 1; |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
396 | } |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
397 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
398 | FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
399 | FILE *destf = fopen(argv[2], "w"); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
400 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
401 | size_t n = ucx_stream_copy(srcf, destf, fread, fwrite); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
402 | printf("%zu bytes copied.\n", n); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
403 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
404 | fclose(srcf); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
405 | fclose(destf); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
406 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
407 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
408 | return 0; |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
409 | } |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
410 | ``` |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
411 | |
281
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
412 | ### Automatic allocation for formatted strings |
279
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
413 | |
281
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
414 | The UCX utility function `ucx_asprintf()` and it's convenient shortcut |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
415 | `ucx_sprintf` allow easy formatting of strings, without ever having to worry |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
416 | about the required space. |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
417 | ```C |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
418 | sstr_t mystring = ucx_sprintf("The answer is: %d!", 42); |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
419 | ``` |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
420 | Still, you have to pass `mystring.ptr` to `free()` (or the free function of |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
421 | your allocator, if you use `ucx_asprintf`). |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
422 | If you don't have all the information ready to build your string, you can even |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
423 | use a [UcxBuffer](#buffer) as a target with the utility function |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
424 | `ucx_bprintf()`. |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
425 | ```C |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
426 | UcxBuffer* strbuffer = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
427 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
428 | for (unsigned int i = 2 ; i < 100 ; i++) { |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
429 | ucx_bprintf(strbuffer, "Integer %d is %s\n", |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
430 | i, prime(i) ? "prime" : "not prime"); |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
431 | } |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
432 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
433 | // print the result to stdout |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
434 | printf("%s", (char*)strbuffer->space); |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
435 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
436 | ucx_buffer_free(strbuffer); |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
437 | ``` |