xref: /aoo42x/main/tools/source/stream/strmwnt.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // no include "precompiled_tools.hxx" because this file is included in strmsys.cxx
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir /*
31*cdf0e10cSrcweir     Todo: StreamMode <-> AllocateMemory
32*cdf0e10cSrcweir */
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <string.h>
35*cdf0e10cSrcweir #include <limits.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <tools/svwin.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <tools/debug.hxx>
40*cdf0e10cSrcweir #include <tools/fsys.hxx>
41*cdf0e10cSrcweir #include <tools/stream.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // class FileBase
44*cdf0e10cSrcweir #include <osl/file.hxx>
45*cdf0e10cSrcweir using namespace osl;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir // -----------------------------------------------------------------------
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir // --------------
50*cdf0e10cSrcweir // - StreamData -
51*cdf0e10cSrcweir // --------------
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir class StreamData
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir public:
56*cdf0e10cSrcweir     HANDLE      hFile;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir                 StreamData()
59*cdf0e10cSrcweir                 {
60*cdf0e10cSrcweir                     hFile = 0;
61*cdf0e10cSrcweir                 }
62*cdf0e10cSrcweir };
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir // -----------------------------------------------------------------------
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir static sal_uIntPtr GetSvError( DWORD nWntError )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     static struct { DWORD wnt; sal_uIntPtr sv; } errArr[] =
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir         { ERROR_SUCCESS,                SVSTREAM_OK },
71*cdf0e10cSrcweir         { ERROR_ACCESS_DENIED,          SVSTREAM_ACCESS_DENIED },
72*cdf0e10cSrcweir         { ERROR_ACCOUNT_DISABLED,       SVSTREAM_ACCESS_DENIED },
73*cdf0e10cSrcweir         { ERROR_ACCOUNT_EXPIRED,        SVSTREAM_ACCESS_DENIED },
74*cdf0e10cSrcweir         { ERROR_ACCOUNT_RESTRICTION,    SVSTREAM_ACCESS_DENIED },
75*cdf0e10cSrcweir         { ERROR_ATOMIC_LOCKS_NOT_SUPPORTED, SVSTREAM_INVALID_PARAMETER },
76*cdf0e10cSrcweir         { ERROR_BAD_PATHNAME,           SVSTREAM_PATH_NOT_FOUND },
77*cdf0e10cSrcweir         // Filename too long
78*cdf0e10cSrcweir         { ERROR_BUFFER_OVERFLOW,        SVSTREAM_INVALID_PARAMETER },
79*cdf0e10cSrcweir         { ERROR_DIRECTORY,              SVSTREAM_INVALID_PARAMETER },
80*cdf0e10cSrcweir         { ERROR_DRIVE_LOCKED,           SVSTREAM_LOCKING_VIOLATION },
81*cdf0e10cSrcweir         { ERROR_FILE_NOT_FOUND,         SVSTREAM_FILE_NOT_FOUND },
82*cdf0e10cSrcweir         { ERROR_FILENAME_EXCED_RANGE,   SVSTREAM_INVALID_PARAMETER },
83*cdf0e10cSrcweir         { ERROR_INVALID_ACCESS,         SVSTREAM_INVALID_ACCESS },
84*cdf0e10cSrcweir         { ERROR_INVALID_DRIVE,          SVSTREAM_PATH_NOT_FOUND },
85*cdf0e10cSrcweir         { ERROR_INVALID_HANDLE,         SVSTREAM_INVALID_HANDLE },
86*cdf0e10cSrcweir         { ERROR_INVALID_NAME,           SVSTREAM_PATH_NOT_FOUND },
87*cdf0e10cSrcweir         { ERROR_INVALID_PARAMETER,      SVSTREAM_INVALID_PARAMETER },
88*cdf0e10cSrcweir         { ERROR_IS_SUBST_PATH,          SVSTREAM_INVALID_PARAMETER },
89*cdf0e10cSrcweir         { ERROR_IS_SUBST_TARGET,        SVSTREAM_INVALID_PARAMETER },
90*cdf0e10cSrcweir         { ERROR_LOCK_FAILED,            SVSTREAM_LOCKING_VIOLATION },
91*cdf0e10cSrcweir         { ERROR_LOCK_VIOLATION,         SVSTREAM_LOCKING_VIOLATION },
92*cdf0e10cSrcweir         { ERROR_NEGATIVE_SEEK,          SVSTREAM_SEEK_ERROR },
93*cdf0e10cSrcweir         { ERROR_PATH_NOT_FOUND,         SVSTREAM_PATH_NOT_FOUND },
94*cdf0e10cSrcweir         { ERROR_READ_FAULT,             SVSTREAM_READ_ERROR },
95*cdf0e10cSrcweir         { ERROR_SEEK,                   SVSTREAM_SEEK_ERROR },
96*cdf0e10cSrcweir         { ERROR_SEEK_ON_DEVICE,         SVSTREAM_SEEK_ERROR },
97*cdf0e10cSrcweir         { ERROR_SHARING_BUFFER_EXCEEDED,SVSTREAM_SHARE_BUFF_EXCEEDED },
98*cdf0e10cSrcweir         { ERROR_SHARING_PAUSED,         SVSTREAM_SHARING_VIOLATION },
99*cdf0e10cSrcweir         { ERROR_SHARING_VIOLATION,      SVSTREAM_SHARING_VIOLATION },
100*cdf0e10cSrcweir         { ERROR_TOO_MANY_OPEN_FILES,    SVSTREAM_TOO_MANY_OPEN_FILES },
101*cdf0e10cSrcweir         { ERROR_WRITE_FAULT,            SVSTREAM_WRITE_ERROR },
102*cdf0e10cSrcweir         { ERROR_WRITE_PROTECT,          SVSTREAM_ACCESS_DENIED },
103*cdf0e10cSrcweir         { ERROR_DISK_FULL,              SVSTREAM_DISK_FULL },
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         { (DWORD)0xFFFFFFFF, SVSTREAM_GENERALERROR }
106*cdf0e10cSrcweir     };
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     sal_uIntPtr nRetVal = SVSTREAM_GENERALERROR;    // Standardfehler
109*cdf0e10cSrcweir     int i=0;
110*cdf0e10cSrcweir     do
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         if( errArr[i].wnt == nWntError )
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             nRetVal = errArr[i].sv;
115*cdf0e10cSrcweir             break;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         i++;
118*cdf0e10cSrcweir     } while( errArr[i].wnt != (DWORD)0xFFFFFFFF );
119*cdf0e10cSrcweir     return nRetVal;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir /*************************************************************************
123*cdf0e10cSrcweir |*
124*cdf0e10cSrcweir |*    SvFileStream::SvFileStream()
125*cdf0e10cSrcweir |*
126*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
127*cdf0e10cSrcweir |*    Ersterstellung    OV 17.06.94
128*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
129*cdf0e10cSrcweir |*
130*cdf0e10cSrcweir *************************************************************************/
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir SvFileStream::SvFileStream( const String& rFileName, StreamMode nMode )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     bIsOpen             = sal_False;
135*cdf0e10cSrcweir     nLockCounter        = 0;
136*cdf0e10cSrcweir     bIsWritable         = sal_False;
137*cdf0e10cSrcweir     pInstanceData       = new StreamData;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     SetBufferSize( 8192 );
140*cdf0e10cSrcweir 	// convert URL to SystemPath, if necessary
141*cdf0e10cSrcweir 	::rtl::OUString aFileName, aNormPath;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	if ( FileBase::getSystemPathFromFileURL( rFileName, aFileName ) != FileBase::E_None )
144*cdf0e10cSrcweir 		aFileName = rFileName;
145*cdf0e10cSrcweir 	Open( aFileName, nMode );
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir /*************************************************************************
149*cdf0e10cSrcweir |*
150*cdf0e10cSrcweir |*    SvFileStream::SvFileStream()
151*cdf0e10cSrcweir |*
152*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
153*cdf0e10cSrcweir |*    Ersterstellung    OV 22.11.94
154*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
155*cdf0e10cSrcweir |*
156*cdf0e10cSrcweir *************************************************************************/
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir SvFileStream::SvFileStream()
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     bIsOpen             = sal_False;
161*cdf0e10cSrcweir     nLockCounter        = 0;
162*cdf0e10cSrcweir     bIsWritable         = sal_False;
163*cdf0e10cSrcweir     pInstanceData       = new StreamData;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     SetBufferSize( 8192 );
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir /*************************************************************************
169*cdf0e10cSrcweir |*
170*cdf0e10cSrcweir |*    SvFileStream::~SvFileStream()
171*cdf0e10cSrcweir |*
172*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
173*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
174*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
175*cdf0e10cSrcweir |*
176*cdf0e10cSrcweir *************************************************************************/
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir SvFileStream::~SvFileStream()
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     Close();
181*cdf0e10cSrcweir     if (pInstanceData)
182*cdf0e10cSrcweir         delete pInstanceData;
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir /*************************************************************************
186*cdf0e10cSrcweir |*
187*cdf0e10cSrcweir |*    SvFileStream::GetFileHandle()
188*cdf0e10cSrcweir |*
189*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
190*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
191*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
192*cdf0e10cSrcweir |*
193*cdf0e10cSrcweir *************************************************************************/
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir sal_uIntPtr SvFileStream::GetFileHandle() const
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     return (sal_uIntPtr)pInstanceData->hFile;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir /*************************************************************************
201*cdf0e10cSrcweir |*
202*cdf0e10cSrcweir |*    SvFileStream::IsA()
203*cdf0e10cSrcweir |*
204*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
205*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
206*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
207*cdf0e10cSrcweir |*
208*cdf0e10cSrcweir *************************************************************************/
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir sal_uInt16 SvFileStream::IsA() const
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     return ID_FILESTREAM;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir /*************************************************************************
216*cdf0e10cSrcweir |*
217*cdf0e10cSrcweir |*    SvFileStream::GetData()
218*cdf0e10cSrcweir |*
219*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW, Prueft nicht Eof; IsEof danach rufbar
220*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
221*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
222*cdf0e10cSrcweir |*
223*cdf0e10cSrcweir *************************************************************************/
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir sal_uIntPtr SvFileStream::GetData( void* pData, sal_uIntPtr nSize )
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	DWORD nCount = 0;
228*cdf0e10cSrcweir 	if( IsOpen() )
229*cdf0e10cSrcweir 	{
230*cdf0e10cSrcweir 		bool bResult = ReadFile(pInstanceData->hFile,(LPVOID)pData,nSize,&nCount,NULL);
231*cdf0e10cSrcweir 		if( !bResult )
232*cdf0e10cSrcweir 		{
233*cdf0e10cSrcweir 			sal_uIntPtr nTestError = GetLastError();
234*cdf0e10cSrcweir 			SetError(::GetSvError( nTestError ) );
235*cdf0e10cSrcweir 		}
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir 	return (DWORD)nCount;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir /*************************************************************************
241*cdf0e10cSrcweir |*
242*cdf0e10cSrcweir |*    SvFileStream::PutData()
243*cdf0e10cSrcweir |*
244*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
245*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
246*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
247*cdf0e10cSrcweir |*
248*cdf0e10cSrcweir *************************************************************************/
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir sal_uIntPtr SvFileStream::PutData( const void* pData, sal_uIntPtr nSize )
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir 	DWORD nCount = 0;
253*cdf0e10cSrcweir 	if( IsOpen() )
254*cdf0e10cSrcweir 	{
255*cdf0e10cSrcweir 		if(!WriteFile(pInstanceData->hFile,(LPVOID)pData,nSize,&nCount,NULL))
256*cdf0e10cSrcweir 			SetError(::GetSvError( GetLastError() ) );
257*cdf0e10cSrcweir 	}
258*cdf0e10cSrcweir 	return nCount;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir /*************************************************************************
262*cdf0e10cSrcweir |*
263*cdf0e10cSrcweir |*    SvFileStream::SeekPos()
264*cdf0e10cSrcweir |*
265*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
266*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
267*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
268*cdf0e10cSrcweir |*
269*cdf0e10cSrcweir *************************************************************************/
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir sal_uIntPtr SvFileStream::SeekPos( sal_uIntPtr nPos )
272*cdf0e10cSrcweir {
273*cdf0e10cSrcweir 	DWORD nNewPos = 0;
274*cdf0e10cSrcweir 	if( IsOpen() )
275*cdf0e10cSrcweir 	{
276*cdf0e10cSrcweir 		if( nPos != STREAM_SEEK_TO_END )
277*cdf0e10cSrcweir 			// 64-Bit files werden nicht unterstuetzt
278*cdf0e10cSrcweir 			nNewPos=SetFilePointer(pInstanceData->hFile,nPos,NULL,FILE_BEGIN);
279*cdf0e10cSrcweir 		else
280*cdf0e10cSrcweir 			nNewPos=SetFilePointer(pInstanceData->hFile,0L,NULL,FILE_END);
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 		if( nNewPos == 0xFFFFFFFF )
283*cdf0e10cSrcweir 		{
284*cdf0e10cSrcweir 			SetError(::GetSvError( GetLastError() ) );
285*cdf0e10cSrcweir 			nNewPos = 0L;
286*cdf0e10cSrcweir 		}
287*cdf0e10cSrcweir 	}
288*cdf0e10cSrcweir 	else
289*cdf0e10cSrcweir 		SetError( SVSTREAM_GENERALERROR );
290*cdf0e10cSrcweir 	return (sal_uIntPtr)nNewPos;
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir /*************************************************************************
294*cdf0e10cSrcweir |*
295*cdf0e10cSrcweir |*    SvFileStream::Tell()
296*cdf0e10cSrcweir |*
297*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
298*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
299*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
300*cdf0e10cSrcweir |*
301*cdf0e10cSrcweir *************************************************************************/
302*cdf0e10cSrcweir /*
303*cdf0e10cSrcweir sal_uIntPtr SvFileStream::Tell()
304*cdf0e10cSrcweir {
305*cdf0e10cSrcweir     sal_uIntPtr nPos = 0L;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir     if( IsOpen() )
308*cdf0e10cSrcweir     {
309*cdf0e10cSrcweir         DWORD nPos;
310*cdf0e10cSrcweir         nPos = SetFilePointer(pInstanceData->hFile,0L,NULL,FILE_CURRENT);
311*cdf0e10cSrcweir         if( nPos = 0xFFFFFFFF )
312*cdf0e10cSrcweir         {
313*cdf0e10cSrcweir             SetError( ::GetSvError( GetLastError() ) );
314*cdf0e10cSrcweir             nPos = 0L;
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir     }
317*cdf0e10cSrcweir     return nPos;
318*cdf0e10cSrcweir }
319*cdf0e10cSrcweir */
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir /*************************************************************************
322*cdf0e10cSrcweir |*
323*cdf0e10cSrcweir |*    SvFileStream::FlushData()
324*cdf0e10cSrcweir |*
325*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
326*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
327*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
328*cdf0e10cSrcweir |*
329*cdf0e10cSrcweir *************************************************************************/
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir void SvFileStream::FlushData()
332*cdf0e10cSrcweir {
333*cdf0e10cSrcweir 	if( IsOpen() )
334*cdf0e10cSrcweir 	{
335*cdf0e10cSrcweir 		if( !FlushFileBuffers(pInstanceData->hFile) )
336*cdf0e10cSrcweir 			SetError(::GetSvError(GetLastError()));
337*cdf0e10cSrcweir 	}
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir /*************************************************************************
341*cdf0e10cSrcweir |*
342*cdf0e10cSrcweir |*    SvFileStream::LockRange()
343*cdf0e10cSrcweir |*
344*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
345*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
346*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
347*cdf0e10cSrcweir |*
348*cdf0e10cSrcweir *************************************************************************/
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir sal_Bool SvFileStream::LockRange( sal_uIntPtr nByteOffset, sal_uIntPtr nBytes )
351*cdf0e10cSrcweir {
352*cdf0e10cSrcweir 	bool bRetVal = false;
353*cdf0e10cSrcweir 	if( IsOpen() )
354*cdf0e10cSrcweir 	{
355*cdf0e10cSrcweir 		bRetVal = ::LockFile(pInstanceData->hFile,nByteOffset,0L,nBytes,0L );
356*cdf0e10cSrcweir 		if( !bRetVal )
357*cdf0e10cSrcweir 			SetError(::GetSvError(GetLastError()));
358*cdf0e10cSrcweir 	}
359*cdf0e10cSrcweir 	return bRetVal;
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir /*************************************************************************
363*cdf0e10cSrcweir |*
364*cdf0e10cSrcweir |*    SvFileStream::UnlockRange()
365*cdf0e10cSrcweir |*
366*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
367*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
368*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
369*cdf0e10cSrcweir |*
370*cdf0e10cSrcweir *************************************************************************/
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir sal_Bool SvFileStream::UnlockRange( sal_uIntPtr nByteOffset, sal_uIntPtr nBytes )
373*cdf0e10cSrcweir {
374*cdf0e10cSrcweir 	bool bRetVal = false;
375*cdf0e10cSrcweir 	if( IsOpen() )
376*cdf0e10cSrcweir 	{
377*cdf0e10cSrcweir 		bRetVal = ::UnlockFile(pInstanceData->hFile,nByteOffset,0L,nBytes,0L );
378*cdf0e10cSrcweir 		if( !bRetVal )
379*cdf0e10cSrcweir 			SetError(::GetSvError(GetLastError()));
380*cdf0e10cSrcweir 	}
381*cdf0e10cSrcweir 	return bRetVal;
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir /*************************************************************************
385*cdf0e10cSrcweir |*
386*cdf0e10cSrcweir |*    SvFileStream::LockFile()
387*cdf0e10cSrcweir |*
388*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
389*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
390*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
391*cdf0e10cSrcweir |*
392*cdf0e10cSrcweir *************************************************************************/
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir sal_Bool SvFileStream::LockFile()
395*cdf0e10cSrcweir {
396*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
397*cdf0e10cSrcweir     if( !nLockCounter )
398*cdf0e10cSrcweir     {
399*cdf0e10cSrcweir         if( LockRange( 0L, LONG_MAX ) )
400*cdf0e10cSrcweir         {
401*cdf0e10cSrcweir             nLockCounter = 1;
402*cdf0e10cSrcweir             bRetVal = sal_True;
403*cdf0e10cSrcweir         }
404*cdf0e10cSrcweir     }
405*cdf0e10cSrcweir     else
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         nLockCounter++;
408*cdf0e10cSrcweir         bRetVal = sal_True;
409*cdf0e10cSrcweir     }
410*cdf0e10cSrcweir     return bRetVal;
411*cdf0e10cSrcweir }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir /*************************************************************************
414*cdf0e10cSrcweir |*
415*cdf0e10cSrcweir |*    SvFileStream::UnlockFile()
416*cdf0e10cSrcweir |*
417*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
418*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
419*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
420*cdf0e10cSrcweir |*
421*cdf0e10cSrcweir *************************************************************************/
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir sal_Bool SvFileStream::UnlockFile()
424*cdf0e10cSrcweir {
425*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
426*cdf0e10cSrcweir     if( nLockCounter > 0)
427*cdf0e10cSrcweir     {
428*cdf0e10cSrcweir         if( nLockCounter == 1)
429*cdf0e10cSrcweir         {
430*cdf0e10cSrcweir             if( UnlockRange( 0L, LONG_MAX ) )
431*cdf0e10cSrcweir             {
432*cdf0e10cSrcweir                 nLockCounter = 0;
433*cdf0e10cSrcweir                 bRetVal = sal_True;
434*cdf0e10cSrcweir             }
435*cdf0e10cSrcweir         }
436*cdf0e10cSrcweir         else
437*cdf0e10cSrcweir         {
438*cdf0e10cSrcweir             nLockCounter--;
439*cdf0e10cSrcweir             bRetVal = sal_True;
440*cdf0e10cSrcweir         }
441*cdf0e10cSrcweir     }
442*cdf0e10cSrcweir     return bRetVal;
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir /*************************************************************************
447*cdf0e10cSrcweir |*
448*cdf0e10cSrcweir |*    SvFileStream::Open()
449*cdf0e10cSrcweir |*
450*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
451*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
452*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
453*cdf0e10cSrcweir |*
454*cdf0e10cSrcweir *************************************************************************/
455*cdf0e10cSrcweir /*
456*cdf0e10cSrcweir       NOCREATE       TRUNC   NT-Action
457*cdf0e10cSrcweir       ----------------------------------------------
458*cdf0e10cSrcweir          0 (Create)    0     OPEN_ALWAYS
459*cdf0e10cSrcweir          0 (Create)    1     CREATE_ALWAYS
460*cdf0e10cSrcweir          1             0     OPEN_EXISTING
461*cdf0e10cSrcweir          1             1     TRUNCATE_EXISTING
462*cdf0e10cSrcweir */
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir void SvFileStream::Open( const String& rFilename, StreamMode nMode )
465*cdf0e10cSrcweir {
466*cdf0e10cSrcweir 	String aParsedFilename(rFilename);
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 	SetLastError( ERROR_SUCCESS );
469*cdf0e10cSrcweir 	Close();
470*cdf0e10cSrcweir 	SvStream::ClearBuffer();
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 	eStreamMode = nMode;
473*cdf0e10cSrcweir 	eStreamMode &= ~STREAM_TRUNC; // beim ReOpen nicht cutten
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir 	//    !!! NoOp: Ansonsten ToAbs() verwendern
476*cdf0e10cSrcweir 	//    !!! DirEntry aDirEntry( rFilename );
477*cdf0e10cSrcweir 	//    !!! aFilename = aDirEntry.GetFull();
478*cdf0e10cSrcweir 	aFilename = aParsedFilename;
479*cdf0e10cSrcweir #ifdef BOOTSTRAP
480*cdf0e10cSrcweir 	ByteString aFileNameA( aFilename, gsl_getSystemTextEncoding());
481*cdf0e10cSrcweir #else
482*cdf0e10cSrcweir 	ByteString aFileNameA( aFilename, osl_getThreadTextEncoding());
483*cdf0e10cSrcweir 	FSysRedirector::DoRedirect( aFilename );
484*cdf0e10cSrcweir #endif
485*cdf0e10cSrcweir 	SetLastError( ERROR_SUCCESS );	// ggf. durch Redirector geaendert!
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir 	/*
488*cdf0e10cSrcweir 	#ifdef DBG_UTIL
489*cdf0e10cSrcweir 	String aTraceStr( "SvFileStream::Open(): " );
490*cdf0e10cSrcweir 	aTraceStr += aFilename;
491*cdf0e10cSrcweir 	DBG_TRACE( aTraceStr );
492*cdf0e10cSrcweir 	#endif
493*cdf0e10cSrcweir 	*/
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir 	DWORD   nOpenAction;
496*cdf0e10cSrcweir 	DWORD   nShareMode      = FILE_SHARE_READ | FILE_SHARE_WRITE;
497*cdf0e10cSrcweir 	DWORD   nAccessMode     = 0L;
498*cdf0e10cSrcweir 	UINT    nOldErrorMode = SetErrorMode( SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX );
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir 	if( nMode & STREAM_SHARE_DENYREAD)
501*cdf0e10cSrcweir 		nShareMode &= ~FILE_SHARE_READ;
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir 	if( nMode & STREAM_SHARE_DENYWRITE)
504*cdf0e10cSrcweir 		nShareMode &= ~FILE_SHARE_WRITE;
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir 	if( nMode & STREAM_SHARE_DENYALL)
507*cdf0e10cSrcweir 		nShareMode = 0;
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir 	if( (nMode & STREAM_READ) )
510*cdf0e10cSrcweir 		nAccessMode |= GENERIC_READ;
511*cdf0e10cSrcweir 	if( (nMode & STREAM_WRITE) )
512*cdf0e10cSrcweir 		nAccessMode |= GENERIC_WRITE;
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 	if( nAccessMode == GENERIC_READ )       // ReadOnly ?
515*cdf0e10cSrcweir 		nMode |= STREAM_NOCREATE;   // wenn ja, nicht erzeugen
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir 	// Zuordnung siehe obige Wahrheitstafel
518*cdf0e10cSrcweir 	if( !(nMode & STREAM_NOCREATE) )
519*cdf0e10cSrcweir 	{
520*cdf0e10cSrcweir 		if( nMode & STREAM_TRUNC )
521*cdf0e10cSrcweir 			nOpenAction = CREATE_ALWAYS;
522*cdf0e10cSrcweir 		else
523*cdf0e10cSrcweir 			nOpenAction = OPEN_ALWAYS;
524*cdf0e10cSrcweir 	}
525*cdf0e10cSrcweir 	else
526*cdf0e10cSrcweir 	{
527*cdf0e10cSrcweir 		if( nMode & STREAM_TRUNC )
528*cdf0e10cSrcweir 			nOpenAction = TRUNCATE_EXISTING;
529*cdf0e10cSrcweir 		else
530*cdf0e10cSrcweir 			nOpenAction = OPEN_EXISTING;
531*cdf0e10cSrcweir 	}
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir 	pInstanceData->hFile = CreateFile(
534*cdf0e10cSrcweir 		aFileNameA.GetBuffer(),
535*cdf0e10cSrcweir 		nAccessMode,
536*cdf0e10cSrcweir 		nShareMode,
537*cdf0e10cSrcweir 		(LPSECURITY_ATTRIBUTES)NULL,
538*cdf0e10cSrcweir 		nOpenAction,
539*cdf0e10cSrcweir 		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
540*cdf0e10cSrcweir 		(HANDLE) NULL
541*cdf0e10cSrcweir 	);
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir 	if(  pInstanceData->hFile!=INVALID_HANDLE_VALUE && (
544*cdf0e10cSrcweir 		// Hat Create Always eine existierende Datei ueberschrieben ?
545*cdf0e10cSrcweir 		GetLastError() == ERROR_ALREADY_EXISTS ||
546*cdf0e10cSrcweir 		// Hat Open Always eine neue Datei angelegt ?
547*cdf0e10cSrcweir 		GetLastError() == ERROR_FILE_NOT_FOUND  ))
548*cdf0e10cSrcweir 	{
549*cdf0e10cSrcweir 		// wenn ja, dann alles OK
550*cdf0e10cSrcweir 		if( nOpenAction == OPEN_ALWAYS || nOpenAction == CREATE_ALWAYS )
551*cdf0e10cSrcweir 			SetLastError( ERROR_SUCCESS );
552*cdf0e10cSrcweir 	}
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir 	// Bei Fehler pruefen, ob wir lesen duerfen
555*cdf0e10cSrcweir 	if( (pInstanceData->hFile==INVALID_HANDLE_VALUE) &&
556*cdf0e10cSrcweir 		 (nAccessMode & GENERIC_WRITE))
557*cdf0e10cSrcweir 	{
558*cdf0e10cSrcweir 		sal_uIntPtr nErr = ::GetSvError( GetLastError() );
559*cdf0e10cSrcweir 		if(nErr==SVSTREAM_ACCESS_DENIED || nErr==SVSTREAM_SHARING_VIOLATION)
560*cdf0e10cSrcweir 		{
561*cdf0e10cSrcweir 			nMode &= (~STREAM_WRITE);
562*cdf0e10cSrcweir 			nAccessMode = GENERIC_READ;
563*cdf0e10cSrcweir 			// OV, 28.1.97: Win32 setzt die Datei auf 0-Laenge, wenn
564*cdf0e10cSrcweir 			// die Openaction CREATE_ALWAYS ist!!!!
565*cdf0e10cSrcweir 			nOpenAction = OPEN_EXISTING;
566*cdf0e10cSrcweir 			SetLastError( ERROR_SUCCESS );
567*cdf0e10cSrcweir 			pInstanceData->hFile = CreateFile(
568*cdf0e10cSrcweir 				aFileNameA.GetBuffer(),
569*cdf0e10cSrcweir 				GENERIC_READ,
570*cdf0e10cSrcweir 				nShareMode,
571*cdf0e10cSrcweir 				(LPSECURITY_ATTRIBUTES)NULL,
572*cdf0e10cSrcweir 				nOpenAction,
573*cdf0e10cSrcweir 				FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
574*cdf0e10cSrcweir 				(HANDLE) NULL
575*cdf0e10cSrcweir 			);
576*cdf0e10cSrcweir 			if( GetLastError() == ERROR_ALREADY_EXISTS )
577*cdf0e10cSrcweir 				SetLastError( ERROR_SUCCESS );
578*cdf0e10cSrcweir 		}
579*cdf0e10cSrcweir 	}
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir 	if( GetLastError() != ERROR_SUCCESS )
582*cdf0e10cSrcweir 	{
583*cdf0e10cSrcweir 		bIsOpen = sal_False;
584*cdf0e10cSrcweir 		SetError(::GetSvError( GetLastError() ) );
585*cdf0e10cSrcweir 	}
586*cdf0e10cSrcweir 	else
587*cdf0e10cSrcweir 	{
588*cdf0e10cSrcweir 		bIsOpen     = sal_True;
589*cdf0e10cSrcweir 		// pInstanceData->bIsEof = sal_False;
590*cdf0e10cSrcweir 		if( nAccessMode & GENERIC_WRITE )
591*cdf0e10cSrcweir 			bIsWritable = sal_True;
592*cdf0e10cSrcweir 	}
593*cdf0e10cSrcweir 	SetErrorMode( nOldErrorMode );
594*cdf0e10cSrcweir }
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir /*************************************************************************
597*cdf0e10cSrcweir |*
598*cdf0e10cSrcweir |*    SvFileStream::ReOpen()
599*cdf0e10cSrcweir |*
600*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
601*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
602*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
603*cdf0e10cSrcweir |*
604*cdf0e10cSrcweir *************************************************************************/
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir void SvFileStream::ReOpen()
607*cdf0e10cSrcweir {
608*cdf0e10cSrcweir 	if( !bIsOpen && aFilename.Len() )
609*cdf0e10cSrcweir 		Open( aFilename, eStreamMode );
610*cdf0e10cSrcweir }
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir /*************************************************************************
613*cdf0e10cSrcweir |*
614*cdf0e10cSrcweir |*    SvFileStream::Close()
615*cdf0e10cSrcweir |*
616*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
617*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
618*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
619*cdf0e10cSrcweir |*
620*cdf0e10cSrcweir *************************************************************************/
621*cdf0e10cSrcweir 
622*cdf0e10cSrcweir void SvFileStream::Close()
623*cdf0e10cSrcweir {
624*cdf0e10cSrcweir 	if( IsOpen() )
625*cdf0e10cSrcweir 	{
626*cdf0e10cSrcweir 		if( nLockCounter )
627*cdf0e10cSrcweir 		{
628*cdf0e10cSrcweir 			nLockCounter = 1;
629*cdf0e10cSrcweir 			UnlockFile();
630*cdf0e10cSrcweir 		}
631*cdf0e10cSrcweir 		Flush();
632*cdf0e10cSrcweir 		CloseHandle( pInstanceData->hFile );
633*cdf0e10cSrcweir 	}
634*cdf0e10cSrcweir 	bIsOpen     = sal_False;
635*cdf0e10cSrcweir 	nLockCounter= 0;
636*cdf0e10cSrcweir 	bIsWritable = sal_False;
637*cdf0e10cSrcweir 	SvStream::ClearBuffer();
638*cdf0e10cSrcweir 	SvStream::ClearError();
639*cdf0e10cSrcweir }
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir /*************************************************************************
642*cdf0e10cSrcweir |*
643*cdf0e10cSrcweir |*    SvFileStream::ResetError()
644*cdf0e10cSrcweir |*
645*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW; Setzt Filepointer auf Dateianfang
646*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
647*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
648*cdf0e10cSrcweir |*
649*cdf0e10cSrcweir *************************************************************************/
650*cdf0e10cSrcweir 
651*cdf0e10cSrcweir void SvFileStream::ResetError()
652*cdf0e10cSrcweir {
653*cdf0e10cSrcweir     SvStream::ClearError();
654*cdf0e10cSrcweir }
655*cdf0e10cSrcweir 
656*cdf0e10cSrcweir /*************************************************************************
657*cdf0e10cSrcweir |*
658*cdf0e10cSrcweir |*    SvFileStream::SetSize()
659*cdf0e10cSrcweir |*
660*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
661*cdf0e10cSrcweir |*    Ersterstellung    OV 19.10.95
662*cdf0e10cSrcweir |*    Letzte Aenderung  TPF 15.07.98
663*cdf0e10cSrcweir |*
664*cdf0e10cSrcweir *************************************************************************/
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir void SvFileStream::SetSize( sal_uIntPtr nSize )
667*cdf0e10cSrcweir {
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir 	if( IsOpen() )
670*cdf0e10cSrcweir 	{
671*cdf0e10cSrcweir 		int bError = sal_False;
672*cdf0e10cSrcweir 		HANDLE hFile = pInstanceData->hFile;
673*cdf0e10cSrcweir 		sal_uIntPtr nOld = SetFilePointer( hFile, 0L, NULL, FILE_CURRENT );
674*cdf0e10cSrcweir 		if( nOld != 0xffffffff )
675*cdf0e10cSrcweir 		{
676*cdf0e10cSrcweir 			if( SetFilePointer(hFile,nSize,NULL,FILE_BEGIN ) != 0xffffffff)
677*cdf0e10cSrcweir 			{
678*cdf0e10cSrcweir 				bool bSucc = SetEndOfFile( hFile );
679*cdf0e10cSrcweir 				if( !bSucc )
680*cdf0e10cSrcweir 					bError = sal_True;
681*cdf0e10cSrcweir 			}
682*cdf0e10cSrcweir 			if( SetFilePointer( hFile,nOld,NULL,FILE_BEGIN ) == 0xffffffff)
683*cdf0e10cSrcweir 				bError = sal_True;
684*cdf0e10cSrcweir 		}
685*cdf0e10cSrcweir 		if( bError )
686*cdf0e10cSrcweir 			SetError(::GetSvError( GetLastError() ) );
687*cdf0e10cSrcweir 	}
688*cdf0e10cSrcweir }
689*cdf0e10cSrcweir 
690