forked from LogicAndTrick/sledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompositeFile.cs
155 lines (127 loc) · 4.21 KB
/
CompositeFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Sledge.FileSystem
{
/// <summary>
/// Represents a group of files or containers across multiple file systems.
/// If multiple files are found with the same name, the first one is given priority.
/// </summary>
public class CompositeFile : IFile
{
private IEnumerable<IFile> Files { get; set; }
public CompositeFile(IFile parent, IEnumerable<IFile> files)
{
Files = new List<IFile>(files.Where(x => x != null));
Parent = parent;
if (!Files.Any()) throw new FileNotFoundException("Cannot create a composite file with no files.");
}
public FileSystemType Type
{
get { return FileSystemType.Composite; }
}
public IEnumerable<IFile> GetCompositeFiles()
{
return new List<IFile>(Files);
}
public IFile Parent { get; set; }
public virtual string FullPathName
{
get { return Parent == null ? "\\" : Path.Combine(Parent.FullPathName, Name); }
}
private T First<T>(Func<IFile, T> func)
{
var f = Files.FirstOrDefault(x => x.Exists) ?? Files.First();
return func(f);
}
public string Name
{
get { return First(x => x.Name); }
}
public string NameWithoutExtension
{
get { return First(x => x.NameWithoutExtension); }
}
public string Extension
{
get { return First(x => x.Extension); }
}
public bool Exists
{
get { return Files.Any(x => x.Exists); }
}
public long Size
{
get { return First(x => x.Size); }
}
public bool IsContainer
{
get { return First(x => x.IsContainer); }
}
public int NumChildren
{
get { return Files.Sum(x => x.NumChildren); }
}
public int NumFiles
{
get { return Files.Sum(x => x.NumFiles); }
}
public Stream Open()
{
return First(x => x.Open());
}
public byte[] ReadAll()
{
return First(x => x.ReadAll());
}
public byte[] Read(long offset, long count)
{
return First(x => x.Read(offset, count));
}
private IEnumerable<IFile> MergeByName(IEnumerable<IFile> files)
{
return files.GroupBy(x => new {Name = x.Name.ToLower(), x.IsContainer})
.Where(x => x.Any())
.Select(x => new CompositeFile(this, x));
}
public IEnumerable<IFile> GetRelatedFiles()
{
return MergeByName(Files.SelectMany(x => x.GetRelatedFiles()));
}
public IFile GetRelatedFile(string extension)
{
return new CompositeFile(this, Files.Select(x => x.GetRelatedFile(extension)));
}
public IFile GetChild(string name)
{
var children = Files.Select(x => x.GetChild(name)).Where(x => x != null).ToList();
return !children.Any() ? null : new CompositeFile(this, children);
}
public IEnumerable<IFile> GetChildren()
{
return MergeByName(Files.SelectMany(x => x.GetChildren()));
}
public IEnumerable<IFile> GetChildren(string regex)
{
return MergeByName(Files.SelectMany(x => x.GetChildren(regex)));
}
public IFile GetFile(string name)
{
var files = Files.Select(x => x.GetFile(name)).Where(x => x != null).ToList();
return !files.Any() ? null : new CompositeFile(this, files);
}
public IEnumerable<IFile> GetFiles()
{
return MergeByName(Files.SelectMany(x => x.GetFiles()));
}
public IEnumerable<IFile> GetFiles(string regex)
{
return MergeByName(Files.SelectMany(x => x.GetFiles(regex)));
}
public IEnumerable<IFile> GetFilesWithExtension(string extension)
{
return MergeByName(Files.SelectMany(x => x.GetFilesWithExtension(extension)));
}
}
}