resource.cpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. slop::Resource::Resource() {
  22. // Find the configuration directory.
  23. // usually ~/.config/slop and /usr/share/slop
  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/";
  29. return;
  30. }
  31. usrconfig += config;
  32. usrconfig += "/slop/";
  33. }
  34. std::string slop::Resource::getRealPath( std::string localpath ) {
  35. if ( validatePath( usrconfig + localpath ) ) {
  36. return usrconfig + localpath;
  37. }
  38. std::string err = "The file or folder " + localpath + " was not found in " + usrconfig + "\n";
  39. throw std::runtime_error(err);
  40. return localpath;
  41. }
  42. bool slop::Resource::validatePath( std::string path ) {
  43. struct stat st;
  44. const char* dirname = path.c_str();
  45. if ( stat( dirname, &st ) != 0 ) {
  46. return false;
  47. }
  48. // Lookin' good!
  49. return true;
  50. }