| 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 | DWORD off0 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT); |
| 126 | if (off0 == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
| 127 | return -1; |
| 128 | |
| 129 | DWORD off1 = SetFilePointer((HANDLE) fd, 0, NULL, FILE_END); |
| 130 | if (off1 == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
| 131 | return -1; |
| 132 | |
| 133 | if (off0 == off1) |
| 134 | return 1; |
| 135 | else |
| 136 | { |
| 137 | SetFilePointer((HANDLE) fd, off0, NULL, FILE_BEGIN); |
| 138 | return 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int wxRead(int fd, void *buf, unsigned int count) |
| 143 | { |
| 144 | DWORD bytesRead = 0; |
| 145 | |
| 146 | if (ReadFile((HANDLE) fd, buf, (DWORD) count, &bytesRead, NULL)) |
| 147 | return bytesRead; |
| 148 | else |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | int wxWrite(int fd, const void *buf, unsigned int count) |
| 153 | { |
| 154 | DWORD bytesWritten = 0; |
| 155 | |
| 156 | if (WriteFile((HANDLE) fd, buf, (DWORD) count, &bytesWritten, NULL)) |
| 157 | return bytesWritten; |
| 158 | else |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | __int64 wxSeek(int fd, __int64 offset, int origin) |
| 163 | { |
| 164 | int method; |
| 165 | switch ( origin ) { |
| 166 | default: |
| 167 | wxFAIL_MSG(_("unknown seek origin")); |
| 168 | |
| 169 | case SEEK_SET: |
| 170 | method = FILE_BEGIN; |
| 171 | break; |
| 172 | |
| 173 | case SEEK_CUR: |
| 174 | method = FILE_CURRENT; |
| 175 | break; |
| 176 | |
| 177 | case SEEK_END: |
| 178 | method = FILE_END; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | DWORD res = SetFilePointer((HANDLE) fd, offset, NULL, 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 | // WinCE apparently doesn't support lpDistanceToMoveHigh. |
| 195 | // LONG high = 0; |
| 196 | DWORD res = SetFilePointer((HANDLE) fd, 0, NULL, FILE_CURRENT) ; |
| 197 | if (res == 0xFFFFFFFF && GetLastError() != NO_ERROR) |
| 198 | { |
| 199 | wxLogSysError(_("can't get seek position on file descriptor %d"), fd); |
| 200 | return wxInvalidOffset; |
| 201 | } |
| 202 | else |
| 203 | return res ; // + (((__int64)high) << 32); |
| 204 | } |
| 205 | |
| 206 | int wxFsync(int WXUNUSED(fd)) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | #endif //__WXWINCE__ |