BadScript 2
Loading...
Searching...
No Matches
BadFileApi.cs
Go to the documentation of this file.
1using BadScript2.IO;
4
6
7[BadInteropApi("File", true)]
8internal partial class BadFileApi
9{
14
15 public BadFileApi(IFileSystem fileSystem) : this()
16 {
17 m_FileSystem = fileSystem;
18 }
19
20 [BadMethod(description: "Writes the specified string to a file, overwriting the file if it already exists.")]
21 private void WriteAllText([BadParameter(description: "The Path of the file to write")] string path, [BadParameter(description: "The Content")] string content)
22 {
23 m_FileSystem.WriteAllText(path, content);
24 }
25
26 [BadMethod(description: "Opens a text file, reads all content of the file, and then closes the file.")]
27 [return: BadReturn("The content of the file")]
28 private string ReadAllText([BadParameter(description: "The Path of the file to read")] string path)
29 {
30 return m_FileSystem.ReadAllText(path);
31 }
32
33 [BadMethod(description: "Determines whether the specified file exists.")]
34 [return: BadReturn("True if the caller has the required permissions and path contains the name of an existing file; otherwise, false.")]
35 private bool Exists([BadParameter(description: "The Path to check")] string path)
36 {
37 return m_FileSystem.Exists(path) && m_FileSystem.IsFile(path);
38 }
39
40 [BadMethod(description: "Opens a file, reads all lines of the file, and then closes the file.")]
41 [return: BadReturn("The content of the file")]
42 private BadArray ReadAllLines([BadParameter(description: "The Path of the file to read")] string path)
43 {
44 return new BadArray(m_FileSystem.ReadAllLines(path).Select(x => (BadObject)x).ToList());
45 }
46
47 [BadMethod(description: "Opens a file, writes all lines to the file, and then closes the file.")]
48 private void WriteAllLines([BadParameter(description: "The Path of the file to write")] string path, [BadParameter(description: "The Content")] string[] content)
49 {
50 m_FileSystem.WriteAllLines(path, content);
51 }
52
53 [BadMethod(description: "Opens a file, writes all bytes to the file, and then closes the file.")]
54 private void WriteAllBytes([BadParameter(description: "The Path of the file to write")] string path, [BadParameter(description: "The Content")] byte[] content)
55 {
56 using Stream stream = m_FileSystem.OpenWrite(path, BadWriteMode.CreateNew);
57 stream.Write(content, 0, content.Length);
58 }
59
60 [BadMethod(description: "Opens a file, reads all bytes of the file, and then closes the file.")]
61 [return: BadReturn("The content of the file")]
62 private BadArray ReadAllBytes([BadParameter(description: "The Path of the file to read")] string path)
63 {
64 using Stream stream = m_FileSystem.OpenRead(path);
65 byte[] bytes = new byte[stream.Length];
66 int read = stream.Read(bytes, 0, bytes.Length);
67
68 if (read != bytes.Length)
69 {
70 throw new BadRuntimeException("IO.File.ReadAllBytes: Could not read all bytes");
71 }
72
73 return new BadArray(bytes.Select(x => (BadObject)x).ToList());
74 }
75
76 [BadMethod(description: "Deletes the specified file.")]
77 private void Delete([BadParameter(description: "The Path of the file to delete")] string path)
78 {
80 }
81
82 [BadMethod(description: "Moves a specified file to a new location, providing the option to specify a new file name.")]
83 private void Move(
84 [BadParameter(description: "The Path of the file to move")]
85 string source,
86 [BadParameter(description: "The Destination Path")]
87 string destination,
88 [BadParameter(description: "If true, allows an existing file to be overwritten; otherwise, false.")]
89 bool overwrite = false)
90 {
91 m_FileSystem.Move(source, destination, overwrite);
92 }
93
94 [BadMethod(description: "Copies a specified file to a new location, providing the option to specify a new file name.")]
95 private void Copy(
96 [BadParameter(description: "The Path of the file to copy")]
97 string source,
98 [BadParameter(description: "The Destination Path")]
99 string destination,
100 [BadParameter(description: "If true, allows an existing file to be overwritten; otherwise, false.")]
101 bool overwrite = false)
102 {
103 m_FileSystem.Copy(source, destination, overwrite);
104 }
105}
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.
void WriteAllLines([BadParameter(description:"The Path of the file to write")] string path, [BadParameter(description:"The Content")] string[] content)
Definition BadFileApi.cs:48
void WriteAllBytes([BadParameter(description:"The Path of the file to write")] string path, [BadParameter(description:"The Content")] byte[] content)
Definition BadFileApi.cs:54
void Copy([BadParameter(description:"The Path of the file to copy")] string source, [BadParameter(description:"The Destination Path")] string destination, [BadParameter(description:"If true, allows an existing file to be overwritten; otherwise, false.")] bool overwrite=false)
Definition BadFileApi.cs:95
string ReadAllText([BadParameter(description:"The Path of the file to read")] string path)
Definition BadFileApi.cs:28
readonly IFileSystem m_FileSystem
The FileSystem Instance.
Definition BadFileApi.cs:13
bool Exists([BadParameter(description:"The Path to check")] string path)
Definition BadFileApi.cs:35
void Delete([BadParameter(description:"The Path of the file to delete")] string path)
Definition BadFileApi.cs:77
BadFileApi(IFileSystem fileSystem)
Definition BadFileApi.cs:15
BadArray ReadAllLines([BadParameter(description:"The Path of the file to read")] string path)
Definition BadFileApi.cs:42
void Move([BadParameter(description:"The Path of the file to move")] string source, [BadParameter(description:"The Destination Path")] string destination, [BadParameter(description:"If true, allows an existing file to be overwritten; otherwise, false.")] bool overwrite=false)
Definition BadFileApi.cs:83
void WriteAllText([BadParameter(description:"The Path of the file to write")] string path, [BadParameter(description:"The Content")] string content)
Definition BadFileApi.cs:21
BadArray ReadAllBytes([BadParameter(description:"The Path of the file to read")] string path)
Definition BadFileApi.cs:62
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Defines the interface for a file system.
Definition IFileSystem.cs:7
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
bool Exists(string path)
Returns true if the given path is a file or directory.
bool IsFile(string path)
Returns true if the given path is a file.
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
void DeleteFile(string path)
Deletes a file.
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.
Contains IO Extensions and APIs for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10