Skip to content

Commit 6e9b5a7

Browse files
rbertucatmpictor
authored andcommitted
- Fix SDAIString (read and write) for P21
- Add support for \S\' in P21 string
1 parent defe0dd commit 6e9b5a7

File tree

4 files changed

+71
-104
lines changed

4 files changed

+71
-104
lines changed

src/clstepcore/read_func.cc

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -559,39 +559,7 @@ NumberValidLevel( const char * attrValue, ErrorDescriptor * err,
559559

560560
void
561561
PushPastString( istream & in, std::string & s, ErrorDescriptor * err ) {
562-
char messageBuf[BUFSIZ];
563-
messageBuf[0] = '\0';
564-
565-
char c;
566-
in >> ws; // skip whitespace
567-
in >> c;
568-
int i_quote = 0;
569-
if ( c == STRING_DELIM ) {
570-
s += c;
571-
while ( i_quote != -1 && in.get(c) ) {
572-
s += c;
573-
// to handle a string like 'hi'''
574-
if ( c == STRING_DELIM) {
575-
i_quote++;
576-
} else {
577-
// # of quotes is odd
578-
if ( i_quote % 2 != 0 ) {
579-
i_quote = -1;
580-
// put back last char, take it off from s and update c
581-
in.putback(c);
582-
s = s.substr(0, s.size() - 1);
583-
c = *s.rbegin();
584-
}
585-
}
586-
}
587-
588-
if( c != STRING_DELIM ) {
589-
err->GreaterSeverity( SEVERITY_INPUT_ERROR );
590-
sprintf( messageBuf, "Invalid string value.\n" );
591-
err->AppendToDetailMsg( messageBuf );
592-
s.append( "\'" );
593-
}
594-
}
562+
s = ToExpressStr(in, err);
595563
}
596564

597565
// assign 's' so that it contains an exchange file format aggregate read from

src/clstepcore/sdaiString.cc

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,12 @@ SCLP23( String )::operator= ( const char * s ) {
2323

2424
void
2525
SCLP23( String )::STEPwrite( ostream & out ) const {
26-
const char * str = 0;
27-
if( empty() ) {
28-
out << "\'\'";
29-
} else {
30-
out << "\'";
31-
str = c_str();
32-
while( *str ) {
33-
if( *str == STRING_DELIM ) {
34-
out << STRING_DELIM;
35-
}
36-
out << *str;
37-
str++;
38-
}
39-
out << "\'";
40-
}
26+
out << c_str();
4127
}
4228

4329
void
4430
SCLP23( String )::STEPwrite( std::string & s ) const {
45-
const char * str = 0;
46-
if( empty() ) {
47-
s = "\'\'";
48-
} else {
49-
s = "\'";
50-
str = c_str();
51-
while( *str ) {
52-
if( *str == STRING_DELIM ) {
53-
s += STRING_DELIM;
54-
}
55-
s += *str;
56-
str++;
57-
}
58-
s += STRING_DELIM;
59-
}
31+
s += c_str();
6032
}
6133

6234
Severity
@@ -73,12 +45,7 @@ SCLP23( String )::StrToVal( const char * s ) {
7345
// starting with a single quote
7446
Severity
7547
SCLP23( String )::STEPread( istream & in, ErrorDescriptor * err ) {
76-
int foundEndQuote = 0; // need so this string is not ok: 'hi''
7748
clear(); // clear the old string
78-
char c;
79-
in >> ws; // skip white space
80-
in >> c;
81-
8249
// remember the current format state to restore the previous settings
8350
#if defined(__GNUC__) && (__GNUC__ > 2)
8451
ios_base::fmtflags flags = in.flags();
@@ -87,44 +54,26 @@ SCLP23( String )::STEPread( istream & in, ErrorDescriptor * err ) {
8754
#endif
8855
in.unsetf( ios::skipws );
8956

90-
if( c == STRING_DELIM ) {
91-
while( ( c != '\0' ) && in.good() && in.get( c ) ) {
92-
if( c == STRING_DELIM ) {
93-
in.get( c );
94-
if( ! in.good() ) {
95-
// it is the final quote and no extra char was read
96-
foundEndQuote = 1;
97-
c = '\0';
98-
continue;
99-
} else if( !( c == STRING_DELIM ) ) {
100-
// it is the final quote and extra char was read
101-
in.putback( c ); // put back non-quote extra char
102-
foundEndQuote = 1;
103-
c = '\0';
104-
continue;
105-
}
106-
// else { ; } // do nothing it is an embedded quote
107-
}
108-
operator+= ( c );
109-
}
57+
// extract the string from the inputstream
58+
string s = ToExpressStr(in, err);
59+
operator+= (s);
11060

111-
if( foundEndQuote ) {
112-
return SEVERITY_NULL;
113-
} else {
114-
// non-recoverable error
115-
err->AppendToDetailMsg( "Missing closing quote on string value.\n" );
116-
err->AppendToUserMsg( "Missing closing quote on string value.\n" );
117-
err->GreaterSeverity( SEVERITY_INPUT_ERROR );
118-
return SEVERITY_INPUT_ERROR;
119-
}
120-
}
121-
// otherwise there was not a quote
122-
in.putback( c );
123-
in.flags( flags ); // set the format state back to previous settings
61+
// retrieve current severity
62+
Severity sev = err -> severity();
12463

125-
clear();
64+
// Not missing closing quote on string value
65+
if (sev != SEVERITY_INPUT_ERROR && s.compare("") != 0) {
66+
sev = SEVERITY_NULL;
67+
}
12668

127-
return err -> GreaterSeverity( SEVERITY_INCOMPLETE );
69+
// There was no quote
70+
if ( !(sev == SEVERITY_INPUT_ERROR || sev == SEVERITY_NULL) ) {
71+
in.flags( flags ); // set the format state back to previous settings
72+
clear();
73+
err -> GreaterSeverity( SEVERITY_INCOMPLETE );
74+
sev = SEVERITY_INCOMPLETE;
75+
}
76+
return sev;
12877
}
12978

13079
Severity

src/clutils/Str.cc

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
*/
1212

1313
#include <Str.h>
14-
14+
#ifndef STRING_DELIM
15+
#define STRING_DELIM '\''
16+
#endif
1517
/******************************************************************
1618
** Procedure: string functions
1719
** Description: These functions take a character or a string and return
@@ -130,6 +132,52 @@ char * PrettyNewName( const char * oldname ) {
130132
return name;
131133
}
132134

135+
/*
136+
* Extract the string conform to P21 from the istream
137+
*/
138+
std::string ToExpressStr( istream &in, ErrorDescriptor *err ) {
139+
char c;
140+
in >> ws; // skip white space
141+
in >> c;
142+
int i_quote = 0;
143+
size_t found;
144+
string s = "";
145+
if ( c == STRING_DELIM ) {
146+
s += c;
147+
while ( i_quote != -1 && in.get(c) ) {
148+
s += c;
149+
// to handle a string like 'hi'''
150+
if ( c == STRING_DELIM) {
151+
// to handle \S\'
152+
found = s.find_last_of("\\S\\");
153+
if ( !(found != string::npos && (s.size() == found + 2)) ) {
154+
i_quote++;
155+
}
156+
} else {
157+
// # of quotes is odd
158+
if ( i_quote % 2 != 0 ) {
159+
i_quote = -1;
160+
// put back last char, take it off from s and update c
161+
in.putback(c);
162+
s = s.substr(0, s.size() - 1);
163+
c = *s.rbegin();
164+
}
165+
}
166+
}
167+
168+
if ( c != STRING_DELIM ) {
169+
// non-recoverable error
170+
err->AppendToDetailMsg("Missing closing quote on string value.\n");
171+
err->AppendToUserMsg("Missing closing quote on string value.\n");
172+
err->GreaterSeverity(SEVERITY_INPUT_ERROR);
173+
}
174+
} else {
175+
in.putback(c);
176+
}
177+
178+
return s;
179+
}
180+
133181
// This function is used to check an input stream following a read. It writes
134182
// error messages in the 'ErrorDescriptor &err' argument as appropriate.
135183
// 'const char *tokenList' argument contains a string made up of delimiters

src/clutils/Str.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const char * PrettyTmpName (const char * oldname);
3939
char * PrettyNewName (const char * oldname);
4040
char * EntityClassName ( char * oldname);
4141

42+
std::string ToExpressStr (istream &in, ErrorDescriptor *err);
43+
4244
extern Severity CheckRemainingInput
4345
(istream &in, ErrorDescriptor *err,
4446
const char *typeName, // used in error message

0 commit comments

Comments
 (0)