BadScript 2
Loading...
Searching...
No Matches
BadFileSystem.cs
Go to the documentation of this file.
1
4
5namespace BadScript2.IO;
6
10public static class BadFileSystem
11{
16
21 public static IFileSystem Instance =>
22 s_FileSystem ?? throw new InvalidOperationException("FileSystem is not initialized");
23
28 public static void SetFileSystem(IFileSystem fileSystem)
29 {
30 s_FileSystem = fileSystem;
31 }
32
38 public static void WriteAllText(string path, string contents)
39 {
40 Instance.WriteAllText(path, contents);
41 }
42
43 public static void WriteAllText(this IFileSystem fileSystem, string path, string contents)
44 {
45 using Stream s = fileSystem.OpenWrite(path, BadWriteMode.CreateNew);
46 using StreamWriter sw = new StreamWriter(s);
47 sw.Write(contents);
48 }
49
50 public static string ReadAllText(this IFileSystem fileSystem, string path)
51 {
52 using Stream s = fileSystem.OpenRead(path);
53 using StreamReader sw = new StreamReader(s);
54
55 return sw.ReadToEnd();
56 }
57
62 public static string ReadAllText(string path)
63 {
64 return Instance.ReadAllText(path);
65 }
66
67
68 public static IEnumerable<string> ReadAllLines(this IFileSystem fileSystem, string path)
69 {
70 using Stream s = fileSystem.OpenRead(path);
71 using StreamReader sw = new StreamReader(s);
72
73
74 while (!sw.EndOfStream)
75 {
76 yield return sw.ReadLine() ?? "";
77 }
78 }
79
84 public static IEnumerable<string> ReadAllLines(string path)
85 {
86 return Instance.ReadAllLines(path);
87 }
88
89 public static void WriteAllLines(this IFileSystem fileSystem, string path, IEnumerable<string> lines)
90 {
91 using Stream s = fileSystem.OpenWrite(path, BadWriteMode.CreateNew);
92 using StreamWriter sw = new StreamWriter(s);
93
94 foreach (string line in lines)
95 {
96 sw.WriteLine(line);
97 }
98 }
99
105 public static void WriteAllLines(string path, IEnumerable<string> lines)
106 {
107 Instance.WriteAllLines(path, lines);
108 }
109}
Public interface for the filesystem abstraction of the BadScript Engine.
static void WriteAllLines(this IFileSystem fileSystem, string path, IEnumerable< string > lines)
static string ReadAllText(this IFileSystem fileSystem, string path)
static IEnumerable< string > ReadAllLines(this IFileSystem fileSystem, string path)
static void WriteAllLines(string path, IEnumerable< string > lines)
Wrapper for IFileSystem.OpenWrite that creates the file if it does not exist.
static ? IFileSystem s_FileSystem
File System implementation.
static IEnumerable< string > ReadAllLines(string path)
Wrapper for IFileSystem.OpenRead
static void WriteAllText(string path, string contents)
Wrapper for IFileSystem.OpenWrite that creates the file if it does not exist.
static void WriteAllText(this IFileSystem fileSystem, string path, string contents)
static void SetFileSystem(IFileSystem fileSystem)
Sets the FileSystem implementation.
static string ReadAllText(string path)
Wrapper for IFileSystem.OpenRead
static IFileSystem Instance
File System implementation.
Implements a wrapper for the actual OS file system.
Defines the interface for a file system.
Definition IFileSystem.cs:7
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
Stream OpenRead(string path)
Opens a file for reading.
Contains IO Implementation for the BadScript2 Runtime.
BadWriteMode
The Write Modes of the File System Abstraction.