Skip to content

Commit 971787e

Browse files
author
Gin
committed
Retrieve program name from argv and make it available in Globals.h
1 parent 12eee1e commit 971787e

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QFile>
1010
#include <QFileInfo>
1111
#include <QDir>
12+
#include <filesystem>
1213
#include "Globals.h"
1314

1415
namespace PokemonAutomation{
@@ -140,7 +141,8 @@ std::string get_runtime_base_path(){
140141
// QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) returns
141142
// "/Users/$USERNAME/Library/Application Support/SerialPrograms/UserSettings", the parent folder
142143
// to hold application-specific persistent data
143-
QString appSupportPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/SerialPrograms";
144+
QString appSupportPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) +
145+
"/" + QString::fromStdString(PROGRAM_BASENAME());
144146
QDir dir(appSupportPath);
145147
if (!dir.exists()) {
146148
dir.mkpath(".");
@@ -216,5 +218,33 @@ const std::string& ML_MODEL_CACHE_PATH(){
216218
return path;
217219
}
218220

221+
// Program executable path information
222+
namespace {
223+
std::string g_program_absolute_path;
224+
std::string g_program_filename;
225+
std::string g_program_basename;
226+
}
227+
228+
void set_program_path(const char* argv0) {
229+
if (argv0 != nullptr) {
230+
std::filesystem::path program_path(argv0);
231+
g_program_absolute_path = std::filesystem::absolute(program_path).string();
232+
g_program_filename = program_path.filename().string();
233+
g_program_basename = program_path.stem().string();
234+
}
235+
}
236+
237+
const std::string& PROGRAM_ABSOLUTE_PATH() {
238+
return g_program_absolute_path;
239+
}
240+
241+
const std::string& PROGRAM_FILENAME() {
242+
return g_program_filename;
243+
}
244+
245+
const std::string& PROGRAM_BASENAME() {
246+
return g_program_basename;
247+
}
248+
219249
}
220250

SerialPrograms/Source/CommonFramework/Globals.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ extern const std::string COMPILER_VERSION;
4343

4444
extern const size_t LOG_HISTORY_LINES;
4545

46-
// Path to the parent folder that holds all other folders, e.g. settings folder, screenshot folder, etc.
46+
// Path to the parent folder that holds all other folders, e.g. settings folder, screenshot folder, etc.
4747
const std::string& RUNTIME_BASE_PATH();
4848

49+
// Program executable information (retrieved from argv[0])
50+
// These must be initialized by calling set_program_path() from main() before use.
51+
void set_program_path(const char* argv0);
52+
const std::string& PROGRAM_ABSOLUTE_PATH(); // Full absolute path to executable
53+
const std::string& PROGRAM_FILENAME(); // Filename with extension
54+
const std::string& PROGRAM_BASENAME(); // Filename without extension
55+
4956
// Folder path (end with "/") to hold program setting files.
5057
const std::string& SETTINGS_PATH();
5158
// The setting JSON file path. This path is a child of the folder SETTINGS_PATH().

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ int run_program(int argc, char *argv[]){
148148

149149

150150
int main(int argc, char *argv[]){
151+
// Retrieve and store program name
152+
set_program_path(argv[0]);
153+
151154
setup_crash_handler();
152155

153156

0 commit comments

Comments
 (0)