BadScript 2
Loading...
Searching...
No Matches
BadVirtualFileSystem.cs
Go to the documentation of this file.
2
4
9{
13 private readonly BadVirtualRoot m_Root = new BadVirtualRoot();
14
18 private string m_CurrentDirectory = "/";
19
20#region IFileSystem Members
21
22 public string GetStartupDirectory()
23 {
24 return "/";
25 }
26
27 public bool Exists(string path)
28 {
29 return IsFile(path) || IsDirectory(path);
30 }
31
32 public bool IsFile(string path)
33 {
36
37 for (int i = 0; i < parts.Length - 1; i++)
38 {
39 if (!current.DirectoryExists(parts[i]))
40 {
41 return false;
42 }
43
44 current = current.GetDirectory(parts[i]);
45 }
46
47 return current.FileExists(parts[parts.Length - 1]);
48 }
49
50 public bool IsDirectory(string path)
51 {
53
54 if (parts.Length == 1 && string.IsNullOrEmpty(parts[0]))
55 {
56 return true;
57 }
58
60
61 foreach (string t in parts)
62 {
63 if (!current.DirectoryExists(t))
64 {
65 return false;
66 }
67
68 current = current.GetDirectory(t);
69 }
70
71 return true;
72 }
73
74 public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
75 {
76 if (recursive)
77 {
79 extension
80 );
81 }
82
84 }
85
86 public IEnumerable<string> GetDirectories(string path, bool recursive)
87 {
88 return recursive
91 }
92
93 public void CreateDirectory(string path, bool recursive = false)
94 {
97
98 for (int i = 0; i < parts.Length - 1; i++)
99 {
100 if (!current.DirectoryExists(parts[i]))
101 {
102 if (!recursive)
103 {
104 throw new IOException("Directory does not exist");
105 }
106
107 current = current.CreateDirectory(parts[i]);
108 }
109 else
110 {
111 current = current.GetDirectory(parts[i]);
112 }
113 }
114
115 current.CreateDirectory(parts[parts.Length - 1]);
116 }
117
118 public void DeleteDirectory(string path, bool recursive)
119 {
121 parent.DeleteDirectory(Path.GetFileName(path), recursive);
122 }
123
124 public void DeleteFile(string path)
125 {
127 parent.DeleteFile(Path.GetFileName(path));
128 }
129
130 public string GetFullPath(string path)
131 {
133 }
134
135 public Stream OpenRead(string path)
136 {
137 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
138
139 if (!Exists(fullPath) || !IsFile(fullPath))
140 {
141 throw new FileNotFoundException(fullPath);
142 }
143
145
146 string[] paths = BadVirtualPathReader.SplitPath(fullPath);
147
148 return dir.GetFile(paths[paths.Length - 1])
149 .OpenRead();
150 }
151
152 public Stream OpenWrite(string path, BadWriteMode mode)
153 {
154 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
155
156 if (mode == BadWriteMode.Append && (!Exists(fullPath) || !IsFile(fullPath)))
157 {
158 throw new FileNotFoundException(path);
159 }
160
162 string[] paths = BadVirtualPathReader.SplitPath(fullPath);
163
164 return dir.GetOrCreateFile(paths[paths.Length - 1])
165 .OpenWrite(mode);
166 }
167
168 public string GetCurrentDirectory()
169 {
170 return m_CurrentDirectory;
171 }
172
173 public void SetCurrentDirectory(string path)
174 {
175 if (!Exists(path) || !IsDirectory(path))
176 {
177 throw new DirectoryNotFoundException(path);
178 }
179
181 }
182
183 public void Copy(string src, string dst, bool overwrite = true)
184 {
185 if (IsDirectory(src))
186 {
187 if (IsSubfolderOf(src, dst))
188 {
189 throw new IOException("Cannot copy a directory to a subfolder of itself.");
190 }
191
192 if (!overwrite && IsDirectory(src))
193 {
194 throw new IOException("Directory already exists.");
195 }
196
197 CopyDirectoryToDirectory(src, dst);
198 }
199 else if (IsFile(src))
200 {
201 if (!overwrite && IsFile(src))
202 {
203 throw new IOException("File already exists.");
204 }
205
206 CopyFileToFile(src, dst);
207 }
208 else
209 {
210 throw new IOException("Source path is not a file or directory");
211 }
212 }
213
214 public void Move(string src, string dst, bool overwrite = true)
215 {
216 Copy(src, dst, overwrite);
217
218 if (IsDirectory(src))
219 {
220 DeleteDirectory(src, true);
221 }
222 else
223 {
224 DeleteFile(src);
225 }
226 }
227
228#endregion
229
235 {
236 return m_Root;
237 }
238
245 {
249 )
250 )
251 .SkipLast(1)
252 )
253 );
254 }
255
262 {
263 if (string.IsNullOrEmpty(path) || BadVirtualPathReader.IsRootPath(path))
264 {
265 return m_Root;
266 }
267
268 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
269 string[] parts = BadVirtualPathReader.SplitPath(fullPath);
270
271 return parts.Aggregate<string, BadVirtualDirectory>(m_Root, (current1, t) => current1.GetDirectory(t));
272 }
273
279 private static IEnumerable<string> GetDirectories(BadVirtualDirectory directory)
280 {
281 return directory.Directories.Select(sub => sub.AbsolutePath);
282 }
283
289 private IEnumerable<string> GetDirectoriesRecursive(BadVirtualDirectory directory)
290 {
291 foreach (string s in GetDirectories(directory))
292 {
293 yield return s;
294 }
295
296 foreach (BadVirtualDirectory sub in directory.Directories)
297 {
298 foreach (string s in GetDirectoriesRecursive(sub))
299 {
300 yield return s;
301 }
302 }
303 }
304
311 private static IEnumerable<string> GetFiles(BadVirtualDirectory directory, string extension)
312 {
313 return from file in directory.Files
314 where extension == "" || Path.GetExtension(file.Name) == extension
315 select file.AbsolutePath;
316 }
317
324 private IEnumerable<string> GetFilesRecursive(BadVirtualDirectory directory, string extension)
325 {
326 foreach (string file in GetFiles(directory, extension))
327 {
328 yield return file;
329 }
330
331 foreach (BadVirtualDirectory subDirectory in directory.Directories)
332 {
333 foreach (string file in GetFilesRecursive(subDirectory, extension))
334 {
335 yield return file;
336 }
337 }
338 }
339
346 private bool IsSubfolderOf(string root, string sub)
347 {
348 return GetFullPath(sub)
349 .StartsWith(GetFullPath(root));
350 }
351
357 private void CopyFileToFile(string src, string dst)
358 {
359 using Stream s = OpenRead(src);
360 using Stream d = OpenWrite(dst, BadWriteMode.CreateNew);
361 s.CopyTo(d);
362 }
363
369 private void CopyDirectoryToDirectory(string src, string dst)
370 {
371 foreach (string directory in GetDirectories(src, true))
372 {
373 CreateDirectory(directory);
374 }
375
376 foreach (string file in GetFiles(src, "*", true))
377 {
378 CopyFileToFile(file, file.Replace(src, dst));
379 }
380 }
381}
Represents a Virtual File System Directory Entry.
BadVirtualDirectory GetDirectory(string name)
Returns an existing directory.
BadVirtualFile GetFile(string name)
Returns an existing File.
void DeleteDirectory(string name, bool recursive=true)
Deletes a subdirectory with the specified name.
bool FileExists(string name)
Returns true if the file with the specified name exist in this directory.
BadVirtualDirectory CreateDirectory(string name)
Returns the existing directory or Creates a new directory with the specified name.
IEnumerable< BadVirtualDirectory > Directories
Subdirectories.
void DeleteFile(string name)
Deletes a file from the current directory.
bool DirectoryExists(string name)
Returns true if the directory with the specified name exist in this directory.
BadVirtualFile GetOrCreateFile(string name)
Returns an existing file or creates a new file with the specified name.
Stream OpenWrite(BadWriteMode mode)
Returns a writable stream for the file.
Stream OpenRead()
Returns a readable stream for the file.
Virtual File System Implementation for the BadScript Engine.
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
static IEnumerable< string > GetFiles(BadVirtualDirectory directory, string extension)
Returns the files in the specified path that match the specified extension.
bool IsFile(string path)
Returns true if the given path is a file.
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
string GetCurrentDirectory()
Returns the Current Directory.
void CopyFileToFile(string src, string dst)
Copies a file to a file.
BadVirtualDirectory GetDirectory(string path)
Returns the directory at the specified path.
BadVirtualRoot GetRoot()
Returns the root directory of the filesystem.
IEnumerable< string > GetDirectoriesRecursive(BadVirtualDirectory directory)
Returns the directories in the specified path recursively.
string GetStartupDirectory()
The Startup Directory of the Application.
bool IsSubfolderOf(string root, string sub)
Returns true if sub is a subfolder of root.
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
BadVirtualDirectory GetParentDirectory(string path)
Returns the Parent directory of the specified path.
Stream OpenRead(string path)
Opens a file for reading.
void SetCurrentDirectory(string path)
Sets the current Directory.
bool Exists(string path)
Returns true if the given path is a file or directory.
void CopyDirectoryToDirectory(string src, string dst)
Copies a directory to a directory.
static IEnumerable< string > GetDirectories(BadVirtualDirectory directory)
Returns the directories in the specified path.
IEnumerable< string > GetFilesRecursive(BadVirtualDirectory directory, string extension)
Returns the files in the specified path that match the specified extension recursively.
string GetFullPath(string path)
Returns the full path of the given path.
string m_CurrentDirectory
The Current Directory.
void DeleteFile(string path)
Deletes a file.
readonly BadVirtualRoot m_Root
The Root Directory.
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
bool IsDirectory(string path)
Returns true if the given path is a directory.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
Implements Operations to read and manipulate File System Paths.
static string JoinPath(IEnumerable< string > parts)
static string ResolvePath(string path, string currentDir)
Implements a Virtual File System Root Directory.
Contains the Implementation of the BadScript Virtual File System.
BadWriteMode
The Write Modes of the File System Abstraction.
Contains Utility Functions and Classes.
Definition BadEnum.cs:5