merge the formfield patch from ooo-build
[ooovba.git] / cosv / source / storage / file.cxx
blobc376a7e74ac0a4ffa455031d1de83ed8f28d5da6
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: file.cxx,v $
10 * $Revision: 1.4 $
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 #include <precomp.h>
32 #include <cosv/file.hxx>
34 // NOT FULLY DECLARED SERVICES
37 namespace csv
41 File::File( uintt i_nMode )
42 : // aPath,
43 pStream(0),
44 nMode(i_nMode),
45 eLastIO(io_none)
49 File::File( const ploc::Path & i_rLocation,
50 uintt i_nMode )
51 : aPath(i_rLocation),
52 pStream(0),
53 nMode(i_nMode),
54 eLastIO(io_none)
58 File::File( const char * i_sLocation,
59 uintt i_nMode )
60 : aPath(i_sLocation),
61 pStream(0),
62 nMode(i_nMode),
63 eLastIO(io_none)
67 File::File( const String & i_sLocation,
68 uintt i_nMode )
69 : aPath(i_sLocation),
70 pStream(0),
71 nMode(i_nMode),
72 eLastIO(io_none)
76 File::~File()
78 if ( inq_is_open() )
79 close();
82 bool
83 File::Assign( ploc::Path i_rLocation )
85 if (is_open() )
86 return false;
88 InvalidatePath();
89 aPath = i_rLocation;
90 return true;
93 bool
94 File::Assign( const char * i_sLocation )
96 if (is_open() )
97 return false;
99 InvalidatePath();
100 aPath.Set( i_sLocation );
101 return true;
104 bool
105 File::Assign( const String & i_sLocation )
107 if (is_open() )
108 return false;
110 InvalidatePath();
111 aPath.Set( i_sLocation );
112 return true;
115 uintt
116 File::do_read( void * out_pDest,
117 uintt i_nNrofBytes )
119 if ( NOT inq_is_open() )
120 return 0;
122 if ( eLastIO == io_write )
123 ::fseek( pStream, 0, SEEK_CUR );
124 uintt ret = position();
125 int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream );
126 if ( iRet < 0 ) {
127 fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__);
129 ret = position() - ret;
131 eLastIO = io_read;
132 return ret;
135 bool
136 File::inq_eod() const
138 if ( NOT inq_is_open() )
139 return true;
140 return feof(pStream) != 0;
143 uintt
144 File::do_write( const void * i_pSrc,
145 uintt i_nNrofBytes )
147 if ( NOT inq_is_open() )
148 return 0;
150 if ( eLastIO == io_write )
151 ::fseek( pStream, 0, SEEK_CUR );
152 uintt ret = position();
153 ::fwrite( i_pSrc, 1, i_nNrofBytes, pStream );
154 ret = position() - ret;
156 eLastIO = io_write;
157 return ret;
160 uintt
161 File::do_seek( intt i_nDistance,
162 seek_dir i_eStartPoint )
164 if ( NOT inq_is_open() )
165 return uintt(-1);
167 static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
169 ::fseek( pStream,
170 intt(i_nDistance),
171 eSearchDir[i_eStartPoint] );
172 return position();
175 uintt
176 File::inq_position() const
178 if ( inq_is_open() )
179 return uintt( ::ftell(pStream) );
180 else
181 return uintt(-1);
184 bool
185 File::do_open( uintt i_nOpenMode )
187 if ( inq_is_open() )
189 if ( i_nOpenMode == 0 OR i_nOpenMode == nMode )
190 return true;
191 close();
194 if ( i_nOpenMode != 0 )
195 nMode = i_nOpenMode;
197 const char * sFacadeMode = "";
198 switch ( nMode )
200 case CFM_RW: sFacadeMode = "r+b";
201 break;
202 case CFM_CREATE: sFacadeMode = "w+b";
203 break;
204 case CFM_READ: sFacadeMode = "rb";
205 break;
206 default:
207 sFacadeMode = "rb";
210 pStream = ::fopen( StrPath(), sFacadeMode );
211 if ( pStream == 0 AND nMode == CFM_RW )
213 sFacadeMode = "w+b";
214 pStream = ::fopen( StrPath(), sFacadeMode );
217 return pStream != 0;
220 void
221 File::do_close()
223 if ( inq_is_open() )
225 ::fclose(pStream);
226 pStream = 0;
228 eLastIO = io_none;
231 bool
232 File::inq_is_open() const
234 return pStream != 0;
237 const ploc::Path &
238 File::inq_MyPath() const
240 return aPath;
244 } // namespace csv