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