Remember last time file
This commit is contained in:
parent
844b32a6fd
commit
1b2ef4240d
@ -32,7 +32,10 @@ namespace mainspring {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Application::run() {
|
void Application::run() {
|
||||||
// Read last time here
|
std::string configPath = getConfigPath();
|
||||||
|
std::string recentFile;
|
||||||
|
if (readFile(configPath.c_str(), recentFile) && !recentFile.empty())
|
||||||
|
open(recentFile);
|
||||||
|
|
||||||
m_dlg = CreateDialogParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast<DLGPROC>(mainDlgProc), reinterpret_cast<LPARAM>(this));
|
m_dlg = CreateDialogParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast<DLGPROC>(mainDlgProc), reinterpret_cast<LPARAM>(this));
|
||||||
|
|
||||||
@ -174,13 +177,17 @@ namespace mainspring {
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WM_CLOSE:
|
case WM_CLOSE: {
|
||||||
if (!app->m_file.empty())
|
if (!app->m_file.empty())
|
||||||
app->save();
|
app->save();
|
||||||
|
|
||||||
|
std::string configPath = app->getConfigPath();
|
||||||
|
writeFile(configPath.c_str(), app->m_file);
|
||||||
|
|
||||||
KillTimer(dlg, 1);
|
KillTimer(dlg, 1);
|
||||||
DestroyWindow(dlg);
|
DestroyWindow(dlg);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
case WM_DESTROY:
|
case WM_DESTROY:
|
||||||
PostQuitMessage(0);
|
PostQuitMessage(0);
|
||||||
@ -190,6 +197,12 @@ namespace mainspring {
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Application::getConfigPath() {
|
||||||
|
char appDataPath[MAX_PATH] = {};
|
||||||
|
SHGetFolderPath(nullptr, CSIDL_APPDATA, nullptr, NULL, appDataPath);
|
||||||
|
return std::string(appDataPath) + "\\" + m_appName + "\\last";
|
||||||
|
}
|
||||||
|
|
||||||
Seconds Application::getElapsed() {
|
Seconds Application::getElapsed() {
|
||||||
if (m_timing)
|
if (m_timing)
|
||||||
return Duration(Clock::now() - m_startTime).count();
|
return Duration(Clock::now() - m_startTime).count();
|
||||||
@ -228,21 +241,25 @@ namespace mainspring {
|
|||||||
SetTimer(m_dlg, IDT_SAVEDTEXT, 3000, nullptr);
|
SetTimer(m_dlg, IDT_SAVEDTEXT, 3000, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::open() {
|
void Application::open(std::string path) {
|
||||||
OPENFILENAME ofn = {};
|
if (path.empty()) {
|
||||||
ofn.lStructSize = sizeof(ofn);
|
OPENFILENAME ofn = {};
|
||||||
ofn.hwndOwner = m_dlg;
|
ofn.lStructSize = sizeof(ofn);
|
||||||
ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
|
ofn.hwndOwner = m_dlg;
|
||||||
ofn.lpstrDefExt = "mspring";
|
ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
|
||||||
char fileBuf[MAX_PATH] = {};
|
ofn.lpstrDefExt = "mspring";
|
||||||
ofn.lpstrFile = fileBuf;
|
char fileBuf[MAX_PATH] = {};
|
||||||
ofn.nMaxFile = MAX_PATH;
|
ofn.lpstrFile = fileBuf;
|
||||||
ofn.Flags = OFN_FILEMUSTEXIST;
|
ofn.nMaxFile = MAX_PATH;
|
||||||
|
ofn.Flags = OFN_FILEMUSTEXIST;
|
||||||
|
|
||||||
if (GetOpenFileName(&ofn))
|
if (GetOpenFileName(&ofn))
|
||||||
m_file = fileBuf;
|
m_file = fileBuf;
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return;
|
m_file = path;
|
||||||
|
|
||||||
std::string openData;
|
std::string openData;
|
||||||
if (readFile(m_file.c_str(), openData))
|
if (readFile(m_file.c_str(), openData))
|
||||||
|
@ -15,11 +15,13 @@ namespace mainspring {
|
|||||||
private:
|
private:
|
||||||
static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
|
static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||||
|
|
||||||
|
std::string getConfigPath();
|
||||||
|
|
||||||
Seconds getElapsed(); // Gets the current timing session time
|
Seconds getElapsed(); // Gets the current timing session time
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
void save(bool saveas = false);
|
void save(bool saveas = false);
|
||||||
void open();
|
void open(std::string path = "");
|
||||||
|
|
||||||
const char* m_appName;
|
const char* m_appName;
|
||||||
const char* m_appVersion;
|
const char* m_appVersion;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// IO and strings
|
// IO and strings
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
|
||||||
@ -13,3 +14,4 @@
|
|||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <commdlg.h>
|
#include <commdlg.h>
|
||||||
|
#include <ShlObj.h>
|
@ -16,6 +16,10 @@ namespace mainspring {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool writeFile(const char* filename, const std::string& in) {
|
bool writeFile(const char* filename, const std::string& in) {
|
||||||
|
std::string folder = filename;
|
||||||
|
folder.resize(folder.find_last_of('\\'));
|
||||||
|
if (!std::filesystem::exists(folder))
|
||||||
|
std::filesystem::create_directories(folder);
|
||||||
std::ofstream fileStream(filename, std::ios::binary | std::ios::trunc);
|
std::ofstream fileStream(filename, std::ios::binary | std::ios::trunc);
|
||||||
if (!fileStream.is_open())
|
if (!fileStream.is_open())
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user