1
2
3
4
5
6 using System;
7
8 namespace System.IO
9 {
10 public const int stdin = 0;
11 public const int stdout = 1;
12 public const int stderr = 2;
13
14 public nothrow bool IsConsoleHandle(int handle)
15 {
16 return RtIsConsoleHandle(handle);
17 }
18
19 public enum OpenMode : byte
20 {
21 none = 0u,
22 read = 1u << 0u,
23 write = 1u << 1u,
24 append = 1u << 2u,
25 binary = 1u << 3u
26 }
27
28 public class FileSystemException : Exception
29 {
30 public nothrow FileSystemException(const string& message_) : base(message_)
31 {
32 }
33 }
34
35 public class FileByteStream : ByteStream
36 {
37 public FileByteStream(int handle) : file(null)
38 {
39 int errorStringHandle = -1;
40 file = RtOpenStdFile(handle, errorStringHandle);
41 if (file == null)
42 {
43 string errorMessage = RtGetError(errorStringHandle);
44 RtDisposeError(errorStringHandle);
45 throw FileSystemException(errorMessage);
46 }
47 }
48 public FileByteStream(const string& filePath, OpenMode openMode) : file(null)
49 {
50 Open(filePath, openMode);
51 }
52 suppress FileByteStream(const FileByteStream&);
53 suppress void operator=(const FileByteStream&);
54 public nothrow FileByteStream(FileByteStream&& that) : file(that.file)
55 {
56 that.file = null;
57 }
58 public default nothrow void operator=(FileByteStream&&);
59 public override ~FileByteStream()
60 {
61 try
62 {
63 if (file != null)
64 {
65 int errorStringHandle = -1;
66 RtDisposeFile(file, errorStringHandle);
67 }
68 }
69 catch (const Exception&)
70 {
71 }
72 }
73 public inline nothrow bool IsOpen() const
74 {
75 return file != null;
76 }
77 public void Open(const string& filePath, OpenMode openMode)
78 {
79 Close();
80 int errorStringHandle = -1;
81 file = RtOpen(filePath.Chars(), openMode, errorStringHandle);
82 if (file == null)
83 {
84 string errorMessage = RtGetError(errorStringHandle);
85 RtDisposeError(errorStringHandle);
86 throw FileSystemException(errorMessage);
87 }
88 }
89 public void Close()
90 {
91 if (file != null)
92 {
93 int errorStringHandle = -1;
94 if (!RtClose(file, errorStringHandle))
95 {
96 string errorMessage = RtGetError(errorStringHandle);
97 RtDisposeError(errorStringHandle);
98 throw FileSystemException(errorMessage);
99 }
100 file = null;
101 }
102 }
103 public override int ReadByte()
104 {
105 int result = RtReadByte(file);
106 if (result == -1)
107 {
108 if (RtEof(file))
109 {
110 return -1;
111 }
112 else
113 {
114 int errorStringHandle = -1;
115 if (RtGetFileError(file, errorStringHandle))
116 {
117 string errorMessage = RtGetError(errorStringHandle);
118 RtDisposeError(errorStringHandle);
119 throw FileSystemException(errorMessage);
120 }
121 }
122 }
123 return result;
124 }
125 public override long Read(byte* buffer, long count)
126 {
127 if (count < 0)
128 {
129 ThrowInvalidParameterException();
130 }
131 int errorStringHandle = -1;
132 long result = RtRead(file, buffer, count, errorStringHandle);
133 if (result == -1)
134 {
135 string errorMessage = RtGetError(errorStringHandle);
136 RtDisposeError(errorStringHandle);
137 throw FileSystemException(errorMessage);
138 }
139 return result;
140 }
141 public override void Write(byte x)
142 {
143 int errorStringHandle = -1;
144 if (!RtWriteByte(file, x, errorStringHandle))
145 {
146 string errorMessage = RtGetError(errorStringHandle);
147 RtDisposeError(errorStringHandle);
148 throw FileSystemException(errorMessage);
149 }
150 }
151 public override void Write(byte* buffer, long count)
152 {
153 if (count < 0)
154 {
155 ThrowInvalidParameterException();
156 }
157 int errorStringHandle = -1;
158 long result = RtWrite(file, buffer, count, errorStringHandle);
159 if (result == -1)
160 {
161 string errorMessage = RtGetError(errorStringHandle);
162 RtDisposeError(errorStringHandle);
163 throw FileSystemException(errorMessage);
164 }
165 }
166 public override void Seek(long pos, Origin origin)
167 {
168 int errorStringHandle = -1;
169 if (!RtSeek(file, pos, origin, errorStringHandle))
170 {
171 string errorMessage = RtGetError(errorStringHandle);
172 RtDisposeError(errorStringHandle);
173 throw FileSystemException(errorMessage);
174 }
175 }
176 public override long Tell()
177 {
178 int errorStringHandle = -1;
179 long result = RtTell(file, errorStringHandle);
180 if (result == -1)
181 {
182 string errorMessage = RtGetError(errorStringHandle);
183 RtDisposeError(errorStringHandle);
184 throw FileSystemException(errorMessage);
185 }
186 return result;
187 }
188 public override void Flush()
189 {
190 int errorStringHandle = -1;
191 if (!RtFlush(file, errorStringHandle))
192 {
193 string errorMessage = RtGetError(errorStringHandle);
194 RtDisposeError(errorStringHandle);
195 throw FileSystemException(errorMessage);
196 }
197 }
198 private void* file;
199 }
200 }
201
202 namespace System
203 {
204 public class Endl
205 {
206 public nothrow Endl()
207 {
208 }
209 }
210
211 public nothrow Endl endl()
212 {
213 return Endl();
214 }
215 }