💿🐜 Antkeeper source code https://antkeeper.com
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.

147 lines
4.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "paths.hpp"
  20. #include <cstddef>
  21. #include <limits.h>
  22. #include <stdexcept>
  23. #if defined(_WIN32)
  24. #include <Shlobj.h>
  25. #include <windows.h>
  26. #else
  27. #include <pwd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <unistd.h>
  31. #endif
  32. #if defined(_WIN32)
  33. std::string narrow(const std::wstring& wstring)
  34. {
  35. std::string string(WideCharToMultiByte(CP_UTF8, 0, &wstring[0], static_cast<int>(wstring.size()), nullptr, 0, nullptr, nullptr), '\0');
  36. WideCharToMultiByte(CP_UTF8, 0, &wstring[0], static_cast<int>(wstring.size()), &string[0], static_cast<int>(string.size()), nullptr, nullptr);
  37. return string;
  38. }
  39. std::wstring widen(const std::string& string)
  40. {
  41. std::wstring wstring(MultiByteToWideChar(CP_UTF8, 0, &string[0], static_cast<int>(string.size()), nullptr, 0), L'\0');
  42. MultiByteToWideChar(CP_UTF8, 0, &string[0], static_cast<int>(string.size()), &wstring[0], static_cast<int>(wstring.size()));
  43. return wstring;
  44. }
  45. #endif
  46. std::string get_executable_path()
  47. {
  48. std::string executable_path;
  49. #if defined(_WIN32)
  50. // Get executable path on Windows
  51. HMODULE hModule = GetModuleHandleW(nullptr);
  52. std::wstring wpath(MAX_PATH, L'\0');
  53. GetModuleFileNameW(hModule, &wpath[0], MAX_PATH);
  54. wpath.erase(std::find(wpath.begin(), wpath.end(), L'\0'), wpath.end());
  55. executable_path = narrow(wpath);
  56. #else
  57. // Get executable path on Linux
  58. char path[PATH_MAX];
  59. ssize_t length = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
  60. if (length != -1)
  61. {
  62. path[length] = '\0';
  63. executable_path = path;
  64. }
  65. #endif
  66. return executable_path;
  67. }
  68. std::string get_data_path(const std::string& application_name)
  69. {
  70. std::string data_path;
  71. #if defined(_WIN32)
  72. std::string executable_path = get_executable_path();
  73. std::size_t delimeter = executable_path.find_last_of("\\/") + 1;
  74. data_path = executable_path.substr(0, delimeter);
  75. #else
  76. std::string executable_path = get_executable_path();
  77. std::size_t delimeter = executable_path.find_last_of("\\/") + 1;
  78. data_path = executable_path.substr(0, delimeter) + std::string("../share/") + application_name + std::string("/");
  79. #endif
  80. return data_path;
  81. }
  82. std::string get_config_path(const std::string& application_name)
  83. {
  84. std::string config_path;
  85. #if defined(_WIN32)
  86. std::wstring wpath(MAX_PATH, L'\0');
  87. if (SHGetSpecialFolderPathW(nullptr, &wpath[0], CSIDL_LOCAL_APPDATA, FALSE))
  88. {
  89. wpath.erase(std::find(wpath.begin(), wpath.end(), L'\0'), wpath.end());
  90. config_path = narrow(wpath);
  91. config_path += std::string("\\") + application_name + std::string("\\");
  92. }
  93. #else
  94. // Determine home path
  95. std::string home_path = std::string(getpwuid(getuid())->pw_dir);
  96. // Determine config path
  97. char* xdgConfigHome = std::getenv("XDG_CONFIG_HOME");
  98. if (!xdgConfigHome)
  99. {
  100. // Default to $HOME/.config/ as per:
  101. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
  102. config_path = home_path + std::string("/.config/") + application_name + std::string("/");
  103. }
  104. else
  105. {
  106. config_path = xdgConfigHome + std::string("/") + application_name + std::string("/");
  107. }
  108. #endif
  109. return config_path;
  110. }
  111. bool path_exists(const std::string& path)
  112. {
  113. #if defined(_WIN32)
  114. std::wstring wpath = widen(path);
  115. DWORD attributes = GetFileAttributesW(wpath.c_str());
  116. return (attributes != INVALID_FILE_ATTRIBUTES);
  117. #else
  118. struct stat info;
  119. return (stat(path.c_str(), &info) == 0);
  120. #endif
  121. }
  122. bool create_directory(const std::string& path)
  123. {
  124. #if defined(_WIN32)
  125. std::wstring wpath = widen(path);
  126. return (CreateDirectoryW(wpath.c_str(), nullptr) != 0);
  127. #else
  128. return (mkdir(path.c_str(), 0777) == 0);
  129. #endif
  130. }