universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@192: * Copyright 2015 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@89: #include "ucx.h" olaf@142: #include "string.h" olaf@142: #include "allocator.h" universe@218: #include universe@89: #include olaf@142: #include 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@94: void *ucx_strcpy(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@94: void *ucx_memcpy(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@140: size_t ucx_stream_copy(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@140: * Shorthand for ucx_stream_copy using the 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: * @return total number of bytes copied universe@140: */ universe@140: #define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ universe@218: src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, (size_t)-1) universe@140: universe@140: /** universe@140: * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. 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@140: #define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ universe@140: src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) 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@89: int ucx_strcmp(void *s1, 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@89: int ucx_strncmp(void *s1, void *s2, void *n); universe@89: universe@89: /** 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@89: int ucx_intcmp(void *i1, void *i2, void *data); universe@89: universe@89: /** 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@92: int ucx_floatcmp(void *f1, 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@92: int ucx_doublecmp(void *d1, 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@89: int ucx_ptrcmp(void *ptr1, 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@91: int ucx_memcmp(void *ptr1, 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: universe@190: /** Shortcut for ucx_asprintf() with default allocator. */ universe@190: #define ucx_sprintf(fmt, ...) \ universe@191: ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) universe@190: 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: olaf@147: /** olaf@147: * A printf() like function which writes the output to an 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: