1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include <precomp.h> 29*cdf0e10cSrcweir #include <cosv/file.hxx> 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir namespace csv 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir File::File( uintt i_nMode ) 39*cdf0e10cSrcweir : // aPath, 40*cdf0e10cSrcweir pStream(0), 41*cdf0e10cSrcweir nMode(i_nMode), 42*cdf0e10cSrcweir eLastIO(io_none) 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir } 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir File::File( const ploc::Path & i_rLocation, 47*cdf0e10cSrcweir uintt i_nMode ) 48*cdf0e10cSrcweir : aPath(i_rLocation), 49*cdf0e10cSrcweir pStream(0), 50*cdf0e10cSrcweir nMode(i_nMode), 51*cdf0e10cSrcweir eLastIO(io_none) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir } 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir File::File( const char * i_sLocation, 56*cdf0e10cSrcweir uintt i_nMode ) 57*cdf0e10cSrcweir : aPath(i_sLocation), 58*cdf0e10cSrcweir pStream(0), 59*cdf0e10cSrcweir nMode(i_nMode), 60*cdf0e10cSrcweir eLastIO(io_none) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir File::File( const String & i_sLocation, 65*cdf0e10cSrcweir uintt i_nMode ) 66*cdf0e10cSrcweir : aPath(i_sLocation), 67*cdf0e10cSrcweir pStream(0), 68*cdf0e10cSrcweir nMode(i_nMode), 69*cdf0e10cSrcweir eLastIO(io_none) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir File::~File() 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir if ( inq_is_open() ) 76*cdf0e10cSrcweir close(); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir bool 80*cdf0e10cSrcweir File::Assign( ploc::Path i_rLocation ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir if (is_open() ) 83*cdf0e10cSrcweir return false; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir InvalidatePath(); 86*cdf0e10cSrcweir aPath = i_rLocation; 87*cdf0e10cSrcweir return true; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir bool 91*cdf0e10cSrcweir File::Assign( const char * i_sLocation ) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir if (is_open() ) 94*cdf0e10cSrcweir return false; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir InvalidatePath(); 97*cdf0e10cSrcweir aPath.Set( i_sLocation ); 98*cdf0e10cSrcweir return true; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir bool 102*cdf0e10cSrcweir File::Assign( const String & i_sLocation ) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir if (is_open() ) 105*cdf0e10cSrcweir return false; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir InvalidatePath(); 108*cdf0e10cSrcweir aPath.Set( i_sLocation ); 109*cdf0e10cSrcweir return true; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir uintt 113*cdf0e10cSrcweir File::do_read( void * out_pDest, 114*cdf0e10cSrcweir uintt i_nNrofBytes ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir if ( NOT inq_is_open() ) 117*cdf0e10cSrcweir return 0; 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir if ( eLastIO == io_write ) 120*cdf0e10cSrcweir ::fseek( pStream, 0, SEEK_CUR ); 121*cdf0e10cSrcweir uintt ret = position(); 122*cdf0e10cSrcweir int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream ); 123*cdf0e10cSrcweir if ( iRet < 0 ) { 124*cdf0e10cSrcweir fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir ret = position() - ret; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir eLastIO = io_read; 129*cdf0e10cSrcweir return ret; 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir bool 133*cdf0e10cSrcweir File::inq_eod() const 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir if ( NOT inq_is_open() ) 136*cdf0e10cSrcweir return true; 137*cdf0e10cSrcweir return feof(pStream) != 0; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir uintt 141*cdf0e10cSrcweir File::do_write( const void * i_pSrc, 142*cdf0e10cSrcweir uintt i_nNrofBytes ) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir if ( NOT inq_is_open() ) 145*cdf0e10cSrcweir return 0; 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir if ( eLastIO == io_write ) 148*cdf0e10cSrcweir ::fseek( pStream, 0, SEEK_CUR ); 149*cdf0e10cSrcweir uintt ret = position(); 150*cdf0e10cSrcweir ::fwrite( i_pSrc, 1, i_nNrofBytes, pStream ); 151*cdf0e10cSrcweir ret = position() - ret; 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir eLastIO = io_write; 154*cdf0e10cSrcweir return ret; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir uintt 158*cdf0e10cSrcweir File::do_seek( intt i_nDistance, 159*cdf0e10cSrcweir seek_dir i_eStartPoint ) 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir if ( NOT inq_is_open() ) 162*cdf0e10cSrcweir return uintt(-1); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END }; 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir ::fseek( pStream, 167*cdf0e10cSrcweir intt(i_nDistance), 168*cdf0e10cSrcweir eSearchDir[i_eStartPoint] ); 169*cdf0e10cSrcweir return position(); 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir uintt 173*cdf0e10cSrcweir File::inq_position() const 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir if ( inq_is_open() ) 176*cdf0e10cSrcweir return uintt( ::ftell(pStream) ); 177*cdf0e10cSrcweir else 178*cdf0e10cSrcweir return uintt(-1); 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir bool 182*cdf0e10cSrcweir File::do_open( uintt i_nOpenMode ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir if ( inq_is_open() ) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir if ( i_nOpenMode == 0 OR i_nOpenMode == nMode ) 187*cdf0e10cSrcweir return true; 188*cdf0e10cSrcweir close(); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir if ( i_nOpenMode != 0 ) 192*cdf0e10cSrcweir nMode = i_nOpenMode; 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir const char * sFacadeMode = ""; 195*cdf0e10cSrcweir switch ( nMode ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir case CFM_RW: sFacadeMode = "r+b"; 198*cdf0e10cSrcweir break; 199*cdf0e10cSrcweir case CFM_CREATE: sFacadeMode = "w+b"; 200*cdf0e10cSrcweir break; 201*cdf0e10cSrcweir case CFM_READ: sFacadeMode = "rb"; 202*cdf0e10cSrcweir break; 203*cdf0e10cSrcweir default: 204*cdf0e10cSrcweir sFacadeMode = "rb"; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir pStream = ::fopen( StrPath(), sFacadeMode ); 208*cdf0e10cSrcweir if ( pStream == 0 AND nMode == CFM_RW ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir sFacadeMode = "w+b"; 211*cdf0e10cSrcweir pStream = ::fopen( StrPath(), sFacadeMode ); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir return pStream != 0; 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir void 218*cdf0e10cSrcweir File::do_close() 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir if ( inq_is_open() ) 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir ::fclose(pStream); 223*cdf0e10cSrcweir pStream = 0; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir eLastIO = io_none; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir bool 229*cdf0e10cSrcweir File::inq_is_open() const 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir return pStream != 0; 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir const ploc::Path & 235*cdf0e10cSrcweir File::inq_MyPath() const 236*cdf0e10cSrcweir { 237*cdf0e10cSrcweir return aPath; 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir } // namespace csv 242*cdf0e10cSrcweir 243