xref: /aoo41x/main/cosv/source/storage/file.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include <cosv/file.hxx>
30 
31 // NOT FULLY DECLARED SERVICES
32 
33 
34 namespace csv
35 {
36 
37 
38 File::File( uintt  i_nMode )
39 	:	// aPath,
40         pStream(0),
41         nMode(i_nMode),
42         eLastIO(io_none)
43 {
44 }
45 
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)
52 {
53 }
54 
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)
61 {
62 }
63 
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)
70 {
71 }
72 
73 File::~File()
74 {
75     if ( inq_is_open() )
76         close();
77 }
78 
79 bool
80 File::Assign( ploc::Path i_rLocation )
81 {
82     if (is_open() )
83 		return false;
84 
85     InvalidatePath();
86     aPath = i_rLocation;
87     return true;
88 }
89 
90 bool
91 File::Assign( const char * i_sLocation )
92 {
93     if (is_open() )
94 		return false;
95 
96     InvalidatePath();
97     aPath.Set( i_sLocation );
98 	return true;
99 }
100 
101 bool
102 File::Assign( const String & i_sLocation )
103 {
104     if (is_open() )
105 		return false;
106 
107     InvalidatePath();
108     aPath.Set( i_sLocation );
109     return true;
110 }
111 
112 uintt
113 File::do_read( void *	       out_pDest,
114 			   uintt           i_nNrofBytes )
115 {
116 	if ( NOT inq_is_open() )
117 		return 0;
118 
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__);
125     }
126     ret = position() - ret;
127 
128     eLastIO = io_read;
129 	return ret;
130 }
131 
132 bool
133 File::inq_eod() const
134 {
135 	if ( NOT inq_is_open() )
136 		return true;
137     return feof(pStream) != 0;
138 }
139 
140 uintt
141 File::do_write( const void *   	i_pSrc,
142 				uintt           i_nNrofBytes )
143 {
144     if ( NOT inq_is_open() )
145 		return 0;
146 
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;
152 
153     eLastIO = io_write;
154 	return ret;
155 }
156 
157 uintt
158 File::do_seek( intt 	i_nDistance,
159                seek_dir i_eStartPoint )
160 {
161     if ( NOT inq_is_open() )
162         return uintt(-1);
163 
164     static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
165 
166     ::fseek( pStream,
167              intt(i_nDistance),
168              eSearchDir[i_eStartPoint] );
169 	return position();
170 }
171 
172 uintt
173 File::inq_position() const
174 {
175     if ( inq_is_open() )
176     	return uintt( ::ftell(pStream) );
177     else
178         return uintt(-1);
179 }
180 
181 bool
182 File::do_open( uintt i_nOpenMode )
183 {
184     if ( inq_is_open() )
185     {
186         if ( i_nOpenMode == 0 OR i_nOpenMode == nMode )
187             return true;
188         close();
189     }
190 
191     if ( i_nOpenMode != 0 )
192         nMode = i_nOpenMode;
193 
194     const char * sFacadeMode = "";
195     switch ( nMode )
196     {
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";
205     }
206 
207     pStream = ::fopen( StrPath(), sFacadeMode );
208     if ( pStream == 0 AND nMode == CFM_RW )
209     {
210         sFacadeMode = "w+b";
211         pStream = ::fopen( StrPath(), sFacadeMode );
212     }
213 
214     return pStream != 0;
215 }
216 
217 void
218 File::do_close()
219 {
220     if ( inq_is_open() )
221     {
222     	::fclose(pStream);
223         pStream = 0;
224     }
225     eLastIO = io_none;
226 }
227 
228 bool
229 File::inq_is_open() const
230 {
231     return pStream != 0;
232 }
233 
234 const ploc::Path &
235 File::inq_MyPath() const
236 {
237  	return aPath;
238 }
239 
240 
241 }   // namespace csv
242 
243