Consolix
Loading...
Searching...
No Matches
system_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_SYSTEM_UTILS_HPP_INCLUDED
3#define _CONSOLIX_SYSTEM_UTILS_HPP_INCLUDED
4
7
8#include <string>
9#include <cstdlib>
10#include <chrono>
11#ifdef _WIN32
12#include <windows.h>
13#elif defined(__linux__) || defined(__APPLE__)
14#include <unistd.h>
15#include <sys/sysinfo.h>
16#include <sys/types.h>
17#include <pwd.h>
18#endif
19
20namespace consolix {
21
25 bool copy_to_clipboard(const std::string& text) {
26# ifdef _WIN32
27 if (OpenClipboard(nullptr)) {
28 EmptyClipboard();
29 size_t size = (text.size() + 1) * sizeof(char);
30 HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
31 if (!hMem) {
32 CloseClipboard();
33 return false;
34 }
35 memcpy(GlobalLock(hMem), text.c_str(), size);
36 GlobalUnlock(hMem);
37 SetClipboardData(CF_TEXT, hMem);
38 CloseClipboard();
39 return true;
40 }
41 return false;
42# elif defined(__APPLE__)
43 std::string command = "echo \"" + text + "\" | pbcopy";
44 return system(command.c_str()) == 0;
45# elif defined(__linux__)
46 std::string command = "echo \"" + text + "\" | xclip -selection clipboard";
47 return system(command.c_str()) == 0;
48# else
49 return false;
50# endif
51 }
52
55 std::string get_clipboard_text() {
56# ifdef _WIN32
57 if (!OpenClipboard(nullptr)) return "";
58 HANDLE hData = GetClipboardData(CF_TEXT);
59 if (!hData) {
60 CloseClipboard();
61 return "";
62 }
63 char* text = static_cast<char*>(GlobalLock(hData));
64 std::string result(text ? text : "");
65 GlobalUnlock(hData);
66 CloseClipboard();
67 return result;
68# elif defined(__APPLE__)
69 return system("pbpaste") == 0 ? "(clipboard contents)" : "";
70# elif defined(__linux__)
71 return system("xclip -selection clipboard -o") == 0 ? "(clipboard contents)" : "";
72# else
73 return "";
74# endif
75 }
76
79 std::string get_os_name() {
80# ifdef _WIN32
81 return "Windows";
82# elif defined(__APPLE__)
83 return "macOS";
84# elif defined(__linux__)
85 return "Linux";
86# else
87 return "Unknown OS";
88# endif
89 }
90
93 uint64_t get_system_time_ms() {
94 return std::chrono::duration_cast<std::chrono::milliseconds>(
95 std::chrono::system_clock::now().time_since_epoch()
96 ).count();
97 }
98
102# ifdef _WIN32
103 SYSTEM_INFO sysinfo;
104 GetSystemInfo(&sysinfo);
105 return sysinfo.dwNumberOfProcessors;
106# elif defined(__linux__) || defined(__APPLE__)
107 return sysconf(_SC_NPROCESSORS_ONLN);
108# else
109 return 1;
110# endif
111 }
112
115 std::string get_home_directory() {
116# ifdef _WIN32
117 char* home = getenv("USERPROFILE");
118 return home ? std::string(home) : "";
119# elif defined(__linux__) || defined(__APPLE__)
120 struct passwd* pw = getpwuid(getuid());
121 return pw ? std::string(pw->pw_dir) : "";
122# else
123 return "";
124# endif
125 }
126
129 std::string get_temp_directory() {
130# ifdef _WIN32
131 char tempPath[MAX_PATH];
132 GetTempPathA(MAX_PATH, tempPath);
133 return std::string(tempPath);
134# elif defined(__linux__) || defined(__APPLE__)
135 char* temp = getenv("TMPDIR");
136 return temp ? std::string(temp) : "/tmp";
137# else
138 return "";
139# endif
140 }
141
145 std::string get_env_var(const std::string& var_name) {
146 const char* value = getenv(var_name.c_str());
147 return value ? std::string(value) : "";
148 }
149
150}; // namespace consolix
151
152#endif // _CONSOLIX_SYSTEM_UTILS_HPP_INCLUDED
< Utility modules and helpers.
int get_cpu_count()
Gets the number of logical CPU cores.
std::string get_env_var(const std::string &var_name)
Retrieves an environment variable's value.
std::string get_temp_directory()
Gets the system temporary directory path.
bool copy_to_clipboard(const std::string &text)
Copies the given text to the system clipboard.
std::string get_home_directory()
Gets the user's home directory path.
std::string get_clipboard_text()
Retrieves text from the system clipboard.
uint64_t get_system_time_ms()
Gets the current system time in milliseconds.
std::string get_os_name()
Gets the name of the operating system.