Debian Codegraph @

src/windows/FileUtils.cpp

raw
1 /*
2 * Copyright (C) 2005-2020 Team Kodi (https://kodi.tv)
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSE.md for more information.
6 */
7
8 #include "../FileUtils.h"
9 #include <memory>
10 #include <string>
11 #include "../utils.h"
12 #ifdef TARGET_WINDOWS_DESKTOP
13 #include <Shlobj.h>
14 #endif
15
16 #include <windows.h>
17 #include <fileapi.h>
18
19 std::wstring ToW(const char* str, size_t length)
20 {
21 int result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, nullptr, 0);
22 if (result == 0)
23 return std::wstring();
24
25 auto newStr = std::make_unique<wchar_t[]>(result);
26 result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, newStr.get(), result);
27
28 if (result == 0)
29 return std::wstring();
30
31 return std::wstring(newStr.get(), result);
32 }
33
34 namespace OS
35 {
36 bool CFile::Exists(const std::string& strFileName, long* errCode)
37 {
38 std::string strWinFile = ToWindowsPath(strFileName);
39 std::wstring strWFile = ToW(strWinFile.c_str(), -1);
40 DWORD dwAttr = GetFileAttributesW(strWFile.c_str());
41
42 if(dwAttr != 0xffffffff)
43 {
44 return true;
45 }
46
47 if (errCode)
48 *errCode = GetLastError();
49
50 return false;
51 }
52
53 #ifdef TARGET_WINDOWS_DESKTOP
54 /**
55 * Return the location of the Program Data folder
56 */
57 bool GetProgramData(std::string& programData)
58 {
59 LPWSTR wszPath = NULL;
60 if (SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &wszPath) != S_OK)
61 {
62 return false;
63 }
64 std::wstring wPath = wszPath;
65 CoTaskMemFree(wszPath);
66 programData = WStringToString(wPath);
67
68 return true;
69 }
70
71 #endif
72 }