-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_utils.cpp
More file actions
117 lines (102 loc) · 3.42 KB
/
Copy pathplatform_utils.cpp
File metadata and controls
117 lines (102 loc) · 3.42 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "platform_utils.h"
#include "logging.h"
#include <iostream>
#include <filesystem>
// Platform-specific includes
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#elif __linux__
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
#include <pwd.h>
#endif
namespace PlatformUtils {
const int KEY_UP = 1000;
const int KEY_DOWN = 1001;
const int KEY_ENTER = 10;
std::string getDocumentsPath() {
#ifdef _WIN32
// Windows implementation - your existing code
char* homeDirCStr = nullptr;
size_t len = 0;
if (_dupenv_s(&homeDirCStr, &len, "USERPROFILE") == 0 && homeDirCStr) {
std::string documentsPath = std::string(homeDirCStr) + "\\Documents";
free(homeDirCStr); // Important: free the memory allocated by _dupenv_s
return documentsPath;
}
return ""; // Return empty string on error
#elif __linux__
// Linux implementation
const char* homeDir = getenv("HOME");
dbg(LOG_NOTE, "Detected HOME environment variable as: " + std::string(homeDir ? homeDir : "NULL"));
if (homeDir) {
// On most Linux distributions, Documents is in the home directory
std::string documentsPath = std::string(homeDir) + "/Documents";
// Check if Documents directory exists, if not, fall back to home directory
if (std::filesystem::exists(documentsPath)) {
return documentsPath;
}
else {
// Some systems might not have a Documents folder, so return home directory
dbg(LOG_NOTE, "Documents directory does not exist, falling back to home directory.");
return std::string(homeDir);
}
}
// Fallback: try to get user info from password database
struct passwd* pw = getpwuid(getuid());
if (pw && pw->pw_dir) {
std::string documentsPath = std::string(pw->pw_dir) + "/Documents";
if (std::filesystem::exists(documentsPath)) {
return documentsPath;
}
return std::string(pw->pw_dir);
}
return ""; // Return empty string if all methods fail
#else
#error "Unsupported platform"
#endif
}
int getch() {
#ifdef _WIN32
// Windows implementation - use the existing conio.h function
int c = _getch();
if (c == 72) { // Up arrow
return KEY_UP;
}
else if (c == 80) { // Down arrow
return KEY_DOWN;
}
else if (c == 13) { // Enter key
return KEY_ENTER;
}
return c;
#elif __linux__
struct termios oldTermios, newTermios;
int ch;
tcgetattr(STDIN_FILENO, &oldTermios);
newTermios = oldTermios;
newTermios.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newTermios);
ch = getchar();
// Handle arrow keys: ESC[A (up) and ESC[B (down)
if (ch == 27) { // ESC character
getchar(); // consume '['
int arrow = getchar();
if (arrow == 65) { // 'A' - up arrow
ch = KEY_UP;
}
else if (arrow == 66) { // 'B' - down arrow
ch = KEY_DOWN;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldTermios);
return ch;
#else
#error "Unsupported platform"
return -1;
#endif
}
} // namespace PlatformUtils