1 
2 /*
3 * NIST STEP Core Class Library
4 * clstepcore/sdaiString.cc
5 * April 1997
6 * KC Morris
7 
8 * Development of this software was funded by the United States Government,
9 * and is not subject to copyright.
10 */
11 
12 #include <sdai.h>
13 #include <sstream>
14 #include "sc_memmgr.h"
15 
SDAI_String(const char * str,size_t max)16 SDAI_String::SDAI_String( const char * str, size_t max ) {
17     if( !str ) {
18         str = "";
19     }
20 
21     if( max == std::string::npos ) {
22         content = std::string( str );
23     } else {
24         content = std::string( str, max );
25     }
26 }
27 
SDAI_String(const std::string & s)28 SDAI_String::SDAI_String( const std::string & s )
29     : content( std::string( s ) ) {
30 }
31 
SDAI_String(const SDAI_String & s)32 SDAI_String::SDAI_String( const SDAI_String & s )
33     : content( std::string( s.c_str() ) ) {
34 }
35 
~SDAI_String(void)36 SDAI_String::~SDAI_String( void ) {
37 }
38 
operator =(const char * s)39 SDAI_String & SDAI_String::operator= ( const char * s ) {
40     content = std::string( s );
41     return *this;
42 }
43 
operator ==(const char * s) const44 bool SDAI_String::operator== ( const char * s ) const {
45     return ( content == s );
46 }
47 
clear(void)48 void SDAI_String::clear( void ) {
49     content.clear();
50 }
51 
empty(void) const52 bool SDAI_String::empty( void ) const {
53     return content.empty();
54 }
55 
c_str(void) const56 const char * SDAI_String::c_str( void ) const {
57     return content.c_str();
58 }
59 
60 
STEPwrite(ostream & out) const61 void SDAI_String::STEPwrite( ostream & out ) const {
62     out << c_str();
63 }
64 
STEPwrite(std::string & s) const65 void SDAI_String::STEPwrite( std::string & s ) const {
66     s += c_str();
67 }
68 
StrToVal(const char * s)69 Severity SDAI_String::StrToVal( const char * s ) {
70     operator= ( s );
71     if( ! strcmp( c_str(),  s ) ) {
72         return SEVERITY_NULL ;
73     } else {
74         return SEVERITY_INPUT_ERROR;
75     }
76 }
77 
78 /**
79  * STEPread reads a string in exchange file format
80  * starting with a single quote
81  */
STEPread(istream & in,ErrorDescriptor * err)82 Severity SDAI_String::STEPread( istream & in, ErrorDescriptor * err ) {
83     clear();  // clear the old string
84     // remember the current format state to restore the previous settings
85     ios_base::fmtflags flags = in.flags();
86     in.unsetf( ios::skipws );
87 
88     // extract the string from the inputstream
89     std::string s = GetLiteralStr( in, err );
90     content += s;
91 
92     // retrieve current severity
93     Severity sev = err -> severity();
94 
95     if( s.empty() ) {
96         // no string was read
97         in.flags( flags ); // set the format state back to previous settings
98         err -> GreaterSeverity( SEVERITY_INCOMPLETE );
99         sev = SEVERITY_INCOMPLETE;
100     } else if( sev != SEVERITY_INPUT_ERROR ) {
101         // read valid string
102         sev = SEVERITY_NULL;
103     }
104     return sev;
105 }
106 
107 /**
108  * \copydoc STEPread( istream & in, ErrorDescriptor * err )
109  */
STEPread(const char * s,ErrorDescriptor * err)110 Severity SDAI_String::STEPread( const char * s, ErrorDescriptor * err ) {
111     istringstream in( ( char * )s );
112     return STEPread( in, err );
113 }
114