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