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

95 lines
2.6 KiB

  1. /*
  2. * Copyright (C) 2017-2019 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 <unistd.h>
  22. #include <limits.h>
  23. #include <pwd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <stdexcept>
  27. #include <iostream>
  28. static std::string getExecutablePath()
  29. {
  30. char path[PATH_MAX];
  31. ssize_t length = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
  32. if (length != -1)
  33. {
  34. path[length] = '\0';
  35. return std::string(path);
  36. }
  37. return std::string();
  38. }
  39. std::string getDataPath()
  40. {
  41. std::string executablePath = getExecutablePath();
  42. std::size_t delimeter = executablePath.find_last_of("\\/") + 1;
  43. std::string executableName = executablePath.substr(delimeter, executablePath.size() - delimeter + 1);
  44. return executablePath.substr(0, delimeter) + std::string("../share/") + executableName + std::string("/");
  45. }
  46. std::string getConfigPath()
  47. {
  48. std::string executablePath = getExecutablePath();
  49. std::size_t delimeter = executablePath.find_last_of("\\/") + 1;
  50. std::string executableName = executablePath.substr(delimeter, executablePath.size() - delimeter + 1);
  51. std::string configPath;
  52. // Determine home path
  53. std::string homePath = std::string(getpwuid(getuid())->pw_dir);
  54. // Determine config path
  55. char* xdgConfigHome = std::getenv("XDG_CONFIG_HOME");
  56. if (!xdgConfigHome)
  57. {
  58. // Default to $HOME/.config/ as per:
  59. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
  60. configPath = homePath + std::string("/.config/") + executableName + std::string("/");
  61. }
  62. else
  63. {
  64. configPath = xdgConfigHome + std::string("/") + executableName + std::string("/");
  65. }
  66. return configPath;
  67. }
  68. bool pathExists(const std::string& path)
  69. {
  70. struct stat info;
  71. return (stat(path.c_str(), &info) == 0);
  72. }
  73. bool createDirectory(const std::string& path)
  74. {
  75. int error = 0;
  76. #if defined(_WIN32)
  77. error = _mkdir(path.c_str());
  78. #else
  79. error = mkdir(path.c_str(), 0777);
  80. #endif
  81. return (error == 0);
  82. }