summaryrefslogtreecommitdiff
path: root/tools/MakeDef/lib_str.cpp
blob: d573c33b08e72e41586550298bdbb2ce9dc820db (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
105
106
107
108
109
110
111
112
113
#include <string.h>

#include "h_util.h"

void placeString(char* pDest, char* pSrc, size_t pLen)
{
	size_t tLen = strlen(pSrc);
	if (tLen < pLen)
		strcpy(pDest, pSrc);
	else {
		memcpy(pDest, pSrc, pLen);
		pDest[pLen] = EOS;
	}
}

//================================================================================

char* rtrim(char *string)
{
	if (string == 0)
		return 0;

	char* p = string + strlen(string) - 1;

	while (p >= string) {
		if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
			*p-- = 0;
		else
			break;
	}
	return string;
}

char* ltrim(char* string)
{
	if (string == 0)
		return 0;

	char* p;

	for (p = string; *p && (*p == ' ' || *p == '\t' || *p == '\n'); p++)
		*p = 0;

	return p;
}

char* trim(char* string)
{
	return ltrim(rtrim(string));
}

char* strdel(char* string, size_t len)
{
	size_t slen = strlen(string);
	if (len > slen)
		len = slen;

	char* tempstr = newStr(string + len);
	char* p = strcpy(string, tempstr);
	delete tempstr;
	return p;
}

//================================================================================

char* rightJust(char *string, size_t l)
{
	size_t p = strlen(string) - 1;
	char* s = &(string[p]);

	while (*s == ' ' && p >= 0) { s--; p--; }
	p++;

	string[l] = 0;
	memmove(string + l - p, string, p);
	memset(string, ' ', l - p);

	return string;
}

char* leftJust(char *string, size_t l)
{
	char* s = string;
	size_t p = 0;

	while (*s == ' ' && p < l) { s++; p++; }

	strncpy(string, s, l - p + 1);

	return string;
}

const size_t maxCenterStrLen = 256;

char* centerStr(char* dst, const char* src, size_t len)
{
	size_t l = strlen(src);
	if (l > maxCenterStrLen)
		return strcpy(dst, src);

	char str[maxCenterStrLen + 1];
	char* tmpPtr = trim(strcpy(str, src));
	size_t sLen = strlen(tmpPtr);

	if (sLen < len && len != 0) {
		memset(dst, ' ', len);
		memcpy(dst + (len - sLen) / 2, tmpPtr, sLen);
	}
	else memcpy(dst, tmpPtr, sLen);

	dst[len] = 0;
	return dst;
}