IFileSystemAccessor.cs
1.26 KB
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
using System.IO;
using System.Text;
namespace UniJSON
{
public interface IFileSystemAccessor
{
string ReadAllText();
string ReadAllText(string relativePath);
IFileSystemAccessor Get(string relativePath);
}
public class FileSystemAccessor : IFileSystemAccessor
{
string m_path;
string m_baseDir;
public FileSystemAccessor(string path)
{
m_path = path;
if (Directory.Exists(path))
{
m_baseDir = path;
}
else
{
m_baseDir = Path.GetDirectoryName(path);
}
}
public override string ToString()
{
return "<" + Path.GetFileName(m_path) + ">";
}
public string ReadAllText()
{
return File.ReadAllText(m_path, Encoding.UTF8);
}
public string ReadAllText(string relativePath)
{
var path = Path.Combine(m_baseDir, relativePath);
return File.ReadAllText(path, Encoding.UTF8);
}
public IFileSystemAccessor Get(string relativePath)
{
var path = Path.Combine(m_baseDir, relativePath);
return new FileSystemAccessor(path);
}
}
}