Util.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "Util.hpp"
  2. #include "Config.hpp"
  3. #include <dirent.h>
  4. #include <sys/unistd.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <cpp3ds/System/FileSystem.hpp>
  9. #include <cpp3ds/System/I18n.hpp>
  10. namespace FreeShop
  11. {
  12. bool pathExists(const char* path, bool escape)
  13. {
  14. struct stat buffer;
  15. if (escape)
  16. return stat(cpp3ds::FileSystem::getFilePath(path).c_str(), &buffer) == 0;
  17. else
  18. return stat(path, &buffer) == 0;
  19. }
  20. void makeDirectory(const char *dir, mode_t mode)
  21. {
  22. char tmp[256];
  23. char *p = NULL;
  24. size_t len;
  25. snprintf(tmp, sizeof(tmp),"%s",dir);
  26. len = strlen(tmp);
  27. if(tmp[len - 1] == '/')
  28. tmp[len - 1] = 0;
  29. for(p = tmp + 1; *p; p++)
  30. if(*p == '/') {
  31. *p = 0;
  32. mkdir(tmp, mode);
  33. *p = '/';
  34. }
  35. mkdir(tmp, mode);
  36. }
  37. int removeDirectory(const char *path, bool onlyIfEmpty)
  38. {
  39. DIR *d = opendir(path);
  40. size_t path_len = strlen(path);
  41. int r = -1;
  42. if (d) {
  43. struct dirent *p;
  44. r = 0;
  45. while (!r && (p = readdir(d))) {
  46. int r2 = -1;
  47. char *buf;
  48. size_t len;
  49. /* Skip the names "." and ".." as we don't want to recurse on them. */
  50. if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
  51. continue;
  52. len = path_len + strlen(p->d_name) + 2;
  53. buf = (char*) malloc(len);
  54. if (buf) {
  55. struct stat sb;
  56. snprintf(buf, len, "%s/%s", path, p->d_name);
  57. if (!stat(buf, &sb)) {
  58. if (S_ISDIR(sb.st_mode))
  59. r2 = removeDirectory(buf, onlyIfEmpty);
  60. else if (!onlyIfEmpty)
  61. r2 = unlink(buf);
  62. }
  63. free(buf);
  64. }
  65. r = r2;
  66. }
  67. closedir(d);
  68. }
  69. if (!r)
  70. r = rmdir(path);
  71. return r;
  72. }
  73. std::string getCountryCode(int region)
  74. {
  75. std::string language = Config::get(Config::Language).GetString();
  76. if (language == "auto")
  77. language = cpp3ds::I18n::getInstance().getLangString(cpp3ds::I18n::getLanguage());
  78. #ifdef _3DS
  79. u8 consoleRegion;
  80. CFGU_SecureInfoGetRegion(&consoleRegion);
  81. if (language == "pt")
  82. language = (consoleRegion == CFG_REGION_USA) ? "pt_BR" : "pt_PT";
  83. else if (language == "es")
  84. language = (consoleRegion == CFG_REGION_USA) ? "es_US" : "es_ES";
  85. #endif
  86. if (language == "zh")
  87. return "HK";
  88. else if (region == 0)
  89. {
  90. if (language == "en") return "US";
  91. else if (language == "pt_PT") return "PT";
  92. else if (language == "pt_BR") return "BR";
  93. else if (language == "es_US") return "MX";
  94. else if (language == "es_ES") return "ES";
  95. }
  96. else
  97. {
  98. if (language == "en")
  99. return (region & (1 << 1)) ? "US" : "GB";
  100. else if (language == "pt_PT" || language == "pt_BR")
  101. return (region & (1 << 1)) ? "BR" : "PT";
  102. else if (language == "es_US" || language == "es_ES")
  103. return (region & (1 << 1)) ? "MX" : "ES";
  104. }
  105. for (auto &c: language)
  106. c = toupper(c);
  107. return language;
  108. }
  109. } // namespace FreeShop