make it possible to check STEPfile r/w progress

Adds member functions `float GetReadProgress() const` and `float GetWriteProgress() const`,
and some protected data members. The new functions are intended to be thread safe.
This commit is contained in:
Mark Pictor 2012-04-06 10:02:13 -04:00
parent 225180f35d
commit 0bea3efca0
3 changed files with 65 additions and 1 deletions

View file

@ -55,6 +55,43 @@ const std::string STEPfile::SetFileName( const std::string newName ) {
return _fileName;
}
/** Returns read progress,scaled 0-100. Will return a value < 0 if
* called *immediately* after read operation starts, if no file has
* been specified, or if file has been closed. If result < 50,
* ReadData1() hasn't completed yet.
*
* This function is useless unless it is called from another thread.
*/
float STEPfile::GetReadProgress() const {
if( _iFileSize < 1 ) {
return -1;
}
//the file is read once by ReadData1(), and again by ReadData2. Each gets 50%.
float percent = ( static_cast<float>( _iFileCurrentPosition ) / _iFileSize ) * 50.0;
if( _iFileStage1Done ) {
percent += 50;
}
return percent;
}
/** Returns write progress,scaled 0-100. Will return a value < 0 if
* called *immediately* after write operation starts, if no file has
* been specified, or if file has been closed.
* \sa GetReadProgress()
*
* Caveat: Only works for files written via WriteExchangeFile() / WriteData().
*
* This function is useless unless it is called from another thread.
*/
float STEPfile::GetWriteProgress() const {
int total = _instances.InstanceCount();
if( total > 0 ) {
return ( static_cast<float>( _oFileInstsWritten ) / total ) * 100.0;
} else {
return -1;
}
}
/**
* \param in The input stream from which the file is read.
*
@ -463,6 +500,7 @@ int STEPfile::ReadData1( istream & in ) {
SkipInstance( in, tmpbuf );
} else {
obj = CreateInstance( in, cout );
_iFileCurrentPosition = in.tellg();
}
if( obj != ENTITY_NULL ) {
@ -511,6 +549,7 @@ int STEPfile::ReadData1( istream & in ) {
_error.AppendToUserMsg( "Error in input file.\n" );
}
_iFileStage1Done = true;
return instance_count;
}
@ -590,6 +629,7 @@ int STEPfile::ReadData2( istream & in, bool useTechCor ) {
SkipInstance( in, tmpbuf );
} else {
obj = ReadInstance( in, cout, cmtStr, useTechCor );
_iFileCurrentPosition = in.tellg();
}
cmtStr.clear();
@ -1573,12 +1613,14 @@ void STEPfile::WriteHeaderInstanceFileSchema( ostream & out ) {
***************************/
void STEPfile::WriteData( ostream & out, int writeComments ) {
_oFileInstsWritten = 0;
std::string currSch = schemaName();
out << "DATA;\n";
int n = instances().InstanceCount();
for( int i = 0; i < n; ++i ) {
instances().GetMgrNode( i )->GetApplication_instance()->STEPwrite( out, currSch.c_str(), writeComments );
_oFileInstsWritten++;
}
out << "ENDSEC;\n";

View file

@ -61,6 +61,12 @@ class SCL_EDITOR_EXPORT STEPfile {
DirObj * _currentDir;
std::string _fileName;
//the following are used to compute read/write progress
std::ifstream::pos_type _iFileSize; ///< input file size
std::ifstream::pos_type _iFileCurrentPosition; ///< input file position (from ifstream::tellg())
bool _iFileStage1Done; ///< set immediately before ReadData1() returns
int _oFileInstsWritten; ///< number of instances that have been written
//error information
ErrorDescriptor _error;
@ -111,6 +117,8 @@ class SCL_EDITOR_EXPORT STEPfile {
}
const std::string SetFileName( const std::string name = "" );
const std::string TruncFileName( const std::string name ) const;
float GetReadProgress() const;
float GetWriteProgress() const;
//error information
ErrorDescriptor & Error() { /* const */

View file

@ -29,7 +29,9 @@ STEPfile::STEPfile( Registry & r, InstMgr & i, const std::string filename, bool
_instances( i ), _reg( r ), _fileIdIncr( 0 ), _headerId( 0 ),
_entsNotCreated( 0 ), _entsInvalid( 0 ), _entsIncomplete( 0 ),
_entsWarning( 0 ), _errorCount( 0 ), _warningCount( 0 ),
_maxErrorCount( 5000 ), _strict( strict ) {
_maxErrorCount( 5000 ), _strict( strict ),_iFileSize( 0 ),
_iFileCurrentPosition( 0 ), _oFileInstsWritten( 0 ),
_iFileStage1Done( false ) {
SetFileType( VERSION_CURRENT );
SetFileIdIncrement();
_currentDir = new DirObj( "" );
@ -174,6 +176,8 @@ Severity STEPfile::AppendWorkingFile( const std::string filename, bool useTechCo
/******************************************************/
istream * STEPfile::OpenInputFile( const std::string filename ) {
_iFileCurrentPosition = 0;
// if there's no filename to use, fail
if( filename.empty() && FileName().empty() ) {
_error.AppendToUserMsg( "Unable to open file for input. No current file name.\n" );
@ -205,6 +209,10 @@ istream * STEPfile::OpenInputFile( const std::string filename ) {
return ( 0 );
}
//check size of file
in->seekg( 0, std::ifstream::end );
_iFileSize = in->tellg();
in->seekg( 0, std::ifstream::beg );
return in;
}
@ -213,6 +221,10 @@ void STEPfile::CloseInputFile( istream * in ) {
if( in && *in != std::cin ) {
delete in;
}
//reset file size
_iFileSize = 0;
_iFileCurrentPosition = 0;
}
@ -240,10 +252,12 @@ ofstream * STEPfile::OpenOutputFile( const std::string filename ) {
_error.AppendToUserMsg( "unable to open file for output\n" );
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
}
_oFileInstsWritten = 0;
return out;
}
void STEPfile::CloseOutputFile( ostream * out ) {
_oFileInstsWritten = 0;
delete out;
}