FreeRDP
Loading...
Searching...
No Matches
PathCchAddExtension.h
1
2/*
3#define DEFINE_UNICODE FALSE
4#define CUR_PATH_SEPARATOR_CHR '\\'
5#define PATH_CCH_ADD_EXTENSION PathCchAddExtensionA
6*/
7
8#if DEFINE_UNICODE
9
10HRESULT PATH_CCH_ADD_EXTENSION(PWSTR pszPath, size_t cchPath, PCWSTR pszExt)
11{
12 LPWSTR pDot;
13 BOOL bExtDot;
14 LPWSTR pBackslash;
15 size_t pszExtLength;
16 size_t pszPathLength;
17
18 if (!pszPath)
19 return E_INVALIDARG;
20
21 if (!pszExt)
22 return E_INVALIDARG;
23
24 pszExtLength = _wcslen(pszExt);
25 pszPathLength = _wcslen(pszPath);
26 bExtDot = (pszExt[0] == '.') ? TRUE : FALSE;
27
28 pDot = _wcsrchr(pszPath, '.');
29 pBackslash = _wcsrchr(pszPath, CUR_PATH_SEPARATOR_CHR);
30
31 if (pDot && pBackslash)
32 {
33 if (pDot > pBackslash)
34 return S_FALSE;
35 }
36
37 if (cchPath > pszPathLength + pszExtLength + ((bExtDot) ? 0 : 1))
38 {
39 const WCHAR dot[] = { '.', '\0' };
40 WCHAR* ptr = &pszPath[pszPathLength];
41 *ptr = '\0';
42
43 if (!bExtDot)
44 _wcsncat(ptr, dot, _wcslen(dot));
45 _wcsncat(ptr, pszExt, pszExtLength);
46
47 return S_OK;
48 }
49
50 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
51}
52
53#else
54
55HRESULT PATH_CCH_ADD_EXTENSION(PSTR pszPath, size_t cchPath, PCSTR pszExt)
56{
57 if (!pszPath)
58 return E_INVALIDARG;
59
60 if (!pszExt)
61 return E_INVALIDARG;
62
63 const size_t pszExtLength = strlen(pszExt);
64 const size_t pszPathLength = strlen(pszPath);
65 const BOOL bExtDot = (pszExt[0] == '.') ? TRUE : FALSE;
66
67 const char* pDot = strrchr(pszPath, '.');
68 const char* pBackslash = strrchr(pszPath, CUR_PATH_SEPARATOR_CHR);
69
70 if (pDot && pBackslash)
71 {
72 if (pDot > pBackslash)
73 return S_FALSE;
74 }
75
76 if (cchPath > pszPathLength + pszExtLength + ((bExtDot) ? 0 : 1))
77 {
78 if (bExtDot)
79 {
80 if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszExt) < 0)
81 return S_FALSE;
82 }
83 else
84 {
85 if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, ".%s", pszExt) < 0)
86 return S_FALSE;
87 }
88
89 return S_OK;
90 }
91
92 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
93}
94
95#endif
96
97/*
98#undef DEFINE_UNICODE
99#undef CUR_PATH_SEPARATOR_CHR
100#undef PATH_CCH_ADD_EXTENSION
101*/