3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_UTILS_HPP_INCLUDED
19 #define CARLA_UTILS_HPP_INCLUDED
21 #include "CarlaDefines.h"
29 #ifdef CARLA_PROPER_CPP11_SUPPORT
42 # define WIN32_LEAN_AND_MEAN 1
43 # include <winsock2.h>
49 // --------------------------------------------------------------------------------------------------------------------
53 * Return "true" or "false" according to yesNo.
56 const char* bool2str(const bool yesNo
) noexcept
58 return yesNo
? "true" : "false";
62 * Set a string as empty/null.
65 void nullStrBuf(char* const strBuf
) noexcept
74 void pass() noexcept
{}
76 // --------------------------------------------------------------------------------------------------------------------
77 // string print functions
80 * Internal noexcept-safe fopen function.
83 FILE* __carla_fopen(const char* const filename
, FILE* const fallback
) noexcept
86 if (std::getenv("CARLA_CAPTURE_CONSOLE_OUTPUT") == nullptr)
92 ret
= std::fopen(filename
, "a+");
93 } CARLA_CATCH_UNWIND
catch (...) {}
107 * Print a string to stdout with newline (gray color).
108 * Does nothing if DEBUG is not defined.
111 # define carla_debug(...)
114 void carla_debug(const char* const fmt
, ...) noexcept
116 static FILE* const output
= __carla_fopen("/tmp/carla.debug.log", stdout
);
122 if (output
== stdout
)
125 std::fprintf(output
, "\x1b[37;1m[carla] ");
127 std::fprintf(output
, "\x1b[30;1m[carla] ");
129 std::vfprintf(output
, fmt
, args
);
130 std::fprintf(output
, "\x1b[0m\n");
134 std::fprintf(output
, "[carla] ");
135 std::vfprintf(output
, fmt
, args
);
136 std::fprintf(output
, "\n");
141 } CARLA_CATCH_UNWIND
catch (...) {}
146 * Print a string to stdout with newline.
149 void carla_stdout(const char* const fmt
, ...) noexcept
151 static FILE* const output
= __carla_fopen("/tmp/carla.stdout.log", stdout
);
156 std::fprintf(output
, "[carla] ");
157 std::vfprintf(output
, fmt
, args
);
158 std::fprintf(output
, "\n");
160 if (output
!= stdout
)
164 } CARLA_CATCH_UNWIND
catch (...) {}
168 * Print a string to stderr with newline.
171 void carla_stderr(const char* const fmt
, ...) noexcept
173 static FILE* const output
= __carla_fopen("/tmp/carla.stderr.log", stderr
);
178 std::fprintf(output
, "[carla] ");
179 std::vfprintf(output
, fmt
, args
);
180 std::fprintf(output
, "\n");
182 if (output
!= stderr
)
186 } CARLA_CATCH_UNWIND
catch (...) {}
190 * Print a string to stderr with newline (red color).
193 void carla_stderr2(const char* const fmt
, ...) noexcept
195 static FILE* const output
= __carla_fopen("/tmp/carla.stderr2.log", stderr
);
201 if (output
== stderr
)
203 std::fprintf(output
, "\x1b[31m[carla] ");
204 std::vfprintf(output
, fmt
, args
);
205 std::fprintf(output
, "\x1b[0m\n");
209 std::fprintf(output
, "[carla] ");
210 std::vfprintf(output
, fmt
, args
);
211 std::fprintf(output
, "\n");
216 } CARLA_CATCH_UNWIND
catch (...) {}
219 // --------------------------------------------------------------------------------------------------------------------
220 // carla_safe_assert*
223 * Print a safe assertion error message.
226 void carla_safe_assert(const char* const assertion
, const char* const file
, const int line
) noexcept
228 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion
, file
, line
);
232 * Print a safe assertion error message, with 1 extra signed integer value.
235 void carla_safe_assert_int(const char* const assertion
, const char* const file
,
236 const int line
, const int value
) noexcept
238 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion
, file
, line
, value
);
242 * Print a safe assertion error message, with 1 extra unsigned integer value.
245 void carla_safe_assert_uint(const char* const assertion
, const char* const file
,
246 const int line
, const uint value
) noexcept
248 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion
, file
, line
, value
);
252 * Print a safe assertion error message, with 2 extra signed integer values.
255 void carla_safe_assert_int2(const char* const assertion
, const char* const file
,
256 const int line
, const int v1
, const int v2
) noexcept
258 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion
, file
, line
, v1
, v2
);
262 * Print a safe assertion error message, with 2 extra unsigned integer values.
265 void carla_safe_assert_uint2(const char* const assertion
, const char* const file
,
266 const int line
, const uint v1
, const uint v2
) noexcept
268 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion
, file
, line
, v1
, v2
);
272 * Print a safe assertion error message, with a custom error message.
275 void carla_custom_safe_assert(const char* const message
,
276 const char* const assertion
, const char* const file
, const int line
) noexcept
278 carla_stderr2("Carla assertion failure: %s, condition \"%s\" in file %s, line %i", message
, assertion
, file
, line
);
281 // --------------------------------------------------------------------------------------------------------------------
282 // carla_safe_exception*
285 * Print a safe exception error message.
288 void carla_safe_exception(const char* const exception
, const char* const file
, const int line
) noexcept
290 carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception
, file
, line
);
293 // --------------------------------------------------------------------------------------------------------------------
297 * Set environment variable 'key' to 'value'.
300 void carla_setenv(const char* const key
, const char* const value
) noexcept
302 CARLA_SAFE_ASSERT_RETURN(key
!= nullptr && key
[0] != '\0',);
303 CARLA_SAFE_ASSERT_RETURN(value
!= nullptr,);
307 ::SetEnvironmentVariableA(key
, value
);
308 } CARLA_SAFE_EXCEPTION("carla_setenv");
310 ::setenv(key
, value
, 1);
315 * Unset environment variable 'key'.
318 void carla_unsetenv(const char* const key
) noexcept
320 CARLA_SAFE_ASSERT_RETURN(key
!= nullptr && key
[0] != '\0',);
324 ::SetEnvironmentVariableA(key
, nullptr);
325 } CARLA_SAFE_EXCEPTION("carla_unsetenv");
331 // --------------------------------------------------------------------------------------------------------------------
335 * Custom 'strdup' function.
336 * Returned value is always valid, and must be freed with "delete[] var".
340 const char* carla_strdup(const char* const strBuf
)
342 CARLA_SAFE_ASSERT(strBuf
!= nullptr);
344 const std::size_t bufferLen
= (strBuf
!= nullptr) ? std::strlen(strBuf
) : 0;
345 char* const buffer
= new char[bufferLen
+1];
348 std::memcpy(buffer
, strBuf
, bufferLen
);
350 buffer
[bufferLen
] = '\0';
356 * Custom 'strdup' function.
357 * Calls "std::free(strBuf)".
358 * Returned value is always valid, and must be freed with "delete[] var".
362 const char* carla_strdup_free(char* const strBuf
)
364 const char* const buffer(carla_strdup(strBuf
));
370 * Custom 'strdup' function, safe version.
371 * Returned value may be null. It must be freed with "delete[] var".
374 const char* carla_strdup_safe(const char* const strBuf
) noexcept
376 CARLA_SAFE_ASSERT_RETURN(strBuf
!= nullptr, nullptr);
378 const std::size_t bufferLen
= std::strlen(strBuf
);
382 buffer
= new char[bufferLen
+1];
383 } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
386 std::memcpy(buffer
, strBuf
, bufferLen
);
388 buffer
[bufferLen
] = '\0';
393 // --------------------------------------------------------------------------------------------------------------------
397 * Add array values to another array.
401 void carla_add(T dest
[], const T src
[], const std::size_t count
) noexcept
403 CARLA_SAFE_ASSERT_RETURN(dest
!= nullptr,);
404 CARLA_SAFE_ASSERT_RETURN(src
!= nullptr,);
405 CARLA_SAFE_ASSERT_RETURN(dest
!= src
,);
406 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
408 for (std::size_t i
=0; i
<count
; ++i
)
413 * Add array values to another array, with a multiplication factor.
417 void carla_addWithMultiply(T dest
[], const T src
[], const T
& multiplier
, const std::size_t count
) noexcept
419 CARLA_SAFE_ASSERT_RETURN(dest
!= nullptr,);
420 CARLA_SAFE_ASSERT_RETURN(src
!= nullptr,);
421 CARLA_SAFE_ASSERT_RETURN(dest
!= src
,);
422 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
424 for (std::size_t i
=0; i
<count
; ++i
)
425 dest
[i
] += src
[i
] * multiplier
;
429 * Copy array values to another array.
433 void carla_copy(T dest
[], const T src
[], const std::size_t count
) noexcept
435 CARLA_SAFE_ASSERT_RETURN(dest
!= nullptr,);
436 CARLA_SAFE_ASSERT_RETURN(src
!= nullptr,);
437 CARLA_SAFE_ASSERT_RETURN(dest
!= src
,);
438 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
440 std::memcpy(dest
, src
, count
*sizeof(T
));
444 * Copy array values to another array, with a multiplication factor.
448 void carla_copyWithMultiply(T dest
[], const T src
[], const T
& multiplier
, const std::size_t count
) noexcept
450 CARLA_SAFE_ASSERT_RETURN(dest
!= nullptr,);
451 CARLA_SAFE_ASSERT_RETURN(src
!= nullptr,);
452 CARLA_SAFE_ASSERT_RETURN(dest
!= src
,);
453 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
455 for (std::size_t i
=0; i
<count
; ++i
)
456 dest
[i
] = src
[i
] * multiplier
;
460 * Fill an array with a fixed value.
464 void carla_fill(T data
[], const T
& value
, const std::size_t count
) noexcept
466 CARLA_SAFE_ASSERT_RETURN(data
!= nullptr,);
467 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
471 std::memset(data
, 0, count
*sizeof(T
));
475 for (std::size_t i
=0; i
<count
; ++i
)
481 * Multiply an array with a fixed value.
485 void carla_multiply(T data
[], const T
& multiplier
, const std::size_t count
) noexcept
487 CARLA_SAFE_ASSERT_RETURN(data
!= nullptr,);
488 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
492 std::memset(data
, 0, count
*sizeof(T
));
496 for (std::size_t i
=0; i
<count
; ++i
)
497 data
[i
] *= multiplier
;
502 * Clear a byte array.
505 void carla_zeroBytes(uint8_t bytes
[], const std::size_t count
) noexcept
507 CARLA_SAFE_ASSERT_RETURN(bytes
!= nullptr,);
508 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
510 std::memset(bytes
, 0, count
*sizeof(uint8_t));
514 * Clear a char array.
517 void carla_zeroChars(char chars
[], const std::size_t count
) noexcept
519 CARLA_SAFE_ASSERT_RETURN(chars
!= nullptr,);
520 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
522 std::memset(chars
, 0, count
*sizeof(char));
526 * Clear a pointer array.
530 void carla_zeroPointers(T
* ptrs
[], const std::size_t count
) noexcept
532 CARLA_SAFE_ASSERT_RETURN(ptrs
!= nullptr,);
533 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
535 std::memset(ptrs
, 0, count
*sizeof(T
*));
539 * Clear a single struct.
541 template <typename T
>
543 void carla_zeroStruct(T
& s
) noexcept
545 std::memset(&s
, 0, sizeof(T
));
549 * Clear a struct array.
551 template <typename T
>
553 void carla_zeroStructs(T structs
[], const std::size_t count
) noexcept
555 CARLA_SAFE_ASSERT_RETURN(structs
!= nullptr,);
556 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
558 std::memset(structs
, 0, count
*sizeof(T
));
562 * Copy a single struct.
564 template <typename T
>
566 void carla_copyStruct(T
& dest
, const T
& src
) noexcept
568 std::memcpy(&dest
, &src
, sizeof(T
));
572 * Copy a struct array.
574 template <typename T
>
576 void carla_copyStructs(T dest
[], const T src
[], const std::size_t count
) noexcept
578 CARLA_SAFE_ASSERT_RETURN(dest
!= nullptr,);
579 CARLA_SAFE_ASSERT_RETURN(src
!= nullptr,);
580 CARLA_SAFE_ASSERT_RETURN(dest
!= src
,);
581 CARLA_SAFE_ASSERT_RETURN(count
> 0,);
583 std::memcpy(dest
, src
, count
*sizeof(T
));
586 // --------------------------------------------------------------------------------------------------------------------
588 #endif // CARLA_UTILS_HPP_INCLUDED