path_parser.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string>
  6. #include <vector>
  7. #include "core/file_sys/filesystem.h"
  8. namespace FileSys {
  9. /**
  10. * A helper class parsing and verifying a string-type Path.
  11. * Every archives with a sub file system should use this class to parse the path argument and check
  12. * the status of the file / directory in question on the host file system.
  13. */
  14. class PathParser {
  15. public:
  16. explicit PathParser(const Path& path);
  17. /**
  18. * Checks if the Path is valid.
  19. * This function should be called once a PathParser is constructed.
  20. * A Path is valid if:
  21. * - it is a string path (with type LowPathType::Char or LowPathType::Wchar),
  22. * - it starts with "/" (this seems a hard requirement in real 3DS),
  23. * - it doesn't contain invalid characters, and
  24. * - it doesn't go out of the root directory using "..".
  25. */
  26. bool IsValid() const {
  27. return is_valid;
  28. }
  29. /// Checks if the Path represents the root directory.
  30. bool IsRootDirectory() const {
  31. return is_root;
  32. }
  33. enum HostStatus {
  34. InvalidMountPoint,
  35. PathNotFound, // "/a/b/c" when "a" doesn't exist
  36. FileInPath, // "/a/b/c" when "a" is a file
  37. FileFound, // "/a/b/c" when "c" is a file
  38. DirectoryFound, // "/a/b/c" when "c" is a directory
  39. NotFound // "/a/b/c" when "a/b/" exists but "c" doesn't exist
  40. };
  41. /// Checks the status of the specified file / directory by the Path on the host file system.
  42. HostStatus GetHostStatus(const std::string& mount_point) const;
  43. /// Builds a full path on the host file system.
  44. std::string BuildHostPath(const std::string& mount_point) const;
  45. private:
  46. std::vector<std::string> path_sequence;
  47. bool is_valid{};
  48. bool is_root{};
  49. };
  50. } // namespace FileSys