summaryrefslogtreecommitdiff
path: root/store/source/fileos2.hxx
blob: 8e74054a30d37f41cb3eaea6b5d8f12b572d60ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: fileos2.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 08:41:12 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef INCLUDED_STORE_FILEOS2_HXX
#define INCLUDED_STORE_FILEOS2_HXX

#define INCL_DOS
#define INCL_DOSERRORS
#include <os2def.h>
#include <bsedos.h>
#include <bseerr.h>

typedef HFILE HSTORE;

/*========================================================================
 *
 * File I/O (inline) implementation.
 *
 *======================================================================*/
/*
 * __store_errcode_map.
 */
static const __store_errcode_mapping_st __store_errcode_map[] =
{
    { NO_ERROR,                   store_E_None             },
    { ERROR_FILE_NOT_FOUND,       store_E_NotExists        },
    { ERROR_PATH_NOT_FOUND,       store_E_NotExists        },
    { ERROR_ACCESS_DENIED,        store_E_AccessViolation  },
    { ERROR_SHARING_VIOLATION,    store_E_AccessViolation  },
    { ERROR_LOCK_VIOLATION,       store_E_LockingViolation },
    { ERROR_INVALID_ACCESS,       store_E_InvalidAccess    },
    { ERROR_INVALID_HANDLE,       store_E_InvalidHandle    },
    { ERROR_INVALID_PARAMETER,    store_E_InvalidParameter },
    { ERROR_FILENAME_EXCED_RANGE, store_E_NameTooLong      },
    { ERROR_TOO_MANY_OPEN_FILES,  store_E_NoMoreFiles      }
};

/*
 * __store_errno.
 */
inline sal_Int32 __store_errno (void)
{
    return (sal_Int32)errno;
}

/*
 * __store_malign (unsupported).
 */
inline sal_uInt32 __store_malign (void)
{
    return (sal_uInt32)(-1);
}

/*
 * __store_fmap (readonly, unsupported).
 */
inline HSTORE __store_fmap (HSTORE hFile)
{
    return ((HSTORE)0);
}

/*
 * __store_funmap.
 */
inline void __store_funmap (HSTORE hMap)
{
}

/*
 * __store_mmap (readonly, unsupported).
 */
inline sal_uInt8* __store_mmap (HSTORE h, sal_uInt32 k, sal_uInt32 n)
{
    return (sal_uInt8*)NULL;
}

/*
 * __store_munmap (unsupported).
 */
inline void __store_munmap (sal_uInt8 *p, sal_uInt32 n)
{
}

/*
 * __store_fopen.
 */
inline storeError __store_fopen (
    const sal_Char *pszName, sal_uInt32 nMode, HSTORE &rhFile)
{
    // Initialize [out] param.
    rhFile = 0;

    // Access mode.
    sal_uInt32 nAccessMode = OPEN_ACCESS_READONLY;
    if (nMode & store_File_OpenWrite)
        nAccessMode = OPEN_ACCESS_READWRITE;

    if (nAccessMode == OPEN_ACCESS_READONLY)
    {
        nMode |=  store_File_OpenNoCreate;
        nMode &= ~store_File_OpenTruncate;
    }

    // Share mode.
    sal_uInt32 nShareMode = OPEN_SHARE_DENYNONE;
    if (nMode & store_File_OpenWrite)
        nShareMode = OPEN_SHARE_DENYWRITE;

    // Open action.
    sal_uInt32 nOpenAction = 0, nDoneAction = 0;
    if (!(nMode & store_File_OpenNoCreate))
        nOpenAction = OPEN_ACTION_CREATE_IF_NEW; // Open always.
    else
        nOpenAction = OPEN_ACTION_FAIL_IF_NEW;   // Open existing.

    if (nMode & store_File_OpenTruncate)
        nOpenAction |= OPEN_ACTION_REPLACE_IF_EXISTS;
    else
        nOpenAction |= OPEN_ACTION_OPEN_IF_EXISTS;

    // Create file handle.
    APIRET result = ::DosOpen (
        pszName,
        &rhFile,
        &nDoneAction,
        0L,
        FILE_NORMAL,
        nOpenAction,
        nAccessMode | nShareMode | OPEN_FLAGS_NOINHERIT,
        0L);

    // Check result.
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fread.
 */
inline storeError __store_fread (
    HSTORE h, sal_uInt32 offset, void *p, sal_uInt32 n, sal_uInt32 &k)
{
    APIRET result;
    if ((result = ::DosSetFilePtr (h, (long)offset, FILE_BEGIN, &k)) != 0)
        return ERROR_FROM_NATIVE(result);
    if ((result = ::DosRead (h, p, n, &k)) != 0)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fwrite.
 */
inline storeError __store_fwrite (
    HSTORE h, sal_uInt32 offset, const void *p, sal_uInt32 n, sal_uInt32 &k)
{
    APIRET result;
    if ((result = ::DosSetFilePtr (h, (long)offset, FILE_BEGIN, &k)) != 0)
        return ERROR_FROM_NATIVE(result);
    if ((result = ::DosWrite (h, (PVOID)p, n, &k)) != 0)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fseek.
 */
inline storeError __store_fseek (HSTORE h, sal_uInt32 n)
{
    sal_uInt32 k = 0;
    APIRET result = ::DosSetFilePtr (h, (long)n, FILE_BEGIN, &k);
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fsize.
 */
inline storeError __store_fsize (HSTORE h, sal_uInt32 &k)
{
    APIRET result = ::DosSetFilePtr (h, 0L, FILE_END, &k);
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_ftrunc.
 */
inline storeError __store_ftrunc (HSTORE h, sal_uInt32 n)
{
    APIRET result = ::DosSetFileSize (h, n);
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fsync.
 */
inline storeError __store_fsync (HSTORE h)
{
    APIRET result = ::DosResetBuffer (h);
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

/*
 * __store_fclose.
 */
inline storeError __store_fclose (HSTORE h)
{
    APIRET result = ::DosClose (h);
    if (result)
        return ERROR_FROM_NATIVE(result);
    else
        return store_E_None;
}

#endif /* INCLUDED_STORE_FILEOS2_HXX */