-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreferences.cpp
More file actions
98 lines (84 loc) · 2.43 KB
/
preferences.cpp
File metadata and controls
98 lines (84 loc) · 2.43 KB
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
#pragma warning(push, 0)
#include <FL/Fl_Preferences.H>
#include <FL/filename.H>
#include <FL/fl_utf8.h>
#pragma warning(pop)
#include "version.h"
#include "preferences.h"
#include "utils.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
Fl_Preferences *Preferences::_preferences = NULL;
static void get_program_file_dir(char *path, size_t n, const char *argv0) {
char name[FL_PATH_MAX] = {};
#ifdef _WIN32
wchar_t buffer[MAX_PATH] = {};
if (GetModuleFileName(NULL, buffer, _countof(buffer))) {
fl_utf8fromwc(name, FL_PATH_MAX, buffer, MAX_PATH);
}
else {
strncpy(name, argv0, FL_PATH_MAX);
}
#else
if (readlink("/proc/self/exe", name, FL_PATH_MAX-1) == -1) {
strncpy(name, argv0, FL_PATH_MAX);
}
#endif
name[FL_PATH_MAX-1] = '\0';
fl_filename_absolute(path, (int)n, name);
char *pivot = strrchr(path, *DIR_SEP);
if (pivot) {
*pivot = '\0';
}
else {
strcpy(path, ".");
}
}
void Preferences::initialize(const char *argv0) {
// Get the directory of crystaltracker.exe
char dirname[FL_PATH_MAX] = {};
get_program_file_dir(dirname, _countof(dirname), argv0);
char prefs[FL_PATH_MAX] = {};
// Use crystaltracker.prefs if it exists
strcpy(prefs, dirname);
strcat(prefs, DIR_SEP PROGRAM_EXE_NAME PREFS_EXT);
if (file_exists(prefs)) {
_preferences = new Fl_Preferences(dirname, PROGRAM_AUTHOR, PROGRAM_EXE_NAME, Fl_Preferences::C_LOCALE);
return;
}
// Use Crystal Tracker.prefs if it exists
strcpy(prefs, dirname);
strcat(prefs, DIR_SEP PROGRAM_NAME PREFS_EXT);
if (file_exists(prefs)) {
_preferences = new Fl_Preferences(dirname, PROGRAM_AUTHOR, PROGRAM_NAME, Fl_Preferences::C_LOCALE);
return;
}
// Use the user's FLTK preferences
_preferences = new Fl_Preferences(Fl_Preferences::USER_L, PROGRAM_AUTHOR, PROGRAM_NAME);
}
void Preferences::close() {
_preferences->flush();
delete _preferences;
_preferences = NULL;
}
int Preferences::get(const char *key, int default_) {
int value;
_preferences->get(key, value, default_);
return value;
}
void Preferences::set(const char *key, int value) {
_preferences->set(key, value);
}
std::string Preferences::get_string(const char *key) {
char *value;
_preferences->get(key, value, "");
std::string s(value ? value : "");
free(value);
return s;
}
void Preferences::set_string(const char *key, const std::string &value) {
_preferences->set(key, value.c_str());
}