update dev300-m58
[ooovba.git] / ucb / source / ucp / file / filrec.cxx
blob1580069175b983c001efb53e5d0e71fb0b8edc57
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filrec.cxx,v $
10 * $Revision: 1.3.22.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 #include "filrec.hxx"
36 namespace fileaccess {
38 void ReconnectingFile::disconnect()
40 m_aFile.close();
41 m_bDisconnect = sal_True;
44 sal_Bool ReconnectingFile::reconnect()
46 sal_Bool bResult = sal_False;
47 if ( m_bFlagsSet )
49 disconnect();
50 if ( m_aFile.open( m_nFlags ) == ::osl::FileBase::E_None
51 || m_aFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None )
53 m_bDisconnect = sal_False;
54 bResult = sal_True;
58 return bResult;
61 ::osl::FileBase::RC ReconnectingFile::open( sal_uInt32 uFlags )
63 ::osl::FileBase::RC nResult = m_aFile.open( uFlags );
64 if ( nResult == ::osl::FileBase::E_None )
66 if ( uFlags & OpenFlag_Create )
67 m_nFlags = (uFlags & ( ~OpenFlag_Create )) | OpenFlag_Write;
68 else
69 m_nFlags = uFlags;
71 m_bFlagsSet = sal_True;
74 return nResult;
77 ::osl::FileBase::RC ReconnectingFile::close()
79 m_nFlags = 0;
80 m_bFlagsSet = sal_False;
81 m_bDisconnect = sal_False;
83 return m_aFile.close();
86 ::osl::FileBase::RC ReconnectingFile::setPos( sal_uInt32 uHow, sal_Int64 uPos )
88 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
90 if ( uHow == Pos_Absolut && uPos > 0 )
92 if ( m_bDisconnect )
94 if ( reconnect() )
95 nRes = m_aFile.setPos( uHow, uPos );
97 else
99 // E_INVAL error code means in this case that
100 // the file handler is invalid
101 nRes = m_aFile.setPos( uHow, uPos );
102 if ( ( nRes == ::osl::FileBase::E_NETWORK
103 || nRes == ::osl::FileBase::E_INVAL )
104 && reconnect() )
105 nRes = m_aFile.setPos( uHow, uPos );
108 else
110 if ( !m_bDisconnect )
111 nRes = m_aFile.setPos( uHow, uPos );
114 return nRes;
117 ::osl::FileBase::RC ReconnectingFile::getPos( sal_uInt64& uPos )
119 if ( m_bDisconnect )
120 return ::osl::FileBase::E_NETWORK;
122 return m_aFile.getPos( uPos );
125 ::osl::FileBase::RC ReconnectingFile::setSize( sal_uInt64 uSize )
127 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
129 if ( uSize == 0 )
131 if ( m_bDisconnect )
133 if ( reconnect() )
134 nRes = m_aFile.setSize( uSize );
136 else
138 // E_INVAL error code means in this case that
139 // the file handler is invalid
140 nRes = m_aFile.setSize( uSize );
141 if ( ( nRes == ::osl::FileBase::E_NETWORK
142 || nRes == ::osl::FileBase::E_INVAL )
143 && reconnect() )
144 nRes = m_aFile.setSize( uSize );
147 else
149 if ( !m_bDisconnect )
150 nRes = m_aFile.setSize( uSize );
153 return nRes;
156 ::osl::FileBase::RC ReconnectingFile::getSize( sal_uInt64 &rSize )
158 ::osl::FileBase::RC nRes = ::osl::FileBase::E_NETWORK;
160 if ( !m_bDisconnect )
161 nRes = m_aFile.getSize( rSize );
163 // E_INVAL error code means in this case that
164 // the file handler is invalid
165 if ( ( nRes == ::osl::FileBase::E_NETWORK
166 || nRes == ::osl::FileBase::E_INVAL )
167 && reconnect() )
169 nRes = m_aFile.getSize( rSize );
171 // the repairing should be disconnected, since the position might be wrong
172 // but the result should be retrieved
173 disconnect();
176 return nRes;
179 ::osl::FileBase::RC ReconnectingFile::read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
181 if ( m_bDisconnect )
182 return ::osl::FileBase::E_NETWORK;
184 return m_aFile.read( pBuffer, uBytesRequested, rBytesRead );
187 ::osl::FileBase::RC ReconnectingFile::write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
189 if ( m_bDisconnect )
190 return ::osl::FileBase::E_NETWORK;
192 return m_aFile.write( pBuffer, uBytesToWrite, rBytesWritten );
195 ::osl::FileBase::RC ReconnectingFile::sync() const
197 if ( m_bDisconnect )
198 return ::osl::FileBase::E_NETWORK;
200 return m_aFile.sync();
203 } // namespace fileaccess