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