Remember last time file

This commit is contained in:
Grayson Riffe 2025-01-21 04:16:51 -06:00
parent 844b32a6fd
commit 1b2ef4240d
4 changed files with 42 additions and 17 deletions

View File

@ -32,7 +32,10 @@ namespace mainspring {
}
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));
@ -174,13 +177,17 @@ namespace mainspring {
return TRUE;
}
case WM_CLOSE:
case WM_CLOSE: {
if (!app->m_file.empty())
app->save();
std::string configPath = app->getConfigPath();
writeFile(configPath.c_str(), app->m_file);
KillTimer(dlg, 1);
DestroyWindow(dlg);
return TRUE;
}
case WM_DESTROY:
PostQuitMessage(0);
@ -190,6 +197,12 @@ namespace mainspring {
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() {
if (m_timing)
return Duration(Clock::now() - m_startTime).count();
@ -228,21 +241,25 @@ namespace mainspring {
SetTimer(m_dlg, IDT_SAVEDTEXT, 3000, nullptr);
}
void Application::open() {
OPENFILENAME ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = m_dlg;
ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
ofn.lpstrDefExt = "mspring";
char fileBuf[MAX_PATH] = {};
ofn.lpstrFile = fileBuf;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST;
void Application::open(std::string path) {
if (path.empty()) {
OPENFILENAME ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = m_dlg;
ofn.lpstrFilter = "Mainspring Time File (*.mspring)\0*.mspring\0\0";
ofn.lpstrDefExt = "mspring";
char fileBuf[MAX_PATH] = {};
ofn.lpstrFile = fileBuf;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
m_file = fileBuf;
if (GetOpenFileName(&ofn))
m_file = fileBuf;
else
return;
}
else
return;
m_file = path;
std::string openData;
if (readFile(m_file.c_str(), openData))

View File

@ -15,11 +15,13 @@ namespace mainspring {
private:
static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
std::string getConfigPath();
Seconds getElapsed(); // Gets the current timing session time
void reset();
void save(bool saveas = false);
void open();
void open(std::string path = "");
const char* m_appName;
const char* m_appVersion;

View File

@ -3,6 +3,7 @@
// IO and strings
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <format>
@ -12,4 +13,5 @@
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <commdlg.h>
#include <commdlg.h>
#include <ShlObj.h>

View File

@ -16,6 +16,10 @@ namespace mainspring {
}
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);
if (!fileStream.is_open())
return false;