merged tag ooo/OOO330_m14
[LibreOffice.git] / cosv / source / storage / file.cxx
blobaa1190ec9fab3f62714cb4b47bf9579e4fe83d4a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <precomp.h>
29 #include <cosv/file.hxx>
31 // NOT FULLY DECLARED SERVICES
34 namespace csv
38 File::File( uintt i_nMode )
39 : // aPath,
40 pStream(0),
41 nMode(i_nMode),
42 eLastIO(io_none)
46 File::File( const ploc::Path & i_rLocation,
47 uintt i_nMode )
48 : aPath(i_rLocation),
49 pStream(0),
50 nMode(i_nMode),
51 eLastIO(io_none)
55 File::File( const char * i_sLocation,
56 uintt i_nMode )
57 : aPath(i_sLocation),
58 pStream(0),
59 nMode(i_nMode),
60 eLastIO(io_none)
64 File::File( const String & i_sLocation,
65 uintt i_nMode )
66 : aPath(i_sLocation),
67 pStream(0),
68 nMode(i_nMode),
69 eLastIO(io_none)
73 File::~File()
75 if ( inq_is_open() )
76 close();
79 bool
80 File::Assign( ploc::Path i_rLocation )
82 if (is_open() )
83 return false;
85 InvalidatePath();
86 aPath = i_rLocation;
87 return true;
90 bool
91 File::Assign( const char * i_sLocation )
93 if (is_open() )
94 return false;
96 InvalidatePath();
97 aPath.Set( i_sLocation );
98 return true;
101 bool
102 File::Assign( const String & i_sLocation )
104 if (is_open() )
105 return false;
107 InvalidatePath();
108 aPath.Set( i_sLocation );
109 return true;
112 uintt
113 File::do_read( void * out_pDest,
114 uintt i_nNrofBytes )
116 if ( NOT inq_is_open() )
117 return 0;
119 if ( eLastIO == io_write )
120 ::fseek( pStream, 0, SEEK_CUR );
121 uintt ret = position();
122 int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream );
123 if ( iRet < 0 ) {
124 fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__);
126 ret = position() - ret;
128 eLastIO = io_read;
129 return ret;
132 bool
133 File::inq_eod() const
135 if ( NOT inq_is_open() )
136 return true;
137 return feof(pStream) != 0;
140 uintt
141 File::do_write( const void * i_pSrc,
142 uintt i_nNrofBytes )
144 if ( NOT inq_is_open() )
145 return 0;
147 if ( eLastIO == io_write )
148 ::fseek( pStream, 0, SEEK_CUR );
149 uintt ret = position();
150 ::fwrite( i_pSrc, 1, i_nNrofBytes, pStream );
151 ret = position() - ret;
153 eLastIO = io_write;
154 return ret;
157 uintt
158 File::do_seek( intt i_nDistance,
159 seek_dir i_eStartPoint )
161 if ( NOT inq_is_open() )
162 return uintt(-1);
164 static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
166 ::fseek( pStream,
167 intt(i_nDistance),
168 eSearchDir[i_eStartPoint] );
169 return position();
172 uintt
173 File::inq_position() const
175 if ( inq_is_open() )
176 return uintt( ::ftell(pStream) );
177 else
178 return uintt(-1);
181 bool
182 File::do_open( uintt i_nOpenMode )
184 if ( inq_is_open() )
186 if ( i_nOpenMode == 0 OR i_nOpenMode == nMode )
187 return true;
188 close();
191 if ( i_nOpenMode != 0 )
192 nMode = i_nOpenMode;
194 const char * sFacadeMode = "";
195 switch ( nMode )
197 case CFM_RW: sFacadeMode = "r+b";
198 break;
199 case CFM_CREATE: sFacadeMode = "w+b";
200 break;
201 case CFM_READ: sFacadeMode = "rb";
202 break;
203 default:
204 sFacadeMode = "rb";
207 pStream = ::fopen( StrPath(), sFacadeMode );
208 if ( pStream == 0 AND nMode == CFM_RW )
210 sFacadeMode = "w+b";
211 pStream = ::fopen( StrPath(), sFacadeMode );
214 return pStream != 0;
217 void
218 File::do_close()
220 if ( inq_is_open() )
222 ::fclose(pStream);
223 pStream = 0;
225 eLastIO = io_none;
228 bool
229 File::inq_is_open() const
231 return pStream != 0;
234 const ploc::Path &
235 File::inq_MyPath() const
237 return aPath;
241 } // namespace csv