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.

98 lines
1.8KB

  1. #include <dirent.h>
  2. #include <sys/stat.h>
  3. #if ARCH_WIN
  4. #include <windows.h>
  5. #include <shellapi.h>
  6. #endif
  7. #include "system.hpp"
  8. namespace rack {
  9. namespace system {
  10. std::vector<std::string> listEntries(std::string path) {
  11. std::vector<std::string> filenames;
  12. DIR *dir = opendir(path.c_str());
  13. if (dir) {
  14. struct dirent *d;
  15. while ((d = readdir(dir))) {
  16. std::string filename = d->d_name;
  17. if (filename == "." || filename == "..")
  18. continue;
  19. filenames.push_back(path + "/" + filename);
  20. }
  21. closedir(dir);
  22. }
  23. return filenames;
  24. }
  25. bool isFile(std::string path) {
  26. struct stat statbuf;
  27. if (stat(path.c_str(), &statbuf))
  28. return false;
  29. return S_ISREG(statbuf.st_mode);
  30. }
  31. bool isDirectory(std::string path) {
  32. struct stat statbuf;
  33. if (stat(path.c_str(), &statbuf))
  34. return false;
  35. return S_ISDIR(statbuf.st_mode);
  36. }
  37. void copyFile(std::string srcPath, std::string destPath) {
  38. // Open files
  39. FILE *source = fopen(srcPath.c_str(), "rb");
  40. if (!source)
  41. return;
  42. DEFER({
  43. fclose(source);
  44. });
  45. FILE *dest = fopen(destPath.c_str(), "wb");
  46. if (!dest)
  47. return;
  48. DEFER({
  49. fclose(dest);
  50. });
  51. // Copy buffer
  52. const int bufferSize = (1<<15);
  53. char buffer[bufferSize];
  54. while (1) {
  55. size_t size = fread(buffer, 1, bufferSize, source);
  56. if (size == 0)
  57. break;
  58. size = fwrite(buffer, 1, size, dest);
  59. if (size == 0)
  60. break;
  61. }
  62. }
  63. void createDirectory(std::string path) {
  64. #if ARCH_WIN
  65. CreateDirectory(path.c_str(), NULL);
  66. #else
  67. mkdir(path.c_str(), 0755);
  68. #endif
  69. }
  70. void openBrowser(std::string url) {
  71. #if ARCH_LIN
  72. std::string command = "xdg-open " + url;
  73. (void) std::system(command.c_str());
  74. #endif
  75. #if ARCH_MAC
  76. std::string command = "open " + url;
  77. std::system(command.c_str());
  78. #endif
  79. #if ARCH_WIN
  80. ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
  81. #endif
  82. }
  83. } // namespace system
  84. } // namespace rack