diff options
Diffstat (limited to 'src/include/taler_util.h')
-rw-r--r-- | src/include/taler_util.h | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/include/taler_util.h b/src/include/taler_util.h index 9cfcb3dce..c767a32e3 100644 --- a/src/include/taler_util.h +++ b/src/include/taler_util.h | |||
@@ -75,6 +75,151 @@ | |||
75 | 75 | ||
76 | 76 | ||
77 | /** | 77 | /** |
78 | * Dynamically growing buffer. Can be used to construct | ||
79 | * strings and other objects with dynamic size. | ||
80 | * | ||
81 | * This structure should, in most cases, be stack-allocated and | ||
82 | * zero-initialized, like: | ||
83 | * | ||
84 | * struct TALER_Buffer my_buffer = { 0 }; | ||
85 | */ | ||
86 | struct TALER_Buffer | ||
87 | { | ||
88 | /** | ||
89 | * Capacity of the buffer. | ||
90 | */ | ||
91 | size_t capacity; | ||
92 | |||
93 | /** | ||
94 | * Current write position. | ||
95 | */ | ||
96 | size_t position; | ||
97 | |||
98 | /** | ||
99 | * Backing memory. | ||
100 | */ | ||
101 | char *mem; | ||
102 | |||
103 | /** | ||
104 | * Log a warning if the buffer is grown over its initially allocated capacity. | ||
105 | */ | ||
106 | int warn_grow; | ||
107 | }; | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Initialize a buffer with the given capacity. | ||
112 | * | ||
113 | * When a buffer is allocated with this function, a warning is logged | ||
114 | * when the buffer exceeds the initial capacity. | ||
115 | * | ||
116 | * @param buf the buffer to initialize | ||
117 | * @param capacity the capacity (in bytes) to allocate for @a buf | ||
118 | */ | ||
119 | void | ||
120 | TALER_buffer_prealloc (struct TALER_Buffer *buf, size_t capacity); | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Make sure that at least @a n bytes remaining in the buffer. | ||
125 | * | ||
126 | * @param buf buffer to potentially grow | ||
127 | * @param n number of bytes that should be available to write | ||
128 | */ | ||
129 | void | ||
130 | TALER_buffer_ensure_remaining (struct TALER_Buffer *buf, size_t n); | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Write bytes to the buffer. | ||
135 | * | ||
136 | * Grows the buffer if necessary. | ||
137 | * | ||
138 | * @param buf buffer to write to | ||
139 | * @param data data to read from | ||
140 | * @param len number of bytes to copy from @a data to @a buf | ||
141 | * | ||
142 | */ | ||
143 | void | ||
144 | TALER_buffer_write (struct TALER_Buffer *buf, const char *data, size_t len); | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Write a 0-terminated string to a buffer, excluding the 0-terminator. | ||
149 | * | ||
150 | * Grows the buffer if necessary. | ||
151 | * | ||
152 | * @param buf the buffer to write to | ||
153 | * @param str the string to write to @a buf | ||
154 | */ | ||
155 | void | ||
156 | TALER_buffer_write_str (struct TALER_Buffer *buf, const char *str); | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Write a path component to a buffer, ensuring that | ||
161 | * there is exactly one slash between the previous contents | ||
162 | * of the buffer and the new string. | ||
163 | * | ||
164 | * @param buf buffer to write to | ||
165 | * @param str string containing the new path component | ||
166 | */ | ||
167 | void | ||
168 | TALER_buffer_write_path (struct TALER_Buffer *buf, const char *str); | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Write a 0-terminated formatted string to a buffer, excluding the | ||
173 | * 0-terminator. | ||
174 | * | ||
175 | * Grows the buffer if necessary. | ||
176 | * | ||
177 | * @param buf the buffer to write to | ||
178 | * @param fmt format string | ||
179 | * @param ... format arguments | ||
180 | */ | ||
181 | void | ||
182 | TALER_buffer_write_fstr (struct TALER_Buffer *buf, const char *fmt, ...); | ||
183 | |||
184 | |||
185 | /** | ||
186 | * Write a 0-terminated formatted string to a buffer, excluding the | ||
187 | * 0-terminator. | ||
188 | * | ||
189 | * Grows the buffer if necessary. | ||
190 | * | ||
191 | * @param buf the buffer to write to | ||
192 | * @param fmt format string | ||
193 | * @param args format argument list | ||
194 | */ | ||
195 | void | ||
196 | TALER_buffer_write_vfstr (struct TALER_Buffer *buf, const char *fmt, va_list args); | ||
197 | |||
198 | |||
199 | /** | ||
200 | * Clear the buffer and return the string it contained. | ||
201 | * The caller is responsible to eventually #GNUNET_free | ||
202 | * the returned string. | ||
203 | * | ||
204 | * The returned string is always 0-terminated. | ||
205 | * | ||
206 | * @param buf the buffer to reap the string from | ||
207 | * @returns the buffer contained in the string | ||
208 | */ | ||
209 | char * | ||
210 | TALER_buffer_reap_str (struct TALER_Buffer *buf); | ||
211 | |||
212 | |||
213 | /** | ||
214 | * Free the backing memory of the given buffer. | ||
215 | * Does not free the memory of the buffer control structure, | ||
216 | * which is typically stack-allocated. | ||
217 | */ | ||
218 | void | ||
219 | TALER_buffer_clear (struct TALER_Buffer *buf); | ||
220 | |||
221 | |||
222 | /** | ||
78 | * Initialize Gcrypt library. | 223 | * Initialize Gcrypt library. |
79 | */ | 224 | */ |
80 | void | 225 | void |
@@ -203,6 +348,25 @@ TALER_url_absolute_raw (const char *proto, | |||
203 | 348 | ||
204 | 349 | ||
205 | /** | 350 | /** |
351 | * Make an absolute URL for the given parameters. | ||
352 | * | ||
353 | * @param proto protocol for the URL (typically https) | ||
354 | * @param host hostname for the URL | ||
355 | * @param prefix prefix for the URL | ||
356 | * @param path path for the URL | ||
357 | * @param args NULL-terminated key-value pairs (char *) for query parameters, | ||
358 | * the value will be url-encoded | ||
359 | * @returns the URL, must be freed with #GNUNET_free | ||
360 | */ | ||
361 | char * | ||
362 | TALER_url_absolute_raw_va (const char *proto, | ||
363 | const char *host, | ||
364 | const char *prefix, | ||
365 | const char *path, | ||
366 | va_list args); | ||
367 | |||
368 | |||
369 | /** | ||
206 | * Make an absolute URL for a given MHD connection. | 370 | * Make an absolute URL for a given MHD connection. |
207 | * | 371 | * |
208 | * @param path path of the url | 372 | * @param path path of the url |