This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preferences.cpp
95 lines (82 loc) · 3.01 KB
/
Preferences.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
#include "Preferences.h"
#include <filesystem>
#include <sstream>
std::string Preferences::GetBookDirectory()
{
// Of bookdir is not loaded into memory, load it.
if (std::strlen(bookDir) == 0) {
std::cout << "Bk_Dir not global, creating global." << std::endl;
std::ifstream settingsFile;
settingsFile.open("C:\\Program Files (x86)\\Kobei\\settings.txt");
std::string a;
std::getline(settingsFile, a);
settingsFile.close();
std::strncpy(bookDir, a.c_str(), sizeof(bookDir));
bookDir[sizeof(bookDir) - 1] = 0;
return a;
}
else // else do not.
return bookDir;
}
void Preferences::CreatePreferences() {
A:
System::Windows::Forms::FolderBrowserDialog^ FileDialog = gcnew System::Windows::Forms::FolderBrowserDialog;
System::Windows::Forms::DialogResult DiagR = FileDialog->ShowDialog();
msclr::interop::marshal_context context;
//Show dialog and get dialog result.
if (DiagR == System::Windows::Forms::DialogResult::OK && !System::String::IsNullOrWhiteSpace(FileDialog->SelectedPath)) {
//Check whether the directory exists or not. Then proceed.
if (CreateDirectory("C:\\Program Files (x86)\\Kobei", NULL) || ERROR_ALREADY_EXISTS == GetLastError()) {
//Path to settings file
std::ofstream setingsFile("C:\\Program Files (x86)\\Kobei\\settings.txt");
//Path to selected directory in managed format.
std::string filePath = context.marshal_as<std::string>(FileDialog->SelectedPath);
setingsFile << filePath << std::endl;
//Finish up.
std::string s1 = std::string(filePath + "\\Books");
CreateDirectory(s1.c_str(), NULL);
setingsFile.close();
std::string s2 = filePath + "\\LastRead.txt";
System::IO::File::Create(gcnew System::String(s2.c_str()))->Close();
}
else {
if (GetLastError() == 5) {
char buffer[1024];
sprintf_s(buffer, "Error %i: Access denied\nPlease rerun the program as administrator.", GetLastError());
MessageBox(NULL, buffer, "Kobei: Preferences", MB_ICONERROR | MB_OK);
}
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
std::wstring ws(buf);
MessageBox(NULL, std::string(ws.begin(), ws.end()).c_str(), "Kobei: Preferences", MB_ICONERROR | MB_OK);
}
}
else
{
MessageBox(NULL, "Failed to read path, retry.", "Kobei: Preferences", MB_ICONERROR | MB_OK);
goto A;
}
delete FileDialog;
}
std::string Preferences::GetFileName()
{
char buffer[MAX_PATH];
GetModuleFileNameA(NULL, buffer, MAX_PATH);
return std::string(buffer);
}
std::string Preferences::GetDirectory()
{
// If the directory is not currently loaded into memory, load it into memory.
if (std::strlen(regDir) == 0) {
std::cout << "Ex_DIR not global, creating global." << std::endl;
std::string f = GetFileName();
f = f.substr(0, f.find_last_of("\\/"));
std::strncpy(regDir, f.c_str(), sizeof(regDir));
regDir[sizeof(regDir) - 1] = 0;
return f;
}
else // else return directory.
return std::string(regDir);
}