Add wxFileName::SetPermissions().
[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 // Copyright: (c) 1998 Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/file.h"
27
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #ifdef __WXWINCE__
34 #include "wx/msw/wince/missing.h"
35
36 int wxCRT_Open(const wxChar *filename, int oflag, int WXUNUSED(pmode))
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;
46 disposition = OPEN_EXISTING;
47 }
48 else if ((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == O_WRONLY)
49 {
50 access = GENERIC_WRITE;
51 disposition = OPEN_ALWAYS;
52 }
53 else if ((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDWR)
54 {
55 access = GENERIC_READ|GENERIC_WRITE;
56 disposition = OPEN_ALWAYS;
57 }
58
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 }
67 else
68 {
69 oflag |= O_TRUNC;
70 }
71 }
72 if (oflag & O_TRUNC)
73 {
74 access |= GENERIC_WRITE;
75 shareMode = 0;
76 disposition = oflag & O_CREAT ? CREATE_ALWAYS : TRUNCATE_EXISTING;
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
102 int wxCRT_Access(const wxChar *name, int WXUNUSED(how))
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
115 int wxCRT_Chmod(const wxChar *WXUNUSED(name), int WXUNUSED(how))
116 {
117 // TODO
118 return -1;
119 }
120
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 {
130 DWORD off0 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT);
131 if (off0 == 0xFFFFFFFF && GetLastError() != NO_ERROR)
132 return -1;
133
134 DWORD off1 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_END);
135 if (off1 == 0xFFFFFFFF && GetLastError() != NO_ERROR)
136 return -1;
137
138 if (off0 == off1)
139 return 1;
140 else
141 {
142 SetFilePointer((HANDLE) fd, off0, NULL, FILE_BEGIN);
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
187 DWORD res = SetFilePointer((HANDLE) fd, offset, NULL, method) ;
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 {
199 // WinCE apparently doesn't support lpDistanceToMoveHigh.
200 // LONG high = 0;
201 DWORD res = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT) ;
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
208 return res ; // + (((__int64)high) << 32);
209 }
210
211 int wxFsync(int WXUNUSED(fd))
212 {
213 return 0;
214 }
215
216 #endif //__WXWINCE__