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.

88 lines
1.7KB

  1. #include "util.hpp"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <random>
  6. #include <libgen.h> // for dirname and basename
  7. #if ARCH_WIN
  8. #include <windows.h>
  9. #include <shellapi.h>
  10. #endif
  11. namespace rack {
  12. // TODO
  13. // Convert this to xoroshiro128+ or something, and write custom normal dist implementation
  14. static std::random_device rd;
  15. static std::mt19937 rng(rd());
  16. static std::uniform_real_distribution<float> uniformDist;
  17. static std::normal_distribution<float> normalDist;
  18. uint32_t randomu32() {
  19. return rng();
  20. }
  21. float randomf() {
  22. return uniformDist(rng);
  23. }
  24. float randomNormal(){
  25. return normalDist(rng);
  26. }
  27. std::string stringf(const char *format, ...) {
  28. va_list args;
  29. va_start(args, format);
  30. int size = vsnprintf(NULL, 0, format, args);
  31. va_end(args);
  32. if (size < 0)
  33. return "";
  34. std::string s;
  35. s.resize(size);
  36. va_start(args, format);
  37. vsnprintf(&s[0], size+1, format, args);
  38. va_end(args);
  39. return s;
  40. }
  41. std::string ellipsize(std::string s, size_t len) {
  42. if (s.size() <= len)
  43. return s;
  44. else
  45. return s.substr(0, len - 3) + "...";
  46. }
  47. std::string extractDirectory(std::string path) {
  48. char *pathDup = strdup(path.c_str());
  49. std::string directory = dirname(pathDup);
  50. free(pathDup);
  51. return directory;
  52. }
  53. std::string extractFilename(std::string path) {
  54. char *pathDup = strdup(path.c_str());
  55. std::string filename = basename(pathDup);
  56. free(pathDup);
  57. return filename;
  58. }
  59. void openBrowser(std::string url) {
  60. #if ARCH_LIN
  61. std::string command = "xdg-open " + url;
  62. (void)system(command.c_str());
  63. #endif
  64. #if ARCH_MAC
  65. std::string command = "open " + url;
  66. system(command.c_str());
  67. #endif
  68. #if ARCH_WIN
  69. ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
  70. #endif
  71. }
  72. } // namespace rack