UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
misc.cpp
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2014-2024 CESNET
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, is permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of CESNET nor the names of its contributors may be
22 * used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
27 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
29 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "utils/misc.h"
40
41#ifdef _WIN32
42#include <windows.h> // for GetModuleHandle, GetProcAddress
43#else
44#include <unistd.h> // for sysconf, _SC_NPROCESSORS_ONLN
45#endif
46#include <cassert>
47#include <cctype> // for toupper
48#include <cerrno>
49#include <climits>
50#include <cmath>
51#include <cstdio> // for perror, snprintf
52#include <cstdlib> // for strtod
53#include <cstring>
54#include <sstream>
55#include <stdexcept>
56#include <string>
57
58#include "compat/strings.h" // sterror_s
59#include "debug.h"
60#include "utils/macros.h"
61#include "utils/color_out.h"
62
63#ifdef __APPLE__
64#include <sys/types.h>
65#include <sys/sysctl.h>
66#endif
67
68using std::invalid_argument;
69using std::out_of_range;
70using std::stoll;
71
80long long
81unit_evaluate(const char *str, const char **endptr)
82{
83 double ret = unit_evaluate_dbl(str, false, endptr);
84
85 if (ret == NAN || ret >= nexttoward((double) LLONG_MAX, LLONG_MAX)) {
86 return LLONG_MIN;
87 }
88
89 return ret;
90}
91
101double
102unit_evaluate_dbl(const char *str, bool case_sensitive, const char **endptr)
103{
104 char *endptr_tmp = nullptr;
105 errno = 0;
106 double ret = strtod(str, &endptr_tmp);
107 if (errno != 0) {
108 perror("strtod");
109 return NAN;
110 }
111 if (endptr_tmp == str) {
112 log_msg(LOG_LEVEL_ERROR, "'%s' is not a number\n", str);
113 return NAN;
114 }
115 char unit_prefix = case_sensitive ? *endptr_tmp : toupper(*endptr_tmp);
116 endptr_tmp += 1;
117 switch(unit_prefix) {
118 case 'n':
119 case 'N':
120 ret /= 1000'000'000;
121 break;
122 case 'u':
123 case 'U':
124 ret /= 1000'000;
125 break;
126 case 'm':
127 ret /= 1000;
128 break;
129 case 'k':
130 case 'K':
131 ret *= 1000;
132 break;
133 case 'M':
134 ret *= 1000'000LL;
135 break;
136 case 'g':
137 case 'G':
138 ret *= 1000'000'000LL;
139 break;
140 default:
141 endptr_tmp -= 1;
142 }
143
144 if (endptr != nullptr) {
145 *endptr = endptr_tmp;
146 }
147
148 return ret;
149}
150
154const char *format_in_si_units(unsigned long long int val) {
155 const char *si_prefixes[] = { "", "k", "M", "G", "T" };
156 int prefix_idx = 0;
157 int reminder = 0;
158 while (val > 10000) {
159 reminder = val % 1000;
160 val /= 1000;
161 prefix_idx += 1;
162 if (prefix_idx == sizeof si_prefixes / sizeof si_prefixes[0] - 1) {
163 break;
164 }
165 }
166 thread_local char buf[128];
167 snprintf(buf, sizeof buf, "%lld.%d %s", val, reminder / 100, si_prefixes[prefix_idx]);
168 return buf;
169}
170
171
172bool is_wine() {
173#ifdef _WIN32
174 HMODULE hntdll = GetModuleHandle("ntdll.dll");
175 if(!hntdll) {
176 return false;
177 }
178
179 return GetProcAddress(hntdll, "wine_get_version");
180#endif
181 return false;
182}
183
184int get_framerate_n(double fps) {
185 int denominator = get_framerate_d(fps);
186 return round(fps * denominator / 100.0) * 100.0; // rounding to 100s to fix inaccuracy errors
187 // eg. 23.98 * 1001 = 24003.98
188}
189
190int get_framerate_d(double fps) {
191 fps = fps - 0.00001; // we want to round halves down -> base for 10.5 could be 10 rather than 11
192 int fps_rounded_x1000 = round(fps) * 1000;
193 if (fabs(fps * 1001 - fps_rounded_x1000) <
194 fabs(fps * 1000 - fps_rounded_x1000)
195 && fps * 1000 < fps_rounded_x1000) {
196 return 1001;
197 } else {
198 return 1000;
199 }
200}
201
203const char *ug_strerror(int errnum)
204{
205 enum {
206 STRERROR_BUF_LEN = 1024,
207 };
208 static thread_local char strerror_buf[STRERROR_BUF_LEN];
209 strerror_s(strerror_buf, sizeof strerror_buf, errnum); // C11 Annex K (bounds-checking interfaces)
210 return strerror_buf;
211}
212
213void ug_perror(const char *s) {
214 log_msg(LOG_LEVEL_ERROR, "%s: %s\n", s, ug_strerror(errno));
215}
216
219{
220#ifdef _WIN32
221 SYSTEM_INFO sysinfo;
222 GetSystemInfo(&sysinfo);
223 return MIN(sysinfo.dwNumberOfProcessors, INT_MAX);
224#else
225 long numCPU = sysconf(_SC_NPROCESSORS_ONLN);
226 if (numCPU == -1) {
227 perror("sysconf(_SC_NPROCESSORS_ONLN)");
228 return 1;
229 }
230 return MIN(numCPU, INT_MAX);
231#endif
232}
233
242void print_module_usage(const char *module_name, const struct key_val *options, const struct key_val *options_full, bool print_full_help) {
243 UNUSED(options_full), UNUSED(print_full_help);
244 struct key_val nullopts[] = { { nullptr, nullptr } };
245 if (options == nullptr) {
246 options = nullopts;
247 }
248 std::ostringstream oss;
249 oss << TERM_BOLD << TERM_FG_RED << module_name << TERM_FG_RESET;
250 int max_key_len = 0;
251 auto desc_key = [](const char *key) {
252 // print only <val> for key=<val> if right side is "simple" (mandatory and without further opts like "key=<val>:opt=<o>")
253 if (const char *k = strchr(key, '='); k != nullptr && strpbrk(key, "[:") == nullptr) {
254 key = k + 1;
255 }
256 return key;
257 };
258 bool first = true;
259 auto add_opt = [&](const struct key_val *it) {
260 if (first) {
261 oss << "[";
262 first = false;
263 } else {
264 oss << "|";
265 }
266 max_key_len = MAX(max_key_len, (int) strlen(desc_key(it->key)));
267 oss << ":" << it->key;
268 };
269 for (const auto *it = options; it->key != nullptr; ++it) {
270 add_opt(it);
271 }
272 if (print_full_help && options_full) {
273 for (const auto *it = options_full; it->key != nullptr; ++it) {
274 add_opt(it);
275 }
276 }
277 if (!first) { // == has at least one option
278 oss << "] | " << module_name << (options_full ? ":[full]help" : ":help") << TERM_RESET;
279 }
280
281 color_printf("Usage:\n\t%s\n", oss.str().c_str());
282 if (first) { // no opts
283 return;
284 }
285 color_printf("where\n");
286 for (const auto *it = options; it->key != nullptr; ++it) {
287 color_printf("\t" TBOLD("%*s") " - %s\n", max_key_len, desc_key(it->key), it->val);
288 }
289 if (print_full_help && options_full) {
290 for (const auto *it = options_full; it->key != nullptr; ++it) {
291 color_printf("\t" TBOLD("%*s") " - %s\n", max_key_len, desc_key(it->key), it->val);
292 }
293 }
294}
295
296uint32_t parse_uint32(const char *value_str) noexcept(false)
297{
298 size_t pos = 0;
299 long long val = stoll(value_str, &pos);
300 if (val < 0) {
301 throw out_of_range("negative number");
302 }
303 if (val > UINT32_MAX) {
304 throw out_of_range("higher value than range of uint32");
305 }
306 if (pos == 0) {
307 throw invalid_argument("no conversion was performed");
308 }
309 return val;
310}
311
316#ifdef __APPLE__
317 char machine[256];
318 size_t len = sizeof machine;
319 if (sysctlbyname("hw.machine", &machine, &len, NULL, 0) != 0) {
320 perror("sysctlbyname hw.machine");
321 return false;
322 }
323 int proc_translated = -1; // Rosetta2 - 0 off, 1 on; sysctl ENOENT - x86_64 mac
324 len = sizeof proc_translated;
325 if (int ret = sysctlbyname("sysctl.proc_translated", &proc_translated, &len, NULL, 0); ret != 0 && errno != ENOENT) {
326 perror("sysctlbyname sysctl.proc_translated");
327 }
328 return strstr(machine, "arm") != NULL || proc_translated == 1;
329#else
330 return false;
331#endif
332}
333
340bool invalid_arg_is_numeric(const char *what) {
341 if (what == nullptr) {
342 return false;
343 }
344 return strncmp(what, "stoi", 4) == 0 || strncmp(what, "stod", 4) == 0;
345}
346
#define UNUSED(x)
int void * val
int void size_t * len
int color_printf(const char *format,...)
#define TERM_BOLD
Definition color_out.h:48
#define TERM_FG_RED
Definition color_out.h:50
#define TERM_RESET
Definition color_out.h:47
#define TERM_FG_RESET
Definition color_out.h:59
#define TBOLD(x)
Definition color_out.h:70
static __device__ vec3 round(const vec3 &v)
Definition cuda_dxt.cu:122
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MIN(a, b)
Definition macros.h:63
#define MAX(a, b)
Definition macros.h:65
bool invalid_arg_is_numeric(const char *what)
Definition misc.cpp:340
void ug_perror(const char *s)
Definition misc.cpp:213
void print_module_usage(const char *module_name, const struct key_val *options, const struct key_val *options_full, bool print_full_help)
Definition misc.cpp:242
double unit_evaluate_dbl(const char *str, bool case_sensitive, const char **endptr)
Definition misc.cpp:102
int get_framerate_d(double fps)
Definition misc.cpp:190
int get_cpu_core_count(void)
Definition misc.cpp:218
bool is_arm_mac()
Definition misc.cpp:315
long long unit_evaluate(const char *str, const char **endptr)
Definition misc.cpp:81
bool is_wine()
Definition misc.cpp:172
int get_framerate_n(double fps)
Definition misc.cpp:184
const char * ug_strerror(int errnum)
Definition misc.cpp:203
const char * format_in_si_units(unsigned long long int val)
Definition misc.cpp:154
uint32_t parse_uint32(const char *value_str) noexcept(false)
Definition misc.cpp:296
codec_t first
Definition sdl2.c:423
#define strerror_s(buf, bufsz, errnum)
Definition strings.h:66
Definition misc.h:68
const char * key
Definition misc.h:69
Definition rtp.c:253
const char * key
Definition cineform.cpp:125