Mana
Loading...
Searching...
No Matches
specialfolder.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2024 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "specialfolder.h"
22
23#ifdef _WIN32
24#include <windows.h>
25#include <stdlib.h>
26
27#ifdef SPECIALFOLDERLOCATION_TEST
28// compile with -DSPECIALFOLDERLOCATION_TEST to get a standalone
29// binary for testing
30#include <iostream>
31#endif
32
33/*
34 * Retrieve the pathname of special folders on Windows, or an empty string
35 * on error.
36 *
37 * See https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
38 * for a list of folder IDs.
39 */
40std::string getSpecialFolderLocation(const KNOWNFOLDERID &folderId)
41{
42 std::string ret;
43 PWSTR widePath = 0;
44
45 HRESULT hr = SHGetKnownFolderPath(folderId, 0, NULL, &widePath);
46 if (hr == S_OK)
47 {
48 // determine needed bytes
49 size_t len;
50 if (wcstombs_s(&len, nullptr, 0, widePath, 0) == 0)
51 {
52 ret.resize(len - 1); // subtract null character
53 if (wcstombs_s(nullptr, ret.data(), len, widePath, len - 1) != 0)
54 ret.clear();
55 }
56 }
57
58 CoTaskMemFree(widePath);
59
60 return ret;
61}
62
63#ifdef SPECIALFOLDERLOCATION_TEST
64int main()
65{
66 std::cout << "RoamingAppData " << getSpecialFolderLocation(FOLDERID_RoamingAppData)
67 << std::endl;
68 std::cout << "Desktop " << getSpecialFolderLocation(FOLDERID_Desktop)
69 << std::endl;
70 std::cout << "LocalAppData " << getSpecialFolderLocation(FOLDERID_LocalAppData)
71 << std::endl;
72 std::cout << "Pictures " << getSpecialFolderLocation(FOLDERID_Pictures)
73 << std::endl;
74 std::cout << "Documents " << getSpecialFolderLocation(FOLDERID_Documents)
75 << std::endl;
76}
77#endif
78#endif // _WIN32
79
80#ifdef __APPLE__
81#include <CoreFoundation/CFBundle.h>
82
83std::string getResourcesLocation()
84{
85 CFBundleRef mainBundle = CFBundleGetMainBundle();
86 CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
87 char path[PATH_MAX];
88 if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path,
89 PATH_MAX))
90 {
91 fprintf(stderr, "Can't find Resources directory\n");
92 }
93 CFRelease(resourcesURL);
94 return path;
95}
96#endif // __APPLE__
int main(int argc, char *argv[])
Definition main.cpp:235
std::string_view path(std::string_view fullFilePath)
Returns the path without the file name.
Definition path.cpp:29