Serenity Operating System
at hosted 116 lines 3.9 kB view raw
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 27#include <AK/FileSystemPath.h> 28#include <AK/StringBuilder.h> 29#include <AK/StringView.h> 30#include <AK/Vector.h> 31 32namespace AK { 33 34FileSystemPath::FileSystemPath(const StringView& s) 35 : m_string(s) 36{ 37 canonicalize(); 38 m_is_valid = true; 39} 40 41void FileSystemPath::canonicalize() 42{ 43 if (m_string.is_empty()) { 44 m_parts.clear(); 45 return; 46 } 47 48 bool is_absolute_path = m_string[0] == '/'; 49 auto parts = m_string.split_view('/'); 50 51 if (!is_absolute_path) 52 parts.prepend("."); 53 54 size_t approximate_canonical_length = 0; 55 Vector<String> canonical_parts; 56 57 for (size_t i = 0; i < parts.size(); ++i) { 58 auto& part = parts[i]; 59 if (is_absolute_path || i != 0) { 60 if (part == ".") 61 continue; 62 } 63 if (part == "..") { 64 if (!canonical_parts.is_empty()) 65 canonical_parts.take_last(); 66 continue; 67 } 68 if (!part.is_empty()) { 69 approximate_canonical_length += part.length() + 1; 70 canonical_parts.append(part); 71 } 72 } 73 if (canonical_parts.is_empty()) { 74 m_string = m_basename = m_dirname = "/"; 75 return; 76 } 77 78 StringBuilder dirname_builder(approximate_canonical_length); 79 for (size_t i = 0; i < canonical_parts.size() - 1; ++i) { 80 auto& canonical_part = canonical_parts[i]; 81 if (is_absolute_path || i != 0) 82 dirname_builder.append('/'); 83 dirname_builder.append(canonical_part); 84 } 85 m_dirname = dirname_builder.to_string(); 86 87 m_basename = canonical_parts.last(); 88 auto name_parts = m_basename.split('.'); 89 m_title = name_parts.is_empty() ? String() : name_parts[0]; 90 if (name_parts.size() > 1) 91 m_extension = name_parts[1]; 92 93 StringBuilder builder(approximate_canonical_length); 94 for (size_t i = 0; i < canonical_parts.size(); ++i) { 95 auto& canonical_part = canonical_parts[i]; 96 if (is_absolute_path || i != 0) 97 builder.append('/'); 98 builder.append(canonical_part); 99 } 100 m_parts = move(canonical_parts); 101 m_string = builder.to_string(); 102} 103 104bool FileSystemPath::has_extension(StringView extension) const 105{ 106 // FIXME: This is inefficient, expand StringView with enough functionality that we don't need to copy strings here. 107 String extension_string = extension; 108 return m_string.to_lowercase().ends_with(extension_string.to_lowercase()); 109} 110 111String canonicalized_path(const StringView& path) 112{ 113 return FileSystemPath(path).string(); 114} 115 116}