FileSystemPath.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "FileSystemPath.h"
  2. #include "StringBuilder.h"
  3. #include "Vector.h"
  4. #include "kstdio.h"
  5. namespace AK {
  6. FileSystemPath::FileSystemPath(const StringView& s)
  7. : m_string(s)
  8. {
  9. m_is_valid = canonicalize();
  10. }
  11. bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
  12. {
  13. // FIXME: Implement "resolve_symbolic_links"
  14. (void)resolve_symbolic_links;
  15. auto parts = m_string.split('/');
  16. Vector<String> canonical_parts;
  17. for (auto& part : parts) {
  18. if (part == ".")
  19. continue;
  20. if (part == "..") {
  21. if (!canonical_parts.is_empty())
  22. canonical_parts.take_last();
  23. continue;
  24. }
  25. if (!part.is_empty())
  26. canonical_parts.append(part);
  27. }
  28. if (canonical_parts.is_empty()) {
  29. m_string = m_basename = "/";
  30. return true;
  31. }
  32. m_basename = canonical_parts.last();
  33. StringBuilder builder;
  34. for (auto& cpart : canonical_parts) {
  35. builder.append('/');
  36. builder.append(cpart);
  37. }
  38. m_parts = move(canonical_parts);
  39. m_string = builder.to_string();
  40. return true;
  41. }
  42. bool FileSystemPath::has_extension(StringView extension) const
  43. {
  44. // FIXME: This is inefficient, expand StringView with enough functionality that we don't need to copy strings here.
  45. String extension_string = extension;
  46. return m_string.to_lowercase().ends_with(extension_string.to_lowercase());
  47. }
  48. }