FileSystemPath.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "FileSystemPath.h"
  27. #include "StringBuilder.h"
  28. #include "Vector.h"
  29. namespace AK {
  30. FileSystemPath::FileSystemPath(const StringView& s)
  31. : m_string(s)
  32. {
  33. canonicalize();
  34. m_is_valid = true;
  35. }
  36. void FileSystemPath::canonicalize()
  37. {
  38. if (m_string.is_empty()) {
  39. m_parts.clear();
  40. return;
  41. }
  42. bool is_absolute_path = m_string[0] == '/';
  43. auto parts = m_string.split_view('/');
  44. if (!is_absolute_path)
  45. parts.prepend(".");
  46. size_t approximate_canonical_length = 0;
  47. Vector<String> canonical_parts;
  48. for (size_t i = 0; i < parts.size(); ++i) {
  49. auto& part = parts[i];
  50. if (is_absolute_path || i != 0) {
  51. if (part == ".")
  52. continue;
  53. }
  54. if (part == "..") {
  55. if (!canonical_parts.is_empty())
  56. canonical_parts.take_last();
  57. continue;
  58. }
  59. if (!part.is_empty()) {
  60. approximate_canonical_length += part.length() + 1;
  61. canonical_parts.append(part);
  62. }
  63. }
  64. if (canonical_parts.is_empty()) {
  65. m_string = m_basename = m_dirname = "/";
  66. return;
  67. }
  68. StringBuilder dirname_builder(approximate_canonical_length);
  69. for (size_t i = 0; i < canonical_parts.size() - 1; ++i) {
  70. auto& canonical_part = canonical_parts[i];
  71. if (is_absolute_path || i != 0)
  72. dirname_builder.append('/');
  73. dirname_builder.append(canonical_part);
  74. }
  75. m_dirname = dirname_builder.to_string();
  76. m_basename = canonical_parts.last();
  77. auto name_parts = m_basename.split('.');
  78. m_title = name_parts.is_empty() ? String() : name_parts[0];
  79. if (name_parts.size() > 1)
  80. m_extension = name_parts[1];
  81. StringBuilder builder(approximate_canonical_length);
  82. for (size_t i = 0; i < canonical_parts.size(); ++i) {
  83. auto& canonical_part = canonical_parts[i];
  84. if (is_absolute_path || i != 0)
  85. builder.append('/');
  86. builder.append(canonical_part);
  87. }
  88. m_parts = move(canonical_parts);
  89. m_string = builder.to_string();
  90. }
  91. bool FileSystemPath::has_extension(StringView extension) const
  92. {
  93. // FIXME: This is inefficient, expand StringView with enough functionality that we don't need to copy strings here.
  94. String extension_string = extension;
  95. return m_string.to_lowercase().ends_with(extension_string.to_lowercase());
  96. }
  97. String canonicalized_path(const StringView& path)
  98. {
  99. return FileSystemPath(path).string();
  100. }
  101. }