-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
197 lines (158 loc) · 5.96 KB
/
common.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "common.h"
const wchar_t szProgramName[] = L"Xbox Controller Button Remapper";
bool debug = false;
std::wstring folder_path = L"";
std::wstring filename = L"";
std::wstring file_path = L"";
std::wstring captures_location = L"C:\\Screenshots";
bool capture_border = true;
bool capture_cursor = false;
void message(const LPCWSTR&& text)
{
MessageBox(NULL, text, szProgramName, MB_OK);
}
void error(const LPCWSTR&& text)
{
MessageBox(NULL, text, L"Error", MB_OK);
}
void fatal_error(const LPCWSTR&& text)
{
MessageBox(NULL, text, L"Fatal Error", MB_OK);
exit(1);
}
std::string get_date_time()
{
std::time_t t = std::time(nullptr);
char date_time[20];
std::strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
return date_time;
}
// Source: https://gist.github.com/utilForever/fdf1540cea0de65cfc0a1a69d8cafb63
// Replace some pattern in std::wstring with another pattern
std::wstring replace_wstring_with_pattern(__in const std::wstring& message, __in const std::wstring& pattern, __in const std::wstring& replace)
{
std::wstring result = message;
std::wstring::size_type pos = 0;
std::wstring::size_type offset = 0;
while ((pos = result.find(pattern, offset)) != std::wstring::npos)
{
result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
offset = pos + replace.size();
}
return result;
}
const std::wstring forbidden_chars = L"\\/:*?\"<>|";
bool is_forbidden(wchar_t c)
{
return std::wstring::npos != forbidden_chars.find(c);
}
// Source: https://gist.github.com/danzek/d6a0e4a48a5439e7f808ed1497f6268e
std::wstring string_to_wstring(const std::string& str)
{
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
// Source: https://stackoverflow.com/a/217605
void ltrim(std::wstring& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
{
return !std::isspace(ch);
}));
}
void rtrim(std::wstring& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
{
return !std::isspace(ch);
}).base(), s.end());
}
void trim(std::wstring& s)
{
rtrim(s);
ltrim(s);
}
/*const std::string currentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[20];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %H-%M-%S", &tstruct);
return buf;
}*/
/*const std::wstring get_current_date_time()
{
time_t now = time(0);
struct tm tstruct;
wchar_t buf[40];
tstruct = *localtime(&now);
wcsftime(buf, (sizeof(buf) / sizeof(wchar_t)), L"%Y-%m-%d %H-%M-%S", &tstruct);
return buf;
}*/
/*std::wstring get_current_date_time2()
{
SYSTEMTIME ltime;
wchar_t TimeStamp[30];
GetLocalTime(<ime);
swprintf(TimeStamp, (sizeof(TimeStamp) / sizeof(wchar_t)), L"%d-%02d-%02d %02d-%02d-%02d-%03d", ltime.wYear, ltime.wMonth, ltime.wDay, ltime.wHour, ltime.wMinute, ltime.wSecond, ltime.wMilliseconds);
return TimeStamp;
}*/
/*std::string get_current_date_time3()
{
std::string date_time = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff");
return date_time;
}*/
void set_file_path(std::wstring extension)
{
HWND foreground = GetForegroundWindow();
WCHAR window_title[256];
GetWindowText(foreground, window_title, _countof(window_title));
std::wstring window_title2(window_title);
//window_title2 = L"Test\\/:*?\"<>|";
//window_title2 = replace_wstring_with_pattern(window_title2, L":", L"꞉"); // Replace forbidden character with allowed character that looks similar
window_title2 = replace_wstring_with_pattern(window_title2, L":", L" -");
std::replace_if(window_title2.begin(), window_title2.end(), is_forbidden, '_');
trim(window_title2);
if (window_title2.empty())
{
window_title2 = L"Screenshot"; // Default title
}
//LPWSTR wnd_title{};
//HWND hWnd = GetForegroundWindow(); // Get handle of currently active window
//GetWindowText(hWnd, wnd_title, sizeof(wnd_title));
/*TCHAR current_path[MAX_PATH];
GetModuleFileName(NULL, current_path, MAX_PATH);
//PathRemoveFileSpec(current_path);
PathCchRemoveFileSpec(current_path, MAX_PATH);*/
/*std::string s1 = std::format("{:%F %T}", std::chrono::system_clock::now());
std::string s2 = std::format("{:%F %T %Z}", std::chrono::system_clock::now());
std::string s3 = std::format("{:%F %T %Z}", std::chrono::zoned_time{ std::chrono::current_zone(), std::chrono::system_clock::now() });*/
std::string datetime = std::format("{:%Y-%m-%d %H-%M-%S}", std::chrono::zoned_time{ std::chrono::current_zone(), std::chrono::system_clock::now() });
//std::time_t t = std::time(nullptr);
//auto time = std::chrono::system_clock::now();
//std::string datetime0 = fmt::format("{:%Y-%m-%d %H-%M-%S}", fmt::localtime(t));
//std::string datetime = fmt::format("{:%Y-%m-%d %H-%M-%S}", std::chrono::system_clock::now());
//std::string datetime = get_current_date_time();
//std::wstring datetime2 = std::filesystem::path(datetime).wstring(); // Convert from string to wstring
std::wstring datetime2 = string_to_wstring(datetime); // Convert from string to wstring
//std::wstring datetime2 = get_current_date_time2();
datetime2 = datetime2.substr(0, datetime2.find('.') + 4); // Keep last 3 digits only
std::replace(datetime2.begin(), datetime2.end(), '.', '-');
//std::wstring file_path = std::wstring(captures_location) + L"\\" + window_title2 + L" Screenshot " + datetime2 + extension;
folder_path = std::wstring(captures_location) + L"\\" + window_title2;
if (!std::filesystem::exists(folder_path)) // Check if folder exists
{
if (!std::filesystem::create_directories(folder_path)) // Create folder
{
if (debug)
{
SDL_Log("%s: Could not create folder for screenshot.\n", get_date_time().c_str());
}
return;
}
}
filename = window_title2 + L" " + datetime2 + extension;
file_path = folder_path + L"\\" + filename;
}