Repository navigation
Expand file tree
/
Copy pathpathcch.cpp
More file actions
74 lines (64 loc) · 1.52 KB
/
Copy pathpathcch.cpp
File metadata and controls
74 lines (64 loc) · 1.52 KB
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
/***********************************************
*
* Implementation of functions from pathcch.h
*
***********************************************/
#include <wchar.h>
#include "pathcch.h"
#include "winerror.h"
HRESULT PathCchRemoveFileSpec(wchar_t* pszPath, size_t cchPath)
{
if (pszPath == nullptr || cchPath == 0)
return E_INVALIDARG;
// This routine is positioned as "safe" so we cannot rely on null-termination and use wcsrchr
wchar_t* slash = nullptr;
for (size_t i = 0; i < cchPath; ++i)
{
if (pszPath[i] == L'\0')
break;
if (pszPath[i] == L'/')
slash = &pszPath[i];
}
if (slash == nullptr)
{
// No path separator at all. In this case Windows return empty string.
*pszPath = L'\0';
}
else if (slash == pszPath)
{
// Nothing to remove
if (slash[1] == L'\0')
return S_FALSE;
slash[1] = L'\0';
}
else
{
*slash = L'\0';
}
return S_OK;
}
HRESULT PathCchAddBackslash(wchar_t* pszPath, size_t cchPath)
{
// On windows this function crashes if pszPath is nullptr. Let's be smarter.
if (pszPath == nullptr)
return E_INVALIDARG;
// This routine is positioned as "safe" so we cannot rely on null-termination
for (size_t i = 0; i < cchPath; ++i)
{
if (pszPath[i] == L'\0')
{
// This is crazy, but Windows really returns S_FALSE on empty string and DON'T add backslash
if (i == 0 || pszPath[i - 1] == L'/')
return S_FALSE;
if (i + 1 < cchPath)
{
pszPath[i++] = L'/';
pszPath[i] = L'\0';
return S_OK;
}
// else
break;
}
}
return E_NOT_SUFFICIENT_BUFFER;
}