-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_storage.cpp
118 lines (101 loc) · 2.91 KB
/
user_storage.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "shlobj.h"
#include "user_storage.h"
BOOL GetApplicationUserDataStorageDirectory(TCHAR* path)
{
// RETRIEVE THE PATH TO THE USER'S LOCAL APPDATA FOLDER.
// The SHGetFolderPath function is deprecated, but is being used to maintain backwards compatibility
// with older operating systems, such as Windows XP and earlier.
const HWND NO_WINDOW_HANDLE_OWNER = nullptr;
const HANDLE NO_ACCESS_TOKEN = nullptr;
const DWORD NO_ADDITIONAL_OPTIONS = 0;
if (!SUCCEEDED(SHGetFolderPath(
NO_WINDOW_HANDLE_OWNER,
CSIDL_LOCAL_APPDATA,
NO_ACCESS_TOKEN,
NO_ADDITIONAL_OPTIONS,
path)))
{
// Return indicating that the path could not be retrieved.
return FALSE;
}
const char* const POPULOUS_EDITOR_USER_DIRECTORY = "\\PopEdt\\";
const size_t POPULOUS_EDITOR_USER_DIRECTORY_LENGTH = strlen(POPULOUS_EDITOR_USER_DIRECTORY);
size_t full_path_length = strlen(path) + POPULOUS_EDITOR_USER_DIRECTORY_LENGTH + 1;
if (full_path_length > MAX_PATH)
{
return FALSE;
}
TCHAR* path_pos = path + strlen(path);
strcpy_s(path_pos, strlen(POPULOUS_EDITOR_USER_DIRECTORY) + 1, POPULOUS_EDITOR_USER_DIRECTORY);
return TRUE;
}
BOOL GetApplicationUserDataFilePath(const TCHAR* file_name, TCHAR* path)
{
if (!GetApplicationUserDataStorageDirectory(path))
{
return FALSE;
}
// Add two characters for the delimiting \ and null-terminator.
size_t full_path_length = strlen(path) + strlen(file_name) + 2;
if (full_path_length > MAX_PATH)
{
return FALSE;
}
strcat_s(path, sizeof(TCHAR) * MAX_PATH, file_name);
return TRUE;
}
BOOL GetUserDocumentsDirectory(TCHAR* path)
{
// RETRIEVE THE PATH TO THE USER'S LOCAL APPDATA FOLDER.
// The SHGetFolderPath function is deprecated, but is being used to maintain backwards compatibility
// with older operating systems, such as Windows XP and earlier.
const HWND NO_WINDOW_HANDLE_OWNER = nullptr;
const HANDLE NO_ACCESS_TOKEN = nullptr;
const DWORD NO_ADDITIONAL_OPTIONS = 0;
if (!SUCCEEDED(SHGetFolderPath(
NO_WINDOW_HANDLE_OWNER,
CSIDL_PERSONAL,
NO_ACCESS_TOKEN,
NO_ADDITIONAL_OPTIONS,
path)))
{
// Return indicating that the path could not be retrieved.
return FALSE;
}
return TRUE;
}
BOOL InitializeUserFiles()
{
TCHAR user_files_path[MAX_PATH];
if (!GetApplicationUserDataStorageDirectory(user_files_path))
{
return FALSE;
}
TCHAR folder[MAX_PATH];
TCHAR *end;
ZeroMemory(folder, MAX_PATH * sizeof(TCHAR));
end = strchr(user_files_path, _T('\\'));
while (end != NULL)
{
strncpy(folder, user_files_path, end - user_files_path + 1);
const size_t DRIVE_LETTER_LENGTH = 3;
bool folder_is_drive_letter = (
DRIVE_LETTER_LENGTH == strlen(folder) &&
(0 == strcmp(folder + 1, _T(":\\"))));
if (folder_is_drive_letter)
{
end = strchr(++end, L'\\');
continue;
}
if (!CreateDirectory(folder, NULL))
{
DWORD err = GetLastError();
if (err != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
}
end = strchr(++end, L'\\');
}
return TRUE;
}