X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FSMESHUtils%2FSMESH_File.cxx;h=d75cd108adc87461108262cbca7bdc22a3cc4c99;hp=da6c5122f0b087deb901b14f1e204a047bf0a7b0;hb=9acd4347a6c7488c3ead4c68861b0944e83e545f;hpb=bd8f1aee7c78f7d2eb82bd4fec5e08c9e3d280ce diff --git a/src/SMESHUtils/SMESH_File.cxx b/src/SMESHUtils/SMESH_File.cxx index da6c5122f..d75cd108a 100644 --- a/src/SMESHUtils/SMESH_File.cxx +++ b/src/SMESHUtils/SMESH_File.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -21,14 +21,8 @@ // Created : Wed Mar 10 11:23:25 2010 // Author : Edward AGAPOV (eap) // -#include "SMESH_File.hxx" -#include "utilities.h" -#include -#include -#include -#include -#include +#include "SMESH_File.hxx" #include #include @@ -40,6 +34,10 @@ #include #endif +#include + +namespace boofs = boost::filesystem; + //================================================================================ /*! * \brief Creator opening the file for reading by default @@ -74,7 +72,7 @@ bool SMESH_File::open() int length = size(); if ( !_map && length > 0 ) { -#ifdef WNT +#ifdef WIN32 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); bool ok = ( _file != INVALID_HANDLE_VALUE ); @@ -84,7 +82,7 @@ bool SMESH_File::open() #endif if ( ok ) { -#ifdef WNT +#ifdef WIN32 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL); _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 ); #else @@ -99,7 +97,7 @@ bool SMESH_File::open() } else { -#ifdef WNT +#ifdef WIN32 CloseHandle(_mapObj); CloseHandle(_file); #else @@ -121,7 +119,7 @@ void SMESH_File::close() { if ( _map != NULL ) { -#ifdef WNT +#ifdef WIN32 UnmapViewOfFile(_map); CloseHandle(_mapObj); CloseHandle(_file); @@ -133,6 +131,16 @@ void SMESH_File::close() _pos = _end = 0; _size = -1; } + else if ( _file >= 0 ) + { +#ifdef WIN32 + CloseHandle(_file); + _file = INVALID_HANDLE_VALUE; +#else + ::close(_file); + _file = -1; +#endif + } } //================================================================================ @@ -144,15 +152,12 @@ void SMESH_File::close() bool SMESH_File::remove() { close(); - try { - OSD_Path filePath(TCollection_AsciiString((char*)_name.data())); - OSD_File(filePath).Remove(); - } - catch ( Standard_ProgramError ) { - MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied"); - return false; - } - return true; + + boost::system::error_code err; + boofs::remove( _name, err ); + _error = err.message(); + + return !err; } //================================================================================ @@ -161,21 +166,45 @@ bool SMESH_File::remove() */ //================================================================================ -int SMESH_File::size() const +long SMESH_File::size() { - if ( _size >= 0 ) return _size; // size of open file + if ( _size >= 0 ) return _size; // size of an open file - int size = -1; - int file = ::open( _name.data(), O_RDONLY ); - if ( file > 0 ) - { - struct stat status; - int err = fstat( file, &status); - if ( !err ) - size = status.st_size; - ::close( file ); - } - return size; + boost::system::error_code err; + boost::uintmax_t size = boofs::file_size( _name, err ); + _error = err.message(); + + return err ? -1 : (long) size; +} + +//================================================================================ +/*! + * \brief Check existence + */ +//================================================================================ + +bool SMESH_File::exists() +{ + boost::system::error_code err; + bool res = boofs::exists( _name, err ); + _error = err.message(); + + return err ? false : res; +} + +//================================================================================ +/*! + * \brief Check existence + */ +//================================================================================ + +bool SMESH_File::isDirectory() +{ + boost::system::error_code err; + bool res = boofs::is_directory( _name, err ); + _error = err.message(); + + return err ? false : res; } //================================================================================ @@ -238,3 +267,56 @@ bool SMESH_File::getInts(std::vector& ints) } return ( i == ints.size() ); } + +//================================================================================ +/*! + * \brief Open for binary writing only. + */ +//================================================================================ + +bool SMESH_File::openForWriting() +{ +#ifdef WIN32 + + _file = CreateFile( _name.c_str(), // name of the write + GENERIC_WRITE, // open for writing + 0, // do not share + NULL, // default security + OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING + FILE_ATTRIBUTE_NORMAL, // normal file + NULL); // no attr. template + return ( _file != INVALID_HANDLE_VALUE ); + +#else + + _file = ::open( _name.c_str(), + O_WRONLY | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r-- + return _file >= 0; + +#endif +} + +//================================================================================ +/*! + * \brief Write binary data + */ +//================================================================================ + +bool SMESH_File::writeRaw(const void* data, size_t size) +{ +#ifdef WIN32 + + DWORD nbWritten = 0; + BOOL err = WriteFile( _file, data, size, & nbWritten, NULL); + + return (( err == FALSE ) && + ( nbWritten == (DWORD) size )); + +#else + + ssize_t nbWritten = ::write( _file, data, size ); + return ( nbWritten == size ); + +#endif +}