💿🐜 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.

115 lines
3.5 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
  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::filesystem::path get_executable_path()
  47. {
  48. std::filesystem::path 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::filesystem::path get_data_path(const std::string& application_name)
  69. {
  70. #if defined(_WIN32)
  71. return get_executable_path().parent_path();
  72. #else
  73. return get_executable_path().parent_path().parent_path() / "share" / application_name;
  74. #endif
  75. }
  76. std::filesystem::path get_config_path(const std::string& application_name)
  77. {
  78. std::filesystem::path config_path;
  79. #if defined(_WIN32)
  80. std::wstring wpath(MAX_PATH, L'\0');
  81. if (SHGetSpecialFolderPathW(nullptr, &wpath[0], CSIDL_LOCAL_APPDATA, FALSE))
  82. {
  83. wpath.erase(std::find(wpath.begin(), wpath.end(), L'\0'), wpath.end());
  84. config_path = std::filesystem::path(narrow(wpath)) / application_name;
  85. }
  86. #else
  87. // Determine home path
  88. std::filesystem::path home_path = getpwuid(getuid())->pw_dir;
  89. // Determine config path
  90. char* xdgConfigHome = std::getenv("XDG_CONFIG_HOME");
  91. if (!xdgConfigHome)
  92. {
  93. // Default to $HOME/.config/ as per:
  94. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
  95. config_path = home_path / ".config/" / application_name;
  96. }
  97. else
  98. {
  99. config_path = std::filesystem::path(xdgConfigHome) / application_name;
  100. }
  101. #endif
  102. return config_path;
  103. }