이미 열린 파일은 닫고, 파일을 새로 연다.
79{
82
84#if __DCL_WINDOWS
85 __fileType = UNKNOWN;
88
89 FileType fileType = UNKNOWN;
90
91 HANDLE
handle = CreateFileW(_path, GENERIC_READ, 0,
NULL, OPEN_EXISTING, 0,
NULL);
93 DWORD dw;
94 DCB dcb;
96 fileType = PIPE;
97 else if (GetConsoleMode(
handle, &dw))
98 fileType = CONSOLE;
99 else if (GetCommState(
handle, &dcb))
100 fileType = COMM;
103 }
104
105 DWORD dwDesiredAccess = GENERIC_READ;
106 DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
107 SECURITY_ATTRIBUTES securityAttributes = {
sizeof(SECURITY_ATTRIBUTES),
NULL,
TRUE };
108 DWORD dwCreationDisposition = OPEN_EXISTING;
109 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
110
111 switch (_oflags & (READONLY | WRITEONLY | READWRITE)) {
112 default:
113
114 case READONLY:
115 dwDesiredAccess = GENERIC_READ;
116 break;
117 case WRITEONLY:
118 if (_oflags & APPEND)
119 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
120 else
121 dwDesiredAccess = GENERIC_WRITE;
122 break;
123 case READWRITE:
124 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
125 break;
126 }
127
128 if (_oflags & EXCLUSIVE) {
129 dwShareMode = 0;
130 }
131
132 switch (_oflags & (CREATE | EXCLUSIVE | TRUNCATE)) {
133 default:
134 case EXCLUSIVE:
135 dwCreationDisposition = OPEN_EXISTING;
136 break;
137 case CREATE:
138 case CREATE | TRUNCATE:
139 dwCreationDisposition = OPEN_ALWAYS;
140 break;
141 case CREATE | EXCLUSIVE:
142 case CREATE | EXCLUSIVE | TRUNCATE:
143 dwCreationDisposition = CREATE_NEW;
144 break;
145 case TRUNCATE:
146 case TRUNCATE | EXCLUSIVE:
147 dwCreationDisposition = TRUNCATE_EXISTING;
148 break;
149 }
150
151 if (_oflags & NONBLOCK)
152 dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
153
154 if (_oflags & SYNC)
155 dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
156
157 if (fileType == CONSOLE || fileType == PIPE) {
158
159 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode, &securityAttributes,
160 OPEN_EXISTING, dwFlagsAndAttributes,
NULL);
161 }
162 else if (fileType == COMM) {
163 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode, &securityAttributes,
164 OPEN_EXISTING, dwFlagsAndAttributes,
NULL);
165 }
166 else {
167
168 switch (dwDesiredAccess & (GENERIC_READ | GENERIC_WRITE)) {
169 case GENERIC_READ:
170 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
171 break;
172 case GENERIC_READ | GENERIC_WRITE:
173 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
174 break;
175 }
176
177 if ((_oflags & TRUNCATE) && ((dwDesiredAccess & GENERIC_WRITE) == GENERIC_WRITE))
178 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode,
179 &securityAttributes, TRUNCATE_EXISTING, dwFlagsAndAttributes,
NULL);
180
182 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode,
183 &securityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
NULL);
184
186 && (_oflags & APPEND) && ((dwDesiredAccess & GENERIC_READ) == GENERIC_READ)) {
187 fileType = REGULAR;
188 if (SetFilePointer(
handle, 0,
NULL, FILE_END) == INVALID_SET_FILE_POINTER) {
189 DWORD dwSaveError = GetLastError();
192 SetLastError(dwSaveError);
193 }
194 }
195 }
196
197
198 HANDLE readEvent =
NULL, writeEvent =
NULL;
200 __DCL_ASSERT(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);
201 if (_oflags & (WRITEONLY | READWRITE))
203
204
205 if (_oflags & (READONLY | READWRITE) || writeEvent ==
NULL)
207
208 if (readEvent ==
NULL && writeEvent ==
NULL) {
209 DWORD dwSaveError = GetLastError();
210 if (readEvent)
211 CloseHandle(readEvent);
212 if (writeEvent)
213 CloseHandle(writeEvent);
214
217 SetLastError(dwSaveError);
218 }
219 }
220
222 throw new IOException(_path, GetLastError());
223
224 __fileType = fileType;
225 __readEvent = readEvent;
226 __writeEvent = writeEvent;
227#else
228 int oflags = 0;
229 if (_oflags & READONLY)
230 oflags |= O_RDONLY;
231 if (_oflags & WRITEONLY)
232 oflags |= O_WRONLY;
233 if (_oflags & READWRITE)
234 oflags |= O_RDWR;
235 if (_oflags & CREATE)
236 oflags |= O_CREAT;
237 if (_oflags & EXCLUSIVE)
238 oflags |= O_EXCL;
239 if (_oflags & NOCTTY)
240 oflags |= O_NOCTTY;
241 if (_oflags & APPEND)
242 oflags |= O_APPEND;
243 if (_oflags & TRUNCATE)
244 oflags |= O_TRUNC;
245 if (_oflags & NONBLOCK)
246 oflags |= O_NONBLOCK;
247 if (_oflags & SYNC)
248 oflags |= O_SYNC;
249
252 throw new IOException(_path, errno);
253#endif
256}
DCLCAPI int __open(const String &_path, int _oflags,...)
#define __DCL_ASSERT(expr)