1 |
torben |
38 |
#ifndef __STRING_NOCASE_H__ |
2 |
|
|
#define __STRING_NOCASE_H__ |
3 |
|
|
|
4 |
|
|
#include <string> |
5 |
|
|
#include <iostream> |
6 |
|
|
#include <cctype> |
7 |
torben |
114 |
#include <strings.h> |
8 |
torben |
38 |
|
9 |
|
|
// case insensitive character traits |
10 |
|
|
// inherited copy (preserves case), |
11 |
|
|
// case insensitive comparison, search |
12 |
|
|
struct traits_nocase : std::char_traits<char> |
13 |
|
|
{ |
14 |
torben |
196 |
static bool eq( const char& c1, const char& c2 ) |
15 |
|
|
{ |
16 |
|
|
return toupper(c1) == toupper(c2) ; |
17 |
|
|
} |
18 |
|
|
static bool lt( const char& c1, const char& c2 ) |
19 |
|
|
{ |
20 |
|
|
return toupper(c1) < toupper(c2) ; |
21 |
|
|
} |
22 |
|
|
static int compare( const char* s1, const char* s2, size_t N ) |
23 |
|
|
{ |
24 |
|
|
return strncasecmp( s1, s2, N ) ; // posix |
25 |
|
|
// mirosoft C++ - use _strnicmp instead |
26 |
|
|
} |
27 |
|
|
static const char* find( const char* s, size_t N, const char& a ) |
28 |
|
|
{ |
29 |
|
|
for ( size_t i=0 ; i<N ; ++i ) |
30 |
|
|
if ( toupper(s[i]) == toupper(a) ) return s+i ; |
31 |
|
|
return 0 ; |
32 |
|
|
} |
33 |
|
|
static bool eq_int_type ( const int_type& c1, const int_type& c2 ) |
34 |
|
|
{ |
35 |
|
|
return toupper(c1) == toupper(c2) ; |
36 |
|
|
} |
37 |
torben |
38 |
}; |
38 |
|
|
|
39 |
|
|
// string preserves case; comparisons are case insensitive |
40 |
|
|
typedef std::basic_string< char, traits_nocase > string_nocase ; |
41 |
|
|
|
42 |
torben |
196 |
// make string_nocase work like a std::string |
43 |
torben |
38 |
// with streams using std::char_traits |
44 |
|
|
// std::basic_istream< char, std::char_traits<char> > (std::istream) and |
45 |
|
|
// std::basic_ostream< char, std::char_traits<char> > (std::ostream) |
46 |
torben |
196 |
inline std::ostream& operator<< ( std::ostream& stm, const string_nocase& str ) |
47 |
|
|
{ |
48 |
|
|
return stm << reinterpret_cast<const std::string&>(str) ; |
49 |
|
|
} |
50 |
torben |
38 |
|
51 |
torben |
196 |
inline std::istream& operator>> ( std::istream& stm, string_nocase& str ) |
52 |
|
|
{ |
53 |
|
|
std::string s ; |
54 |
|
|
stm >> s ; |
55 |
|
|
if (stm) str.assign(s.begin(),s.end()) ; |
56 |
|
|
return stm ; |
57 |
torben |
38 |
} |
58 |
|
|
|
59 |
|
|
inline std::istream& getline( std::istream& stm, string_nocase& str ) |
60 |
torben |
196 |
{ |
61 |
|
|
std::string s ; |
62 |
|
|
std::getline(stm,s) ; |
63 |
|
|
if (stm) str.assign(s.begin(),s.end()) ; |
64 |
|
|
return stm ; |
65 |
torben |
38 |
} |
66 |
|
|
|
67 |
|
|
#endif // __STRING_NOCASE_H__ |