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