1
2
3
4
5
6 using System;
7 using System.Collections;
8
9 namespace System.IO
10 {
11 public static class File
12 {
13 public static Result<bool> Exists(const string& filePath)
14 {
15 int errorId = 0;
16 bool exists = RtmFileExists(filePath.Chars(), errorId);
17 if (errorId > 0)
18 {
19 return Result<bool>(ErrorId(errorId));
20 }
21 else
22 {
23 return Result<bool>(exists);
24 }
25 }
26 public static Result<long> Size(const string& filePath)
27 {
28 int errorId = 0;
29 long fileSize = RtmGetFileSize(filePath.Chars(), errorId);
30 if (fileSize == -1)
31 {
32 return Result<long>(ErrorId(errorId));
33 }
34 else
35 {
36 return Result<long>(fileSize);
37 }
38 }
39 public static Result<bool> LastWriteTimeLess(const string& filePath1, const string& filePath2)
40 {
41 int errorId = 0;
42 bool result = RtmLastWriteTimeLess(filePath1.Chars(), filePath2.Chars(), errorId);
43 if (errorId != 0)
44 {
45 return Result<bool>(ErrorId(errorId));
46 }
47 return Result<bool>(result);
48 }
49 public static Result<StreamWriter> CreateText(const string& filePath)
50 {
51 UniquePtr<FileStream> fileStream = new FileStream(filePath, OpenMode.write);
52 if (fileStream->Error())
53 {
54 return Result<StreamWriter>(ErrorId(fileStream->GetErrorId()));
55 }
56 UniquePtr<BufferedStream> bufferedStream = new BufferedStream(fileStream.Get());
57 if (bufferedStream->Error())
58 {
59 return Result<StreamWriter>(ErrorId(bufferedStream->GetErrorId()));
60 }
61 StreamWriter writer(bufferedStream.Get());
62 writer.Own(fileStream.Release());
63 writer.Own(bufferedStream.Release());
64 if (writer.Error())
65 {
66 return Result<StreamWriter>(ErrorId(writer.GetErrorId()));
67 }
68 return Result<StreamWriter>(Rvalue(writer));
69 }
70 public static Result<BinaryWriter> CreateBinary(const string& filePath)
71 {
72 UniquePtr<FileStream> fileStream = new FileStream(filePath, cast<OpenMode>(OpenMode.write | OpenMode.binary));
73 if (fileStream->Error())
74 {
75 return Result<BinaryWriter>(ErrorId(fileStream->GetErrorId()));
76 }
77 UniquePtr<BufferedStream> bufferedStream = new BufferedStream(fileStream.Get());
78 if (bufferedStream->Error())
79 {
80 return Result<BinaryWriter>(ErrorId(bufferedStream->GetErrorId()));
81 }
82 BinaryWriter writer(bufferedStream.Get());
83 writer.Own(fileStream.Release());
84 writer.Own(bufferedStream.Release());
85 if (writer.Error())
86 {
87 return Result<BinaryWriter>(ErrorId(writer.GetErrorId()));
88 }
89 return Result<BinaryWriter>(Rvalue(writer));
90 }
91 public static Result<StreamWriter> AppendText(const string& filePath)
92 {
93 UniquePtr<FileStream> fileStream = new FileStream(filePath, OpenMode.append);
94 if (fileStream->Error())
95 {
96 return Result<StreamWriter>(ErrorId(fileStream->GetErrorId()));
97 }
98 UniquePtr<BufferedStream> bufferedStream = new BufferedStream(fileStream.Get());
99 if (bufferedStream->Error())
100 {
101 return Result<StreamWriter>(ErrorId(bufferedStream->GetErrorId()));
102 }
103 StreamWriter writer(bufferedStream.Get());
104 writer.Own(fileStream.Release());
105 writer.Own(bufferedStream.Release());
106 if (writer.Error())
107 {
108 return Result<StreamWriter>(ErrorId(writer.GetErrorId()));
109 }
110 return Result<StreamWriter>(Rvalue(writer));
111 }
112 public static Result<StreamReader> OpenRead(const string& filePath)
113 {
114 UniquePtr<FileStream> fileStream = new FileStream(filePath, OpenMode.read);
115 if (fileStream->Error())
116 {
117 return Result<StreamReader>(ErrorId(fileStream->GetErrorId()));
118 }
119 UniquePtr<BufferedStream> bufferedStream = new BufferedStream(fileStream.Get());
120 if (bufferedStream->Error())
121 {
122 return Result<StreamReader>(ErrorId(bufferedStream->GetErrorId()));
123 }
124 StreamReader reader(bufferedStream.Get());
125 reader.Own(fileStream.Release());
126 reader.Own(bufferedStream.Release());
127 if (reader.Error())
128 {
129 return Result<StreamReader>(ErrorId(reader.GetErrorId()));
130 }
131 return Result<StreamReader>(Rvalue(reader));
132 }
133 public static Result<BinaryReader> OpenBinary(const string& filePath)
134 {
135 UniquePtr<FileStream> fileStream = new FileStream(filePath, cast<OpenMode>(OpenMode.read | OpenMode.binary));
136 if (fileStream->Error())
137 {
138 return Result<BinaryReader>(ErrorId(fileStream->GetErrorId()));
139 }
140 UniquePtr<BufferedStream> bufferedStream = new BufferedStream(fileStream.Get());
141 if (bufferedStream->Error())
142 {
143 return Result<BinaryReader>(ErrorId(bufferedStream->GetErrorId()));
144 }
145 BinaryReader reader(bufferedStream.Get());
146 reader.Own(fileStream.Release());
147 reader.Own(bufferedStream.Release());
148 if (reader.Error())
149 {
150 return Result<BinaryReader>(ErrorId(reader.GetErrorId()));
151 }
152 return Result<BinaryReader>(Rvalue(reader));
153 }
154 public static Result<string> ReadAllText(const string& filePath)
155 {
156 Result<StreamReader> reader = OpenRead(filePath);
157 if (reader.Error())
158 {
159 return Result<string>(reader.GetErrorId());
160 }
161 Result<string> contentResult = reader.Value().ReadToEnd();
162 if (contentResult.Error()) return contentResult;
163 string contentValue = Rvalue(contentResult.Value());
164 if (contentValue.Length() >= 3 && cast<byte>(contentValue[0]) == 239u && cast<byte>(contentValue[1]) == 187u && cast<byte>(contentValue[2]) == 191u )
165 {
166 return Result<string>(contentValue.Substring(3));
167 }
168 else
169 {
170 return Result<string>(Rvalue(contentValue));
171 }
172 }
173 public static Result<List<string>> ReadAllLines(const string& filePath)
174 {
175 List<string> lines;
176 bool start = true;
177 Result<StreamReader> readerResult = OpenRead(filePath);
178 if (readerResult.Error())
179 {
180 return Result<List<string>>(ErrorId(readerResult.GetErrorId()));
181 }
182 StreamReader& reader = readerResult.Value();
183 auto line = reader.ReadLine();
184 if (line.Error())
185 {
186 return Result<List<string>>(ErrorId(line.GetErrorId()));
187 }
188 string lineValue = Rvalue(line.Value());
189 while (!reader.EndOfStream())
190 {
191 if (start)
192 {
193 if (lineValue.Length() >= 3 && cast<byte>(lineValue[0]) == 239u && cast<byte>(lineValue[1]) == 187u && cast<byte>(lineValue[2]) == 191u )
194 {
195 lineValue = lineValue.Substring(3);
196 }
197 start = false;
198 }
199 lines.Add(Rvalue(lineValue));
200 line = reader.ReadLine();
201 if (line.Error())
202 {
203 return Result<List<string>>(ErrorId(line.GetErrorId()));
204 }
205 lineValue = Rvalue(line.Value());
206 }
207 if (!lineValue.IsEmpty())
208 {
209 lines.Add(Rvalue(lineValue));
210 }
211 return Result<List<string>>(lines);
212 }
213 public static Result<bool> Remove(const string& filePath)
214 {
215 int errorId = 0;
216 if (!RtmRemoveFile(filePath.Chars(), errorId))
217 {
218 return Result<bool>(ErrorId(errorId));
219 }
220 return Result<bool>(true);
221 }
222 public static Result<bool> Copy(const string& sourceFilePath, const string& targetFilePath)
223 {
224 int errorId = 0;
225 if (!RtmCopyFile(sourceFilePath.Chars(), targetFilePath.Chars(), errorId))
226 {
227 return Result<bool>(ErrorId(errorId));
228 }
229 return Result<bool>(true);
230 }
231 public static Result<bool> Move(const string& sourceFilePath, const string& targetFilePath)
232 {
233 int errorId = 0;
234 if (!RtmMoveFile(sourceFilePath.Chars(), targetFilePath.Chars(), errorId))
235 {
236 return Result<bool>(ErrorId(errorId));
237 }
238 return Result<bool>(true);
239 }
240 }