universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@103: */ universe@103: universe@135: /** universe@135: * @file utils.h universe@135: * olaf@147: * Compare, copy and printf functions. universe@135: * universe@135: * @author Mike Becker universe@135: * @author Olaf Wintermann universe@135: */ universe@135: olaf@120: #ifndef UCX_UTILS_H universe@150: #define UCX_UTILS_H universe@89: universe@150: #ifdef __cplusplus universe@89: extern "C" { universe@89: #endif universe@89: universe@259: #include "ucx.h" universe@259: #include "string.h" universe@259: #include "allocator.h" universe@218: #include universe@89: #include olaf@142: #include universe@224: universe@224: /** universe@224: * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy(). universe@224: */ universe@222: #define UCX_STREAM_COPY_BUFSIZE 4096 universe@89: universe@89: /** universe@94: * Copies a string. universe@94: * @param s the string to copy universe@94: * @param data omitted universe@94: * @return a pointer to a copy of s1 that can be passed to free(void*) universe@94: */ universe@244: void *ucx_strcpy(const void *s, void *data); universe@94: universe@94: /** universe@94: * Copies a memory area. universe@94: * @param m a pointer to the memory area universe@94: * @param n a pointer to the size_t containing the size of the memory area universe@94: * @return a pointer to a copy of the specified memory area that can universe@94: * be passed to free(void*) universe@94: */ universe@244: void *ucx_memcpy(const void *m, void *n); universe@94: universe@140: universe@140: /** universe@140: * Reads data from a stream and writes it to another stream. universe@140: * universe@140: * @param src the source stream universe@140: * @param dest the destination stream universe@140: * @param rfnc the read function universe@140: * @param wfnc the write function universe@140: * @param buf a pointer to the copy buffer or NULL if a buffer universe@140: * shall be implicitly created on the heap universe@140: * @param bufsize the size of the copy buffer - if NULL was universe@140: * provided for buf, this is the size of the buffer that shall be universe@140: * implicitly created universe@140: * @param n the maximum number of bytes that shall be copied universe@140: * @return the total number of bytes copied universe@140: */ universe@222: size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc, universe@140: char* buf, size_t bufsize, size_t n); universe@140: universe@140: /** universe@222: * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer. universe@140: * universe@140: * @param src the source stream universe@140: * @param dest the destination stream universe@140: * @param rfnc the read function universe@140: * @param wfnc the write function universe@140: * @return total number of bytes copied universe@222: * universe@222: * @see #UCX_STREAM_COPY_BUFSIZE universe@140: */ universe@222: #define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\ universe@222: src, dest, (read_func)rfnc, (write_func)wfnc, \ universe@222: NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1) universe@140: universe@140: /** universe@222: * Shorthand for ucx_stream_bncopy using a default copy buffer. universe@140: * universe@140: * @param src the source stream universe@140: * @param dest the destination stream universe@140: * @param rfnc the read function universe@140: * @param wfnc the write function universe@140: * @param n maximum number of bytes that shall be copied universe@140: * @return total number of bytes copied universe@140: */ universe@222: #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\ universe@222: src, dest, (read_func)rfnc, (write_func)wfnc, \ universe@222: NULL, UCX_STREAM_COPY_BUFSIZE, n) universe@222: universe@222: /** universe@222: * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer. universe@222: * universe@222: * @param src the source stream universe@222: * @param dest the destination stream universe@222: * @param rfnc the read function universe@222: * @param wfnc the write function universe@222: * @param buf a pointer to the copy buffer or NULL if a buffer universe@222: * shall be implicitly created on the heap universe@222: * @param bufsize the size of the copy buffer - if NULL was universe@222: * provided for buf, this is the size of the buffer that shall be universe@222: * implicitly created universe@222: * @return total number of bytes copied universe@222: */ universe@222: #define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\ universe@222: src, dest, (read_func)rfnc, (write_func)wfnc, \ universe@222: buf, bufsize, (size_t)-1) universe@140: universe@94: /** universe@89: * Wraps the strcmp function. universe@89: * @param s1 string one universe@89: * @param s2 string two universe@89: * @param data omitted universe@89: * @return the result of strcmp(s1, s2) universe@89: */ universe@308: int ucx_cmp_str(const void *s1, const void *s2, void *data); universe@89: universe@89: /** universe@89: * Wraps the strncmp function. universe@89: * @param s1 string one universe@89: * @param s2 string two universe@89: * @param n a pointer to the size_t containing the third strncmp parameter universe@89: * @return the result of strncmp(s1, s2, *n) universe@89: */ universe@309: int ucx_cmp_strn(const void *s1, const void *s2, void *n); universe@89: universe@89: /** universe@292: * Wraps the sstrcmp function. universe@292: * @param s1 sstr one universe@292: * @param s2 sstr two universe@292: * @param data ignored universe@292: * @return the result of sstrcmp(s1, s2) universe@292: */ universe@310: int ucx_cmp_sstr(const void *s1, const void *s2, void *data); universe@292: universe@292: /** universe@89: * Compares two integers of type int. universe@89: * @param i1 pointer to integer one universe@89: * @param i2 pointer to integer two universe@89: * @param data omitted universe@89: * @return -1, if *i1 is less than *i2, 0 if both are equal, universe@89: * 1 if *i1 is greater than *i2 universe@89: */ universe@314: int ucx_cmp_int(const void *i1, const void *i2, void *data); universe@89: universe@89: /** universe@285: * Compares two integers of type long int. universe@285: * @param i1 pointer to long integer one universe@285: * @param i2 pointer to long integer two universe@285: * @param data omitted universe@285: * @return -1, if *i1 is less than *i2, 0 if both are equal, universe@285: * 1 if *i1 is greater than *i2 universe@285: */ universe@314: int ucx_cmp_longint(const void *i1, const void *i2, void *data); universe@285: universe@286: universe@286: /** universe@286: * Distance function for integers of type int. universe@286: * @param i1 pointer to integer one universe@286: * @param i2 pointer to integer two universe@286: * @param data omitted universe@286: * @return i1 minus i2 universe@286: */ universe@314: intmax_t ucx_dist_int(const void *i1, const void *i2, void *data); universe@286: universe@286: /** universe@286: * Distance function for integers of type long int. universe@286: * @param i1 pointer to long integer one universe@286: * @param i2 pointer to long integer two universe@286: * @param data omitted universe@286: * @return i1 minus i2 universe@286: */ universe@314: intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data); universe@286: universe@285: /** universe@92: * Compares two real numbers of type float. universe@92: * @param f1 pointer to float one universe@92: * @param f2 pointer to float two universe@138: * @param data if provided: a pointer to precision (default: 1e-6f) universe@92: * @return -1, if *f1 is less than *f2, 0 if both are equal, universe@92: * 1 if *f1 is greater than *f2 universe@92: */ universe@92: universe@313: int ucx_cmp_float(const void *f1, const void *f2, void *data); universe@92: universe@92: /** universe@92: * Compares two real numbers of type double. universe@138: * @param d1 pointer to double one universe@138: * @param d2 pointer to double two universe@138: * @param data if provided: a pointer to precision (default: 1e-14) universe@92: * @return -1, if *d1 is less than *d2, 0 if both are equal, universe@92: * 1 if *d1 is greater than *d2 universe@92: */ universe@313: int ucx_cmp_double(const void *d1, const void *d2, void *data); universe@92: universe@92: /** universe@89: * Compares two pointers. universe@89: * @param ptr1 pointer one universe@89: * @param ptr2 pointer two universe@89: * @param data omitted universe@89: * @return -1 if ptr1 is less than ptr2, 0 if both are equal, universe@89: * 1 if ptr1 is greater than ptr2 universe@89: */ universe@312: int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data); universe@89: universe@91: /** universe@91: * Compares two memory areas. universe@91: * @param ptr1 pointer one universe@91: * @param ptr2 pointer two universe@91: * @param n a pointer to the size_t containing the third parameter for memcmp universe@91: * @return the result of memcmp(ptr1, ptr2, *n) universe@91: */ universe@311: int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n); universe@91: olaf@142: /** universe@146: * A printf() like function which writes the output to a stream by universe@146: * using a write_func(). universe@146: * @param stream the stream the data is written to universe@146: * @param wfc the write function olaf@142: * @param fmt format string olaf@142: * @param ... additional arguments olaf@142: * @return the total number of bytes written olaf@142: */ olaf@142: int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); olaf@142: olaf@142: /** universe@146: * va_list version of ucx_fprintf(). universe@146: * @param stream the stream the data is written to universe@146: * @param wfc the write function olaf@142: * @param fmt format string olaf@142: * @param ap argument list olaf@142: * @return the total number of bytes written olaf@142: * @see ucx_fprintf() olaf@142: */ olaf@142: int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); olaf@142: olaf@142: /** universe@146: * A printf() like function which allocates space for a sstr_t universe@146: * the result is written to. olaf@142: * universe@146: * Attention: The sstr_t data is allocated with the allocators universe@146: * ucx_allocator_malloc() function. So it is implementation dependent, if universe@146: * the returned sstr_t.ptr pointer must be passed to the allocators universe@146: * ucx_allocator_free() function manually. olaf@142: * universe@146: * Note: The sstr_t.ptr of the return value will always be universe@146: * NULL-terminated. olaf@142: * universe@146: * @param allocator the UcxAllocator used for allocating the result sstr_t olaf@142: * @param fmt format string olaf@142: * @param ... additional arguments universe@146: * @return a sstr_t containing the formatted string olaf@142: */ olaf@142: sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); olaf@142: olaf@142: /** universe@146: * va_list version of ucx_asprintf(). universe@146: * universe@146: * @param allocator the UcxAllocator used for allocating the result sstr_t olaf@142: * @param fmt format string olaf@142: * @param ap argument list universe@146: * @return a sstr_t containing the formatted string universe@146: * @see ucx_asprintf() olaf@142: */ olaf@142: sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); olaf@142: universe@223: /** Shortcut for ucx_asprintf() with default allocator. */ universe@223: #define ucx_sprintf(...) \ universe@223: ucx_asprintf(ucx_default_allocator(), __VA_ARGS__) universe@223: olaf@147: /** universe@225: * A printf() like function which writes the output to a olaf@147: * UcxBuffer. olaf@147: * olaf@147: * @param buffer the buffer the data is written to olaf@147: * @param ... format string and additional arguments olaf@147: * @return the total number of bytes written olaf@147: * @see ucx_fprintf() olaf@147: */ olaf@147: #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ olaf@147: (write_func)ucx_buffer_write, __VA_ARGS__) olaf@147: universe@150: #ifdef __cplusplus universe@89: } universe@89: #endif universe@89: universe@150: #endif /* UCX_UTILS_H */ universe@89: