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