|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Foster.Framework.Storage |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Default Content implementation. |
| 13 | + /// Should work well for PC, etc, but can be overridden for custom handling. |
| 14 | + /// </summary> |
| 15 | + public class Content |
| 16 | + { |
| 17 | + public string CurrentDirectory { get; set; } = ""; |
| 18 | + |
| 19 | + private class ContentEnumerator : IEnumerator<string> |
| 20 | + { |
| 21 | + public string[] Locations; |
| 22 | + public int Index = -1; |
| 23 | + |
| 24 | + public string Current |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + return Locations[Index]; |
| 31 | + } |
| 32 | + catch (IndexOutOfRangeException) |
| 33 | + { |
| 34 | + throw new InvalidOperationException(); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + object IEnumerator.Current => Current; |
| 40 | + |
| 41 | + public ContentEnumerator(string[] locations) |
| 42 | + { |
| 43 | + Locations = locations; |
| 44 | + } |
| 45 | + |
| 46 | + public bool MoveNext() |
| 47 | + { |
| 48 | + Index++; |
| 49 | + if (Index >= Locations.Length) |
| 50 | + return false; |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + public void Reset() => Index = -1; |
| 55 | + |
| 56 | + public void Dispose() { } |
| 57 | + } |
| 58 | + |
| 59 | + public Content() { } |
| 60 | + public Content(string content) : this() |
| 61 | + { |
| 62 | + CurrentDirectory = content; |
| 63 | + } |
| 64 | + |
| 65 | + #region Directory |
| 66 | + public virtual bool FileExists(string relativePath) |
| 67 | + { |
| 68 | + return File.Exists(CurrentDirectory + relativePath); |
| 69 | + } |
| 70 | + public virtual bool DirectoryExists(string relativePath) |
| 71 | + { |
| 72 | + return Directory.Exists(CurrentDirectory + relativePath); |
| 73 | + } |
| 74 | + public virtual bool Exists(string name) |
| 75 | + { |
| 76 | + return FileExists(name) || DirectoryExists(name); |
| 77 | + } |
| 78 | + |
| 79 | + public virtual IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive) |
| 80 | + { |
| 81 | + return new ContentEnumerator( |
| 82 | + Directory.GetFiles( |
| 83 | + CurrentDirectory + path, |
| 84 | + searchPattern, |
| 85 | + recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly |
| 86 | + )); |
| 87 | + } |
| 88 | + |
| 89 | + public virtual IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive) |
| 90 | + { |
| 91 | + |
| 92 | + return new ContentEnumerator( |
| 93 | + Directory.GetDirectories( |
| 94 | + CurrentDirectory + path, |
| 95 | + searchPattern, |
| 96 | + recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly |
| 97 | + )); |
| 98 | + } |
| 99 | + |
| 100 | + #endregion |
| 101 | + |
| 102 | + #region File |
| 103 | + |
| 104 | + public virtual Stream OpenRead(string relativePath) |
| 105 | + { |
| 106 | + return File.OpenRead(CurrentDirectory + relativePath); |
| 107 | + } |
| 108 | + |
| 109 | + public virtual byte[] ReadAllBytes(string relativePath) |
| 110 | + { |
| 111 | + return File.ReadAllBytes(CurrentDirectory + relativePath); |
| 112 | + } |
| 113 | + |
| 114 | + public virtual string ReadAllText(string relativePath) |
| 115 | + { |
| 116 | + return File.ReadAllText(CurrentDirectory + relativePath); |
| 117 | + } |
| 118 | + |
| 119 | + #endregion |
| 120 | + } |
| 121 | +} |
0 commit comments