Skip to content

Commit 271e273

Browse files
author
Nicholas Reed
committed
Simplify filepath handling and use more std::string and bool. SCL git 19a1bae and b277759.
1 parent 1e193e1 commit 271e273

File tree

7 files changed

+150
-552
lines changed

7 files changed

+150
-552
lines changed

src/cleditor/STEPfile.cc

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,23 @@
3434
#include <STEPundefined.h>
3535
#include <vector>
3636

37-
/***************************
38-
function: SetFileName
39-
returns: (const char*) The new file name for the class.
40-
parameters: (const char*) The file name to be set.
41-
description:
42-
This function sets the (char*)_fileName member variable to an
43-
absolute path name to a directory on the file system. If newName
44-
cannot be resolved to a file, then an empty char* is returned.
45-
side effects: STEPfile::_fileName value may change.
46-
***************************/
47-
const char * STEPfile::SetFileName( const char * newName ) {
48-
char tmp[MAXPATHLEN + 1];
49-
const char * path = tmp;
50-
37+
/**
38+
* \returns The new file name for the class.
39+
* \param newName The file name to be set.
40+
*
41+
* This function sets the _fileName member variable to an
42+
* absolute path name to a directory on the file system. If newName
43+
* cannot be resolved to a file, then an empty string is returned.
44+
*
45+
* side effects: STEPfile::_fileName value may change.
46+
*/
47+
std::string STEPfile::SetFileName( const std::string newName ) {
5148
// if a newName is not given or is the same as the old, use the old name
52-
if( ( !newName ) || ( !strcmp( newName, "" ) ) || ( newName == _fileName ) ) {
49+
if( ( newName.empty() ) || ( newName == _fileName ) ) {
5350
return FileName();
5451
}
5552

56-
path = _currentDir->Normalize( newName );
57-
if( !_currentDir->LoadDirectory( path ) ) {
58-
_error.AppendToUserMsg( "Cannot Load Directory.\n" );
59-
return ( char * )0;
60-
}
61-
62-
delete [] _fileName;
63-
_fileName = new char[strlen( newName ) + 1];
64-
if( !_fileName ) {
65-
_error.AppendToUserMsg( "free store exhausted.\n" );
66-
return ( char * )0;
67-
}
68-
strcpy( _fileName, newName );
53+
_fileName = DirObj::Normalize( newName );
6954
return _fileName;
7055
}
7156

@@ -1333,7 +1318,7 @@ SDAI_Application_instance * STEPfile::ReadInstance( istream & in, ostream & out,
13331318

13341319

13351320

1336-
/******************************************************
1321+
/**
13371322
This function uses the C library function system to issue
13381323
a shell command which checks for the existence of the
13391324
file name and creates a backup file if the file exists.
@@ -1344,25 +1329,24 @@ sh command. The command is:
13441329
BUG: doesn't check to see if the backup command works.
13451330
the results of the system call are not used by the
13461331
by this function
1347-
******************************************************/
1332+
*/
13481333
void STEPfile::MakeBackupFile() {
1349-
char backup_call [2 * BUFSIZ];
1350-
char backup_file [BUFSIZ];
1334+
std::string bckup = FileName();
1335+
bckup.append( ".bak" );
13511336

1352-
sprintf( backup_file, "%s.bak", FileName() );
1337+
std::fstream f( FileName().c_str(), std::fstream::in | std::fstream::binary );
1338+
f << std::noskipws;
1339+
std::istream_iterator<unsigned char> begin( f );
1340+
std::istream_iterator<unsigned char> end;
13531341

1354-
sprintf( backup_call,
1355-
"if (test -f %s )"
1356-
" then mv %s %s ;",
1357-
FileName(),
1358-
FileName(), backup_file );
1342+
std::fstream f2( bckup.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary );
1343+
std::ostream_iterator<char> begin2( f2 );
13591344

1360-
strcat( backup_call, "fi" );
1345+
copy( begin, end, begin2 );
13611346

13621347
_error.AppendToDetailMsg( "Making backup file: " );
1363-
_error.AppendToDetailMsg( backup_file );
1348+
_error.AppendToDetailMsg( bckup.c_str() );
13641349
_error.AppendToDetailMsg( "\n" );
1365-
system( backup_call );
13661350
}
13671351

13681352
Severity STEPfile::WriteExchangeFile( ostream & out, int validate, int clearError,
@@ -1390,7 +1374,7 @@ Severity STEPfile::WriteExchangeFile( ostream & out, int validate, int clearErro
13901374
return rval;
13911375
}
13921376

1393-
Severity STEPfile::WriteExchangeFile( const char * filename, int validate, int clearError,
1377+
Severity STEPfile::WriteExchangeFile( const std::string filename, int validate, int clearError,
13941378
int writeComments ) {
13951379
Severity rval = SEVERITY_NULL;
13961380

@@ -1614,13 +1598,13 @@ Severity STEPfile::AppendFile( istream * in, int useTechCor ) {
16141598
sprintf( errbuf,
16151599
"Faulty input at beginning of file. \"ISO-10303-21;\" or"
16161600
" \"STEP_WORKING_SESSION;\" expected. File not read: %s\n",
1617-
( ( strcmp( FileName(), "-" ) == 0 ) ? "standard input" : FileName() ) );
1601+
( ( FileName().compare( "-" ) == 0 ) ? "standard input" : FileName().c_str() ) );
16181602
_error.AppendToUserMsg( errbuf );
16191603
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
16201604
return SEVERITY_INPUT_ERROR;
16211605
}
16221606

1623-
cout << "Reading Data from " << ( ( strcmp( FileName(), "-" ) == 0 ) ? "standard input" : FileName() ) << "...\n";
1607+
cout << "Reading Data from " << ( ( FileName().compare( "-" ) == 0 ) ? "standard input" : FileName().c_str() ) << "...\n";
16241608

16251609
// Read header
16261610
rval = ReadHeader( *in );
@@ -1696,7 +1680,7 @@ Severity STEPfile::AppendFile( istream * in, int useTechCor ) {
16961680
ReadTokenSeparator( *in2 );
16971681
if( total_insts != valid_insts ) {
16981682
sprintf( errbuf, "%d invalid instances in file: %s\n",
1699-
total_insts - valid_insts, ( ( strcmp( FileName(), "-" ) == 0 ) ? "standard input" : FileName() ) );
1683+
total_insts - valid_insts, ( ( FileName().compare( "-" ) == 0 ) ? "standard input" : FileName().c_str() ) );
17001684
_error.AppendToUserMsg( errbuf );
17011685
CloseInputFile( in2 );
17021686
return _error.GreaterSeverity( SEVERITY_WARNING );
@@ -1761,7 +1745,7 @@ Severity STEPfile::WriteWorkingFile( ostream & out, int clearError, int writeCom
17611745
return _error.severity();
17621746
}
17631747

1764-
Severity STEPfile::WriteWorkingFile( const char * filename, int clearError,
1748+
Severity STEPfile::WriteWorkingFile( const std::string filename, int clearError,
17651749
int writeComments ) {
17661750
if( clearError ) {
17671751
_error.ClearErrorMsg();

src/cleditor/STEPfile.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class STEPfile {
6060

6161
//file information
6262
DirObj * _currentDir;
63-
char * _fileName;
63+
std::string _fileName;
6464

6565
//error information
6666
ErrorDescriptor _error;
@@ -105,11 +105,11 @@ class STEPfile {
105105
SDAI_Application_instance * HeaderDefaultFileSchema();
106106

107107
//file information
108-
const char * FileName() const {
108+
std::string FileName() const {
109109
return _fileName;
110110
}
111-
const char * SetFileName( const char * name = "" );
112-
const char * TruncFileName( const char * name ) const;
111+
std::string SetFileName( const std::string name = "" );
112+
std::string TruncFileName( const std::string name ) const;
113113

114114
//error information
115115
ErrorDescriptor & Error() { /* const */
@@ -133,40 +133,40 @@ class STEPfile {
133133
int SetFileType( FileTypeCode ft = VERSION_CURRENT );
134134

135135
//Reading and Writing
136-
Severity ReadExchangeFile( const char * filename = 0, int useTechCor = 1 );
137-
Severity AppendExchangeFile( const char * filename = 0, int useTechCor = 1 );
136+
Severity ReadExchangeFile( const std::string filename = "", int useTechCor = 1 );
137+
Severity AppendExchangeFile( const std::string filename = "", int useTechCor = 1 );
138138

139-
Severity ReadWorkingFile( const char * filename = 0, int useTechCor = 1 );
140-
Severity AppendWorkingFile( const char * filename = 0, int useTechCor = 1 );
139+
Severity ReadWorkingFile( const std::string filename = "", int useTechCor = 1 );
140+
Severity AppendWorkingFile( const std::string filename = "", int useTechCor = 1 );
141141

142142
Severity AppendFile( istream * in, int useTechCor = 1 ) ;
143143

144144
Severity WriteExchangeFile( ostream & out, int validate = 1,
145145
int clearError = 1, int writeComments = 1 );
146-
Severity WriteExchangeFile( const char * filename = 0, int validate = 1,
146+
Severity WriteExchangeFile( const std::string filename = "", int validate = 1,
147147
int clearError = 1, int writeComments = 1 );
148148
Severity WriteValuePairsFile( ostream & out, int validate = 1,
149149
int clearError = 1,
150150
int writeComments = 1, int mixedCase = 1 );
151151

152152
Severity WriteWorkingFile( ostream & out, int clearError = 1,
153153
int writeComments = 1 );
154-
Severity WriteWorkingFile( const char * filename = 0, int clearError = 1,
154+
Severity WriteWorkingFile( const std::string filename = "", int clearError = 1,
155155
int writeComments = 1 );
156156

157157
stateEnum EntityWfState( char c );
158158

159159
void Renumber();
160160

161161
//constructors
162-
STEPfile( Registry & r, InstMgr & i, const char * filename = ( const char * )0 );
162+
STEPfile( Registry & r, InstMgr & i, const std::string filename = "" );
163163
virtual ~STEPfile();
164164

165165
protected:
166166
//member functions
167167
std::string schemaName(); /**< Returns and copies out schema name from header instances.
168168
Called by ReadExchangeFile */
169-
istream * OpenInputFile( const char * filename = "" );
169+
istream * OpenInputFile( const std::string filename = "" );
170170
void CloseInputFile( istream * in );
171171

172172
Severity ReadHeader( istream & in );
@@ -210,7 +210,7 @@ class STEPfile {
210210
void WriteWorkingData( ostream & out, int writeComments = 1 );
211211

212212
//called by WriteExchangeFile
213-
ofstream * OpenOutputFile( const char * filename = 0 );
213+
ofstream * OpenOutputFile( const std::string filename = "" );
214214
void CloseOutputFile( ostream * out );
215215

216216
void WriteHeader( ostream & out );

src/cleditor/STEPfile.inline.cc

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ extern void HeaderSchemaInit( Registry & reg );
2323

2424
//constructor & destructor
2525

26-
STEPfile::STEPfile( Registry & r, InstMgr & i, const char * filename )
27-
:
26+
STEPfile::STEPfile( Registry & r, InstMgr & i, const std::string filename ) :
2827
_reg( r ), _instances( i ),
29-
_headerId( 0 ), _maxErrorCount( 5000 ),
30-
_fileName( 0 ), _entsNotCreated( 0 ), _entsInvalid( 0 ),
31-
_entsIncomplete( 0 ), _entsWarning( 0 ),
32-
_errorCount( 0 ), _warningCount( 0 ),
33-
_fileIdIncr( 0 )
34-
28+
_headerId( 0 ), _maxErrorCount( 5000 ),
29+
_fileName( "" ), _entsNotCreated( 0 ), _entsInvalid( 0 ),
30+
_entsIncomplete( 0 ), _entsWarning( 0 ),
31+
_errorCount( 0 ), _warningCount( 0 ),
32+
_fileIdIncr( 0 )
3533
{
3634
SetFileType( VERSION_CURRENT );
3735
SetFileIdIncrement();
3836
_currentDir = new DirObj( "" );
3937
_headerRegistry = new Registry( HeaderSchemaInit );
4038
_headerInstances = new InstMgr;
41-
if( filename ) {
39+
if( !filename.empty() ) {
4240
ReadExchangeFile( filename );
4341
}
4442
}
@@ -86,19 +84,23 @@ int STEPfile::SetFileType( FileTypeCode ft ) {
8684

8785

8886
/******************************************************/
89-
const char * STEPfile::TruncFileName( const char * filename ) const {
90-
const char * tmp = strrchr( filename, '/' );
91-
if( tmp ) {
92-
return tmp++;
93-
} else {
87+
std::string STEPfile::TruncFileName( const std::string filename ) const {
88+
#if defined(__WIN32__) && !defined(__mingw32__)
89+
char slash = '\\';
90+
#else
91+
char slash = '/';
92+
#endif
93+
size_t l = filename.find_last_of( slash );
94+
if( l == std::string::npos ) {
9495
return filename;
96+
} else {
97+
return filename.substr( l );
9598
}
96-
9799
}
98100

99101

100102
/******************************************************/
101-
Severity STEPfile::ReadExchangeFile( const char * filename, int useTechCor ) {
103+
Severity STEPfile::ReadExchangeFile( const std::string filename, int useTechCor ) {
102104
_error.ClearErrorMsg();
103105
_errorCount = 0;
104106
istream * in = OpenInputFile( filename );
@@ -117,7 +119,7 @@ Severity STEPfile::ReadExchangeFile( const char * filename, int useTechCor ) {
117119
return rval;
118120
}
119121

120-
Severity STEPfile::AppendExchangeFile( const char * filename, int useTechCor ) {
122+
Severity STEPfile::AppendExchangeFile( const std::string filename, int useTechCor ) {
121123
_error.ClearErrorMsg();
122124
_errorCount = 0;
123125
istream * in = OpenInputFile( filename );
@@ -130,7 +132,7 @@ Severity STEPfile::AppendExchangeFile( const char * filename, int useTechCor ) {
130132
return rval;
131133
}
132134

133-
Severity STEPfile::ReadWorkingFile( const char * filename, int useTechCor ) {
135+
Severity STEPfile::ReadWorkingFile( const std::string filename, int useTechCor ) {
134136
_error.ClearErrorMsg();
135137
_errorCount = 0;
136138
istream * in = OpenInputFile( filename );
@@ -150,7 +152,7 @@ Severity STEPfile::ReadWorkingFile( const char * filename, int useTechCor ) {
150152
}
151153

152154

153-
Severity STEPfile::AppendWorkingFile( const char * filename, int useTechCor ) {
155+
Severity STEPfile::AppendWorkingFile( const std::string filename, int useTechCor ) {
154156
_error.ClearErrorMsg();
155157
_errorCount = 0;
156158
istream * in = OpenInputFile( filename );
@@ -165,33 +167,33 @@ Severity STEPfile::AppendWorkingFile( const char * filename, int useTechCor ) {
165167
return rval;
166168
}
167169

168-
istream * STEPfile::OpenInputFile( const char * filename ) {
170+
istream * STEPfile::OpenInputFile( const std::string filename ) {
169171
// if there's no filename to use, fail
170-
if( !( strcmp( filename, "" ) || strcmp( FileName(), "" ) ) ) {
172+
if( filename.empty() && FileName().empty() ) {
171173
_error.AppendToUserMsg( "Unable to open file for input. No current file name.\n" );
172174
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
173-
return ( 0 );
175+
return( 0 );
174176
} else {
175-
if( !SetFileName( filename ) && ( strcmp( filename, "-" ) != 0 ) ) {
177+
if( SetFileName( filename ).empty() && ( filename.compare( "-" ) != 0 ) ) {
176178
char msg[BUFSIZ];
177-
sprintf( msg, "Unable to find file for input: \'%s\'. File not read.\n", filename );
179+
sprintf( msg, "Unable to find file for input: \'%s\'. File not read.\n", filename.c_str() );
178180
_error.AppendToUserMsg( msg );
179181
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
180-
return ( 0 );
182+
return( 0 );
181183
}
182184
}
183185

184186
std::istream * in;
185187

186-
if( strcmp( filename, "-" ) == 0 ) {
188+
if( filename.compare( "-" ) == 0 ) {
187189
in = &std::cin;
188190
} else {
189-
in = new ifstream( FileName() );
191+
in = new ifstream( FileName().c_str() );
190192
}
191193

192194
if( !in || !( in -> good() ) ) {
193195
char msg[BUFSIZ];
194-
sprintf( msg, "Unable to open file for input: \'%s\'. File not read.\n", filename );
196+
sprintf( msg, "Unable to open file for input: \'%s\'. File not read.\n", filename.c_str() );
195197
_error.AppendToUserMsg( msg );
196198
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
197199
return ( 0 );
@@ -206,27 +208,25 @@ void STEPfile::CloseInputFile( istream * in ) {
206208
}
207209
}
208210

209-
ofstream * STEPfile::OpenOutputFile( const char * filename ) {
210-
if( !filename ) {
211-
if( !FileName() ) {
211+
ofstream * STEPfile::OpenOutputFile( std::string filename ) {
212+
if( filename.empty() ) {
213+
if( FileName().empty() ) {
212214
_error.AppendToUserMsg( "No current file name.\n" );
213215
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
214216
}
215217
} else {
216-
if( !SetFileName( filename ) ) {
218+
if( SetFileName( filename ).empty() ) {
217219
char msg[BUFSIZ];
218-
sprintf( msg, "can't find file: %s\nFile not written.\n", filename );
220+
sprintf( msg, "can't find file: %s\nFile not written.\n", filename.c_str() );
219221
_error.AppendToUserMsg( msg );
220222
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
221223
}
222224
}
223225

224-
if( _currentDir->FileExists( TruncFileName( FileName() ) ) ) {
226+
if( _currentDir->FileExists( TruncFileName( filename ) ) ) {
225227
MakeBackupFile();
226228
}
227-
228-
ofstream * out = new ofstream( FileName() );
229-
// default for ostream is writeonly and protections are set to 644
229+
ofstream * out = new ofstream( filename.c_str() );
230230
if( !out ) {
231231
_error.AppendToUserMsg( "unable to open file for output\n" );
232232
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );

0 commit comments

Comments
 (0)