diff options
Diffstat (limited to 'common/xstring.c')
-rw-r--r-- | common/xstring.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/common/xstring.c b/common/xstring.c new file mode 100644 index 0000000..31a363f --- /dev/null +++ b/common/xstring.c @@ -0,0 +1,104 @@ +/* + * AUTHOR + * N. Nielsen + * + * LICENSE + * This software is in the public domain. + * + * The software is provided "as is", without warranty of any kind, + * express or implied, including but not limited to the warranties + * of merchantability, fitness for a particular purpose, and + * noninfringement. In no event shall the author(s) be liable for any + * claim, damages, or other liability, whether in an action of + * contract, tort, or otherwise, arising from, out of, or in connection + * with the software or the use or other dealings in the software. + * + * SUPPORT + * Send bug reports to: <nielsen@memberwebs.com> + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include "compat.h" +#include "xstring.h" + + +/* memins: ------------------------------------------------------------- + * Insert bytes at location + */ +void memins(void* buff, size_t cbuff, const void* ins, size_t cins) +{ + memmove((unsigned char*)buff + cins, buff, cbuff); + memcpy(buff, ins, cins); +} + + +/* memrep: ------------------------------------------------------------ + * Replace a number of bytes at a certain location with yet + * another number of bytes + */ +void* memrep(void* buff, size_t len, size_t cold, const void* ins, size_t cnew) +{ + memmove((unsigned char*)buff + cnew, (unsigned char*)buff + cold, len - cold); + memcpy(buff, ins, cnew); + return (unsigned char*)buff + cnew; +} + + +/* starlen: ----------------------------------------------------------- + * Returns the length of a null terminated string array + */ +size_t starlen(const char* array) +{ + size_t cnt = 0; + while(array[0] || array[1]) + { + array += strlen(array); + cnt++; + } + + return cnt; +} + + +/* starend: ----------------------------------------------------------- + * Gets the end of a string array + */ +char* starend(const char* array) +{ + while(array[0]) + array += strlen(array) + 1; + + return (char*)array; +} + + +/* starnadd: ---------------------------------------------------------- + * Add a value to a string array + */ +char* starnadd(char** parray, const char* str, size_t len) +{ + char* last = starend(*parray); + if(!strrsrv(*parray, (last - *parray) + len + 2)) + return NULL; + + // It's possible we were relocated + last = starend(*parray); + + strncpy(last, str, len); + last += len; + last[0] = last[1] = 0; + return last; +} + + +/* starnext: ---------------------------------------------------------- + * Get the next value in a string array + */ +const char* starnext(const char* prev) +{ + prev += strlen(prev) + 1; + return prev[0] ? prev : NULL; +} + |