xref: /aoo4110/main/cosv/source/storage/file.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <precomp.h>
25 #include <cosv/file.hxx>
26 
27 // NOT FULLY DECLARED SERVICES
28 
29 
30 namespace csv
31 {
32 
33 
File(uintt i_nMode)34 File::File( uintt  i_nMode )
35 	:	// aPath,
36         pStream(0),
37         nMode(i_nMode),
38         eLastIO(io_none)
39 {
40 }
41 
File(const ploc::Path & i_rLocation,uintt i_nMode)42 File::File( const ploc::Path &	i_rLocation,
43 			uintt 			    i_nMode )
44 	:	aPath(i_rLocation),
45         pStream(0),
46         nMode(i_nMode),
47         eLastIO(io_none)
48 {
49 }
50 
File(const char * i_sLocation,uintt i_nMode)51 File::File( const char *	i_sLocation,
52 			uintt 			i_nMode )
53 	:	aPath(i_sLocation),
54         pStream(0),
55         nMode(i_nMode),
56         eLastIO(io_none)
57 {
58 }
59 
File(const String & i_sLocation,uintt i_nMode)60 File::File( const String &  i_sLocation,
61 			uintt 			i_nMode )
62 	:	aPath(i_sLocation),
63         pStream(0),
64         nMode(i_nMode),
65         eLastIO(io_none)
66 {
67 }
68 
~File()69 File::~File()
70 {
71     if ( inq_is_open() )
72         close();
73 }
74 
75 bool
Assign(ploc::Path i_rLocation)76 File::Assign( ploc::Path i_rLocation )
77 {
78     if (is_open() )
79 		return false;
80 
81     InvalidatePath();
82     aPath = i_rLocation;
83     return true;
84 }
85 
86 bool
Assign(const char * i_sLocation)87 File::Assign( const char * i_sLocation )
88 {
89     if (is_open() )
90 		return false;
91 
92     InvalidatePath();
93     aPath.Set( i_sLocation );
94 	return true;
95 }
96 
97 bool
Assign(const String & i_sLocation)98 File::Assign( const String & i_sLocation )
99 {
100     if (is_open() )
101 		return false;
102 
103     InvalidatePath();
104     aPath.Set( i_sLocation );
105     return true;
106 }
107 
108 uintt
do_read(void * out_pDest,uintt i_nNrofBytes)109 File::do_read( void *	       out_pDest,
110 			   uintt           i_nNrofBytes )
111 {
112 	if ( NOT inq_is_open() )
113 		return 0;
114 
115     if ( eLastIO == io_write )
116         ::fseek( pStream, 0, SEEK_CUR );
117     uintt ret = position();
118     int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream );
119     if ( iRet < 0  )  {
120 	fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__);
121     }
122     ret = position() - ret;
123 
124     eLastIO = io_read;
125 	return ret;
126 }
127 
128 bool
inq_eod() const129 File::inq_eod() const
130 {
131 	if ( NOT inq_is_open() )
132 		return true;
133     return feof(pStream) != 0;
134 }
135 
136 uintt
do_write(const void * i_pSrc,uintt i_nNrofBytes)137 File::do_write( const void *   	i_pSrc,
138 				uintt           i_nNrofBytes )
139 {
140     if ( NOT inq_is_open() )
141 		return 0;
142 
143     if ( eLastIO == io_write )
144         ::fseek( pStream, 0, SEEK_CUR );
145 	uintt ret = position();
146 	::fwrite( i_pSrc, 1, i_nNrofBytes, pStream );
147     ret = position() - ret;
148 
149     eLastIO = io_write;
150 	return ret;
151 }
152 
153 uintt
do_seek(intt i_nDistance,seek_dir i_eStartPoint)154 File::do_seek( intt 	i_nDistance,
155                seek_dir i_eStartPoint )
156 {
157     if ( NOT inq_is_open() )
158         return uintt(-1);
159 
160     static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
161 
162     ::fseek( pStream,
163              intt(i_nDistance),
164              eSearchDir[i_eStartPoint] );
165 	return position();
166 }
167 
168 uintt
inq_position() const169 File::inq_position() const
170 {
171     if ( inq_is_open() )
172     	return uintt( ::ftell(pStream) );
173     else
174         return uintt(-1);
175 }
176 
177 bool
do_open(uintt i_nOpenMode)178 File::do_open( uintt i_nOpenMode )
179 {
180     if ( inq_is_open() )
181     {
182         if ( i_nOpenMode == 0 OR i_nOpenMode == nMode )
183             return true;
184         close();
185     }
186 
187     if ( i_nOpenMode != 0 )
188         nMode = i_nOpenMode;
189 
190     const char * sFacadeMode = "";
191     switch ( nMode )
192     {
193      	case CFM_RW:        sFacadeMode = "r+b";
194                             break;
195      	case CFM_CREATE:    sFacadeMode = "w+b";
196                             break;
197      	case CFM_READ:      sFacadeMode = "rb";
198                             break;
199         default:
200                             sFacadeMode = "rb";
201     }
202 
203     pStream = ::fopen( StrPath(), sFacadeMode );
204     if ( pStream == 0 AND nMode == CFM_RW )
205     {
206         sFacadeMode = "w+b";
207         pStream = ::fopen( StrPath(), sFacadeMode );
208     }
209 
210     return pStream != 0;
211 }
212 
213 void
do_close()214 File::do_close()
215 {
216     if ( inq_is_open() )
217     {
218     	::fclose(pStream);
219         pStream = 0;
220     }
221     eLastIO = io_none;
222 }
223 
224 bool
inq_is_open() const225 File::inq_is_open() const
226 {
227     return pStream != 0;
228 }
229 
230 const ploc::Path &
inq_MyPath() const231 File::inq_MyPath() const
232 {
233  	return aPath;
234 }
235 
236 
237 }   // namespace csv
238 
239