-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathlazyFileReader.cc
More file actions
118 lines (106 loc) · 3.55 KB
/
Copy pathlazyFileReader.cc
File metadata and controls
118 lines (106 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <assert.h>
#include "cllazyfile/lazyFileReader.h"
#include "cllazyfile/lazyDataSectionReader.h"
#include "cllazyfile/headerSectionReader.h"
#include "cllazyfile/lazyInstMgr.h"
void lazyFileReader::initP21() {
_header = new p21HeaderSectionReader( this, _file, 0, -1 );
for( ;; ) {
lazyDataSectionReader * r;
r = new lazyP21DataSectionReader( this, _file, _file.tellg(), _parent->countDataSections() );
if( !r->success() ) {
delete r; //last read attempt failed
std::cerr << "Corrupted data section" << std::endl;
break;
}
_parent->registerDataSection( r );
if( _parent->cancelled() ) break;
//check for new data section (DATA) or end of file (END-ISO-10303-21;)
for( ;; ) {
while( isspace( _file.peek() ) && _file.good() ) _file.ignore( 1 );
if( _file.peek() != '/' ) break;
std::streampos comment = _file.tellg();
_file.get();
if( _file.peek() != '*' ) {
_file.seekg( comment );
break;
}
_file.get();
int previous = 0;
int current = 0;
while( _file.good() ) {
current = _file.get();
if( previous == '*' && current == '/' ) break;
previous = current;
}
}
std::streampos nextSection = _file.tellg();
if( needKW( "END-ISO-10303-21;" ) ) {
break;
}
_file.clear();
_file.seekg( nextSection );
if( !needKW( "DATA" ) ) {
std::cerr << "Corrupted file - did not find new data section (\"DATA\") or end of file (\"END-ISO-10303-21;\") at offset " << _file.tellg() << std::endl;
break;
}
_file.clear();
_file.seekg( nextSection );
}
}
bool lazyFileReader::needKW( const char * kw ) {
std::streampos start = _file.tellg();
const char * c = kw;
bool found = true;
while( *c ) {
if( *c != _file.get() ) {
found = false;
break;
}
c++;
}
if( !found ) {
_file.clear();
_file.seekg( start );
}
return found;
}
instancesLoaded_t * lazyFileReader::getHeaderInstances() {
return _header->getInstances();
}
lazyFileReader::lazyFileReader( std::string fname, lazyInstMgr * i, fileID fid ): _fileName( fname ), _parent( i ),
_header( 0 ), _schemaWarningClaimed( false ), _fileID( fid ),
_fileSize( 0 ), _valid( false ) {
_file.open( _fileName.c_str(), std::ios::binary );
_file.imbue( std::locale::classic() );
_file.unsetf( std::ios_base::skipws );
if( !_file.is_open() || !_file.good() ) {
LazyDiagnostic diagnostic;
diagnostic.severity = LAZY_DIAGNOSTIC_FATAL;
diagnostic.message = "unable to open exchange file: " + _fileName;
_parent->emitDiagnostic( diagnostic );
return;
}
_file.seekg( 0, std::ios::end );
std::streampos end = _file.tellg();
if( end != std::streampos( -1 ) ) {
_fileSize = static_cast<lazyFileOffset>( static_cast<std::streamoff>( end ) );
}
_file.seekg( 0, std::ios::beg );
detectType();
switch( _fileType ) {
case Part21:
initP21();
break;
case Part28:
//initP28();
//break;
default:
std::cerr << "Reached default case, " << __FILE__ << ":" << __LINE__ << std::endl;
abort();
}
_valid = true;
}
lazyFileReader::~lazyFileReader() {
delete _header;
}