1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.vfs.vfs; 30 31 import std.string; 32 import dlib.core.memory; 33 import dlib.core.stream; 34 import dlib.container.array; 35 import dlib.filesystem.filesystem; 36 import dlib.filesystem.stdfs; 37 38 class StdDirFileSystem: ReadOnlyFileSystem, Freeable 39 { 40 StdFileSystem stdfs; 41 string rootDir; 42 43 this(string rootDir) 44 { 45 this.rootDir = rootDir; 46 stdfs = New!StdFileSystem(); 47 } 48 49 bool stat(string filename, out FileStat stat) 50 { 51 string path = format("%s/%s", rootDir, filename); 52 return stdfs.stat(path, stat); 53 } 54 55 InputStream openForInput(string filename) 56 { 57 string path = format("%s/%s", rootDir, filename); 58 return stdfs.openForInput(path); 59 } 60 61 Directory openDir(string dir) 62 { 63 string path = format("%s/%s", rootDir, dir); 64 return stdfs.openDir(path); 65 } 66 67 ~this() 68 { 69 Delete(stdfs); 70 } 71 72 void free() 73 { 74 Delete(this); 75 } 76 } 77 78 class VirtualFileSystem: ReadOnlyFileSystem, Freeable 79 { 80 DynamicArray!StdDirFileSystem mounted; 81 82 void mount(string dir) 83 { 84 StdDirFileSystem fs = New!StdDirFileSystem(dir); 85 mounted.append(fs); 86 } 87 88 bool stat(string filename, out FileStat stat) 89 { 90 //filename = normalizePath(filename); 91 foreach(i, fs; mounted.data) 92 { 93 FileStat s; 94 if (fs.stat(filename, s)) 95 { 96 stat = s; 97 return true; 98 } 99 } 100 101 return false; 102 } 103 104 InputStream openForInput(string filename) 105 { 106 //filename = normalizePath(filename); 107 foreach(i, fs; mounted.data) 108 { 109 FileStat s; 110 if (fs.stat(filename, s)) 111 return fs.openForInput(filename); 112 } 113 114 return null; 115 } 116 117 Directory openDir(string path) 118 { 119 // TODO 120 return null; 121 } 122 123 ~this() 124 { 125 foreach(i, fs; mounted.data) 126 fs.free(); 127 mounted.free(); 128 } 129 130 void free() 131 { 132 Delete(this); 133 } 134 }