~ubuntu-branches/ubuntu/utopic/sflphone/utopic-proposed

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjlib/src/pj/file_io_win32.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-06-30 11:40:56 UTC
  • mfrom: (4.1.18 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130630114056-0np50jkyqo6vnmii
Tags: 1.2.3-2
* changeset_r92d62cfc54732bbbcfff2b1d36c096b120b981a5.diff 
  - fixes automatic endian detection 
* Update Vcs: fixes vcs-field-not-canonical

Show diffs side-by-side

added added

removed removed

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