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

148 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) 2020 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 <cstdlib>
  21. #include <limits.h>
  22. #include <stdexcept>
  23. #include <iostream>
  24. #if defined(_WIN32)
  25. #include <Shlobj.h>
  26. #include <windows.h>
  27. #else
  28. #include <pwd.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #endif
  33. #if defined(_WIN32)
  34. std::string narrow(const std::wstring& wstring)
  35. {
  36. std::string string(WideCharToMultiByte(CP_UTF8, 0, &wstring[0], static_cast<int>(wstring.size()), nullptr, 0, nullptr, nullptr), '\0');
  37. WideCharToMultiByte(CP_UTF8, 0, &wstring[0], static_cast<int>(wstring.size()), &string[0], static_cast<int>(string.size()), nullptr, nullptr);
  38. return string;
  39. }
  40. std::wstring widen(const std::string& string)
  41. {
  42. std::wstring wstring(MultiByteToWideChar(CP_UTF8, 0, &string[0], static_cast<int>(string.size()), nullptr, 0), L'\0');
  43. MultiByteToWideChar(CP_UTF8, 0, &string[0], static_cast<int>(string.size()), &wstring[0], static_cast<int>(wstring.size()));
  44. return wstring;
  45. }
  46. #endif
  47. std::string get_executable_path()
  48. {
  49. std::string executable_path;
  50. #if defined(_WIN32)
  51. // Get executable path on Windows
  52. HMODULE hModule = GetModuleHandleW(nullptr);
  53. std::wstring wpath(MAX_PATH, L'\0');
  54. GetModuleFileNameW(hModule, &wpath[0], MAX_PATH);
  55. wpath.erase(std::find(wpath.begin(), wpath.end(), L'\0'), wpath.end());
  56. executable_path = narrow(wpath);
  57. #else
  58. // Get executable path on Linux
  59. char path[PATH_MAX];
  60. ssize_t length = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
  61. if (length != -1)
  62. {
  63. path[length] = '\0';
  64. executable_path = path;
  65. }
  66. #endif
  67. return executable_path;
  68. }
  69. std::string get_data_path(const std::string& application_name)
  70. {
  71. std::string data_path;
  72. #if defined(_WIN32)
  73. std::string executable_path = get_executable_path();
  74. std::size_t delimeter = executable_path.find_last_of("\\/") + 1;
  75. data_path = executable_path.substr(0, delimeter);
  76. #else
  77. std::string executable_path = get_executable_path();
  78. std::size_t delimeter = executable_path.find_last_of("\\/") + 1;
  79. data_path = executable_path.substr(0, delimeter) + std::string("../share/") + application_name + std::string("/");
  80. #endif
  81. return data_path;
  82. }
  83. std::string get_config_path(const std::string& application_name)
  84. {
  85. std::string config_path;
  86. #if defined(_WIN32)
  87. std::wstring wpath(MAX_PATH, L'\0');
  88. if (SHGetSpecialFolderPathW(nullptr, &wpath[0], CSIDL_LOCAL_APPDATA, FALSE))
  89. {
  90. wpath.erase(std::find(wpath.begin(), wpath.end(), L'\0'), wpath.end());
  91. config_path = narrow(wpath);
  92. config_path += std::string("\\") + application_name + std::string("\\");
  93. }
  94. #else
  95. // Determine home path
  96. std::string home_path = std::string(getpwuid(getuid())->pw_dir);
  97. // Determine config path
  98. char* xdgConfigHome = std::getenv("XDG_CONFIG_HOME");
  99. if (!xdgConfigHome)
  100. {
  101. // Default to $HOME/.config/ as per:
  102. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
  103. config_path = home_path + std::string("/.config/") + application_name + std::string("/");
  104. }
  105. else
  106. {
  107. config_path = xdgConfigHome + std::string("/") + application_name + std::string("/");
  108. }
  109. #endif
  110. return config_path;
  111. }
  112. bool path_exists(const std::string& path)
  113. {
  114. #if defined(_WIN32)
  115. std::wstring wpath = widen(path);
  116. DWORD attributes = GetFileAttributesW(wpath.c_str());
  117. return (attributes != INVALID_FILE_ATTRIBUTES);
  118. #else
  119. struct stat info;
  120. return (stat(path.c_str(), &info) == 0);
  121. #endif
  122. }
  123. bool create_directory(const std::string& path)
  124. {
  125. #if defined(_WIN32)
  126. std::wstring wpath = widen(path);
  127. return (CreateDirectoryW(wpath.c_str(), nullptr) != 0);
  128. #else
  129. return (mkdir(path.c_str(), 0777) == 0);
  130. #endif
  131. }