-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathStr.cc
More file actions
324 lines (292 loc) · 11.3 KB
/
Str.cc
File metadata and controls
324 lines (292 loc) · 11.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
/*
* NIST Utils Class Library
* clutils/Str.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 "Str.h"
#include <sstream>
#include <string>
/******************************************************************
** Procedure: string functions
** Description: These functions take a character or a string and return
** a temporary copy of the string with the function applied to it.
** Parameters:
** Returns: temporary copy of characters
** Side Effects:
** Status: complete
******************************************************************/
char ToLower( const char c ) {
if( isupper( c ) ) {
return ( tolower( c ) );
} else {
return ( c );
}
}
char ToUpper( const char c ) {
if( islower( c ) ) {
return ( toupper( c ) );
} else {
return ( c );
}
}
// Place in strNew a lowercase version of strOld.
char * StrToLower( const char * strOld, char * strNew ) {
int i = 0;
while( strOld[i] != '\0' ) {
strNew[i] = ToLower( strOld[i] );
i++;
}
strNew[i] = '\0';
return strNew;
}
const char * StrToLower( const char * word, std::string & s ) {
char newword [BUFSIZ];
int i = 0;
while( word [i] != '\0' ) {
newword [i] = ToLower( word [i] );
++i;
}
newword [i] = '\0';
s = newword;
return const_cast<char *>( s.c_str() );
}
const char * StrToUpper( const char * word, std::string & s ) {
char newword [BUFSIZ];
int i = 0;
while( word [i] != '\0' ) {
newword [i] = ToUpper( word [i] );
++i;
}
newword [i] = '\0';
s = newword;
return const_cast<char *>( s.c_str() );
}
const char * StrToConstant( const char * word, std::string & s ) {
char newword [BUFSIZ];
int i = 0;
while( word [i] != '\0' ) {
if( word [i] == '/' || word [i] == '.' ) {
newword [i] = '_';
} else {
newword [i] = ToUpper( word [i] );
}
++i;
}
newword [i] = '\0';
s = newword;
return const_cast<char *>( s.c_str() );
}
/**************************************************************//**
** \fn StrCmpIns (const char * str1, const char * str2)
** \returns Comparison result
** Compares two strings case insensitive (lowercase).
** Returns < 0 when str1 less then str2
** == 0 when str1 equals str2
** > 0 when str1 greater then str2
******************************************************************/
int StrCmpIns( const char * str1, const char * str2 ) {
char c1, c2;
while( ( c1 = tolower( *str1 ) ) == ( c2 = tolower( *str2 ) ) && c1 != '\0' ) {
str1++;
str2++;
}
return c1 - c2;
}
/**
* Test if a string ends with the given suffix.
*/
bool StrEndsWith( const std::string & s, const char * suf ) {
if( suf == NULL ) {
return false;
}
std::string suffix = suf;
size_t sLen = s.length();
size_t suffixLen = suffix.length();
if( sLen < suffixLen ) {
return false;
}
if( s.substr( sLen - suffixLen ).compare( suffix ) == 0 ) {
return true;
}
return false;
}
/**
* Extract the next delimited string from the istream.
*/
std::string GetLiteralStr( istream & in, ErrorDescriptor * err ) {
std::string s;
in >> std::ws; // skip whitespace
if( in.good() && in.peek() == STRING_DELIM ) {
s += in.get();
bool allDelimsEscaped = true;
while( in.good() ) {
if( in.peek() == STRING_DELIM ) {
// A delimiter closes the string unless it's followed by another
// delimiter, in which case it's escaped. \S\ starts an ISO
// 8859 character escape sequence, so we ignore delimiters
// prefixed with \S\.
if( !StrEndsWith( s, "\\S\\" ) ) {
allDelimsEscaped = !allDelimsEscaped;
}
} else if( !allDelimsEscaped ) {
// Found normal char after unescaped delim, so last delim
// that was appended terminated the string.
break;
}
if( !in.eof() ) {
s += in.get();
}
}
if( allDelimsEscaped ) {
// Any delimiters found after the opening delimiter were escaped,
// so the string is unclosed.
// non-recoverable error
err->AppendToDetailMsg( "Missing closing quote on string value.\n" );
err->AppendToUserMsg( "Missing closing quote on string value.\n" );
err->GreaterSeverity( SEVERITY_INPUT_ERROR );
}
}
return s;
}
/**************************************************************//**
** \fn PrettyTmpName (char * oldname)
** \returns a new capitalized name in a static buffer
** Capitalizes first char of word, rest is lowercase. Removes '_'.
** Status: OK 7-Oct-1992 kcm
******************************************************************/
const char * PrettyTmpName( const char * oldname ) {
int i = 0;
static char newname [BUFSIZ];
newname [0] = '\0';
while( ( oldname [i] != '\0' ) && ( i < BUFSIZ ) ) {
newname [i] = ToLower( oldname [i] );
if( oldname [i] == '_' ) { /* character is '_' */
++i;
newname [i] = ToUpper( oldname [i] );
}
if( oldname [i] != '\0' ) {
++i;
}
}
newname [0] = ToUpper( oldname [0] );
newname [i] = '\0';
return newname;
}
/**************************************************************//**
** \fn PrettyNewName (char * oldname)
** \returns a new capitalized name
** Capitalizes first char of word, rest is lowercase. Removes '_'.
** Side Effects: allocates memory for the new name
** Status: OK 7-Oct-1992 kcm
******************************************************************/
char * PrettyNewName( const char * oldname ) {
char * name = new char [strlen( oldname ) + 1];
strcpy( name, PrettyTmpName( oldname ) );
return name;
}
/**
*** This function is used to check an input stream following a read. It writes
*** error messages in the 'ErrorDescriptor &err' argument as appropriate.
*** 'const char *tokenList' argument contains a string made up of delimiters
*** that are used to move the file pointer in the input stream to the end of
*** the value you are reading (i.e. the ending marked by the presence of the
*** delimiter). The file pointer is moved just prior to the delimiter. If the
*** tokenList argument is a null pointer then this function expects to find EOF.
***
*** If input is being read from a stream then a tokenList should be provided so
*** this function can push the file pointer up to but not past the delimiter
*** (i.e. not removing the delimiter from the input stream). If you have a
*** string containing a single value and you expect the whole string to contain
*** a valid value, you can change the string to an istrstream, read the value
*** then send the istrstream to this function with tokenList set to null
*** and this function will set an error for you if any input remains following
*** the value.
*** If the input stream can be readable again then
*** - any error states set for the stream are cleared.
*** - white space skipped in the input stream
*** - if EOF is encountered it returns
*** otherwise it peeks at the next character
*** - if the tokenList argument exists (i.e. is not null)
*** then if looks to see if the char peeked at is in the tokenList string
*** if it is then no error is set in the ErrorDescriptor
*** if the char peeked at is not in the tokenList string that implies
*** that there is garbage following the value that was successfully
*** or unsuccessfully read. The garbage is read until EOF or a
*** delimiter in the tokenList is found.
*** - EOF is found you did not recover -> SEVERITY_INPUT_ERROR
*** - delimiter found you recovered successfully => SEVERITY_WARNING
*** - if tokenList does not exist then it expects to find EOF, if it does
*** not then it is an error but the bad chars are not read since you have
*** no way to know when to stop.
**/
Severity CheckRemainingInput( istream & in, ErrorDescriptor * err,
const char * typeName, // used in error message
const char * delimiterList ) { // e.g. ",)"
string skipBuf;
ostringstream errMsg;
if( in.eof() ) {
// no error
return err->severity();
} else if( in.bad() ) {
// Bad bit must have been set during read. Recovery is impossible.
err->GreaterSeverity( SEVERITY_INPUT_ERROR );
errMsg << "Invalid " << typeName << " value.\n";
err->AppendToUserMsg( errMsg.str().c_str() );
err->AppendToDetailMsg( errMsg.str().c_str() );
} else {
// At most the fail bit is set, so stream can still be read.
// Clear errors and skip whitespace.
in.clear();
in >> ws;
if( in.eof() ) {
// no error
return err->severity();
}
if( delimiterList != NULL ) {
// If the next char is a delimiter then there's no error.
char c = in.peek();
if( strchr( delimiterList, c ) == NULL ) {
// Error. Extra input is more than just a delimiter and is
// now considered invalid. We'll try to recover by skipping
// to the next delimiter.
for( in.get( c ); in && !strchr( delimiterList, c ); in.get( c ) ) {
skipBuf += c;
}
if( strchr( delimiterList, c ) != NULL ) {
// Delimiter found. Recovery succeeded.
in.putback( c );
errMsg << "\tFound invalid " << typeName << " value...\n";
err->AppendToUserMsg( errMsg.str().c_str() );
err->AppendToDetailMsg( errMsg.str().c_str() );
err->AppendToDetailMsg( "\tdata lost looking for end of "
"attribute: " );
err->AppendToDetailMsg( skipBuf.c_str() );
err->AppendToDetailMsg( "\n" );
err->GreaterSeverity( SEVERITY_WARNING );
} else {
// No delimiter found. Recovery failed.
errMsg << "Unable to recover from input error while "
<< "reading " << typeName << " value.\n";
err->AppendToUserMsg( errMsg.str().c_str() );
err->AppendToDetailMsg( errMsg.str().c_str() );
err->GreaterSeverity( SEVERITY_INPUT_ERROR );
}
}
} else if( in.good() ) {
// Error. Have more input, but lack of delimiter list means we
// don't know where we can safely resume. Recovery is impossible.
err->GreaterSeverity( SEVERITY_WARNING );
errMsg << "Invalid " << typeName << " value.\n";
err->AppendToUserMsg( errMsg.str().c_str() );
err->AppendToDetailMsg( errMsg.str().c_str() );
}
}
return err->severity();
}
Severity CheckRemainingInput( std::istream & in, ErrorDescriptor * err, const std::string typeName, const char * tokenList ) {
return CheckRemainingInput( in, err, typeName.c_str(), tokenList );
}