Default to tasks.db in home directory

This commit is contained in:
alejo committed 2026-10-17 03:40:01 -03:00
1 parent b9557789ff
commit 4c69c164e4
5 files changed
+45 -1

No files matched your search

+1
View File
@@ -45,6 +45,7 @@ add_executable(lls
src/Table.cpp src/Table.cpp
src/Terminal.cpp src/Terminal.cpp
src/Tokenizer.cpp src/Tokenizer.cpp
src/HomeDirectory.cpp
) )
target_include_directories(lls PRIVATE include) target_include_directories(lls PRIVATE include)
Regular → Executable
View File
File mode changed.
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <filesystem>
/**
* @brief Retrieves the current user's home directory across different operating systems.
*
* On Windows, it checks the USERPROFILE environment variable.
* On Unix/Linux/macOS, it checks the HOME environment variable, with a fallback
* to checking the password database entry (`getpwuid`) if HOME is unset.
*
* @return std::filesystem::path Path to the user's home directory, or an empty path if unable to resolve.
*/
std::filesystem::path getHomeDirectory();
+29
View File
@@ -0,0 +1,29 @@
#include "HomeDirectory.h"
#include <cstdlib>
#include <filesystem>
#if defined(_WIN32)
// Windows implementation
#elif defined(__unix__) || defined(__APPLE__)
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif
std::filesystem::path getHomeDirectory() {
#if defined(_WIN32)
if (const char* env = std::getenv("USERPROFILE")) {
return std::filesystem::path(env);
}
#else
if (const char* env = std::getenv("HOME")) {
return std::filesystem::path(env);
}
// Fallback for Unix-like systems if HOME environment variable is unset
if (struct passwd* pwd = getpwuid(getuid()); pwd && pwd->pw_dir) {
return std::filesystem::path(pwd->pw_dir);
}
#endif
return {};
}
+2 -1
View File
@@ -2,13 +2,14 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "HomeDirectory.h"
#include "Config.h" #include "Config.h"
#include "Database.h" #include "Database.h"
#include "Display.h" #include "Display.h"
#include "Repl.h" #include "Repl.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
const std::string path = (argc > 1) ? argv[1] : "tasks.db"; const std::string path = (argc > 1) ? argv[1] : (getHomeDirectory() / "tasks.db");
try { try {
Config config; Config config;