]>
Commit | Line | Data |
---|---|---|
6294ac2e | 1 | ///////////////////////////////////////////////////////////////////////////// |
7520f3da | 2 | // Name: src/msw/wince/filefn.cpp |
6294ac2e VZ |
3 | // Purpose: File- and directory-related functions |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
6294ac2e VZ |
7 | // Copyright: (c) 1998 Julian Smart |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
6294ac2e VZ |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
6294ac2e VZ |
21 | |
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
7520f3da WS |
26 | #include "wx/file.h" |
27 | ||
6294ac2e VZ |
28 | #include <ctype.h> |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
6294ac2e | 32 | |
08c63240 | 33 | #ifdef __WXWINCE__ |
45d9ef07 | 34 | #include "wx/msw/wince/missing.h" |
6294ac2e | 35 | |
52de37c7 | 36 | int wxCRT_Open(const wxChar *filename, int oflag, int WXUNUSED(pmode)) |
6294ac2e VZ |
37 | { |
38 | DWORD access = 0; | |
39 | DWORD shareMode = 0; | |
40 | DWORD disposition = 0; | |
41 | ||
42 | if ((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDONLY) | |
43 | { | |
44 | access = GENERIC_READ; | |
45 | shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE; | |
61577c61 | 46 | disposition = OPEN_EXISTING; |
6294ac2e VZ |
47 | } |
48 | else if ((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == O_WRONLY) | |
49 | { | |
50 | access = GENERIC_WRITE; | |
61577c61 | 51 | disposition = OPEN_ALWAYS; |
6294ac2e VZ |
52 | } |
53 | else if ((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDWR) | |
54 | { | |
55 | access = GENERIC_READ|GENERIC_WRITE; | |
61577c61 | 56 | disposition = OPEN_ALWAYS; |
6294ac2e | 57 | } |
61577c61 | 58 | |
6294ac2e VZ |
59 | if (oflag & O_APPEND) |
60 | { | |
61 | if ( wxFile::Exists(filename) ) | |
62 | { | |
63 | access |= GENERIC_WRITE; | |
64 | shareMode = FILE_SHARE_READ; | |
65 | disposition = OPEN_EXISTING; | |
66 | } | |
6294ac2e | 67 | else |
61577c61 | 68 | { |
6294ac2e | 69 | oflag |= O_TRUNC; |
61577c61 | 70 | } |
6294ac2e VZ |
71 | } |
72 | if (oflag & O_TRUNC) | |
73 | { | |
74 | access |= GENERIC_WRITE; | |
75 | shareMode = 0; | |
61577c61 | 76 | disposition = oflag & O_CREAT ? CREATE_ALWAYS : TRUNCATE_EXISTING; |
6294ac2e VZ |
77 | } |
78 | else if (oflag & O_CREAT) | |
79 | { | |
80 | access |= GENERIC_WRITE; | |
81 | shareMode = 0; | |
82 | disposition = CREATE_NEW; | |
83 | } | |
84 | else if (oflag & O_EXCL) | |
85 | { | |
86 | access |= GENERIC_WRITE; | |
87 | shareMode = 0; | |
88 | disposition = TRUNCATE_EXISTING; | |
89 | } | |
90 | ||
91 | int fd = 0; | |
92 | HANDLE fileHandle = ::CreateFile(filename, access, shareMode, NULL, | |
93 | disposition, FILE_ATTRIBUTE_NORMAL, 0); | |
94 | if (fileHandle == INVALID_HANDLE_VALUE) | |
95 | fd = -1; | |
96 | else | |
97 | fd = (int) fileHandle; | |
98 | ||
99 | return fd; | |
100 | } | |
101 | ||
52de37c7 | 102 | int wxCRT_Access(const wxChar *name, int WXUNUSED(how)) |
6294ac2e VZ |
103 | { |
104 | HANDLE fileHandle = ::CreateFile(name, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | |
105 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
106 | ||
107 | if (fileHandle == INVALID_HANDLE_VALUE) | |
108 | return -1; | |
109 | ||
110 | CloseHandle(fileHandle); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
5bd6ad08 VZ |
115 | int wxCRT_Chmod(const wxChar *WXUNUSED(name), int WXUNUSED(how)) |
116 | { | |
117 | // TODO | |
118 | return -1; | |
119 | } | |
120 | ||
6294ac2e VZ |
121 | int wxClose(int fd) |
122 | { | |
123 | if (CloseHandle((HANDLE)fd)) | |
124 | return 0; | |
125 | return -1; | |
126 | } | |
127 | ||
128 | int wxEof(int fd) | |
129 | { | |
ff24aeb7 | 130 | DWORD off0 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT); |
6294ac2e VZ |
131 | if (off0 == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
132 | return -1; | |
7520f3da | 133 | |
ff24aeb7 | 134 | DWORD off1 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_END); |
6294ac2e VZ |
135 | if (off1 == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
136 | return -1; | |
7520f3da | 137 | |
ff24aeb7 | 138 | if (off0 == off1) |
6294ac2e VZ |
139 | return 1; |
140 | else | |
141 | { | |
ff24aeb7 | 142 | SetFilePointer((HANDLE) fd, off0, NULL, FILE_BEGIN); |
6294ac2e VZ |
143 | return 0; |
144 | } | |
145 | } | |
146 | ||
147 | int wxRead(int fd, void *buf, unsigned int count) | |
148 | { | |
149 | DWORD bytesRead = 0; | |
150 | ||
151 | if (ReadFile((HANDLE) fd, buf, (DWORD) count, &bytesRead, NULL)) | |
152 | return bytesRead; | |
153 | else | |
154 | return -1; | |
155 | } | |
156 | ||
157 | int wxWrite(int fd, const void *buf, unsigned int count) | |
158 | { | |
159 | DWORD bytesWritten = 0; | |
160 | ||
161 | if (WriteFile((HANDLE) fd, buf, (DWORD) count, &bytesWritten, NULL)) | |
162 | return bytesWritten; | |
163 | else | |
164 | return -1; | |
165 | } | |
166 | ||
167 | __int64 wxSeek(int fd, __int64 offset, int origin) | |
168 | { | |
169 | int method; | |
170 | switch ( origin ) { | |
171 | default: | |
172 | wxFAIL_MSG(_("unknown seek origin")); | |
173 | ||
174 | case SEEK_SET: | |
175 | method = FILE_BEGIN; | |
176 | break; | |
177 | ||
178 | case SEEK_CUR: | |
179 | method = FILE_CURRENT; | |
180 | break; | |
181 | ||
182 | case SEEK_END: | |
183 | method = FILE_END; | |
184 | break; | |
185 | } | |
186 | ||
ff24aeb7 | 187 | DWORD res = SetFilePointer((HANDLE) fd, offset, NULL, method) ; |
6294ac2e VZ |
188 | if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
189 | { | |
190 | wxLogSysError(_("can't seek on file descriptor %d"), fd); | |
191 | return wxInvalidOffset; | |
192 | } | |
193 | else | |
194 | return (off_t)res; | |
195 | } | |
196 | ||
197 | __int64 wxTell(int fd) | |
198 | { | |
ff24aeb7 JS |
199 | // WinCE apparently doesn't support lpDistanceToMoveHigh. |
200 | // LONG high = 0; | |
201 | DWORD res = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT) ; | |
6294ac2e VZ |
202 | if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
203 | { | |
204 | wxLogSysError(_("can't get seek position on file descriptor %d"), fd); | |
205 | return wxInvalidOffset; | |
206 | } | |
207 | else | |
ff24aeb7 | 208 | return res ; // + (((__int64)high) << 32); |
6294ac2e VZ |
209 | } |
210 | ||
211 | int wxFsync(int WXUNUSED(fd)) | |
212 | { | |
213 | return 0; | |
214 | } | |
215 | ||
08c63240 | 216 | #endif //__WXWINCE__ |