11#include <winpr/wtypes.h>
12#include <winpr/error.h>
13#include <winpr/path.h>
15#if defined(DEFINE_UNICODE) && (DEFINE_UNICODE != 0)
17HRESULT PATH_CCH_APPEND(PWSTR pszPath,
size_t cchPath, PCWSTR pszMore)
25 if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
28 const size_t pszMoreLength = _wcsnlen(pszMore, cchPath);
29 const size_t pszPathLength = _wcsnlen(pszPath, cchPath);
31 BOOL pathBackslash = FALSE;
32 if (pszPathLength > 0)
33 pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
35 const BOOL moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
37 if (pathBackslash && moreBackslash)
39 if (pszMoreLength < 1)
42 if ((pszPathLength + pszMoreLength - 1) < cchPath)
44 WCHAR* ptr = &pszPath[pszPathLength];
46 _wcsncat(ptr, &pszMore[1], pszMoreLength - 1);
50 else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
52 if ((pszPathLength + pszMoreLength) < cchPath)
54 WCHAR* ptr = &pszPath[pszPathLength];
56 _wcsncat(ptr, pszMore, pszMoreLength);
60 else if (!pathBackslash && !moreBackslash)
62 if ((pszPathLength + pszMoreLength + 1) < cchPath)
64 WCHAR* ptr = &pszPath[pszPathLength];
66 _wcsncat(ptr, CUR_PATH_SEPARATOR_STR,
67 _wcsnlen(CUR_PATH_SEPARATOR_STR, ARRAYSIZE(CUR_PATH_SEPARATOR_STR)));
68 _wcsncat(ptr, pszMore, pszMoreLength);
73 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
78HRESULT PATH_CCH_APPEND(PSTR pszPath,
size_t cchPath, PCSTR pszMore)
80 BOOL pathBackslash = FALSE;
81 BOOL moreBackslash = FALSE;
89 if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
92 const size_t pszPathLength = strnlen(pszPath, cchPath);
93 if (pszPathLength > 0)
94 pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
96 const size_t pszMoreLength = strnlen(pszMore, cchPath);
97 if (pszMoreLength > 0)
98 moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
100 if (pathBackslash && moreBackslash)
102 if ((pszPathLength + pszMoreLength - 1) < cchPath)
104 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength,
"%s", &pszMore[1]);
108 else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
110 if ((pszPathLength + pszMoreLength) < cchPath)
112 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength,
"%s", pszMore);
116 else if (!pathBackslash && !moreBackslash)
118 if ((pszPathLength + pszMoreLength + 1) < cchPath)
120 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength,
"%s%s",
121 CUR_PATH_SEPARATOR_STR, pszMore);
126 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);