xref: /aoo42x/main/tools/source/stream/strmos2.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 #include <string.h>
29*cdf0e10cSrcweir #include <limits.h>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #define INCL_PM
32*cdf0e10cSrcweir #define INCL_DOS
33*cdf0e10cSrcweir #define INCL_DOSERRORS
34*cdf0e10cSrcweir #include <svpm.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <tools/debug.hxx>
37*cdf0e10cSrcweir #include <tools/fsys.hxx>
38*cdf0e10cSrcweir #include <tools/stream.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // class FileBase
41*cdf0e10cSrcweir #include <osl/file.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace osl;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // class FileBase
46*cdf0e10cSrcweir #ifndef _OSL_FILE_HXX_
47*cdf0e10cSrcweir #include <osl/file.hxx>
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace osl;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // -----------------------------------------------------------------------
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir // --------------
55*cdf0e10cSrcweir // - StreamData -
56*cdf0e10cSrcweir // --------------
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir class StreamData
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir public:
61*cdf0e10cSrcweir     HFILE   hFile;
62*cdf0e10cSrcweir     sal_Bool    bIsEof;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir             StreamData()
65*cdf0e10cSrcweir             {
66*cdf0e10cSrcweir                 hFile = 0;
67*cdf0e10cSrcweir                 bIsEof = sal_True;
68*cdf0e10cSrcweir             }
69*cdf0e10cSrcweir };
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir // -----------------------------------------------------------------------
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir sal_uIntPtr GetSvError( APIRET nPMError )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     static struct { APIRET pm; sal_uIntPtr sv; } errArr[] =
76*cdf0e10cSrcweir     {
77*cdf0e10cSrcweir         { ERROR_FILE_NOT_FOUND,         SVSTREAM_FILE_NOT_FOUND },
78*cdf0e10cSrcweir         { ERROR_PATH_NOT_FOUND,         SVSTREAM_PATH_NOT_FOUND },
79*cdf0e10cSrcweir         { ERROR_TOO_MANY_OPEN_FILES,    SVSTREAM_TOO_MANY_OPEN_FILES },
80*cdf0e10cSrcweir         { ERROR_ACCESS_DENIED,          SVSTREAM_ACCESS_DENIED },
81*cdf0e10cSrcweir         { ERROR_INVALID_ACCESS,         SVSTREAM_INVALID_ACCESS },
82*cdf0e10cSrcweir         { ERROR_SHARING_VIOLATION,      SVSTREAM_SHARING_VIOLATION },
83*cdf0e10cSrcweir         { ERROR_SHARING_BUFFER_EXCEEDED,SVSTREAM_SHARE_BUFF_EXCEEDED },
84*cdf0e10cSrcweir         { ERROR_CANNOT_MAKE,            SVSTREAM_CANNOT_MAKE },
85*cdf0e10cSrcweir         { ERROR_INVALID_PARAMETER,      SVSTREAM_INVALID_PARAMETER },
86*cdf0e10cSrcweir         { ERROR_DRIVE_LOCKED,           SVSTREAM_LOCKING_VIOLATION },
87*cdf0e10cSrcweir         { ERROR_LOCK_VIOLATION,      SVSTREAM_LOCKING_VIOLATION },
88*cdf0e10cSrcweir         { ERROR_FILENAME_EXCED_RANGE,   SVSTREAM_INVALID_PARAMETER },
89*cdf0e10cSrcweir         { ERROR_ATOMIC_LOCK_NOT_SUPPORTED, SVSTREAM_INVALID_PARAMETER },
90*cdf0e10cSrcweir         { ERROR_READ_LOCKS_NOT_SUPPORTED, SVSTREAM_INVALID_PARAMETER },
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         { 0xFFFF, SVSTREAM_GENERALERROR }
94*cdf0e10cSrcweir     };
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     sal_uIntPtr nRetVal = SVSTREAM_GENERALERROR;    // Standardfehler
97*cdf0e10cSrcweir     int i=0;
98*cdf0e10cSrcweir     do
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         if( errArr[i].pm == nPMError )
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             nRetVal = errArr[i].sv;
103*cdf0e10cSrcweir             break;
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir         i++;
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir     while( errArr[i].pm != 0xFFFF );
108*cdf0e10cSrcweir     return nRetVal;
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir /*************************************************************************
112*cdf0e10cSrcweir |*
113*cdf0e10cSrcweir |*    SvFileStream::SvFileStream()
114*cdf0e10cSrcweir |*
115*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
116*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
117*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
118*cdf0e10cSrcweir |*
119*cdf0e10cSrcweir *************************************************************************/
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir SvFileStream::SvFileStream( const String& rFileName, StreamMode nOpenMode )
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     bIsOpen             = sal_False;
124*cdf0e10cSrcweir     nLockCounter        = 0;
125*cdf0e10cSrcweir     bIsWritable         = sal_False;
126*cdf0e10cSrcweir     pInstanceData       = new StreamData;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     SetBufferSize( 8192 );
129*cdf0e10cSrcweir 	// convert URL to SystemPath, if necessary
130*cdf0e10cSrcweir 	::rtl::OUString aFileName, aNormPath;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	if ( FileBase::getSystemPathFromFileURL( rFileName, aFileName ) != FileBase::E_None )
133*cdf0e10cSrcweir 		aFileName = rFileName;
134*cdf0e10cSrcweir 	Open( aFileName, nOpenMode );
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir /*************************************************************************
138*cdf0e10cSrcweir |*
139*cdf0e10cSrcweir |*    SvFileStream::SvFileStream()
140*cdf0e10cSrcweir |*
141*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
142*cdf0e10cSrcweir |*    Ersterstellung    OV 22.11.94
143*cdf0e10cSrcweir |*    Letzte Aenderung  OV 22.11.94
144*cdf0e10cSrcweir |*
145*cdf0e10cSrcweir *************************************************************************/
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir SvFileStream::SvFileStream()
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     bIsOpen             = sal_False;
150*cdf0e10cSrcweir     nLockCounter        = 0;
151*cdf0e10cSrcweir     bIsWritable         = sal_False;
152*cdf0e10cSrcweir     pInstanceData       = new StreamData;
153*cdf0e10cSrcweir     SetBufferSize( 8192 );
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir /*************************************************************************
157*cdf0e10cSrcweir |*
158*cdf0e10cSrcweir |*    SvFileStream::~SvFileStream()
159*cdf0e10cSrcweir |*
160*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
161*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
162*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
163*cdf0e10cSrcweir |*
164*cdf0e10cSrcweir *************************************************************************/
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir SvFileStream::~SvFileStream()
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     Close();
169*cdf0e10cSrcweir     if( pInstanceData )
170*cdf0e10cSrcweir         delete pInstanceData;
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir /*************************************************************************
174*cdf0e10cSrcweir |*
175*cdf0e10cSrcweir |*    SvFileStream::GetFileHandle()
176*cdf0e10cSrcweir |*
177*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
178*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
179*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
180*cdf0e10cSrcweir |*
181*cdf0e10cSrcweir *************************************************************************/
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir sal_uIntPtr SvFileStream::GetFileHandle() const
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     return (sal_uIntPtr)pInstanceData->hFile;
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir /*************************************************************************
189*cdf0e10cSrcweir |*
190*cdf0e10cSrcweir |*    SvFileStream::IsA()
191*cdf0e10cSrcweir |*
192*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
193*cdf0e10cSrcweir |*    Ersterstellung    OV 14.06.94
194*cdf0e10cSrcweir |*    Letzte Aenderung  OV 14.06.94
195*cdf0e10cSrcweir |*
196*cdf0e10cSrcweir *************************************************************************/
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir sal_uInt16 SvFileStream::IsA() const
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir     return ID_FILESTREAM;
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir /*************************************************************************
204*cdf0e10cSrcweir |*
205*cdf0e10cSrcweir |*    SvFileStream::GetData()
206*cdf0e10cSrcweir |*
207*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW, Prueft nicht Eof; IsEof danach rufbar
208*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
209*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
210*cdf0e10cSrcweir |*
211*cdf0e10cSrcweir *************************************************************************/
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir sal_uIntPtr SvFileStream::GetData( void* pData, sal_uIntPtr nSize )
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir #ifdef DBG_UTIL
216*cdf0e10cSrcweir     ByteString aTraceStr( "SvFileStream::GetData(): " );
217*cdf0e10cSrcweir     aTraceStr += ByteString::CreateFromInt64(nSize);
218*cdf0e10cSrcweir     aTraceStr += " Bytes from ";
219*cdf0e10cSrcweir     aTraceStr += ByteString(aFilename, osl_getThreadTextEncoding());
220*cdf0e10cSrcweir     DBG_TRACE( aTraceStr.GetBuffer() );
221*cdf0e10cSrcweir #endif
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     sal_uIntPtr nCount = 0L;
224*cdf0e10cSrcweir     if( IsOpen() )
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         APIRET nResult;
227*cdf0e10cSrcweir         nResult = DosRead( pInstanceData->hFile,(PVOID)pData,nSize,&nCount );
228*cdf0e10cSrcweir         if( nResult )
229*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir     return nCount;
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir /*************************************************************************
235*cdf0e10cSrcweir |*
236*cdf0e10cSrcweir |*    SvFileStream::PutData()
237*cdf0e10cSrcweir |*
238*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
239*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
240*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
241*cdf0e10cSrcweir |*
242*cdf0e10cSrcweir *************************************************************************/
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir sal_uIntPtr SvFileStream::PutData( const void* pData, sal_uIntPtr nSize )
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir #ifdef DBG_UTIL
247*cdf0e10cSrcweir     ByteString aTraceStr( "SvFileStrean::PutData: " );
248*cdf0e10cSrcweir     aTraceStr += ByteString::CreateFromInt64(nSize);
249*cdf0e10cSrcweir     aTraceStr += " Bytes to ";
250*cdf0e10cSrcweir     aTraceStr += ByteString(aFilename, osl_getThreadTextEncoding());
251*cdf0e10cSrcweir     DBG_TRACE( aTraceStr.GetBuffer() );
252*cdf0e10cSrcweir #endif
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     sal_uIntPtr nCount = 0L;
255*cdf0e10cSrcweir     if( IsOpen() )
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         APIRET nResult;
258*cdf0e10cSrcweir         nResult = DosWrite( pInstanceData->hFile,(PVOID)pData,nSize,&nCount );
259*cdf0e10cSrcweir         if( nResult )
260*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
261*cdf0e10cSrcweir         else if( !nCount )
262*cdf0e10cSrcweir             SetError( SVSTREAM_DISK_FULL );
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir     return nCount;
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir /*************************************************************************
268*cdf0e10cSrcweir |*
269*cdf0e10cSrcweir |*    SvFileStream::SeekPos()
270*cdf0e10cSrcweir |*
271*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
272*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
273*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
274*cdf0e10cSrcweir |*
275*cdf0e10cSrcweir *************************************************************************/
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir sal_uIntPtr SvFileStream::SeekPos( sal_uIntPtr nPos )
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir     sal_uIntPtr nNewPos = 0L;
280*cdf0e10cSrcweir     if( IsOpen() )
281*cdf0e10cSrcweir     {
282*cdf0e10cSrcweir         APIRET nResult;
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         if( nPos != STREAM_SEEK_TO_END )
285*cdf0e10cSrcweir             nResult = DosSetFilePtr( pInstanceData->hFile,(long)nPos,
286*cdf0e10cSrcweir                  FILE_BEGIN, &nNewPos );
287*cdf0e10cSrcweir         else
288*cdf0e10cSrcweir             nResult = DosSetFilePtr( pInstanceData->hFile,0L,
289*cdf0e10cSrcweir                  FILE_END, &nNewPos );
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         if( nResult )
292*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
293*cdf0e10cSrcweir     }
294*cdf0e10cSrcweir     else
295*cdf0e10cSrcweir         SetError( SVSTREAM_GENERALERROR );
296*cdf0e10cSrcweir     return nNewPos;
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir /*************************************************************************
300*cdf0e10cSrcweir |*
301*cdf0e10cSrcweir |*    SvFileStream::Tell()
302*cdf0e10cSrcweir |*
303*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
304*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
305*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
306*cdf0e10cSrcweir |*
307*cdf0e10cSrcweir *************************************************************************/
308*cdf0e10cSrcweir /*
309*cdf0e10cSrcweir sal_uIntPtr SvFileStream::Tell()
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir     sal_uIntPtr nPos = 0L;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     if( IsOpen() )
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         APIRET nResult;
316*cdf0e10cSrcweir         nResult = DosSetFilePtr(pInstanceData->hFile,0L,FILE_CURRENT,&nPos);
317*cdf0e10cSrcweir         if( nResult )
318*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
319*cdf0e10cSrcweir     }
320*cdf0e10cSrcweir     return nPos;
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir */
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir /*************************************************************************
325*cdf0e10cSrcweir |*
326*cdf0e10cSrcweir |*    SvFileStream::FlushData()
327*cdf0e10cSrcweir |*
328*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
329*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
330*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
331*cdf0e10cSrcweir |*
332*cdf0e10cSrcweir *************************************************************************/
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir void SvFileStream::FlushData()
335*cdf0e10cSrcweir {
336*cdf0e10cSrcweir     if( IsOpen() )
337*cdf0e10cSrcweir     {
338*cdf0e10cSrcweir         APIRET nResult;
339*cdf0e10cSrcweir         nResult = DosResetBuffer(pInstanceData->hFile );
340*cdf0e10cSrcweir         if( nResult )
341*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
342*cdf0e10cSrcweir     }
343*cdf0e10cSrcweir }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir /*************************************************************************
346*cdf0e10cSrcweir |*
347*cdf0e10cSrcweir |*    SvFileStream::LockRange()
348*cdf0e10cSrcweir |*
349*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
350*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
351*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
352*cdf0e10cSrcweir |*
353*cdf0e10cSrcweir *************************************************************************/
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir sal_Bool SvFileStream::LockRange( sal_uIntPtr nByteOffset, sal_uIntPtr nBytes )
356*cdf0e10cSrcweir {
357*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
358*cdf0e10cSrcweir     if( IsOpen() )
359*cdf0e10cSrcweir     {
360*cdf0e10cSrcweir         APIRET   nResult;
361*cdf0e10cSrcweir         FILELOCK aLockArea, aUnlockArea;
362*cdf0e10cSrcweir         aUnlockArea.lOffset = 0L;
363*cdf0e10cSrcweir         aUnlockArea.lRange      = 0L;
364*cdf0e10cSrcweir         aLockArea.lOffset       = (long)nByteOffset;
365*cdf0e10cSrcweir         aLockArea.lRange        = (long)nBytes;
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir         nResult = DosSetFileLocks(pInstanceData->hFile,
368*cdf0e10cSrcweir             &aUnlockArea, &aLockArea,
369*cdf0e10cSrcweir             1000UL,  // Zeit in ms bis Abbruch
370*cdf0e10cSrcweir             0L       // kein Atomic-Lock
371*cdf0e10cSrcweir         );
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         if( nResult )
374*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
375*cdf0e10cSrcweir         else
376*cdf0e10cSrcweir             bRetVal = sal_True;
377*cdf0e10cSrcweir     }
378*cdf0e10cSrcweir     return bRetVal;
379*cdf0e10cSrcweir }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir /*************************************************************************
382*cdf0e10cSrcweir |*
383*cdf0e10cSrcweir |*    SvFileStream::UnlockRange()
384*cdf0e10cSrcweir |*
385*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
386*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
387*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
388*cdf0e10cSrcweir |*
389*cdf0e10cSrcweir *************************************************************************/
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir sal_Bool SvFileStream::UnlockRange( sal_uIntPtr nByteOffset, sal_uIntPtr nBytes )
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
394*cdf0e10cSrcweir     if( IsOpen() )
395*cdf0e10cSrcweir     {
396*cdf0e10cSrcweir         APIRET   nResult;
397*cdf0e10cSrcweir         FILELOCK aLockArea, aUnlockArea;
398*cdf0e10cSrcweir         aLockArea.lOffset       = 0L;
399*cdf0e10cSrcweir         aLockArea.lRange        = 0L;
400*cdf0e10cSrcweir         aUnlockArea.lOffset = (long)nByteOffset;
401*cdf0e10cSrcweir         aUnlockArea.lRange      = (long)nBytes;
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir         nResult = DosSetFileLocks(pInstanceData->hFile,
404*cdf0e10cSrcweir             &aUnlockArea, &aLockArea,
405*cdf0e10cSrcweir             1000UL,  // Zeit in ms bis Abbruch
406*cdf0e10cSrcweir             0L       // kein Atomic-Lock
407*cdf0e10cSrcweir         );
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir         if( nResult )
410*cdf0e10cSrcweir             SetError(::GetSvError(nResult) );
411*cdf0e10cSrcweir         else
412*cdf0e10cSrcweir             bRetVal = sal_True;
413*cdf0e10cSrcweir     }
414*cdf0e10cSrcweir     return bRetVal;
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir /*************************************************************************
418*cdf0e10cSrcweir |*
419*cdf0e10cSrcweir |*    SvFileStream::LockFile()
420*cdf0e10cSrcweir |*
421*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
422*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
423*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
424*cdf0e10cSrcweir |*
425*cdf0e10cSrcweir *************************************************************************/
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir sal_Bool SvFileStream::LockFile()
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
430*cdf0e10cSrcweir     if( !nLockCounter )
431*cdf0e10cSrcweir     {
432*cdf0e10cSrcweir         if( LockRange( 0L, LONG_MAX ) )
433*cdf0e10cSrcweir         {
434*cdf0e10cSrcweir             nLockCounter = 1;
435*cdf0e10cSrcweir             bRetVal = sal_True;
436*cdf0e10cSrcweir         }
437*cdf0e10cSrcweir     }
438*cdf0e10cSrcweir     else
439*cdf0e10cSrcweir     {
440*cdf0e10cSrcweir         nLockCounter++;
441*cdf0e10cSrcweir         bRetVal = sal_True;
442*cdf0e10cSrcweir     }
443*cdf0e10cSrcweir     return bRetVal;
444*cdf0e10cSrcweir }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir /*************************************************************************
447*cdf0e10cSrcweir |*
448*cdf0e10cSrcweir |*    SvFileStream::UnlockFile()
449*cdf0e10cSrcweir |*
450*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
451*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
452*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
453*cdf0e10cSrcweir |*
454*cdf0e10cSrcweir *************************************************************************/
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir sal_Bool SvFileStream::UnlockFile()
457*cdf0e10cSrcweir {
458*cdf0e10cSrcweir     sal_Bool bRetVal = sal_False;
459*cdf0e10cSrcweir     if( nLockCounter > 0)
460*cdf0e10cSrcweir     {
461*cdf0e10cSrcweir         if( nLockCounter == 1)
462*cdf0e10cSrcweir         {
463*cdf0e10cSrcweir             if( UnlockRange( 0L, LONG_MAX ) )
464*cdf0e10cSrcweir             {
465*cdf0e10cSrcweir                 nLockCounter = 0;
466*cdf0e10cSrcweir                 bRetVal = sal_True;
467*cdf0e10cSrcweir             }
468*cdf0e10cSrcweir         }
469*cdf0e10cSrcweir         else
470*cdf0e10cSrcweir         {
471*cdf0e10cSrcweir             nLockCounter--;
472*cdf0e10cSrcweir             bRetVal = sal_True;
473*cdf0e10cSrcweir         }
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir     return bRetVal;
476*cdf0e10cSrcweir }
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir /*************************************************************************
479*cdf0e10cSrcweir |*
480*cdf0e10cSrcweir |*    SvFileStream::Open()
481*cdf0e10cSrcweir |*
482*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
483*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
484*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
485*cdf0e10cSrcweir |*
486*cdf0e10cSrcweir *************************************************************************/
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir #if 0
489*cdf0e10cSrcweir sal_Bool createLongNameEA   ( const PCSZ pszPath, sal_uIntPtr ulAttributes, const String& aLongName );
490*cdf0e10cSrcweir #endif
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir void SvFileStream::Open( const String& rFilename, StreamMode nOpenMode )
493*cdf0e10cSrcweir {
494*cdf0e10cSrcweir         String aParsedFilename;
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir #if 0
497*cdf0e10cSrcweir         if      ( Folder::IsAvailable() && (rFilename.Search('{') < 9) )
498*cdf0e10cSrcweir         {
499*cdf0e10cSrcweir                 String          aVirtualPart;
500*cdf0e10cSrcweir                 String          aRealPart;
501*cdf0e10cSrcweir                 String          aVirtualPath;
502*cdf0e10cSrcweir                 ItemIDPath      aVirtualURL;
503*cdf0e10cSrcweir                 sal_uIntPtr           nDivider = 0;
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir                 String          aVirtualString(rFilename);
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir                 for (int x=aVirtualString.Len(); x>0; x--)
508*cdf0e10cSrcweir                 {
509*cdf0e10cSrcweir                         if (aVirtualString.Copy(x,1).Compare("}")==COMPARE_EQUAL)
510*cdf0e10cSrcweir                         {
511*cdf0e10cSrcweir                                 nDivider = x;
512*cdf0e10cSrcweir                                 break;
513*cdf0e10cSrcweir                         }
514*cdf0e10cSrcweir                 }
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir                 aVirtualPart = aVirtualString.Copy(0,nDivider+1);
517*cdf0e10cSrcweir                 aRealPart = aVirtualString.Copy(nDivider+2);
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir                 aVirtualURL  = aVirtualPart;
520*cdf0e10cSrcweir                 aVirtualPath = aVirtualURL.GetHostNotationPath();
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir                 DirEntry aTempDirEntry(aVirtualPath);
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir                 aTempDirEntry += aRealPart;
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir                 aParsedFilename = aTempDirEntry.GetFull();
527*cdf0e10cSrcweir         }
528*cdf0e10cSrcweir         else
529*cdf0e10cSrcweir #endif // 0
530*cdf0e10cSrcweir         {
531*cdf0e10cSrcweir                 aParsedFilename = rFilename;
532*cdf0e10cSrcweir         }
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir     Close();
535*cdf0e10cSrcweir     SvStream::ClearBuffer();
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir     sal_uIntPtr   nActionTaken;
538*cdf0e10cSrcweir     sal_uIntPtr   nOpenAction     = 0L;
539*cdf0e10cSrcweir     sal_uIntPtr   nShareBits      = 0L;
540*cdf0e10cSrcweir     sal_uIntPtr   nReadWriteBits  = 0L;
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir     eStreamMode = nOpenMode;
543*cdf0e10cSrcweir     eStreamMode &= ~STREAM_TRUNC; // beim ReOpen nicht cutten
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir     nOpenMode |= STREAM_SHARE_DENYNONE;  // definierten Zustand garantieren
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir     // ********* Zugriffsflags ***********
548*cdf0e10cSrcweir     if( nOpenMode & STREAM_SHARE_DENYNONE)
549*cdf0e10cSrcweir         nShareBits = OPEN_SHARE_DENYNONE;
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir     if( nOpenMode & STREAM_SHARE_DENYREAD)
552*cdf0e10cSrcweir         nShareBits = OPEN_SHARE_DENYREAD;
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     if( nOpenMode & STREAM_SHARE_DENYWRITE)
555*cdf0e10cSrcweir         nShareBits = OPEN_SHARE_DENYWRITE;
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir     if( nOpenMode & STREAM_SHARE_DENYALL)
558*cdf0e10cSrcweir         nShareBits = OPEN_SHARE_DENYREADWRITE;
559*cdf0e10cSrcweir 
560*cdf0e10cSrcweir         if( (nOpenMode & STREAM_READ) )
561*cdf0e10cSrcweir     {
562*cdf0e10cSrcweir         if( nOpenMode & STREAM_WRITE )
563*cdf0e10cSrcweir             nReadWriteBits |= OPEN_ACCESS_READWRITE;
564*cdf0e10cSrcweir         else
565*cdf0e10cSrcweir         {
566*cdf0e10cSrcweir             nReadWriteBits |= OPEN_ACCESS_READONLY;
567*cdf0e10cSrcweir             nOpenMode |= STREAM_NOCREATE;
568*cdf0e10cSrcweir         }
569*cdf0e10cSrcweir     }
570*cdf0e10cSrcweir     else
571*cdf0e10cSrcweir         nReadWriteBits |= OPEN_ACCESS_WRITEONLY;
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir     if( nOpenMode & STREAM_NOCREATE )
575*cdf0e10cSrcweir     {
576*cdf0e10cSrcweir         // Datei nicht erzeugen
577*cdf0e10cSrcweir         nOpenAction = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
578*cdf0e10cSrcweir     }
579*cdf0e10cSrcweir     else
580*cdf0e10cSrcweir     {
581*cdf0e10cSrcweir         // Datei erzeugen, wenn nicht vorhanden
582*cdf0e10cSrcweir         nOpenAction = OPEN_ACTION_CREATE_IF_NEW;
583*cdf0e10cSrcweir         if( nOpenMode & STREAM_TRUNC )
584*cdf0e10cSrcweir             // Auf Nullaenge kuerzen, wenn existiert
585*cdf0e10cSrcweir             nOpenAction |= OPEN_ACTION_REPLACE_IF_EXISTS;
586*cdf0e10cSrcweir         else
587*cdf0e10cSrcweir             // Inhalt der Datei nicht wegwerfen
588*cdf0e10cSrcweir             nOpenAction |= OPEN_ACTION_OPEN_IF_EXISTS;
589*cdf0e10cSrcweir     }
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir #if 0 // YD
592*cdf0e10cSrcweir 	//
593*cdf0e10cSrcweir 	// resolves long FAT names used by OS2
594*cdf0e10cSrcweir 	//
595*cdf0e10cSrcweir 	sal_Bool bIsLongOS2=sal_False;
596*cdf0e10cSrcweir 	if (Folder::IsAvailable())
597*cdf0e10cSrcweir 	{
598*cdf0e10cSrcweir 		DirEntry aDirEntry(rFilename);
599*cdf0e10cSrcweir 		if  (aDirEntry.IsLongNameOnFAT())
600*cdf0e10cSrcweir 		{
601*cdf0e10cSrcweir 			// in kurzen Pfad wandeln
602*cdf0e10cSrcweir 			ItemIDPath      aItemIDPath(rFilename);
603*cdf0e10cSrcweir 			aParsedFilename = aItemIDPath.GetHostNotationPath();
604*cdf0e10cSrcweir 			bIsLongOS2 = sal_True;
605*cdf0e10cSrcweir 		}
606*cdf0e10cSrcweir 	}
607*cdf0e10cSrcweir #endif
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir 	aFilename = aParsedFilename;
610*cdf0e10cSrcweir 	ByteString aFileNameA( aFilename, gsl_getSystemTextEncoding());
611*cdf0e10cSrcweir 	FSysRedirector::DoRedirect( aFilename );
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir #ifdef DBG_UTIL
614*cdf0e10cSrcweir 	ByteString aTraceStr( "SvFileStream::Open(): " );
615*cdf0e10cSrcweir 	aTraceStr +=  aFileNameA;
616*cdf0e10cSrcweir 	DBG_TRACE( aTraceStr.GetBuffer() );
617*cdf0e10cSrcweir #endif
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir     APIRET nRet = DosOpen( aFileNameA.GetBuffer(), &pInstanceData->hFile,
620*cdf0e10cSrcweir     &nActionTaken, 0L, FILE_NORMAL, nOpenAction,
621*cdf0e10cSrcweir     nReadWriteBits | nShareBits | OPEN_FLAGS_NOINHERIT, 0L);
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir     if( nRet == ERROR_TOO_MANY_OPEN_FILES )
624*cdf0e10cSrcweir     {
625*cdf0e10cSrcweir 		long nToAdd = 10;
626*cdf0e10cSrcweir 		sal_uIntPtr nCurMaxFH;
627*cdf0e10cSrcweir 		nRet = DosSetRelMaxFH( &nToAdd, &nCurMaxFH );
628*cdf0e10cSrcweir 		nRet = DosOpen( aFileNameA.GetBuffer(), &pInstanceData->hFile,
629*cdf0e10cSrcweir 		&nActionTaken, 0L, FILE_NORMAL, nOpenAction,
630*cdf0e10cSrcweir 		nReadWriteBits | nShareBits | OPEN_FLAGS_NOINHERIT, 0L);
631*cdf0e10cSrcweir     }
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir     // Bei Fehler pruefen, ob wir lesen duerfen
634*cdf0e10cSrcweir     if( nRet==ERROR_ACCESS_DENIED || nRet==ERROR_SHARING_VIOLATION )
635*cdf0e10cSrcweir     {
636*cdf0e10cSrcweir         nReadWriteBits = OPEN_ACCESS_READONLY;
637*cdf0e10cSrcweir         nRet = DosOpen( aFileNameA.GetBuffer(), &pInstanceData->hFile,
638*cdf0e10cSrcweir             &nActionTaken, 0L, FILE_NORMAL, nOpenAction,
639*cdf0e10cSrcweir             nReadWriteBits | nShareBits | OPEN_FLAGS_NOINHERIT, 0L);
640*cdf0e10cSrcweir     }
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir         if( nRet )
643*cdf0e10cSrcweir     {
644*cdf0e10cSrcweir         bIsOpen = sal_False;
645*cdf0e10cSrcweir         SetError(::GetSvError(nRet) );
646*cdf0e10cSrcweir     }
647*cdf0e10cSrcweir     else
648*cdf0e10cSrcweir     {
649*cdf0e10cSrcweir         bIsOpen     = sal_True;
650*cdf0e10cSrcweir         pInstanceData->bIsEof = sal_False;
651*cdf0e10cSrcweir         if( nReadWriteBits != OPEN_ACCESS_READONLY )
652*cdf0e10cSrcweir             bIsWritable = sal_True;
653*cdf0e10cSrcweir     }
654*cdf0e10cSrcweir 
655*cdf0e10cSrcweir #if 0
656*cdf0e10cSrcweir 	if (bIsOpen && bIsLongOS2)
657*cdf0e10cSrcweir 	{
658*cdf0e10cSrcweir 		//file schlie�en, da sonst createLongName u.U. nicht m�glich
659*cdf0e10cSrcweir 		Close();
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir 		// erzeugtem File langen Namen geben
662*cdf0e10cSrcweir 		DirEntry aDirEntry(rFilename);
663*cdf0e10cSrcweir 		createLongNameEA(aFileNameA.GetBuffer(),  FILE_NORMAL, aDirEntry.GetName());
664*cdf0e10cSrcweir 
665*cdf0e10cSrcweir 		// und wieder oeffnen
666*cdf0e10cSrcweir 		ReOpen();
667*cdf0e10cSrcweir 	}
668*cdf0e10cSrcweir #endif
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir }
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir /*************************************************************************
673*cdf0e10cSrcweir |*
674*cdf0e10cSrcweir |*    SvFileStream::ReOpen()
675*cdf0e10cSrcweir |*
676*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
677*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
678*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
679*cdf0e10cSrcweir |*
680*cdf0e10cSrcweir *************************************************************************/
681*cdf0e10cSrcweir 
682*cdf0e10cSrcweir void SvFileStream::ReOpen()
683*cdf0e10cSrcweir {
684*cdf0e10cSrcweir     if( !bIsOpen && aFilename.Len() )
685*cdf0e10cSrcweir         Open( aFilename, eStreamMode );
686*cdf0e10cSrcweir }
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir /*************************************************************************
689*cdf0e10cSrcweir |*
690*cdf0e10cSrcweir |*    SvFileStream::Close()
691*cdf0e10cSrcweir |*
692*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW
693*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
694*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
695*cdf0e10cSrcweir |*
696*cdf0e10cSrcweir *************************************************************************/
697*cdf0e10cSrcweir 
698*cdf0e10cSrcweir void SvFileStream::Close()
699*cdf0e10cSrcweir {
700*cdf0e10cSrcweir     if( IsOpen() )
701*cdf0e10cSrcweir     {
702*cdf0e10cSrcweir #ifdef DBG_UTIL
703*cdf0e10cSrcweir         ByteString aTraceStr( "SvFileStream::Close(): " );
704*cdf0e10cSrcweir         aTraceStr += ByteString(aFilename, osl_getThreadTextEncoding());
705*cdf0e10cSrcweir         DBG_TRACE( aTraceStr.GetBuffer() );
706*cdf0e10cSrcweir #endif
707*cdf0e10cSrcweir 
708*cdf0e10cSrcweir         if( nLockCounter )
709*cdf0e10cSrcweir         {
710*cdf0e10cSrcweir             nLockCounter = 1;
711*cdf0e10cSrcweir             UnlockFile();
712*cdf0e10cSrcweir         }
713*cdf0e10cSrcweir         Flush();
714*cdf0e10cSrcweir         DosClose( pInstanceData->hFile );
715*cdf0e10cSrcweir     }
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir     bIsOpen     = sal_False;
718*cdf0e10cSrcweir     nLockCounter= 0;
719*cdf0e10cSrcweir     bIsWritable = sal_False;
720*cdf0e10cSrcweir     pInstanceData->bIsEof = sal_True;
721*cdf0e10cSrcweir     SvStream::ClearBuffer();
722*cdf0e10cSrcweir     SvStream::ClearError();
723*cdf0e10cSrcweir }
724*cdf0e10cSrcweir 
725*cdf0e10cSrcweir /*************************************************************************
726*cdf0e10cSrcweir |*
727*cdf0e10cSrcweir |*    SvFileStream::ResetError()
728*cdf0e10cSrcweir |*
729*cdf0e10cSrcweir |*    Beschreibung      STREAM.SDW; Setzt Filepointer auf Dateianfang
730*cdf0e10cSrcweir |*    Ersterstellung    OV 15.06.94
731*cdf0e10cSrcweir |*    Letzte Aenderung  OV 15.06.94
732*cdf0e10cSrcweir |*
733*cdf0e10cSrcweir *************************************************************************/
734*cdf0e10cSrcweir 
735*cdf0e10cSrcweir void SvFileStream::ResetError()
736*cdf0e10cSrcweir {
737*cdf0e10cSrcweir     SvStream::ClearError();
738*cdf0e10cSrcweir }
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir /*************************************************************************
741*cdf0e10cSrcweir |*
742*cdf0e10cSrcweir |*    SvFileStream::SetSize()
743*cdf0e10cSrcweir |*
744*cdf0e10cSrcweir |*    Beschreibung
745*cdf0e10cSrcweir |*    Ersterstellung    OV 19.10.95
746*cdf0e10cSrcweir |*    Letzte Aenderung  OV 19.10.95
747*cdf0e10cSrcweir |*
748*cdf0e10cSrcweir *************************************************************************/
749*cdf0e10cSrcweir 
750*cdf0e10cSrcweir void SvFileStream::SetSize( sal_uIntPtr nSize )
751*cdf0e10cSrcweir {
752*cdf0e10cSrcweir     if( IsOpen() )
753*cdf0e10cSrcweir     {
754*cdf0e10cSrcweir         APIRET nRet = DosSetFileSize( pInstanceData->hFile, nSize );
755*cdf0e10cSrcweir         if( nRet )
756*cdf0e10cSrcweir             SetError( ::GetSvError( nRet ) );
757*cdf0e10cSrcweir     }
758*cdf0e10cSrcweir }
759