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