1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "FileSystem.h" 21 22 #include "Exceptions.h" 23 24 namespace TrenchBroom { 25 namespace IO { TypeMatcher(const bool files,const bool directories)26 FileSystem::TypeMatcher::TypeMatcher(const bool files, const bool directories) : 27 m_files(files), 28 m_directories(directories) {} 29 operator ()(const Path & path,const bool directory) const30 bool FileSystem::TypeMatcher::operator()(const Path& path, const bool directory) const { 31 if (m_files && !directory) 32 return true; 33 if (m_directories && directory) 34 return true; 35 return false; 36 } 37 ExtensionMatcher(const String & extension)38 FileSystem::ExtensionMatcher::ExtensionMatcher(const String& extension) : 39 m_extension(extension) {} 40 operator ()(const Path & path,const bool directory) const41 bool FileSystem::ExtensionMatcher::operator()(const Path& path, const bool directory) const { 42 if (directory) 43 return false; 44 return StringUtils::caseInsensitiveEqual(path.extension(), m_extension); 45 } 46 ~FileSystem()47 FileSystem::~FileSystem() {} 48 directoryExists(const Path & path) const49 bool FileSystem::directoryExists(const Path& path) const { 50 try { 51 if (path.isAbsolute()) 52 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 53 return doDirectoryExists(path); 54 } catch (const PathException& e) { 55 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 56 } 57 } 58 fileExists(const Path & path) const59 bool FileSystem::fileExists(const Path& path) const { 60 try { 61 if (path.isAbsolute()) 62 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 63 return doFileExists(path); 64 } catch (const PathException& e) { 65 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 66 } 67 } 68 findItems(const Path & path) const69 Path::List FileSystem::findItems(const Path& path) const { 70 return findItems(path, TypeMatcher()); 71 } 72 findItemsRecursively(const Path & path) const73 Path::List FileSystem::findItemsRecursively(const Path& path) const { 74 return findItemsRecursively(path, TypeMatcher()); 75 } 76 getDirectoryContents(const Path & path) const77 Path::List FileSystem::getDirectoryContents(const Path& path) const { 78 try { 79 if (path.isAbsolute()) 80 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 81 if (!directoryExists(path)) 82 throw FileSystemException("Directory not found: '" + path.asString() + "'"); 83 return doGetDirectoryContents(path); 84 } catch (const PathException& e) { 85 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 86 } 87 } 88 openFile(const Path & path) const89 const MappedFile::Ptr FileSystem::openFile(const Path& path) const { 90 try { 91 if (path.isAbsolute()) 92 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 93 if (!fileExists(path)) 94 throw FileSystemException("File not found: '" + path.asString() + "'"); 95 return doOpenFile(path); 96 } catch (const PathException& e) { 97 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 98 } 99 } 100 ~WritableFileSystem()101 WritableFileSystem::~WritableFileSystem() {} 102 createDirectory(const Path & path)103 void WritableFileSystem::createDirectory(const Path& path) { 104 try { 105 if (path.isAbsolute()) 106 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 107 doCreateDirectory(path); 108 } catch (const PathException& e) { 109 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 110 } 111 } 112 deleteFile(const Path & path)113 void WritableFileSystem::deleteFile(const Path& path) { 114 try { 115 if (path.isAbsolute()) 116 throw FileSystemException("Path is absolute: '" + path.asString() + "'"); 117 doDeleteFile(path); 118 } catch (const PathException& e) { 119 throw FileSystemException("Invalid path: '" + path.asString() + "'", e); 120 } 121 } 122 moveFile(const Path & sourcePath,const Path & destPath,const bool overwrite)123 void WritableFileSystem::moveFile(const Path& sourcePath, const Path& destPath, const bool overwrite) { 124 try { 125 if (sourcePath.isAbsolute()) 126 throw FileSystemException("Source path is absolute: '" + sourcePath.asString() + "'"); 127 if (destPath.isAbsolute()) 128 throw FileSystemException("Destination path is absolute: '" + destPath.asString() + "'"); 129 doMoveFile(sourcePath, destPath, overwrite); 130 } catch (const PathException& e) { 131 throw FileSystemException("Invalid source or destination path: '" + sourcePath.asString() + "', '" + destPath.asString() + "'", e); 132 } 133 } 134 } 135 } 136