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