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