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 string OpenFlagStr(OpenFlags flags)
15 {
16 string s;
17 if ((flags & OpenFlags.read) != OpenFlags.none)
18 {
19 s.Append('r');
20 }
21 if ((flags & OpenFlags.write) != OpenFlags.none)
22 {
23 s.Append('w');
24 }
25 if ((flags & OpenFlags.create) != OpenFlags.none)
26 {
27 s.Append('c');
28 }
29 if ((flags & OpenFlags.append) != OpenFlags.none)
30 {
31 s.Append('a');
32 }
33 if ((flags & OpenFlags.truncate) != OpenFlags.none)
34 {
35 s.Append('u');
36 }
37 if ((flags & OpenFlags.text) != OpenFlags.none)
38 {
39 s.Append('t');
40 }
41 if ((flags & OpenFlags.random_access) != OpenFlags.none)
42 {
43 s.Append('n');
44 }
45 return s;
46 }
47
48 public class FileSystemException : Exception
49 {
50 public nothrow FileSystemException(const string& message_) : base(message_)
51 {
52 }
53 }
54
55 public class FileStream : Stream
56 {
57 public nothrow FileStream(int fd_) : this(fd_, false)
58 {
59 }
60 public nothrow FileStream(int fd_, bool closeOnExit_) : fd(fd_), closeOnExit(closeOnExit_)
61 {
62 }
63 public FileStream(const string& filePath, OpenFlags flags) : this(filePath, flags, 0)
64 {
65 }
66 public FileStream(const string& filePath, OpenFlags flags, int mode) : fd(-1), closeOnExit(false)
67 {
68 Open(filePath, flags, mode);
69 }
70 suppress FileStream(const FileStream&);
71 suppress void operator=(const FileStream&);
72 public nothrow FileStream(FileStream&& that) : fd(that.fd), closeOnExit(that.closeOnExit)
73 {
74 that.fd = -1;
75 that.closeOnExit = false;
76 }
77 public default nothrow void operator=(FileStream&&);
78 public override ~FileStream()
79 {
80 try
81 {
82 Close();
83 }
84 catch (const Exception& ex)
85 {
86 }
87 }
88 public inline nothrow bool IsOpen() const
89 {
90 return fd != -1;
91 }
92 public void Open(const string& filePath, OpenFlags flags, int mode)
93 {
94 Close();
95 try
96 {
97 fd = System.Os.Open(filePath.Chars(), flags, mode);
98 closeOnExit = true;
99 }
100 catch (const Exception& ex)
101 {
102 throw FileSystemException("error opening file '" + filePath + "' with flags '" + OpenFlagStr(flags) + "': " + ex.Message());
103 }
104 }
105 public void Close()
106 {
107 if (fd != -1 && closeOnExit)
108 {
109 System.Os.Close(fd);
110 fd = -1;
111 closeOnExit = false;
112 }
113 }
114 public override int ReadByte()
115 {
116 byte x;
117 long retval = Read(&x, 1);
118 if (retval == 0)
119 {
120 return -1;
121 }
122 return x;
123 }
124 public override long Read(byte* buf, long count)
125 {
126 return System.Os.Read(fd, buf, count);
127 }
128 public override long Write(byte x)
129 {
130 return Write(&x, 1);
131 }
132 public override long Write(byte* buf, long count)
133 {
134 return System.Os.Write(fd, buf, count);
135 }
136 public override long Seek(long pos, Origin origin)
137 {
138 return System.Os.Seek(fd, pos, origin);
139 }
140 public override long Tell()
141 {
142 return System.Os.Tell(fd);
143 }
144 public inline nothrow int Descriptor() const
145 {
146 return fd;
147 }
148 public override bool IsHostTextFile() const
149 {
150 int result = System.Os.IOCtl(fd, IOControlItem.isHostTextFile, null, 0);
151 return result == 1;
152 }
153 public override bool IsConsole() const
154 {
155 int result = System.Os.IOCtl(fd, IOControlItem.isConsole, null, 0);
156 return result == 1;
157 }
158 private int fd;
159 private bool closeOnExit;
160 }
161 }