SOMHunter Core
os-utils.hpp
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef OS_UTILS_H_
22 #define OS_UTILS_H_
23 
24 #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
25 # include <Windows.h>
26 #endif // defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
27 
28 #include <filesystem>
29 
30 namespace osutils {
31 
35 inline void setup_terminal() {
36  // Enable ANSII colored output if not enabled by default
37 #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
38  // From: https://superuser.com/a/1529908
39 
40  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
41  DWORD dwMode = 0;
42  GetConsoleMode(hOut, &dwMode);
43  dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
44  SetConsoleMode(hOut, dwMode);
45 
46  // References:
47  // SetConsoleMode() and ENABLE_VIRTUAL_TERMINAL_PROCESSING?
48  // https://stackoverflow.com/questions/38772468/setconsolemode-and-enable-virtual-terminal-processing
49 
50  // Windows console with ANSI colors handling
51  // https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling
52 #endif
53 }
54 
58 inline void print_ISA_capibilites() {
59  SHLOG("ISA capibilites: (does not work on Windows)");
60 
61 #ifdef __SSE__
62  SHLOG("__SSE__: true");
63 #else
64  SHLOG("__SSE__: false");
65 #endif // __SSE__
66 
67 #ifdef __SSE2__
68  SHLOG("__SSE2__: true");
69 #else
70  SHLOG("__SSE2__: false");
71 #endif // __SSE2__
72 
73 #ifdef __SSE3__
74  SHLOG("__SSE3__: true");
75 #else
76  SHLOG("__SSE3__: false");
77 #endif // __SSE3__
78 
79 #ifdef __SSE4_2__
80  SHLOG("__SSE4_2__ : true");
81 #else
82  SHLOG("__SSE4_2__ : false");
83 #endif // __SSE4_2__
84 
85 #ifdef __AVX__
86  SHLOG("__AVX__: true");
87 #else
88  SHLOG("__AVX__: false");
89 #endif // __AVX__
90 
91 #ifdef __AVX2__
92  SHLOG("__AVX2__: true");
93 #else
94  SHLOG("__AVX2__: false");
95 #endif // __AVX2__
96 
97 #ifdef __AVX512BW__
98  SHLOG("__AVX512BW__ : true");
99 #else
100  SHLOG("__AVX512BW__ : false");
101 #endif // __AVX512BW__
102 
103 #ifdef __AVX512CD__
104  SHLOG("__AVX512CD__ : true");
105 #else
106  SHLOG("__AVX512CD__ : false");
107 #endif // __AVX512CD__
108 
109 #ifdef __AVX512DQ__
110  SHLOG("__AVX512DQ__ : true");
111 #else
112  SHLOG("__AVX512DQ__ : false");
113 #endif // __AVX512DQ__
114 
115 #ifdef __AVX512F__
116  SHLOG("__AVX512F__ : true");
117 #else
118  SHLOG("__AVX512F__ : false");
119 #endif // __AVX512F__
120 
121 #ifdef __AVX512VL__
122  SHLOG("__AVX512VL__ : true");
123 #else
124  SHLOG("__AVX512VL__ : false");
125 #endif // __AVX512VL__
126 }
127 
128 inline void cd_back(std::size_t count = 1) {
129  auto path = std::filesystem::current_path();
130 
131  for (std::size_t i{ 0 }; i < count; ++i) {
132  std::filesystem::current_path(path.parent_path());
133  }
134 }
135 
139 inline void initialize_aplication() {
140  setup_terminal();
141  // print_ISA_capibilites();
142 
143  // Change binary directory to the parent one.
144  cd_back();
145  SHLOG_I("The binary is running from the directory " << std::filesystem::current_path() << "...");
146 }
147 
148 inline bool file_exists(const std::string& filepath) { return std::filesystem::exists(filepath); }
149 
150 inline bool dir_exists(const std::string& path) { return std::filesystem::is_directory(path); }
151 
152 inline bool dir_create(const std::string& path) {
153  try {
154  if (!std::filesystem::is_directory(path)) {
155  std::filesystem::create_directories(path);
156  }
157  } catch (...) {
158  return false;
159  }
160 
161  return true;
162 }
163 
164 } // namespace osutils
165 
166 #endif // OS_UTILS_H_
Definition: os-utils.hpp:30
bool dir_create(const std::string &path)
Definition: os-utils.hpp:152
void cd_back(std::size_t count=1)
Definition: os-utils.hpp:128
void print_ISA_capibilites()
Prints available instruction set extensions (not working with MSVC).
Definition: os-utils.hpp:58
bool dir_exists(const std::string &path)
Definition: os-utils.hpp:150
bool file_exists(const std::string &filepath)
Definition: os-utils.hpp:148
void initialize_aplication()
Runs all initialization routines related to the OS.
Definition: os-utils.hpp:139
void setup_terminal()
Setups the attached terminal accordingly.
Definition: os-utils.hpp:35
#define SHLOG_I(x)
Definition: static-logger.hpp:172
#define SHLOG(x)
Undecorated log to the current log stream.
Definition: static-logger.hpp:116