You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.3KB

  1. #include "util.hpp"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <random>
  5. namespace rack {
  6. // TODO
  7. // Convert this to xoroshiro128+ or something, and write custom normal dist implementation
  8. static std::random_device rd;
  9. static std::mt19937 rng(rd());
  10. static std::uniform_real_distribution<float> uniformDist;
  11. static std::normal_distribution<float> normalDist;
  12. uint32_t randomu32() {
  13. return rng();
  14. }
  15. float randomf() {
  16. return uniformDist(rng);
  17. }
  18. float randomNormal(){
  19. return normalDist(rng);
  20. }
  21. std::string stringf(const char *format, ...) {
  22. va_list args;
  23. va_start(args, format);
  24. int size = vsnprintf(NULL, 0, format, args);
  25. va_end(args);
  26. if (size < 0)
  27. return "";
  28. std::string s;
  29. s.resize(size);
  30. va_start(args, format);
  31. vsnprintf(&s[0], size+1, format, args);
  32. va_end(args);
  33. return s;
  34. }
  35. std::string ellipsize(std::string s, size_t len) {
  36. if (s.size() <= len)
  37. return s;
  38. else
  39. return s.substr(0, len - 3) + "...";
  40. }
  41. void openBrowser(std::string url) {
  42. // shell injection is possible, so make sure the URL is trusted or hard coded
  43. #if ARCH_LIN
  44. std::string command = "xdg-open " + url;
  45. system(command.c_str());
  46. #endif
  47. #if ARCH_MAC
  48. std::string command = "open " + url;
  49. system(command.c_str());
  50. #endif
  51. #if ARCH_WIN
  52. ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
  53. #endif
  54. }
  55. } // namespace rack