summaryrefslogtreecommitdiff
path: root/common/xstring.c
blob: 31a363f2aee11063bb0bbee779b87911f098839f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}