summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/path/include/PathCchAppend.c
blob: a4f58cb5229cf43bb54567aac860fe8397c2da8b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
#define DEFINE_UNICODE		FALSE
#define CUR_PATH_SEPARATOR_CHR	'\\'
#define CUR_PATH_SEPARATOR_STR	"\\"
#define PATH_CCH_APPEND		PathCchAppendA
*/

#if DEFINE_UNICODE

HRESULT PATH_CCH_APPEND(PWSTR pszPath, size_t cchPath, PCWSTR pszMore)
{
	BOOL pathBackslash;
	BOOL moreBackslash;
	size_t pszMoreLength;
	size_t pszPathLength;

	if (!pszPath)
		return E_INVALIDARG;

	if (!pszMore)
		return E_INVALIDARG;

	if (cchPath == 0 || cchPath > PATHCCH_MAX_CCH)
		return E_INVALIDARG;

	pszMoreLength = _wcslen(pszMore);
	pszPathLength = _wcslen(pszPath);

	pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
	moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;

	if (pathBackslash && moreBackslash)
	{
		if ((pszPathLength + pszMoreLength - 1) < cchPath)
		{
			WCHAR* ptr = &pszPath[pszPathLength];
			*ptr = '\0';
			_wcsncat(ptr, &pszMore[1], _wcslen(&pszMore[1]));
			return S_OK;
		}
	}
	else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
	{
		if ((pszPathLength + pszMoreLength) < cchPath)
		{
			WCHAR* ptr = &pszPath[pszPathLength];
			*ptr = '\0';
			_wcsncat(ptr, pszMore, _wcslen(pszMore));
			return S_OK;
		}
	}
	else if (!pathBackslash && !moreBackslash)
	{
		if ((pszPathLength + pszMoreLength + 1) < cchPath)
		{
			const WCHAR sep[] = CUR_PATH_SEPARATOR_STR;
			WCHAR* ptr = &pszPath[pszPathLength];
			*ptr = '\0';
			_wcsncat(ptr, sep, _wcslen(sep));
			_wcsncat(ptr, pszMore, _wcslen(pszMore));
			return S_OK;
		}
	}

	return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
}

#else

HRESULT PATH_CCH_APPEND(PSTR pszPath, size_t cchPath, PCSTR pszMore)
{
	BOOL pathBackslash = FALSE;
	BOOL moreBackslash = FALSE;
	size_t pszMoreLength;
	size_t pszPathLength;

	if (!pszPath)
		return E_INVALIDARG;

	if (!pszMore)
		return E_INVALIDARG;

	if (cchPath == 0 || cchPath > PATHCCH_MAX_CCH)
		return E_INVALIDARG;

	pszPathLength = strlen(pszPath);
	if (pszPathLength > 0)
		pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;

	pszMoreLength = strlen(pszMore);
	if (pszMoreLength > 0)
		moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;

	if (pathBackslash && moreBackslash)
	{
		if ((pszPathLength + pszMoreLength - 1) < cchPath)
		{
			sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]);
			return S_OK;
		}
	}
	else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
	{
		if ((pszPathLength + pszMoreLength) < cchPath)
		{
			sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore);
			return S_OK;
		}
	}
	else if (!pathBackslash && !moreBackslash)
	{
		if ((pszPathLength + pszMoreLength + 1) < cchPath)
		{
			sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, CUR_PATH_SEPARATOR_STR "%s",
			          pszMore);
			return S_OK;
		}
	}

	return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
}

#endif

/*
#undef DEFINE_UNICODE
#undef CUR_PATH_SEPARATOR_CHR
#undef CUR_PATH_SEPARATOR_STR
#undef PATH_CCH_APPEND
*/