-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaths.h
More file actions
87 lines (74 loc) · 2.89 KB
/
Copy pathPaths.h
File metadata and controls
87 lines (74 loc) · 2.89 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
75
76
77
78
79
80
81
82
83
84
85
86
87
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : Paths.h
/// Description : Contains a variety of utility for the filesystem and paths.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------
#pragma once
#include <filesystem>
#include <string>
#include <windows.h>
///----------------------------------------------------------------------------------------------------
/// Path Namespace
///----------------------------------------------------------------------------------------------------
namespace Path
{
///----------------------------------------------------------------------------------------------------
/// GetModule:
/// Returns the path of the specified module.
///----------------------------------------------------------------------------------------------------
inline std::filesystem::path GetModule(HMODULE aModule)
{
char buff[MAX_PATH]{};
GetModuleFileNameA(aModule, buff, MAX_PATH);
return buff;
}
///----------------------------------------------------------------------------------------------------
/// GetSystem:
/// Returns a system path with the specified string appended.
///----------------------------------------------------------------------------------------------------
inline std::filesystem::path GetSystem(const char* aAppend = nullptr)
{
char buff[MAX_PATH]{};
GetSystemDirectoryA(buff, MAX_PATH);
if (!aAppend)
{
/* return just the system path */
return buff;
}
std::filesystem::path path = buff;
path.append(aAppend);
return path;
}
///----------------------------------------------------------------------------------------------------
/// CreateDir:
/// Wrapper function to create directories recursively.
///----------------------------------------------------------------------------------------------------
inline void CreateDir(const std::filesystem::path& aDirectory)
{
if (!std::filesystem::exists(aDirectory))
{
std::filesystem::create_directories(aDirectory);
}
}
///----------------------------------------------------------------------------------------------------
/// GetUnused:
/// Returns an unused path based on the passed one.
/// Increments like: "filename_#.extension".
///----------------------------------------------------------------------------------------------------
inline std::filesystem::path GetUnused(const std::filesystem::path& aPath)
{
std::filesystem::path probe = aPath;
std::filesystem::path dir = aPath.parent_path();
std::string filename = aPath.stem().string();
std::string ext = aPath.extension().string();
int i = 0;
while (std::filesystem::exists(probe))
{
i++;
probe = dir / (filename + std::to_string(i) + ext);
}
return probe;
}
}