-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathSTEPfile.cc
More file actions
1883 lines (1618 loc) · 60.5 KB
/
STEPfile.cc
File metadata and controls
1883 lines (1618 loc) · 60.5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file STEPfile.cc
* NIST STEP Core Class Library
* cleditor/STEPfile.cc
* February, 1994
* Peter Carr
* K. C. Morris
* David Sauder
* Development of this software was funded by the United States Government,
* and is not subject to copyright.
TODO LIST:
- ReadHeader doesn't merge the information from more than one instance
of the same entity type name. i.e. it doesn't merge info from multiple files
- ReadWorkingFile does not handle references to entities which
aren't in the working session file. for other *incomplete* instances,
it prints error messages to cerr which may not need to be printed. It
doesn't keep track of the incomplete instances, it just puts them on the
list according to the symbol which precedes them in the working session
file
*/
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include "cleditor/STEPfile.h"
#include "clstepcore/sdai.h"
#include "clstepcore/STEPcomplex.h"
#include "clstepcore/STEPattribute.h"
#include "cleditor/SdaiHeaderSchema.h"
// STEPundefined contains
// void PushPastString (istream& in, std::string &s, ErrorDescriptor *err)
#include "clstepcore/STEPundefined.h"
/**
* \returns The new file name for the class.
* \param newName The file name to be set.
*
* This function sets the _fileName member variable to an
* absolute path name to a directory on the file system. If newName
* cannot be resolved to a file, then an empty string is returned.
*
* side effects: STEPfile::_fileName value may change.
*/
std::string STEPfile::SetFileName( const std::string newName ) {
// if a newName is not given or is the same as the old, use the old name
if( ( newName.empty() ) || ( newName == _fileName ) ) {
return FileName();
}
_fileName = DirObj::Normalize( 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.
*
* This function reads in the header section of an exchange file. It
* parses the header section, popluates the _headerInstances, and
* returns an error descriptor.
* It expects to find the "HEADER;" symbol at the beginning of the istream.
*
* side effects: The function gobbles all characters up to and including the
* next "ENDSEC;" from in.
* The STEPfile::_headerInstances may change.
*/
Severity STEPfile::ReadHeader( istream & in ) {
std::string cmtStr;
InstMgr * im = new InstMgr;
SDAI_Application_instance * obj;
Severity objsev = SEVERITY_NULL;
int endsec = 0;
int userDefined = 0;
int fileid;
std::string keywd;
char c = '\0';
char buf [BUFSIZ+1];
std::string strbuf;
ReadTokenSeparator( in );
// Read and gobble all 'junk' up to "HEADER;"
if( !FindHeaderSection( in ) ) {
delete im;
return SEVERITY_INPUT_ERROR;
}
//read the header instances
while( !endsec ) {
ReadTokenSeparator( in, &cmtStr );
if( in.eof() ) {
_error.AppendToDetailMsg( "End of file reached in reading header section.\n" );
_error.GreaterSeverity( SEVERITY_EXIT );
delete im;
return SEVERITY_EXIT;
}
//check for user defined instances
//if it is userDefined, the '!' does not get put back on the istream
in.get( c );
if( c == '!' ) {
userDefined = 1;
} else {
in.putback( c );
}
//get the entity keyword
keywd = GetKeyword( in, ";( /\\", _error );
ReadTokenSeparator( in, &cmtStr );
//check for "ENDSEC"
if( !strncmp( const_cast<char *>( keywd.c_str() ), "ENDSEC", 7 ) ) {
//get the token delimiter
in.get( c ); //should be ';'
endsec = 1;
break; //from while-loop
} else {
//create and read the header instance
// SDAI_Application_instance::STEPread now reads the opening parenthesis
//create header instance
buf[0] = '\0';
if( _fileType == VERSION_OLD ) {
_error.AppendToDetailMsg( "N279 header detected. Files this old are no longer supported.\n" );
_error.GreaterSeverity( SEVERITY_EXIT );
delete im;
return SEVERITY_EXIT;
} else {
strncpy( buf, const_cast<char *>( keywd.c_str() ), BUFSIZ );
}
if( userDefined ) {
//create user defined header instance
// BUG: user defined entities are ignored
//obj = _headerUserDefined->ObjCreate (buf);
//objsev = AppendEntityErrorMsg( &(obj->Error()) );
SkipInstance( in, strbuf );
cerr << "User defined entity in header section " <<
"is ignored.\n\tdata lost: !" << buf << strbuf << "\n";
_error.GreaterSeverity( SEVERITY_WARNING );
break; //from while loop
} else { //not userDefined
obj = _headerRegistry->ObjCreate( buf );
}
//read header instance
if( !obj || ( obj == ENTITY_NULL ) ) {
++_errorCount;
SkipInstance( in, strbuf );
cerr << "Unable to create header section entity: \'" <<
keywd << "\'.\n\tdata lost: " << strbuf << "\n";
_error.GreaterSeverity( SEVERITY_WARNING );
} else { //not ENTITY_NULL
//read the header instance
AppendEntityErrorMsg( &( obj->Error() ) );
//set file_id to reflect the appropriate Header Section Entity
fileid = HeaderId( const_cast<char *>( keywd.c_str() ) );
//read the values from the istream
objsev = obj->STEPread( fileid, 0, ( InstMgr * )0, in, NULL, true, _strict );
_error.GreaterSeverity( objsev );
if( !cmtStr.empty() ) {
obj->PrependP21Comment( cmtStr );
}
in >> ws;
c = in.peek(); // check for semicolon or keyword 'ENDSEC'
if( c != 'E' ) {
in >> c; // read the semicolon
}
//check to see if object was successfully read
AppendEntityErrorMsg( &( obj->Error() ) );
//append to header instance manager
im->Append( obj, completeSE );
}
}
cmtStr.clear();
}
HeaderVerifyInstances( im );
HeaderMergeInstances( im ); // handles delete for im
return _error.severity();
}
/**
* Verify the instances read from the header section of an exchange file.
* If some required instances aren't present, then create them,
* and populate them with default values.
* The required instances are:
* #1 = FILE_DESCRIPTION
* #2 = FILE_NAME
* #3 = FILE_SCHEMA
*/
Severity STEPfile::HeaderVerifyInstances( InstMgr * im ) {
int err = 0;
int fileid;
SDAI_Application_instance * obj;
//check File_Name
fileid = HeaderId( "File_Name" );
if( !( im->FindFileId( fileid ) ) ) {
++err;
cerr << "FILE_NAME instance not found in header section\n";
// create a File_Name entity and assign default values
obj = HeaderDefaultFileName();
im->Append( obj, completeSE );
}
//check File_Description
fileid = HeaderId( "File_Description" );
if( !( im->FindFileId( fileid ) ) ) {
++err;
cerr << "FILE_DESCRIPTION instance not found in header section\n";
// create a File_Description entity and assign default values
obj = HeaderDefaultFileDescription();
im->Append( obj, completeSE );
}
//check File_Schema
fileid = HeaderId( "File_Schema" );
if( !( im->FindFileId( fileid ) ) ) {
++err;
cerr << "FILE_SCHEMA instance not found in header section\n";
// create a File_Schema entity and read in default values
obj = HeaderDefaultFileSchema();
im->Append( obj, completeSE );
}
if( !err ) {
return SEVERITY_NULL;
}
_error.AppendToUserMsg( "Missing required entity in header section.\n" );
_error.GreaterSeverity( SEVERITY_WARNING );
return SEVERITY_WARNING;
}
SDAI_Application_instance * STEPfile::HeaderDefaultFileName() {
SdaiFile_name * fn = new SdaiFile_name;
StringAggregate_ptr tmp = new StringAggregate;
fn->name_( "" );
fn->time_stamp_( "" );
tmp->StrToVal( "", &_error, fn->attributes[2].getADesc()->DomainType(), _headerInstances );
fn->author_( tmp );
tmp->StrToVal( "", &_error, fn->attributes[3].getADesc()->DomainType(), _headerInstances );
fn->organization_( tmp );
fn->preprocessor_version_( "" );
fn->originating_system_( "" );
fn->authorization_( "" );
fn->STEPfile_id = HeaderId( "File_Name" );
return fn;
}
SDAI_Application_instance * STEPfile::HeaderDefaultFileDescription() {
SdaiFile_description * fd = new SdaiFile_description;
fd->implementation_level_( "" );
fd->STEPfile_id = HeaderId( "File_Description" );
return fd;
}
SDAI_Application_instance * STEPfile::HeaderDefaultFileSchema() {
SdaiFile_schema * fs = new SdaiFile_schema;
StringAggregate_ptr tmp = new StringAggregate;
tmp->StrToVal( "", &_error, fs->attributes[0].getADesc()->DomainType(), _headerInstances );
fs->schema_identifiers_( tmp );
fs->STEPfile_id = HeaderId( "File_Schema" );
return fs;
}
/**
* This function has an effect when more than one file
* is being read into a working session.
*
* This function manages space allocation for the Instance
* Manager for the header instances. If the instances in im are
* copied onto the instances of _headerInstances, then im is
* deleted. Otherwise no space is deleted.
*
* Append the values of the given instance manager onto the _headerInstances.
* If the _headerInstances contain no instances, then copy the instances
* from im onto the _headerInstances.
* This only works for an instance manager which contains the following
* header section entities. The file id numbers are important.
*
* #1 = FILE_DESCRIPTION
* #2 = FILE_NAME
* #3 = FILE_SCHEMA
*/
void STEPfile::HeaderMergeInstances( InstMgr * im ) {
SDAI_Application_instance * se = 0;
SDAI_Application_instance * from = 0;
int idnum;
//check for _headerInstances
if( !_headerInstances ) {
_headerInstances = im;
return;
}
if( _headerInstances->InstanceCount() < 4 ) {
delete _headerInstances;
_headerInstances = im;
return;
}
//checking for _headerInstances::FILE_NAME
idnum = HeaderId( "File_Name" );
se = _headerInstances->GetApplication_instance( _headerInstances->FindFileId( idnum ) );
if( se ) {
from = im->GetApplication_instance( im->FindFileId( idnum ) );
// name:
// time_stamp: keep the newer time_stamp
// author: append the list of authors
// organization: append the organization list
// preprocessor_version:
// originating_system:
// authorization:
} else { // No current File_Name instance
from = im->GetApplication_instance( im->FindFileId( idnum ) );
_headerInstances->Append( from, completeSE );
}
//checking for _headerInstances::FILE_DESCRIPTION
idnum = HeaderId( "File_Description" );
se = _headerInstances->GetApplication_instance( _headerInstances->FindFileId( idnum ) );
if( se ) {
from = im->GetApplication_instance( im->FindFileId( idnum ) );
//description
//implementation_level
} else {
from = im->GetApplication_instance( im->FindFileId( idnum ) );
_headerInstances->Append( from, completeSE );
}
//checking for _headerInstances::FILE_SCHEMA
idnum = HeaderId( "File_Schema" );
se = _headerInstances->GetApplication_instance( _headerInstances->FindFileId( idnum ) );
if( se ) {
from = im->GetApplication_instance( im->FindFileId( idnum ) );
//description
//implementation_level
} else {
from = im->GetApplication_instance( im->FindFileId( idnum ) );
_headerInstances->Append( from, completeSE );
}
delete im;
return;
}
stateEnum STEPfile::EntityWfState( char c ) {
switch( c ) {
case wsSaveComplete:
return completeSE;
case wsSaveIncomplete:
return incompleteSE;
case wsDelete:
return deleteSE;
case wsNew:
return newSE;
default:
return noStateSE;
}
}
/**
* PASS 1: create instances
* starts at the data section
*/
int STEPfile::ReadData1( istream & in ) {
int endsec = 0;
_entsNotCreated = 0;
_errorCount = 0; // reset error count
_warningCount = 0; // reset error count
char c;
int instance_count = 0;
char buf[BUFSIZ+1];
buf[0] = '\0';
std::string tmpbuf;
SDAI_Application_instance * obj = ENTITY_NULL;
stateEnum inst_state = noStateSE; // used if reading working file
ErrorDescriptor e;
// PASS 1: create instances
endsec = FoundEndSecKywd( in );
while( in.good() && !endsec ) {
e.ClearErrorMsg();
ReadTokenSeparator( in ); // also skips white space
in >> c;
if( _fileType == WORKING_SESSION ) {
if( strchr( "CIND", c ) ) { // if there is a valid char
inst_state = EntityWfState( c );
ReadTokenSeparator( in );
in >> c; // read the ENTITY_NAME_DELIM
} else {
e.AppendToDetailMsg( "Invalid editing state character: " );
e.AppendToDetailMsg( c );
e.AppendToDetailMsg( "\nAssigning editing state to be INCOMPLETE\n" );
e.GreaterSeverity( SEVERITY_WARNING );
inst_state = incompleteSE;
}
}
if( c != ENTITY_NAME_DELIM ) {
in.putback( c );
while( c != ENTITY_NAME_DELIM && in.good() &&
!( endsec = FoundEndSecKywd( in ) ) ) {
tmpbuf.clear();
FindStartOfInstance( in, tmpbuf );
cout << "ERROR: trying to recover from invalid data. skipping: "
<< tmpbuf << endl;
in >> c;
ReadTokenSeparator( in );
}
}
if( !endsec ) {
obj = ENTITY_NULL;
if( ( _fileType == WORKING_SESSION ) && ( inst_state == deleteSE ) ) {
SkipInstance( in, tmpbuf );
} else {
obj = CreateInstance( in, cout );
_iFileCurrentPosition = in.tellg();
}
if( obj != ENTITY_NULL ) {
if( obj->Error().severity() < SEVERITY_WARNING ) {
++_errorCount;
} else if( obj->Error().severity() < SEVERITY_NULL ) {
++_warningCount;
}
obj->Error().ClearErrorMsg();
if( _fileType == WORKING_SESSION ) {
instances().Append( obj, inst_state );
} else {
instances().Append( obj, newSE );
}
++instance_count;
} else {
++_entsNotCreated;
//old
++_errorCount;
}
if( _entsNotCreated > _maxErrorCount ) {
_error.AppendToUserMsg( "Warning: Too Many Errors in File. Read function aborted.\n" );
cerr << Error().UserMsg();
cerr << Error().DetailMsg();
Error().ClearErrorMsg();
Error().severity( SEVERITY_EXIT );
return instance_count;
}
endsec = FoundEndSecKywd( in );
}
} // end while loop
if( _entsNotCreated ) {
snprintf( buf, sizeof(buf),
"STEPfile Reading File: Unable to create %d instances.\n\tIn first pass through DATA section. Check for invalid entity types.\n",
_entsNotCreated );
_error.AppendToUserMsg( buf );
_error.GreaterSeverity( SEVERITY_WARNING );
}
if( !in.good() ) {
_error.AppendToUserMsg( "Error in input file.\n" );
}
_iFileStage1Done = true;
return instance_count;
}
int STEPfile::ReadWorkingData1( istream & in ) {
return ReadData1( in );
}
/**
* \returns number of valid instances read
* reads in the data portion of the instances in an exchange file
*/
int STEPfile::ReadData2( istream & in, bool useTechCor ) {
_entsInvalid = 0;
_entsIncomplete = 0;
_entsWarning = 0;
int total_instances = 0;
int valid_insts = 0; // used for exchange file only
stateEnum inst_state = noStateSE; // used if reading working file
_errorCount = 0; // reset error count
_warningCount = 0; // reset error count
char c;
char buf[BUFSIZ+1];
buf[0] = '\0';
std::string tmpbuf;
SDAI_Application_instance * obj = ENTITY_NULL;
std::string cmtStr;
int endsec = FoundEndSecKywd( in );
// PASS 2: read instances
while( in.good() && !endsec ) {
ReadTokenSeparator( in, &cmtStr );
in >> c;
if( _fileType == WORKING_SESSION ) {
if( strchr( "CIND", c ) ) { // if there is a valid char
inst_state = EntityWfState( c );
ReadTokenSeparator( in, &cmtStr );
in >> c; // read the ENTITY_NAME_DELIM
}
}
if( c != ENTITY_NAME_DELIM ) {
in.putback( c );
while( c != ENTITY_NAME_DELIM && in.good() &&
!( endsec = FoundEndSecKywd( in ) ) ) {
tmpbuf.clear();
FindStartOfInstance( in, tmpbuf );
cout << "ERROR: trying to recover from invalid data. skipping: "
<< tmpbuf << endl;
in >> c;
ReadTokenSeparator( in, &cmtStr );
}
}
if( !endsec ) {
obj = ENTITY_NULL;
if( ( _fileType == WORKING_SESSION ) && ( inst_state == deleteSE ) ) {
SkipInstance( in, tmpbuf );
} else {
obj = ReadInstance( in, cout, cmtStr, useTechCor );
_iFileCurrentPosition = in.tellg();
}
cmtStr.clear();
if( obj != ENTITY_NULL ) {
if( obj->Error().severity() < SEVERITY_INCOMPLETE ) {
++_entsInvalid;
// old
++_errorCount;
} else if( obj->Error().severity() == SEVERITY_INCOMPLETE ) {
++_entsIncomplete;
++_entsInvalid;
} else if( obj->Error().severity() == SEVERITY_USERMSG ) {
++_entsWarning;
} else { // i.e. if severity == SEVERITY_NULL
++valid_insts;
}
obj->Error().ClearErrorMsg();
++total_instances;
} else {
++_entsInvalid;
// old
++_errorCount;
}
if( _entsInvalid > _maxErrorCount ) {
_error.AppendToUserMsg( "Warning: Too Many Errors in File. Read function aborted.\n" );
cerr << Error().UserMsg();
cerr << Error().DetailMsg();
Error().ClearErrorMsg();
Error().severity( SEVERITY_EXIT );
return valid_insts;
}
endsec = FoundEndSecKywd( in );
}
} // end while loop
if( _entsInvalid ) {
snprintf( buf, sizeof(buf),
"%s \n\tTotal instances: %d \n\tInvalid instances: %d \n\tIncomplete instances (includes invalid instances): %d \n\t%s: %d.\n",
"Second pass complete - instance summary:", total_instances,
_entsInvalid, _entsIncomplete, "Warnings",
_entsWarning );
cout << buf << endl;
_error.AppendToUserMsg( buf );
_error.AppendToDetailMsg( buf );
_error.GreaterSeverity( SEVERITY_WARNING );
}
if( !in.good() ) {
_error.AppendToUserMsg( "Error in input file.\n" );
}
return valid_insts;
}
int STEPfile::ReadWorkingData2( istream & in, bool useTechCor ) {
return ReadData2( in, useTechCor );
}
/** Looks for the word DATA followed by optional whitespace
* followed by a semicolon. When it is looking for the word
* DATA it skips over strings and comments.
*/
int STEPfile::FindDataSection( istream & in ) {
ErrorDescriptor errs;
SDAI_String tmp;
std::string s; // used if need to read a comment
char c;
while( in.good() ) {
in >> c;
if( in.eof() ) {
_error.AppendToUserMsg( "Can't find \"DATA;\" section." );
return 0; //ERROR_WARNING
}
switch( c ) {
case 'D': // look for string "DATA;" with optional whitespace after "DATA"
c = in.peek(); // look at next char (for 'A')
// only peek since it may be 'D' again a we need to start over
if( c == 'A' ) {
in.get( c ); // read char 'A'
c = in.peek(); // look for 'T'
if( c == 'T' ) {
in.get( c ); // read 'T'
c = in.peek(); // look for 'A'
if( c == 'A' ) {
in.get( c ); // read 'A'
in >> ws; // may want to skip comments or print control directives?
c = in.peek(); // look for semicolon
if( c == ';' ) {
in.get( c ); // read the semicolon
return 1; // success
}
}
}
}
break;
case '\'': // get past the string
in.putback( c );
tmp.STEPread( in, &errs );
break;
case '/': // read p21 file comment
in.putback( c ); // looks like a comment
ReadComment( in, s );
break;
case '\0': // problem in input ?
return 0;
default:
break;
}
}
return 0;
}
int STEPfile::FindHeaderSection( istream & in ) {
char buf[BUFSIZ+1];
char * b = buf;
*b = '\0';
ReadTokenSeparator( in );
// find the header section
while( !( b = strstr( buf, "HEADER" ) ) ) {
if( in.eof() ) {
_error.AppendToUserMsg(
"Error: Unable to find HEADER section. File not read.\n" );
_error.GreaterSeverity( SEVERITY_INPUT_ERROR );
return 0;
}
in.getline( buf, BUFSIZ, ';' ); // reads but does not store the ;
}
return 1;
}
//dasdelete
//dasdelete
/**
description:
This function creates an instance based on the KEYWORD
read from the istream.
It expects an ENTITY_NAME (int) from the first set of
characters on the istream. If the (int) is not found,
ENTITY_NULL is returned.
side effects:
The function leaves the istream set to the end of the stream
of characters representing the ENTITY_INSTANCE. It attempts to
recover on errors by reading up to and including the next ';'.
an ENTITY_INSTANCE consists of:
'#'(int)'=' [SCOPE] SIMPLE_RECORD ';' ||
'#'(int)'=' [SCOPE] SUBSUPER_RECORD ';'
The '#' is read from the istream before CreateInstance is called.
*/
SDAI_Application_instance * STEPfile::CreateInstance( istream & in, ostream & out ) {
std::string tmpbuf;
std::string objnm;
char c;
std::string schnm;
int fileid = -1;
SDAI_Application_instance_ptr * scopelist = 0;
SDAI_Application_instance * obj;
ErrorDescriptor result;
// Sent down to CreateSubSuperInstance() to receive error info
ReadTokenSeparator( in );
in >> fileid; // read instance id
fileid = IncrementFileId( fileid );
if( instances().FindFileId( fileid ) ) {
SkipInstance( in, tmpbuf );
out << "ERROR: instance #" << fileid
<< " already exists.\n\tData lost: " << tmpbuf << endl;
return ENTITY_NULL;
}
ReadTokenSeparator( in );
in.get( c ); // read equal sign
if( c != '=' ) {
// ERROR: '=' expected
SkipInstance( in, tmpbuf );
out << "ERROR: instance #" << fileid
<< " \'=\' expected.\n\tData lost: " << tmpbuf << endl;
return ENTITY_NULL;
}
ReadTokenSeparator( in );
c = in.peek(); // peek at the next character on the istream
//check for optional "&SCOPE" construct
if( c == '&' ) { // TODO check this out
Severity s = CreateScopeInstances( in, &scopelist );
if( s < SEVERITY_WARNING ) {
return ENTITY_NULL;
}
ReadTokenSeparator( in );
c = in.peek(); // peek at next char on istream again
}
//check for subtype/supertype record
if( c == '(' ) {
// TODO: implement complex inheritance
obj = CreateSubSuperInstance( in, fileid, result );
if( obj == ENTITY_NULL ) {
SkipInstance( in, tmpbuf );
out << "ERROR: instance #" << fileid
<< " Illegal complex entity.\n"
<< result.UserMsg() << ".\n\n";
return ENTITY_NULL;
}
} else { // not a complex entity
// check for User Defined Entity
int userDefined = 0;
if( c == '!' ) {
userDefined = 1;
in.get( c );
}
ReadStdKeyword( in, objnm, 1 ); // read the type name
if( !in.good() ) {
out << "ERROR: instance #" << fileid
<< " Unexpected file problem in "
<< "STEPfile::CreateInstance.\n";
}
//create the instance using the Registry object
if( userDefined ) {
SkipInstance( in, tmpbuf );
out << "WARNING: instance #" << fileid
<< " User Defined Entity in DATA section ignored.\n"
<< "\tData lost: \'!" << objnm << "\': " << tmpbuf
<< endl;
if (scopelist)
delete[] scopelist;
return ENTITY_NULL;
} else {
schnm = schemaName();
obj = reg().ObjCreate( objnm.c_str(), schnm.c_str() );
if( obj == ENTITY_NULL ) {
// This will be the case if objnm does not exist in the reg.
result.UserMsg( "Unknown ENTITY type" );
} else if( obj->Error().severity() <= SEVERITY_WARNING ) {
// Common causes of error is that obj is an abstract supertype
// or that it can only be instantiated using external mapping.
// If neither are the case, create a generic message.
if( !obj->Error().UserMsg().empty() ) {
result.UserMsg( obj->Error().UserMsg() );
} else {
result.UserMsg( "Could not create ENTITY" );
}
// Delete obj so that below we'll know that an error occur:
delete obj;
obj = ENTITY_NULL;
}
}
}
if( obj == ENTITY_NULL ) {
SkipInstance( in, tmpbuf );
out << "ERROR: instance #" << fileid << " \'" << objnm
<< "\': " << result.UserMsg()
<< ".\n\tData lost: " << tmpbuf << "\n\n";
if (scopelist)
delete[] scopelist;
return ENTITY_NULL;
}
obj -> STEPfile_id = fileid;
// scan values
SkipInstance( in, tmpbuf );
ReadTokenSeparator( in );
if (scopelist)
delete[] scopelist;
return obj;
}
/**
description:
This function reads the SCOPE list for an entity instance,
creates each instance on the scope list, and appending each
instance to the instance manager.
side-effects:
It first searches for "&SCOPE" and reads all characters
from the istream up to and including "ENDSCOPE"
returns: ErrorDescriptor
>= SEVERITY_WARNING: the istream was read up to and including the
"ENDSCOPE" and the export list /#1, ... #N/
< SEVERITY_WARNING: the istream was read up to and including the next ";"
< SEVERITY_BUG: fatal
*/
Severity STEPfile::CreateScopeInstances( istream & in, SDAI_Application_instance_ptr ** scopelist ) {
Severity rval = SEVERITY_NULL;
SDAI_Application_instance * se;
std::string tmpbuf;
char c;
std::vector< SDAI_Application_instance_ptr > inscope;
std::string keywd;
keywd = GetKeyword( in, " \n\t/\\#;", _error );
if( strncmp( const_cast<char *>( keywd.c_str() ), "&SCOPE", 6 ) ) {
//ERROR: "&SCOPE" expected
//TODO: should attempt to recover by reading through ENDSCOPE
//currently recovery is attempted by reading through next ";"
SkipInstance( in, tmpbuf );
cerr << "ERROR: " << "\'&SCOPE\' expected." <<
"\n\tdata lost: " << tmpbuf << "\n";
return SEVERITY_INPUT_ERROR;
}
ReadTokenSeparator( in );
in.get( c );
while( c == '#' ) {
se = CreateInstance( in, cout );
if( se != ENTITY_NULL ) {
//TODO: apply scope information to se
// Add se to scopelist
inscope.push_back( se );
//append the se to the instance manager
instances().Append( se, newSE );
} else {
//ERROR: instance in SCOPE not created
rval = SEVERITY_WARNING;
SkipInstance( in, tmpbuf );
cerr << "instance in SCOPE not created.\n\tdata lost: "
<< tmpbuf << "\n";
++_errorCount;
}
ReadTokenSeparator( in );
in.get( c );
}
in.putback( c );
*scopelist = new SDAI_Application_instance_ptr [inscope.size()];
for( size_t i = 0; i < inscope.size(); ++i ) {
*scopelist[i] = inscope[i];
}
//check for "ENDSCOPE"
keywd = GetKeyword( in, " \t\n/\\#;", _error );
if( strncmp( const_cast<char *>( keywd.c_str() ), "ENDSCOPE", 8 ) ) {
//ERROR: "ENDSCOPE" expected
SkipInstance( in, tmpbuf );
cerr << "ERROR: " << "\'ENDSCOPE\' expected."
<< "\n\tdata lost: " << tmpbuf << "\n";
++_errorCount;
return SEVERITY_INPUT_ERROR;
}
//check for export list
ReadTokenSeparator( in );
in.get( c );
in.putback( c );
if( c == '/' ) {
//read export list
in.get( c );
c = ',';
while( c == ',' ) {
int exportid;
ReadTokenSeparator( in );
in.get( c );
if( c != '#' ) { } //ERROR
in >> exportid;
//TODO: nothing is done with the idnums on the export list
ReadTokenSeparator( in );
in.get( c );
}
if( c != '/' ) {
//ERROR: '/' expected while reading export list
SkipInstance( in, tmpbuf );
cerr << "ERROR: \'/\' expected in export list.\n\tdata lost: " << tmpbuf << "\n";
++_errorCount;
rval = SEVERITY_INPUT_ERROR;
}
ReadTokenSeparator( in );
}