ucx/utils.h

changeset 255
bf19378aed58
parent 110
53895e9a4bbb
child 314
8722a668fb2a
equal deleted inserted replaced
254:d7c4ba50b7d8 255:bf19378aed58
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
43 #endif 43 #endif
44 44
45 #include "ucx.h" 45 #include "ucx.h"
46 #include "string.h" 46 #include "string.h"
47 #include "allocator.h" 47 #include "allocator.h"
48 #include <stdint.h> 48 #include <inttypes.h>
49 #include <string.h> 49 #include <string.h>
50 #include <stdarg.h> 50 #include <stdarg.h>
51
52 /**
53 * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy().
54 */
55 #define UCX_STREAM_COPY_BUFSIZE 4096
51 56
52 /** 57 /**
53 * Copies a string. 58 * Copies a string.
54 * @param s the string to copy 59 * @param s the string to copy
55 * @param data omitted 60 * @param data omitted
80 * provided for <code>buf</code>, this is the size of the buffer that shall be 85 * provided for <code>buf</code>, this is the size of the buffer that shall be
81 * implicitly created 86 * implicitly created
82 * @param n the maximum number of bytes that shall be copied 87 * @param n the maximum number of bytes that shall be copied
83 * @return the total number of bytes copied 88 * @return the total number of bytes copied
84 */ 89 */
85 size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, 90 size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc,
86 char* buf, size_t bufsize, size_t n); 91 char* buf, size_t bufsize, size_t n);
87 92
88 /** 93 /**
89 * Shorthand for ucx_stream_copy using the default copy buffer. 94 * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer.
90 * 95 *
91 * @param src the source stream 96 * @param src the source stream
92 * @param dest the destination stream 97 * @param dest the destination stream
93 * @param rfnc the read function 98 * @param rfnc the read function
94 * @param wfnc the write function 99 * @param wfnc the write function
95 * @return total number of bytes copied 100 * @return total number of bytes copied
96 */ 101 *
97 #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ 102 * @see #UCX_STREAM_COPY_BUFSIZE
98 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, SIZE_MAX) 103 */
99 104 #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\
100 /** 105 src, dest, (read_func)rfnc, (write_func)wfnc, \
101 * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. 106 NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1)
107
108 /**
109 * Shorthand for ucx_stream_bncopy using a default copy buffer.
102 * 110 *
103 * @param src the source stream 111 * @param src the source stream
104 * @param dest the destination stream 112 * @param dest the destination stream
105 * @param rfnc the read function 113 * @param rfnc the read function
106 * @param wfnc the write function 114 * @param wfnc the write function
107 * @param n maximum number of bytes that shall be copied 115 * @param n maximum number of bytes that shall be copied
108 * @return total number of bytes copied 116 * @return total number of bytes copied
109 */ 117 */
110 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ 118 #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\
111 src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) 119 src, dest, (read_func)rfnc, (write_func)wfnc, \
120 NULL, UCX_STREAM_COPY_BUFSIZE, n)
121
122 /**
123 * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer.
124 *
125 * @param src the source stream
126 * @param dest the destination stream
127 * @param rfnc the read function
128 * @param wfnc the write function
129 * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer
130 * shall be implicitly created on the heap
131 * @param bufsize the size of the copy buffer - if <code>NULL</code> was
132 * provided for <code>buf</code>, this is the size of the buffer that shall be
133 * implicitly created
134 * @return total number of bytes copied
135 */
136 #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\
137 src, dest, (read_func)rfnc, (write_func)wfnc, \
138 buf, bufsize, (size_t)-1)
112 139
113 /** 140 /**
114 * Wraps the strcmp function. 141 * Wraps the strcmp function.
115 * @param s1 string one 142 * @param s1 string one
116 * @param s2 string two 143 * @param s2 string two
217 * @param ... additional arguments 244 * @param ... additional arguments
218 * @return a sstr_t containing the formatted string 245 * @return a sstr_t containing the formatted string
219 */ 246 */
220 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); 247 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
221 248
222 /** Shortcut for ucx_asprintf() with default allocator. */
223 #define ucx_sprintf(fmt, ...) \
224 ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__)
225
226 /** 249 /**
227 * <code>va_list</code> version of ucx_asprintf(). 250 * <code>va_list</code> version of ucx_asprintf().
228 * 251 *
229 * @param allocator the UcxAllocator used for allocating the result sstr_t 252 * @param allocator the UcxAllocator used for allocating the result sstr_t
230 * @param fmt format string 253 * @param fmt format string
232 * @return a sstr_t containing the formatted string 255 * @return a sstr_t containing the formatted string
233 * @see ucx_asprintf() 256 * @see ucx_asprintf()
234 */ 257 */
235 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); 258 sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
236 259
237 /** 260 /** Shortcut for ucx_asprintf() with default allocator. */
238 * A <code>printf()</code> like function which writes the output to an 261 #define ucx_sprintf(...) \
262 ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
263
264 /**
265 * A <code>printf()</code> like function which writes the output to a
239 * UcxBuffer. 266 * UcxBuffer.
240 * 267 *
241 * @param buffer the buffer the data is written to 268 * @param buffer the buffer the data is written to
242 * @param ... format string and additional arguments 269 * @param ... format string and additional arguments
243 * @return the total number of bytes written 270 * @return the total number of bytes written

mercurial