-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathsdaiEnum.cc
More file actions
700 lines (615 loc) · 20.3 KB
/
sdaiEnum.cc
File metadata and controls
700 lines (615 loc) · 20.3 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
#include "clstepcore/sdai.h"
/*
* NIST STEP Core Class Library
* cldai/sdaiEnum.cc
* April 1997
* K. C. Morris
* David Sauder
* Development of this software was funded by the United States Government,
* and is not subject to copyright.
*/
#include <sstream>
///////////////////////////////////////////////////////////////////////////////
// class Logical
///////////////////////////////////////////////////////////////////////////////
SDAI_LOGICAL::SDAI_LOGICAL( const char * val ) {
set_value( val );
}
SDAI_LOGICAL::SDAI_LOGICAL( Logical state ) {
set_value( state );
}
SDAI_LOGICAL::SDAI_LOGICAL( const SDAI_LOGICAL & source ) {
set_value( source.asInt() );
}
SDAI_LOGICAL::SDAI_LOGICAL( int i ) {
if( i == 0 ) {
v = LFalse ;
} else {
v = LTrue ;
}
}
SDAI_LOGICAL::~SDAI_LOGICAL() {
}
const char * SDAI_LOGICAL::Name() const {
return "Logical";
}
int SDAI_LOGICAL::no_elements() const {
return 3;
}
const char * SDAI_LOGICAL::element_at( int n ) const {
switch( n ) {
case LUnknown :
return "U";
case LFalse :
return "F";
case LTrue :
return "T";
default:
return "UNSET";
}
}
int SDAI_LOGICAL::exists() const { // return 0 if unset otherwise return 1
return !( v == 2 );
}
void SDAI_LOGICAL::nullify() { // change the receiver to an unset status
v = 2;
}
SDAI_LOGICAL::operator Logical() const {
switch( v ) {
case LFalse :
return LFalse ;
case LTrue :
return LTrue ;
case LUnknown :
return LUnknown ;
case LUnset :
default:
return LUnset ;
}
}
SDAI_LOGICAL & SDAI_LOGICAL::operator= ( const SDAI_LOGICAL & t ) {
set_value( t.asInt() );
return *this;
}
SDAI_LOGICAL SDAI_LOGICAL::operator ==( const SDAI_LOGICAL & t ) const {
if( v == t.asInt() ) {
return LTrue ;
}
return LFalse ;
}
int SDAI_LOGICAL::set_value( const int i ) {
if( i > no_elements() + 1 ) {
v = 2;
return v;
}
const char * tmp = element_at( i );
if( tmp[0] != '\0' ) {
return ( v = i );
}
// otherwise
cerr << "(OLD Warning:) invalid enumeration value " << i
<< " for " << Name() << "\n";
DebugDisplay();
return no_elements() + 1 ;
}
int SDAI_LOGICAL::set_value( const char * n ) {
// assigns the appropriate value based on n
if( !n || ( !strcmp( n, "" ) ) ) {
nullify();
return asInt();
}
int i = 0;
std::string tmp;
while( ( i < ( no_elements() + 1 ) ) &&
( strcmp( ( char * )StrToUpper( n, tmp ), element_at( i ) ) != 0 ) ) {
++i;
}
if( ( no_elements() + 1 ) == i ) { // exhausted all the possible values
nullify();
return v;
}
v = i;
return v;
}
Severity SDAI_LOGICAL::ReadEnum( istream & in, ErrorDescriptor * err, int AssignVal,
int needDelims ) {
if( AssignVal ) {
set_null();
}
std::string str;
char messageBuf[512];
messageBuf[0] = '\0';
in >> ws; // skip white space
if( in.good() ) {
char c;
in.get( c );
if( c == '.' || isalpha( c ) ) {
int validDelimiters = 1;
if( c == '.' ) {
in.get( c ); // push past the delimiter
// since found a valid delimiter it is now invalid until the
// matching ending delim is found
validDelimiters = 0;
}
// look for UPPER
if( in.good() && ( isalpha( c ) || c == '_' ) ) {
str += c;
in.get( c );
}
// look for UPPER or DIGIT
while( in.good() && ( isalnum( c ) || c == '_' ) ) {
str += c;
in.get( c );
}
// if character is not the delimiter unread it
if( in.good() && ( c != '.' ) ) {
in.putback( c );
}
// a value was read
if( str.length() > 0 ) {
int i = 0;
const char * strval = str.c_str();
std::string tmp;
while( ( i < ( no_elements() + 1 ) ) &&
( strcmp( ( char * )StrToUpper( strval, tmp ),
element_at( i ) ) != 0 ) ) {
++i;
}
if( ( no_elements() + 1 ) == i ) {
// exhausted all the possible values
err->GreaterSeverity( SEVERITY_WARNING );
err->AppendToDetailMsg( "Invalid Enumeration value.\n" );
err->AppendToUserMsg( "Invalid Enumeration value.\n" );
} else {
if( AssignVal ) {
v = i;
}
}
// now also check the delimiter situation
if( c == '.' ) { // if found ending delimiter
// if expecting delim (i.e. validDelimiter == 0)
if( !validDelimiters ) {
validDelimiters = 1; // everything is fine
} else { // found ending delimiter but no initial delimiter
validDelimiters = 0;
}
}
// didn't find any delimiters at all and need them.
else if( needDelims ) {
validDelimiters = 0;
}
if( !validDelimiters ) {
err->GreaterSeverity( SEVERITY_WARNING );
if( needDelims )
snprintf( messageBuf, sizeof(messageBuf),
"Enumerated value has invalid period delimiters.\n" );
else
snprintf( messageBuf, sizeof(messageBuf),
"Mismatched period delimiters for enumeration.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
return err->severity();
}
// found valid or invalid delimiters with no associated value
else if( ( c == '.' ) || !validDelimiters ) {
err->GreaterSeverity( SEVERITY_WARNING );
err->AppendToDetailMsg(
"Enumerated has valid or invalid period delimiters with no value.\n"
);
err->AppendToUserMsg(
"Enumerated has valid or invalid period delimiters with no value.\n"
);
return err->severity();
} else { // no delims and no value
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
} else if( ( c == ',' ) || ( c == ')' ) ) {
in.putback( c );
err->GreaterSeverity( SEVERITY_INCOMPLETE );
} else {
in.putback( c );
err->GreaterSeverity( SEVERITY_WARNING );
snprintf( messageBuf, sizeof(messageBuf), "Invalid enumeration value.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
} else { // hit eof (assuming there was no error state for istream passed in)
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
return err->severity();
}
///////////////////////////////////////////////////////////////////////////////
// class BOOLEAN Jan 97
///////////////////////////////////////////////////////////////////////////////
const char * SDAI_BOOLEAN::Name() const {
return "Bool";
}
SDAI_BOOLEAN::SDAI_BOOLEAN( char * val ) {
set_value( val );
}
SDAI_BOOLEAN::SDAI_BOOLEAN( Boolean state ) {
set_value( state );
}
SDAI_BOOLEAN::SDAI_BOOLEAN( const SDAI_BOOLEAN & source ) {
set_value( source.asInt() );
}
SDAI_BOOLEAN::~SDAI_BOOLEAN() {
}
int SDAI_BOOLEAN::no_elements() const {
return 2;
}
SDAI_BOOLEAN::SDAI_BOOLEAN( int i ) {
if( i == 0 ) {
v = BFalse ;
} else {
v = BTrue ;
}
}
SDAI_BOOLEAN::SDAI_BOOLEAN( const SDAI_LOGICAL & val ) {
if( val.asInt() == LUnknown ) {
// this should set error code sdaiVT_NVLD i.e. Invalid value type.
v = BUnset;
return;
}
set_value( val );
}
SDAI_BOOLEAN::operator Boolean() const {
switch( v ) {
case BFalse :
return BFalse ;
case BTrue :
return BTrue ;
case BUnset :
default:
return BUnset ;
}
}
SDAI_BOOLEAN & SDAI_BOOLEAN::operator= ( const SDAI_LOGICAL & t ) {
set_value( t.asInt() );
return *this;
}
SDAI_BOOLEAN & SDAI_BOOLEAN::operator= ( const SDAI_BOOLEAN & t ) {
v = t;
return *this;
}
SDAI_BOOLEAN & SDAI_BOOLEAN::operator= ( const Boolean t ) {
v = t;
return *this;
}
const char * SDAI_BOOLEAN::element_at( int n ) const {
switch( n ) {
case BFalse :
return "F";
case BTrue :
return "T";
default:
return "UNSET";
}
}
SDAI_LOGICAL SDAI_BOOLEAN::operator ==( const SDAI_LOGICAL & t ) const {
if( v == t.asInt() ) {
return LTrue ;
}
return LFalse ;
}
///////////////////////////////////////////////////////////////////////////////
SDAI_Enum::SDAI_Enum() {
v = 0;
}
/**
* \copydoc set_value( const char * n )
*/
int SDAI_Enum::put( int val ) {
return set_value( val );
}
/**
* \copydoc set_value( const char * n )
*/
int SDAI_Enum::put( const char * n ) {
return set_value( n );
}
/// return 0 if unset otherwise return 1
/// WARNING it appears that exists() will return true after a call to nullify(). is this intended?
int SDAI_Enum::exists() const {
return !( v > no_elements() );
}
/**
* change the receiver to an unset status
* unset is generated to be 1 greater than last element
*/
void SDAI_Enum::nullify() {
set_value( no_elements() + 1 );
}
/**************************************************************//**
** prints out some information on the enumerated item for
** debugging purposes
** Status: ok 2/1/91
******************************************************************/
void SDAI_Enum::DebugDisplay( ostream & out ) const {
std::string tmp;
out << "Current " << Name() << " value: " << endl
<< " cardinal: " << v << endl
<< " string: " << asStr( tmp ) << endl
<< " Part 21 file format: ";
STEPwrite( out );
out << endl;
out << "Valid values are: " << endl;
int i = 0;
while( i < ( no_elements() + 1 ) ) {
out << i << " " << element_at( i ) << endl;
i++;
}
out << "\n";
}
/**
** Read an Enumeration value
** ENUMERATION = "." UPPER { UPPER | DIGIT } "."
** *note* UPPER is defined as alpha or underscore.
**
** \returns Severity of the error.
** \param err error message and error Severity is written to ErrorDescriptor *err.
** \param AssignVal is:
** true => value is assigned to the SDAI_Enum;
** true or false => value is read and appropriate error info is set and returned.
** \param int needDelims is:
** false => absence of the period delimiters is not an error;
** true => delimiters must be valid;
** true or false => non-matching delimiters are flagged as an error
*/
Severity SDAI_Enum::ReadEnum( istream & in, ErrorDescriptor * err, int AssignVal,
int needDelims ) {
if( AssignVal ) {
set_null();
}
std::string str;
char messageBuf[512];
messageBuf[0] = '\0';
in >> ws; // skip white space
if( in.good() ) {
char c;
in.get( c );
if( c == '.' || isalpha( c ) ) {
int validDelimiters = 1;
if( c == '.' ) {
in.get( c ); // push past the delimiter
// since found a valid delimiter it is now invalid until the
// matching ending delim is found
validDelimiters = 0;
}
// look for UPPER
if( in.good() && ( isalpha( c ) || c == '_' ) ) {
str += c;
in.get( c );
}
// look for UPPER or DIGIT
while( in.good() && ( isalnum( c ) || c == '_' ) ) {
str += c;
in.get( c );
}
// if character is not the delimiter unread it
if( in.good() && ( c != '.' ) ) {
in.putback( c );
}
// a value was read
if( str.length() > 0 ) {
int i = 0;
const char * strval = str.c_str();
std::string tmp;
while( ( i < no_elements() ) &&
( strcmp( ( char * )StrToUpper( strval, tmp ),
element_at( i ) ) != 0 ) ) {
++i;
}
if( no_elements() == i ) {
// exhausted all the possible values
err->GreaterSeverity( SEVERITY_WARNING );
err->AppendToDetailMsg( "Invalid Enumeration value.\n" );
err->AppendToUserMsg( "Invalid Enumeration value.\n" );
} else {
if( AssignVal ) {
v = i;
}
}
// now also check the delimiter situation
if( c == '.' ) { // if found ending delimiter
// if expecting delim (i.e. validDelimiter == 0)
if( !validDelimiters ) {
validDelimiters = 1; // everything is fine
} else { // found ending delimiter but no initial delimiter
validDelimiters = 0;
}
}
// didn't find any delimiters at all and need them.
else if( needDelims ) {
validDelimiters = 0;
}
if( !validDelimiters ) {
err->GreaterSeverity( SEVERITY_WARNING );
if( needDelims )
snprintf( messageBuf, sizeof(messageBuf),
"Enumerated value has invalid period delimiters.\n" );
else
snprintf( messageBuf, sizeof(messageBuf),
"Mismatched period delimiters for enumeration.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
return err->severity();
}
// found valid or invalid delimiters with no associated value
else if( ( c == '.' ) || !validDelimiters ) {
err->GreaterSeverity( SEVERITY_WARNING );
err->AppendToDetailMsg(
"Enumerated has valid or invalid period delimiters with no value.\n"
);
err->AppendToUserMsg(
"Enumerated has valid or invalid period delimiters with no value.\n"
);
return err->severity();
} else { // no delims and no value
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
} else if( ( c == ',' ) || ( c == ')' ) ) {
in.putback( c );
err->GreaterSeverity( SEVERITY_INCOMPLETE );
} else {
in.putback( c );
err->GreaterSeverity( SEVERITY_WARNING );
snprintf( messageBuf, sizeof(messageBuf), "Invalid enumeration value.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
} else { // hit eof (assuming there was no error state for istream passed in)
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
return err->severity();
}
Severity SDAI_Enum::StrToVal( const char * s, ErrorDescriptor * err, int optional ) {
istringstream in( ( char * )s ); // sz defaults to length of s
ReadEnum( in, err, 1, 0 );
if( ( err->severity() == SEVERITY_INCOMPLETE ) && optional ) {
err->severity( SEVERITY_NULL );
}
return err->severity();
}
/// reads an enumerated value in STEP file format
Severity SDAI_Enum::STEPread( const char * s, ErrorDescriptor * err, int optional ) {
istringstream in( ( char * )s );
return STEPread( in, err, optional );
}
/// reads an enumerated value in STEP file format
Severity SDAI_Enum::STEPread( istream & in, ErrorDescriptor * err, int optional ) {
ReadEnum( in, err, 1, 1 );
if( ( err->severity() == SEVERITY_INCOMPLETE ) && optional ) {
err->severity( SEVERITY_NULL );
}
return err->severity();
}
const char * SDAI_Enum::asStr( std::string & s ) const {
if( exists() ) {
s = element_at( v );
return s.c_str();
} else {
s.clear();
return "";
}
}
void SDAI_Enum::STEPwrite( ostream & out ) const {
if( is_null() ) {
out << '$';
} else {
std::string tmp;
out << "." << asStr( tmp ) << ".";
}
}
const char * SDAI_Enum::STEPwrite( std::string & s ) const {
if( is_null() ) {
s.clear();
} else {
std::string tmp;
s = ".";
s.append( asStr( tmp ) );
s.append( "." );
}
return const_cast<char *>( s.c_str() );
}
Severity SDAI_Enum::EnumValidLevel( istream & in, ErrorDescriptor * err,
int optional, char * tokenList,
int needDelims, int clearError ) {
if( clearError ) {
err->ClearErrorMsg();
}
in >> ws; // skip white space
char c = ' ';
c = in.peek();
if( c == '$' || in.eof() ) {
if( !optional ) {
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
if( in ) {
in >> c;
}
CheckRemainingInput( in, err, "enumeration", tokenList );
return err->severity();
} else {
ErrorDescriptor error;
ReadEnum( in, &error, 0, needDelims );
CheckRemainingInput( in, &error, "enumeration", tokenList );
Severity sev = error.severity();
if( sev < SEVERITY_INCOMPLETE ) {
err->AppendToDetailMsg( error.DetailMsg() );
err->AppendToUserMsg( error.UserMsg() );
err->GreaterSeverity( error.severity() );
} else if( sev == SEVERITY_INCOMPLETE && !optional ) {
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
}
return err->severity();
}
Severity SDAI_Enum::EnumValidLevel( const char * value, ErrorDescriptor * err,
int optional, char * tokenList,
int needDelims, int clearError ) {
istringstream in( ( char * )value );
return EnumValidLevel( in, err, optional, tokenList, needDelims,
clearError );
}
/**************************************************************//**
** sets the value of an enumerated attribute case is not important
** in the character based version if value is not acceptable, a
** warning is printed and processing continues
**
** set_value is the same function as put
**
** Parameter: value to be set
** Status: ok 2.91
** \returns: value set
******************************************************************/
int SDAI_Enum::set_value( const char * n ) {
if( !n || ( !strcmp( n, "" ) ) ) {
nullify();
return asInt();
}
int i = 0;
std::string tmp;
while( ( i < no_elements() ) &&
( strcmp( ( char * )StrToUpper( n, tmp ), element_at( i ) ) != 0 ) ) {
++i;
}
if( no_elements() == i ) { // exhausted all the possible values
return v = no_elements() + 1; // defined as UNSET
}
v = i;
return v;
}
/**
* \copydoc set_value( const char * n )
*/
int SDAI_Enum::set_value( const int i ) {
if( i > no_elements() ) {
v = no_elements() + 1;
return v;
}
const char * tmp = element_at( i );
if( tmp[0] != '\0' ) {
return ( v = i );
}
// otherwise
cerr << "(OLD Warning:) invalid enumeration value " << i
<< " for " << Name() << "\n";
DebugDisplay();
return no_elements() + 1 ;
}
SDAI_Enum & SDAI_Enum::operator= ( const int i ) {
put( i );
return *this;
}
SDAI_Enum & SDAI_Enum::operator= ( const SDAI_Enum & Senum ) {
put( Senum.asInt() );
return *this;
}
ostream & operator<< ( ostream & out, const SDAI_Enum & a ) {
std::string tmp;
out << a.asStr( tmp );
return out;
}