FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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 CHAR* pDot;
58 BOOL bExtDot;
59 CHAR* pBackslash;
60 size_t pszExtLength;
61 size_t pszPathLength;
62
63 if (!pszPath)
64 return E_INVALIDARG;
65
66 if (!pszExt)
67 return E_INVALIDARG;
68
69 pszExtLength = strlen(pszExt);
70 pszPathLength = strlen(pszPath);
71 bExtDot = (pszExt[0] == '.') ? TRUE : FALSE;
72
73 pDot = strrchr(pszPath, '.');
74 pBackslash = strrchr(pszPath, CUR_PATH_SEPARATOR_CHR);
75
76 if (pDot && pBackslash)
77 {
78 if (pDot > pBackslash)
79 return S_FALSE;
80 }
81
82 if (cchPath > pszPathLength + pszExtLength + ((bExtDot) ? 0 : 1))
83 {
84 if (bExtDot)
85 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszExt);
86 else
87 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, ".%s", pszExt);
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*/