bump product version to 4.1.6.2
[LibreOffice.git] / cosv / source / storage / file.cxx
blobc9eed642d545ca53b34a3700239d5f2cfc3197cc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #define CSV_USE_CSV_ASSERTIONS
21 #include <cosv/csv_env.hxx>
23 #include <cosv/comfunc.hxx>
24 #include <cosv/string.hxx>
25 #include <cosv/streamstr.hxx>
26 #include <cosv/std_outp.hxx>
27 #include <cosv/tpl/dyn.hxx>
28 #include <cosv/file.hxx>
30 // NOT FULLY DECLARED SERVICES
33 namespace csv
37 File::File( const char * i_sLocation,
38 uintt i_nMode )
39 : aPath(i_sLocation),
40 pStream(0),
41 nMode(i_nMode),
42 eLastIO(io_none)
46 File::File( const String & i_sLocation,
47 uintt i_nMode )
48 : aPath(i_sLocation),
49 pStream(0),
50 nMode(i_nMode),
51 eLastIO(io_none)
55 File::~File()
57 if ( inq_is_open() )
58 close();
61 uintt
62 File::do_read( void * out_pDest,
63 uintt i_nNrofBytes )
65 if ( NOT inq_is_open() )
66 return 0;
68 if ( eLastIO == io_write )
69 ::fseek( pStream, 0, SEEK_CUR );
70 uintt ret = position();
71 int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream );
72 if ( iRet < 0 ) {
73 fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__);
75 ret = position() - ret;
77 eLastIO = io_read;
78 return ret;
81 bool
82 File::inq_eod() const
84 if ( NOT inq_is_open() )
85 return true;
86 return feof(pStream) != 0;
89 uintt
90 File::do_write( const void * i_pSrc,
91 uintt i_nNrofBytes )
93 if ( NOT inq_is_open() )
94 return 0;
96 if ( eLastIO == io_write )
97 ::fseek( pStream, 0, SEEK_CUR );
99 uintt ret = ::fwrite( i_pSrc, 1, i_nNrofBytes, pStream );
101 eLastIO = io_write;
102 return ret;
105 uintt
106 File::do_seek( intt i_nDistance,
107 seek_dir i_eStartPoint )
109 if ( NOT inq_is_open() )
110 return uintt(-1);
112 static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
114 ::fseek( pStream,
115 intt(i_nDistance),
116 eSearchDir[i_eStartPoint] );
117 return position();
120 uintt
121 File::inq_position() const
123 if ( inq_is_open() )
124 return uintt( ::ftell(pStream) );
125 else
126 return uintt(-1);
129 bool
130 File::do_open( uintt i_nOpenMode )
132 if ( inq_is_open() )
134 if ( i_nOpenMode == 0 OR i_nOpenMode == nMode )
135 return true;
136 close();
139 if ( i_nOpenMode != 0 )
140 nMode = i_nOpenMode;
142 const char * sFacadeMode = "";
143 switch ( nMode )
145 case CFM_RW: sFacadeMode = "r+b";
146 break;
147 case CFM_CREATE: sFacadeMode = "w+b";
148 break;
149 case CFM_READ: sFacadeMode = "rb";
150 break;
151 default:
152 sFacadeMode = "rb";
155 pStream = ::fopen( StrPath(), sFacadeMode );
156 if ( pStream == 0 AND nMode == CFM_RW )
158 sFacadeMode = "w+b";
159 pStream = ::fopen( StrPath(), sFacadeMode );
162 return pStream != 0;
165 void
166 File::do_close()
168 if ( inq_is_open() )
170 ::fclose(pStream);
171 pStream = 0;
173 eLastIO = io_none;
176 bool
177 File::inq_is_open() const
179 return pStream != 0;
182 const ploc::Path &
183 File::inq_MyPath() const
185 return aPath;
189 } // namespace csv
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */