-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogRoutines.cpp
111 lines (91 loc) · 2.52 KB
/
LogRoutines.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
#include "framework.h"
#include "LogRoutines.h"
#include <Commdlg.h>
/// <summary>
/// Clears the log window.
/// </summary>
/// <param name="hEdit">The handle to the Edit control to clear. </param>
void ClearLog(HWND hEdit)
{
// Clear the log
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)L"LogBazooka - Ready...\n");
}
std::string int_to_hex(int i)
{
std::stringstream stream;
stream << "0x"
<< std::setfill('0') << std::setw(sizeof(int) * 2)
<< std::hex << i;
return stream.str();
}
BOOL FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
void SaveLog(HWND hEdit, HWND MainWindow)
{
// Get the path to save to.
OPENFILENAME ofn;
TCHAR szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = MainWindow;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
GetSaveFileName(&ofn);
// Did we choose a file?
if (ofn.lpstrFile != NULL)
{
// Does that file exist?
if (FileExists(ofn.lpstrFile))
{
// Ask the user if we should overwrite the file?
int msgboxID = MessageBox(
MainWindow,
(LPCWSTR)L"File already exists. Overwrite?",
(LPCWSTR)L"Overwrite?",
MB_ICONWARNING | MB_YESNO
);
// Exit if they say NO.
if (msgboxID == IDNO)
{
return;
}
else {
// Delete the file.
DeleteFile(ofn.lpstrFile);
}
}
// Get the text from the log window.
int len = GetWindowTextLength(hEdit);
TCHAR* buffer = new TCHAR[len + 1];
GetWindowText(hEdit, buffer, len + 1);
// Open the file.
HANDLE hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(MainWindow, L"Error opening file.", L"Error", MB_ICONERROR);
return;
}
// Write the BOM for UTF-16
BYTE bom[] = { 0xFF, 0xFE };
DWORD dwBytesWritten = 0;
WriteFile(hFile, bom, sizeof(bom), &dwBytesWritten, NULL);
// Write the text to the file.
WriteFile(hFile, buffer, len * sizeof(wchar_t), &dwBytesWritten, NULL);
// Close the file.
CloseHandle(hFile);
// Free the buffer.
delete[] buffer;
// Tell the user we saved the file.
MessageBox(MainWindow, L"Log saved.", L"Success", MB_ICONINFORMATION);
}
}