UniVRM/UniJSON/Scripts/IFileSystemAccessor.cs
ousttrue df85433628 Merge commit 'df5f79584312cf4f6cc7ec1d73a78f6ba475c9ad' as 'UniJSON'
Co-authored-by: Deatrathias <dmailsec@gmail.com>
Co-authored-by: dj-kusuha <dj.kusuha+github@gmail.com>
Co-authored-by: ousttrue <ousttrue@gmail.com>
Co-authored-by: yutopp <yutopp@gmail.com>
2018-12-28 20:38:39 +09:00

54 lines
1.3 KiB
C#

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);
}
}
}