resource.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* resource.cpp: Handles converting local paths to full paths.
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "resource.hpp"
  21. Resource::Resource() {
  22. // Find the configuration directory.
  23. // usually ~/.config/slop-wayland and /usr/share/slop-wayland
  24. char* config = getenv( "XDG_CONFIG_HOME" );
  25. if ( config == NULL ) {
  26. char* home = getpwuid(getuid())->pw_dir;
  27. usrconfig += home;
  28. usrconfig += "/.config/slop-wayland/";
  29. // SHADER_PREFIX is defined within the CMakeLists.txt
  30. sysconfig = SHADER_PREFIX;
  31. sysconfig += "/share/slop-wayland/";
  32. return;
  33. }
  34. usrconfig += config;
  35. usrconfig += "/slop-wayland/";
  36. sysconfig = SHADER_PREFIX;
  37. sysconfig += "/share/slop-wayland/";
  38. }
  39. std::string Resource::getRealPath( std::string localpath ) {
  40. if ( validatePath( usrconfig + localpath ) ) {
  41. return usrconfig + localpath;
  42. }
  43. if ( validatePath( sysconfig + localpath ) ) {
  44. return sysconfig + localpath;
  45. }
  46. std::string err = "The file or folder " + localpath + " was not found in either " + sysconfig + " or " + usrconfig + "\n";
  47. throw std::runtime_error(err);
  48. return localpath;
  49. }
  50. bool Resource::validatePath( std::string path ) {
  51. struct stat st;
  52. const char* dirname = path.c_str();
  53. if ( stat( dirname, &st ) != 0 ) {
  54. return false;
  55. }
  56. // Lookin' good!
  57. return true;
  58. }