]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/filefnwce.cpp
Remove all lines containing cvs/svn "$Id$" keyword.
[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 wxClose(int fd)
116 {
117 if (CloseHandle((HANDLE)fd))
118 return 0;
119 return -1;
120 }
121
122 int wxEof(int fd)
123 {
124 DWORD off0 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT);
125 if (off0 == 0xFFFFFFFF && GetLastError() != NO_ERROR)
126 return -1;
127
128 DWORD off1 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_END);
129 if (off1 == 0xFFFFFFFF && GetLastError() != NO_ERROR)
130 return -1;
131
132 if (off0 == off1)
133 return 1;
134 else
135 {
136 SetFilePointer((HANDLE) fd, off0, NULL, 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 DWORD res = SetFilePointer((HANDLE) fd, offset, NULL, method) ;
182 if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR)
183 {
184 wxLogSysError(_("can't seek on file descriptor %d"), fd);
185 return wxInvalidOffset;
186 }
187 else
188 return (off_t)res;
189 }
190
191 __int64 wxTell(int fd)
192 {
193 // WinCE apparently doesn't support lpDistanceToMoveHigh.
194 // LONG high = 0;
195 DWORD res = SetFilePointer((HANDLE) fd, 0, NULL, 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
210 #endif //__WXWINCE__