X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FSMESHUtils%2FSMESH_File.cxx;h=2472d9eb3a365ee6bbcd772b4f42e2103886b18c;hp=d75cd108adc87461108262cbca7bdc22a3cc4c99;hb=c5f8cda9bf008812f728073dd5cc04f6c5082bbb;hpb=5d68554076bbca0e1e95fb0db215a6c2b84b6c54 diff --git a/src/SMESHUtils/SMESH_File.cxx b/src/SMESHUtils/SMESH_File.cxx index d75cd108a..2472d9eb3 100644 --- a/src/SMESHUtils/SMESH_File.cxx +++ b/src/SMESHUtils/SMESH_File.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2016 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 @@ -36,6 +36,8 @@ #include +#include + namespace boofs = boost::filesystem; //================================================================================ @@ -45,7 +47,13 @@ namespace boofs = boost::filesystem; //================================================================================ SMESH_File::SMESH_File(const std::string& name, bool open) - :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0) + :_name(name), _size(-1), +#ifdef WIN32 + _file(INVALID_HANDLE_VALUE), +#else + _file(-1), +#endif + _map(0), _pos(0), _end(0) { if ( open ) this->open(); } @@ -69,16 +77,24 @@ SMESH_File::~SMESH_File() bool SMESH_File::open() { - int length = size(); + long length = size(); if ( !_map && length > 0 ) { #ifdef WIN32 - _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ, +#ifdef UNICODE + const wchar_t* name = Kernel_Utils::decode(_name.data()); +#else + char* name = name.data(); +#endif + _file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); bool ok = ( _file != INVALID_HANDLE_VALUE ); +#ifdef UNICODE + delete name; +#endif #else _file = ::open(_name.data(), O_RDONLY ); - bool ok = ( _file > 0 ); + bool ok = ( _file >= 0 ); #endif if ( ok ) { @@ -105,6 +121,10 @@ bool SMESH_File::open() #endif } } + else if ( _error.empty() ) + { + _error = "Can't open for reading an existing file " + _name; + } } return _pos; } @@ -134,11 +154,15 @@ void SMESH_File::close() else if ( _file >= 0 ) { #ifdef WIN32 - CloseHandle(_file); - _file = INVALID_HANDLE_VALUE; + if(_file != INVALID_HANDLE_VALUE) { + CloseHandle(_file); + _file = INVALID_HANDLE_VALUE; + } #else - ::close(_file); - _file = -1; + if(_file != -1) { + ::close(_file); + _file = -1; + } #endif } } @@ -174,7 +198,7 @@ long SMESH_File::size() boost::uintmax_t size = boofs::file_size( _name, err ); _error = err.message(); - return err ? -1 : (long) size; + return !err ? (long) size : -1; } //================================================================================ @@ -257,7 +281,7 @@ void SMESH_File::rewind() bool SMESH_File::getInts(std::vector& ints) { - int i = 0; + size_t i = 0; while ( i < ints.size() ) { while ( !isdigit( *_pos ) && !eof()) ++_pos; @@ -277,14 +301,21 @@ bool SMESH_File::getInts(std::vector& ints) bool SMESH_File::openForWriting() { #ifdef WIN32 - - _file = CreateFile( _name.c_str(), // name of the write +#ifdef UNICODE + const wchar_t* name = Kernel_Utils::decode(_name.data()); +#else + char* name = name.data(); +#endif + _file = CreateFile( name, // 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 +#ifdef UNICODE + delete name; +#endif return ( _file != INVALID_HANDLE_VALUE ); #else @@ -316,7 +347,7 @@ bool SMESH_File::writeRaw(const void* data, size_t size) #else ssize_t nbWritten = ::write( _file, data, size ); - return ( nbWritten == size ); + return ( nbWritten == (ssize_t) size ); #endif }