src/cx/printf.h

changeset 849
edb9f875b7f9
parent 810
85859399a0cc
equal deleted inserted replaced
848:6456036bbb37 849:edb9f875b7f9
166 166
167 167
168 /** 168 /**
169 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 169 * An \c sprintf like function which reallocates the string when the buffer is not large enough.
170 * 170 *
171 * \note The resulting string is guaranteed to be zero-terminated. 171 * The size of the buffer will be updated in \p len when necessary.
172 * That means, when the buffer needed to be reallocated, the new size of the buffer will be 172 *
173 * the length returned by this function plus one. 173 * \note The resulting string is guaranteed to be zero-terminated.
174 * 174 *
175 * @param str a pointer to the string buffer 175 * @param str a pointer to the string buffer
176 * @param len the current length of the buffer 176 * @param len a pointer to the length of the buffer
177 * @param fmt the format string 177 * @param fmt the format string
178 * @param ... additional arguments 178 * @param ... additional arguments
179 * @return the length of produced string 179 * @return the length of produced string
180 */ 180 */
181 #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__) 181 #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)
182 182
183 /** 183 /**
184 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 184 * An \c sprintf like function which reallocates the string when the buffer is not large enough.
185 * 185 *
186 * \note The resulting string is guaranteed to be zero-terminated. 186 * The size of the buffer will be updated in \p len when necessary.
187 * That means, when the buffer needed to be reallocated, the new size of the buffer will be 187 *
188 * the length returned by this function plus one. 188 * \note The resulting string is guaranteed to be zero-terminated.
189 * 189 *
190 * \attention The original buffer MUST have been allocated with the same allocator! 190 * \attention The original buffer MUST have been allocated with the same allocator!
191 * 191 *
192 * @param alloc the allocator to use 192 * @param alloc the allocator to use
193 * @param str a pointer to the string buffer 193 * @param str a pointer to the string buffer
194 * @param len the current length of the buffer 194 * @param len a pointer to the length of the buffer
195 * @param fmt the format string 195 * @param fmt the format string
196 * @param ... additional arguments 196 * @param ... additional arguments
197 * @return the length of produced string 197 * @return the length of produced string
198 */ 198 */
199 __attribute__((__nonnull__(1, 2, 4), __format__(printf, 4, 5))) 199 __attribute__((__nonnull__(1, 2, 3, 4), __format__(printf, 4, 5)))
200 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, ... ); 200 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... );
201 201
202 202
203 /** 203 /**
204 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 204 * An \c sprintf like function which reallocates the string when the buffer is not large enough.
205 * 205 *
206 * \note The resulting string is guaranteed to be zero-terminated. 206 * The size of the buffer will be updated in \p len when necessary.
207 * That means, when the buffer needed to be reallocated, the new size of the buffer will be 207 *
208 * the length returned by this function plus one. 208 * \note The resulting string is guaranteed to be zero-terminated.
209 * 209 *
210 * @param str a pointer to the string buffer 210 * @param str a pointer to the string buffer
211 * @param len the current length of the buffer 211 * @param len a pointer to the length of the buffer
212 * @param fmt the format string 212 * @param fmt the format string
213 * @param ap argument list 213 * @param ap argument list
214 * @return the length of produced string 214 * @return the length of produced string
215 */ 215 */
216 #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap) 216 #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)
217 217
218 /** 218 /**
219 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 219 * An \c sprintf like function which reallocates the string when the buffer is not large enough.
220 * 220 *
221 * \note The resulting string is guaranteed to be zero-terminated. 221 * The size of the buffer will be updated in \p len when necessary.
222 * That means, when the buffer needed to be reallocated, the new size of the buffer will be 222 *
223 * the length returned by this function plus one. 223 * \note The resulting string is guaranteed to be zero-terminated.
224 * 224 *
225 * \attention The original buffer MUST have been allocated with the same allocator! 225 * \attention The original buffer MUST have been allocated with the same allocator!
226 * 226 *
227 * @param alloc the allocator to use 227 * @param alloc the allocator to use
228 * @param str a pointer to the string buffer 228 * @param str a pointer to the string buffer
229 * @param len the current length of the buffer 229 * @param len a pointer to the length of the buffer
230 * @param fmt the format string 230 * @param fmt the format string
231 * @param ap argument list 231 * @param ap argument list
232 * @return the length of produced string 232 * @return the length of produced string
233 */ 233 */
234 __attribute__((__nonnull__)) 234 __attribute__((__nonnull__))
235 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap); 235 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap);
236 236
237 237
238 /** 238 /**
239 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 239 * An \c sprintf like function which allocates a new string when the buffer is not large enough.
240 *
241 * The size of the buffer will be updated in \p len when necessary.
240 * 242 *
241 * The location of the resulting string will \em always be stored to \p str. When the buffer 243 * The location of the resulting string will \em always be stored to \p str. When the buffer
242 * was sufficiently large, \p buf itself will be stored to the location of \p str. 244 * was sufficiently large, \p buf itself will be stored to the location of \p str.
243 * 245 *
244 * \note The resulting string is guaranteed to be zero-terminated. 246 * \note The resulting string is guaranteed to be zero-terminated.
245 * That means, when the buffer needed to be reallocated, the new size of the buffer will be
246 * the length returned by this function plus one.
247 * 247 *
248 * \remark When a new string needed to be allocated, the contents of \p buf will be 248 * \remark When a new string needed to be allocated, the contents of \p buf will be
249 * poisoned after the call, because this function tries to produce the string in \p buf, first. 249 * poisoned after the call, because this function tries to produce the string in \p buf, first.
250 * 250 *
251 * @param buf a pointer to the buffer 251 * @param buf a pointer to the buffer
252 * @param len the length of the buffer 252 * @param len a pointer to the length of the buffer
253 * @param str a pointer to the location 253 * @param str a pointer to the location
254 * @param fmt the format string 254 * @param fmt the format string
255 * @param ... additional arguments 255 * @param ... additional arguments
256 * @return the length of produced string 256 * @return the length of produced string
257 */ 257 */
258 #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__) 258 #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)
259 259
260 /** 260 /**
261 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 261 * An \c sprintf like function which allocates a new string when the buffer is not large enough.
262 *
263 * The size of the buffer will be updated in \p len when necessary.
262 * 264 *
263 * The location of the resulting string will \em always be stored to \p str. When the buffer 265 * The location of the resulting string will \em always be stored to \p str. When the buffer
264 * was sufficiently large, \p buf itself will be stored to the location of \p str. 266 * was sufficiently large, \p buf itself will be stored to the location of \p str.
265 * 267 *
266 * \note The resulting string is guaranteed to be zero-terminated. 268 * \note The resulting string is guaranteed to be zero-terminated.
267 * That means, when the buffer needed to be reallocated, the new size of the buffer will be
268 * the length returned by this function plus one.
269 * 269 *
270 * \remark When a new string needed to be allocated, the contents of \p buf will be 270 * \remark When a new string needed to be allocated, the contents of \p buf will be
271 * poisoned after the call, because this function tries to produce the string in \p buf, first. 271 * poisoned after the call, because this function tries to produce the string in \p buf, first.
272 * 272 *
273 * @param alloc the allocator to use 273 * @param alloc the allocator to use
274 * @param buf a pointer to the buffer 274 * @param buf a pointer to the buffer
275 * @param len the length of the buffer 275 * @param len a pointer to the length of the buffer
276 * @param str a pointer to the location 276 * @param str a pointer to the location
277 * @param fmt the format string 277 * @param fmt the format string
278 * @param ... additional arguments 278 * @param ... additional arguments
279 * @return the length of produced string 279 * @return the length of produced string
280 */ 280 */
281 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6))) 281 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6)))
282 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ); 282 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... );
283 283
284 /** 284 /**
285 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 285 * An \c sprintf like function which allocates a new string when the buffer is not large enough.
286 *
287 * The size of the buffer will be updated in \p len when necessary.
286 * 288 *
287 * The location of the resulting string will \em always be stored to \p str. When the buffer 289 * The location of the resulting string will \em always be stored to \p str. When the buffer
288 * was sufficiently large, \p buf itself will be stored to the location of \p str. 290 * was sufficiently large, \p buf itself will be stored to the location of \p str.
289 * 291 *
290 * \note The resulting string is guaranteed to be zero-terminated. 292 * \note The resulting string is guaranteed to be zero-terminated.
291 * That means, when the buffer needed to be reallocated, the new size of the buffer will be
292 * the length returned by this function plus one.
293 * 293 *
294 * \remark When a new string needed to be allocated, the contents of \p buf will be 294 * \remark When a new string needed to be allocated, the contents of \p buf will be
295 * poisoned after the call, because this function tries to produce the string in \p buf, first. 295 * poisoned after the call, because this function tries to produce the string in \p buf, first.
296 * 296 *
297 * @param buf a pointer to the buffer 297 * @param buf a pointer to the buffer
298 * @param len the length of the buffer 298 * @param len a pointer to the length of the buffer
299 * @param str a pointer to the location 299 * @param str a pointer to the location
300 * @param fmt the format string 300 * @param fmt the format string
301 * @param ap argument list 301 * @param ap argument list
302 * @return the length of produced string 302 * @return the length of produced string
303 */ 303 */
304 #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap) 304 #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)
305 305
306 /** 306 /**
307 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 307 * An \c sprintf like function which allocates a new string when the buffer is not large enough.
308 *
309 * The size of the buffer will be updated in \p len when necessary.
308 * 310 *
309 * The location of the resulting string will \em always be stored to \p str. When the buffer 311 * The location of the resulting string will \em always be stored to \p str. When the buffer
310 * was sufficiently large, \p buf itself will be stored to the location of \p str. 312 * was sufficiently large, \p buf itself will be stored to the location of \p str.
311 * 313 *
312 * \note The resulting string is guaranteed to be zero-terminated. 314 * \note The resulting string is guaranteed to be zero-terminated.
313 * That means, when the buffer needed to be reallocated, the new size of the buffer will be
314 * the length returned by this function plus one.
315 * 315 *
316 * \remark When a new string needed to be allocated, the contents of \p buf will be 316 * \remark When a new string needed to be allocated, the contents of \p buf will be
317 * poisoned after the call, because this function tries to produce the string in \p buf, first. 317 * poisoned after the call, because this function tries to produce the string in \p buf, first.
318 * 318 *
319 * @param alloc the allocator to use 319 * @param alloc the allocator to use
320 * @param buf a pointer to the buffer 320 * @param buf a pointer to the buffer
321 * @param len the length of the buffer 321 * @param len a pointer to the length of the buffer
322 * @param str a pointer to the location 322 * @param str a pointer to the location
323 * @param fmt the format string 323 * @param fmt the format string
324 * @param ap argument list 324 * @param ap argument list
325 * @return the length of produced string 325 * @return the length of produced string
326 */ 326 */
327 __attribute__((__nonnull__)) 327 __attribute__((__nonnull__))
328 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap); 328 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap);
329 329
330 330
331 #ifdef __cplusplus 331 #ifdef __cplusplus
332 } // extern "C" 332 } // extern "C"
333 #endif 333 #endif

mercurial