forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
67 lines (62 loc) · 1.75 KB
/
Copy pathFileSystem.cpp
File metadata and controls
67 lines (62 loc) · 1.75 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
#include "PCH.h"
#include "FileSystem.h"
#include <utility>
#include <filesystem>
#if WIN32
#include <Windows.h>
#include <shlobj_core.h>
#endif
namespace sh::core
{
SH_CORE_API auto FileSystem::GetDesktopDirectory() -> std::string
{
std::string result{};
#if WIN32
char buf[MAX_PATH];
SHGetSpecialFolderPathA(nullptr, buf, CSIDL_DESKTOP, 0);
result = buf;
#elif __linux
result = GetHomeDirectory();
#endif
return result;
}
SH_CORE_API auto FileSystem::GetHomeDirectory() -> std::string
{
#if WIN32
std::string homeDrive = std::getenv("HOMEDRIVE");
std::string homePath = std::getenv("HOMEPATH");
if (!homeDrive.empty() && !homePath.empty())
{
return homeDrive + homePath;
}
#elif __linux__
std::string homePath = std::getenv("HOME");
if (!homePath.empty())
{
return homePath;
}
#endif
return std::filesystem::current_path().root_directory().string();
}
SH_CORE_API void FileSystem::CreateFolder(const std::filesystem::path& path, std::string_view folderName)
{
std::string name{ folderName };
int num = 0;
while (!std::filesystem::create_directory(path / name))
name = std::string{ folderName } + std::to_string(num++);
}
SH_CORE_API auto FileSystem::CreateUniqueFileName(const std::filesystem::path& path, const std::filesystem::path& _fileName) -> std::string
{
std::string fileName{ _fileName.stem().string() };
std::string extension{ _fileName.extension().string() };
std::string newFileName{ fileName };
std::filesystem::path filePath = path / _fileName;
int idx = 0;
while (std::filesystem::exists(filePath) && std::filesystem::is_regular_file(filePath))
{
newFileName = fileName + std::to_string(idx++);
filePath = path / (newFileName + extension);
}
return newFileName + extension;
}
}//namespace