Thu, 03 May 2018 09:49:38 +0200
adds ucx_longintcmp() compare function
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 | ||
54 | ## Buffer | |
55 | ||
56 | *Header file:* [buffer.h](api/buffer_8h.html) | |
57 | *Required modules:* None. | |
58 | ||
59 | Instances of this buffer implementation can be used to read from or to write to | |
60 | 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
|
61 | `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
|
62 | from one buffer to another or from file or network streams to the buffer and |
259 | 63 | vice-versa. |
64 | ||
65 | More features for convenient use of the buffer can be enabled, like automatic | |
66 | memory management and automatic resizing of the buffer space. | |
67 | See the documentation of the macro constants in the header file for more | |
68 | information. | |
69 | ||
70 | ## List | |
71 | ||
72 | *Header file:* [list.h](api/list_8h.html) | |
73 | *Required modules:* [Allocator](#allocator) | |
74 | ||
75 | This module provides the data structure and several functions for a doubly | |
76 | linked list. Among the common operations like insert, remove, search and sort, | |
77 | we allow convenient iteration via a special `UCX_FOREACH` macro. | |
78 | ||
79 | ## Logging | |
80 | ||
81 | *Header file:* [logging.h](api/logging_8h.html) | |
82 | *Required modules:* [Map](#map), [String](#string) | |
83 | ||
84 | The logging module comes with some predefined log levels and allows some more | |
85 | customization. You may choose if you want to get timestamps or source file and | |
86 | line number logged automatically when outputting a message. | |
87 | ||
88 | ||
89 | ## Map | |
90 | ||
91 | *Header file:* [map.h](api/map_8h.html) | |
92 | *Required modules:* [Allocator](#allocator), [String](#string) | |
93 | ||
94 | This module provides a hash map implementation using murmur hash 2 and separate | |
95 | chaining with linked lists. Similarly to the list module, we provide a | |
96 | `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs. | |
97 | ||
98 | ## Memory Pool | |
99 | ||
100 | *Header file:* [mempool.h](api/mempool_8h.html) | |
101 | *Required modules:* [Allocator](#allocator) | |
102 | ||
103 | Here we have a concrete allocator implementation in the sense of a memory pool. | |
104 | This pool allows you to register destructor functions for the allocated memory, | |
105 | which are automatically called on the destruction of the pool. | |
106 | But you may also register *independent* destructor functions within a pool in | |
107 | case, some external library allocated memory for you, which you wish to be | |
108 | destroyed together with this pool. | |
109 | ||
110 | ## Properties | |
111 | ||
112 | *Header file:* [properties.h](api/properties_8h.html) | |
113 | *Required modules:* [Map](#map) | |
114 | ||
115 | This module provides load and store function for `*.properties` files. | |
116 | The key/value pairs are stored within an UCX Map. | |
117 | ||
277
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
118 | ### 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
|
119 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
120 | ```C |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
121 | // 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
|
122 | 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
|
123 | if (!file) { |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
124 | // error handling |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
125 | return 1; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
126 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
127 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
128 | // 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
|
129 | 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
|
130 | 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
|
131 | // error handling |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
132 | fclose(file); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
133 | 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
|
134 | return 1; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
135 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
136 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
137 | // 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
|
138 | char* propval; |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
139 | 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
|
140 | 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
|
141 | 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
|
142 | } |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
143 | |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
144 | // 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
|
145 | 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
|
146 | 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
|
147 | fclose(file); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
267
diff
changeset
|
148 | ``` |
259 | 149 | ## Stack |
150 | ||
151 | *Header file:* [stack.h](api/stack_8h.html) | |
152 | *Required modules:* [Allocator](#allocator) | |
153 | ||
154 | This concrete implementation of an UCX Allocator allows you to grab some amount | |
155 | of memory which is then handled as a stack. | |
156 | Please note, that the term *stack* only refers to the behavior of this | |
157 | allocator. You may still choose if you want to use stack or heap memory | |
158 | for the underlying space. | |
159 | ||
160 | A typical use case is an algorithm where you need to allocate and free large | |
161 | amounts of memory very frequently. | |
162 | ||
163 | ## String | |
164 | ||
165 | *Header file:* [string.h](api/string_8h.html) | |
166 | *Required modules:* [Allocator](#allocator) | |
167 | ||
168 | This module provides a safe implementation of bounded string. | |
169 | Usually C strings do not carry a length. While for zero-terminated strings you | |
170 | can easily get the length with `strlen`, this is not generally possible for | |
171 | arbitrary strings. | |
172 | The `sstr_t` type of this module always carries the string and its length to | |
173 | reduce the risk of buffer overflows dramatically. | |
174 | ||
267
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
175 | ### Initialization |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
176 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
177 | 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
|
178 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
179 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
180 | /* (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
|
181 | sstr_t a = sstr(cstr); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
182 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
183 | /* (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
|
184 | 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
|
185 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
186 | /* (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
|
187 | 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
|
188 | sstr_t c = S("hello"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
189 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
190 | /* (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
|
191 | sstr_t d = ST("hello"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
192 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
193 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
194 | 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
|
195 | 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
|
196 | 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
|
197 | 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
|
198 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
199 | ### 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
|
200 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
201 | 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
|
202 | 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
|
203 | subtraction. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
204 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
205 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
206 | 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
|
207 | sstr_t needle = ST("we"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
208 | 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
|
209 | if (result.ptr) |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
210 | 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
|
211 | else |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
212 | printf("Not found.\n"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
213 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
214 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
215 | ### 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
|
216 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
217 | 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
|
218 | 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
|
219 | 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
|
220 | pool. |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
221 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
222 | ```C |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
223 | 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
|
224 | sstr_t delim = ST("::"); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
225 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
226 | 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
|
227 | 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
|
228 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
229 | 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
|
230 | 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
|
231 | /* 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
|
232 | 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
|
233 | } |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
234 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
235 | ucx_mempool_destroy(pool); |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
236 | ``` |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
237 | The output is: |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
238 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
239 | here |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
240 | are |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
241 | some |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
242 | strings |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
243 | |
f4789572c9d6
restructures documentation + adds some examples for sstr_t
Mike Becker <universe@uap-core.de>
parents:
264
diff
changeset
|
244 | 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
|
245 | |
259 | 246 | ## Testing |
247 | ||
248 | *Header file:* [test.h](api/test_8h.html) | |
249 | *Required modules:* None. | |
250 | ||
251 | This module provides a testing framework which allows you to execute test cases | |
252 | within test suites. | |
253 | To avoid code duplication within tests, we also provide the possibility to | |
254 | define test subroutines. | |
255 | ||
256 | ## Utilities | |
257 | ||
258 | *Header file:* [utils.h](api/utils_8h.html) | |
259 | *Required modules:* [Allocator](#allocator), [String](#string) | |
260 | ||
261 | In this module we provide very general utility function for copy and compare | |
262 | operations. | |
263 | We also provide several `printf` variants to conveniently print formatted data | |
264 | to streams or strings. | |
265 | ||
279
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
266 | ### A simple copy program |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
267 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
268 | 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
|
269 | 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
|
270 | whole files in a single call. |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
271 | This is a minimal working example: |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
272 | ```C |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
273 | #include <stdio.h> |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
274 | #include <ucx/utils.h> |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
275 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
276 | int main(int argc, char** argv) { |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
277 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
278 | if (argc != 3) { |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
279 | 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
|
280 | return 1; |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
281 | } |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
282 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
283 | 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
|
284 | FILE *destf = fopen(argv[2], "w"); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
285 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
286 | 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
|
287 | printf("%zu bytes copied.\n", n); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
288 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
289 | fclose(srcf); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
290 | fclose(destf); |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
291 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
292 | |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
293 | return 0; |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
294 | } |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
295 | ``` |
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
296 | |
281
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
297 | ### Automatic allocation for formatted strings |
279
ee37b179e597
doc: MWE for ucx_stream_copy()
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
298 | |
281
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
299 | 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
|
300 | `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
|
301 | 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
|
302 | ```C |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
303 | 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
|
304 | ``` |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
305 | 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
|
306 | 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
|
307 | 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
|
308 | 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
|
309 | `ucx_bprintf()`. |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
310 | ```C |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
311 | 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
|
312 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
313 | 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
|
314 | 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
|
315 | 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
|
316 | } |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
317 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
318 | // 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
|
319 | 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
|
320 | |
e8146a561e73
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
Mike Becker <universe@uap-core.de>
parents:
280
diff
changeset
|
321 | 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
|
322 | ``` |