blob: f7a4f16232c25c5fd614e1ed0d348d26420f5f4e [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/file_io.h>
20#include <pj/errno.h>
21#include <pj/assert.h>
22
23#include <windows.h>
24
25#ifndef INVALID_SET_FILE_POINTER
26# define INVALID_SET_FILE_POINTER ((DWORD)-1)
27#endif
28
29/**
30 * Check for end-of-file condition on the specified descriptor.
31 *
32 * @param fd The file descriptor.
33 * @param access The desired access.
34 *
35 * @return Non-zero if file is EOF.
36 */
37PJ_DECL(pj_bool_t) pj_file_eof(pj_oshandle_t fd,
38 enum pj_file_access access);
39
40
41PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool,
42 const char *pathname,
43 unsigned flags,
44 pj_oshandle_t *fd)
45{
46 HANDLE hFile;
47 DWORD dwDesiredAccess = 0;
48 DWORD dwShareMode = 0;
49 DWORD dwCreationDisposition = 0;
50 DWORD dwFlagsAndAttributes = 0;
51
52 PJ_UNUSED_ARG(pool);
53
54 PJ_ASSERT_RETURN(pathname!=NULL, PJ_EINVAL);
55
56 if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) {
57 dwDesiredAccess |= GENERIC_WRITE;
58 if ((flags & PJ_O_APPEND) == PJ_O_APPEND) {
59 dwDesiredAccess |= FILE_APPEND_DATA;
60 } else {
61 dwDesiredAccess &= ~(FILE_APPEND_DATA);
62 dwCreationDisposition |= CREATE_ALWAYS;
63 }
64 }
65 if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) {
66 dwDesiredAccess |= GENERIC_READ;
67 if (flags == PJ_O_RDONLY)
68 dwCreationDisposition |= OPEN_EXISTING;
69 }
70
71 if (dwDesiredAccess == 0) {
72 pj_assert(!"Invalid file open flags");
73 return PJ_EINVAL;
74 }
75
76 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
77 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
78
79 hFile = CreateFile(pathname, dwDesiredAccess, dwShareMode, NULL,
80 dwCreationDisposition, dwFlagsAndAttributes, NULL);
81 if (hFile == INVALID_HANDLE_VALUE) {
82 *fd = 0;
83 return PJ_RETURN_OS_ERROR(GetLastError());
84 }
85
86 *fd = hFile;
87 return PJ_SUCCESS;
88}
89
90PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd)
91{
92 if (CloseHandle(fd)==0)
93 return PJ_RETURN_OS_ERROR(GetLastError());
94 return PJ_SUCCESS;
95}
96
97PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd,
98 const void *data,
99 pj_ssize_t *size)
100{
101 BOOL rc;
102 DWORD bytesWritten;
103
104 rc = WriteFile(fd, data, *size, &bytesWritten, NULL);
105 if (!rc) {
106 *size = -1;
107 return PJ_RETURN_OS_ERROR(GetLastError());
108 }
109
110 *size = bytesWritten;
111 return PJ_SUCCESS;
112}
113
114PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd,
115 void *data,
116 pj_ssize_t *size)
117{
118 BOOL rc;
119 DWORD bytesRead;
120
121 rc = ReadFile(fd, data, *size, &bytesRead, NULL);
122 if (!rc) {
123 *size = -1;
124 return PJ_RETURN_OS_ERROR(GetLastError());
125 }
126
127 *size = bytesRead;
128 return PJ_SUCCESS;
129}
130
131/*
132PJ_DEF(pj_bool_t) pj_file_eof(pj_oshandle_t fd, enum pj_file_access access)
133{
134 BOOL rc;
135 DWORD dummy = 0, bytes;
136 DWORD dwStatus;
137
138 if ((access & PJ_O_RDONLY) == PJ_O_RDONLY) {
139 rc = ReadFile(fd, &dummy, 0, &bytes, NULL);
140 } else if ((access & PJ_O_WRONLY) == PJ_O_WRONLY) {
141 rc = WriteFile(fd, &dummy, 0, &bytes, NULL);
142 } else {
143 pj_assert(!"Invalid access");
144 return PJ_TRUE;
145 }
146
147 dwStatus = GetLastError();
148 if (dwStatus==ERROR_HANDLE_EOF)
149 return PJ_TRUE;
150
151 return 0;
152}
153*/
154
155PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd,
156 pj_off_t offset,
157 enum pj_file_seek_type whence)
158{
159 DWORD dwMoveMethod;
160 DWORD dwNewPos;
161 LONG hi32;
162
163 if (whence == PJ_SEEK_SET)
164 dwMoveMethod = FILE_BEGIN;
165 else if (whence == PJ_SEEK_CUR)
166 dwMoveMethod = FILE_CURRENT;
167 else if (whence == PJ_SEEK_END)
168 dwMoveMethod = FILE_END;
169 else {
170 pj_assert(!"Invalid whence in file_setpos");
171 return PJ_EINVAL;
172 }
173
174 hi32 = (LONG)(offset >> 32);
175 dwNewPos = SetFilePointer(fd, (long)offset, &hi32, dwMoveMethod);
176 if (dwNewPos == (DWORD)INVALID_SET_FILE_POINTER) {
177 DWORD dwStatus = GetLastError();
178 if (dwStatus != 0)
179 return PJ_RETURN_OS_ERROR(dwStatus);
180 /* dwNewPos actually is not an error. */
181 }
182
183 return PJ_SUCCESS;
184}
185
186PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd,
187 pj_off_t *pos)
188{
189 LONG hi32 = 0;
190 DWORD lo32;
191
192 lo32 = SetFilePointer(fd, 0, &hi32, FILE_CURRENT);
193 if (lo32 == (DWORD)INVALID_SET_FILE_POINTER) {
194 DWORD dwStatus = GetLastError();
195 if (dwStatus != 0)
196 return PJ_RETURN_OS_ERROR(dwStatus);
197 }
198
199 *pos = hi32;
200 *pos = (*pos << 32) + lo32;
201 return PJ_SUCCESS;
202}
203