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 <chrono>
9#include <cstdlib>
10#include <cstdint>
11#include <cstring>
12#include <string>
13
14#ifdef _WIN32
15#include <windows.h>
16#elif defined(__linux__) || defined(__APPLE__)
17#include <unistd.h>
18#include <sys/types.h>
19#include <pwd.h>
20#endif
21
22namespace consolix {
23
27 inline bool copy_to_clipboard(const std::string& text) {
28# ifdef _WIN32
29 if (OpenClipboard(nullptr)) {
30 EmptyClipboard();
31 const std::size_t size = (text.size() + 1) * sizeof(char);
32 HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
33 if (!hMem) {
34 CloseClipboard();
35 return false;
36 }
37 std::memcpy(GlobalLock(hMem), text.c_str(), size);
38 GlobalUnlock(hMem);
39 SetClipboardData(CF_TEXT, hMem);
40 CloseClipboard();
41 return true;
42 }
43 return false;
44# elif defined(__APPLE__)
45 const std::string command = "echo \"" + text + "\" | pbcopy";
46 return std::system(command.c_str()) == 0;
47# elif defined(__linux__)
48 const std::string command = "echo \"" + text + "\" | xclip -selection clipboard";
49 return std::system(command.c_str()) == 0;
50# else
51 return false;
52# endif
53 }
54
57 inline std::string get_clipboard_text() {
58# ifdef _WIN32
59 if (!OpenClipboard(nullptr)) return "";
60 HANDLE hData = GetClipboardData(CF_TEXT);
61 if (!hData) {
62 CloseClipboard();
63 return "";
64 }
65 char* text = static_cast<char*>(GlobalLock(hData));
66 const std::string result(text ? text : "");
67 GlobalUnlock(hData);
68 CloseClipboard();
69 return result;
70# elif defined(__APPLE__)
71 return std::system("pbpaste") == 0 ? "(clipboard contents)" : "";
72# elif defined(__linux__)
73 return std::system("xclip -selection clipboard -o") == 0 ? "(clipboard contents)" : "";
74# else
75 return "";
76# endif
77 }
78
81 inline std::string get_os_name() {
82# ifdef _WIN32
83 return "Windows";
84# elif defined(__APPLE__)
85 return "macOS";
86# elif defined(__linux__)
87 return "Linux";
88# else
89 return "Unknown OS";
90# endif
91 }
92
95 inline std::uint64_t get_system_time_ms() {
96 return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
97 std::chrono::system_clock::now().time_since_epoch()
98 ).count());
99 }
100
103 inline int get_cpu_count() {
104# ifdef _WIN32
105 SYSTEM_INFO sysinfo;
106 GetSystemInfo(&sysinfo);
107 return static_cast<int>(sysinfo.dwNumberOfProcessors);
108# elif defined(__linux__) || defined(__APPLE__)
109 return static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
110# else
111 return 1;
112# endif
113 }
114
117 inline std::string get_home_directory() {
118# ifdef _WIN32
119 const char* home = std::getenv("USERPROFILE");
120 return home ? std::string(home) : "";
121# elif defined(__linux__) || defined(__APPLE__)
122 struct passwd* pw = getpwuid(getuid());
123 return pw ? std::string(pw->pw_dir) : "";
124# else
125 return "";
126# endif
127 }
128
131 inline std::string get_temp_directory() {
132# ifdef _WIN32
133 char tempPath[MAX_PATH];
134 GetTempPathA(MAX_PATH, tempPath);
135 return std::string(tempPath);
136# elif defined(__linux__) || defined(__APPLE__)
137 const char* temp = std::getenv("TMPDIR");
138 return temp ? std::string(temp) : "/tmp";
139# else
140 return "";
141# endif
142 }
143
147 inline std::string get_env_var(const std::string& var_name) {
148 const char* value = std::getenv(var_name.c_str());
149 return value ? std::string(value) : "";
150 }
151
152}; // namespace consolix
153
154#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.
std::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.