forked from stepcode/stepcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdaiBinary.cc
More file actions
221 lines (195 loc) · 7.01 KB
/
sdaiBinary.cc
File metadata and controls
221 lines (195 loc) · 7.01 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
/*
* NIST STEP Core Class Library
* clstepcore/sdaiBinary.cc
* April 1997
* David Sauder
* Development of this software was funded by the United States Government,
* and is not subject to copyright.
*/
#include <sstream>
#include "clstepcore/sdai.h"
SDAI_Binary::SDAI_Binary( const char * str, int max ) {
content = std::string( str, max );
}
SDAI_Binary::SDAI_Binary( const std::string & s ) {
content = std::string( s );
}
SDAI_Binary::~SDAI_Binary( void ) {
}
SDAI_Binary & SDAI_Binary::operator= ( const char * s ) {
content = std::string( s );
return *this;
}
void SDAI_Binary::clear( void ) {
content.clear();
}
bool SDAI_Binary::empty( void ) const {
return content.empty();
}
const char * SDAI_Binary::c_str( void ) const {
return content.c_str();
}
void SDAI_Binary::STEPwrite( ostream & out ) const {
const char * str = 0;
if( empty() ) {
out << "$";
} else {
out << '\"';
str = c_str();
while( *str ) {
out << *str;
str++;
}
out << '\"';
}
}
const char * SDAI_Binary::STEPwrite( std::string & s ) const {
const char * str = 0;
if( empty() ) {
s = "$";
} else {
s = "\"";
str = c_str();
while( *str ) {
s += *str;
str++;
}
s += BINARY_DELIM;
}
return const_cast<char *>( s.c_str() );
}
Severity SDAI_Binary::ReadBinary( istream & in, ErrorDescriptor * err, int AssignVal,
int needDelims ) {
if( AssignVal ) {
clear();
}
std::string str;
char messageBuf[512];
messageBuf[0] = '\0';
in >> ws; // skip white space
if( in.good() ) {
char c;
in.get( c );
if( ( c == '\"' ) || isxdigit( 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;
}
while( in.good() && isxdigit( c ) ) {
str += c;
in.get( c );
}
if( in.good() && ( c != '\"' ) ) {
in.putback( c );
}
if( AssignVal && ( str.length() > 0 ) ) {
operator= ( str.c_str() );
}
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 )
sprintf( messageBuf,
"Binary value missing double quote delimiters.\n" );
else
sprintf( messageBuf,
"Mismatched double quote delimiters for binary.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
} else {
err->GreaterSeverity( SEVERITY_WARNING );
sprintf( messageBuf, "Invalid binary value.\n" );
err->AppendToDetailMsg( messageBuf );
err->AppendToUserMsg( messageBuf );
}
} else {
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
return err->severity();
}
Severity SDAI_Binary::StrToVal( const char * s, ErrorDescriptor * err ) {
istringstream in( ( char * )s ); // sz defaults to length of s
return ReadBinary( in, err, 1, 0 );
}
/////////////////////////////////////////////////
/// reads a binary in exchange file format delimited by double quotes
Severity SDAI_Binary::STEPread( istream & in, ErrorDescriptor * err ) {
return ReadBinary( in, err, 1, 1 );
}
Severity SDAI_Binary::STEPread( const char * s, ErrorDescriptor * err ) {
istringstream in( ( char * )s );
return STEPread( in, err );
}
/***************************************************************************//**
** * attrValue is validated.
** * err is set if there is an error
** * If optional is 1 then a missing value will be valid otherwise severity
** incomplete will be set.
** * If you don't know if the value may be optional then set it false and
** check to see if SEVERITY_INCOMPLETE is set. You can change it later to
** SEVERITY_NULL if it is valid for the value to be missing. No error
** 'message' will be associated with the value being missing so you won\'t
** have to worry about undoing an error message.
** * tokenList contains characters that terminate the expected value.
** * If tokenList is not zero then the value is expected to terminate before
** a character found in tokenlist. All values read up to the
** terminating character (delimiter) must be valid or err will be set with an
** appropriate error message. White space between the value and the
** terminating character is not considered to be invalid. If tokenList is
** null then attrValue must only contain a valid value and nothing else
** following.
******************************************************************************/
Severity SDAI_Binary::BinaryValidLevel( istream & in, ErrorDescriptor * err,
int optional, char * tokenList,
int needDelims, int clearError ) {
if( clearError ) {
err->ClearErrorMsg();
}
in >> ws; // skip white space
char c = in.peek();
if( c == '$' || in.eof() ) {
if( !optional ) {
err->GreaterSeverity( SEVERITY_INCOMPLETE );
}
if( in ) {
in >> c;
}
CheckRemainingInput( in, err, "binary", tokenList );
return err->severity();
} else {
ErrorDescriptor error;
ReadBinary( in, &error, 0, needDelims );
CheckRemainingInput( in, &error, "binary", 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_Binary::BinaryValidLevel( const char * value, ErrorDescriptor * err,
int optional, char * tokenList,
int needDelims, int clearError ) {
istringstream in( ( char * )value );
return BinaryValidLevel( in, err, optional, tokenList,
needDelims, clearError );
}