Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   can't stream cast from a case insensitive string (http://www.velocityreviews.com/forums/t754722-cant-stream-cast-from-a-case-insensitive-string.html)

aaragon 10-09-2011 01:23 PM

can't stream cast from a case insensitive string
 
Hi all,

I am trying to come up with a case insensitive string, and I found the
following on the web

http://www.gotw.ca/gotw/029.htm

So basically my code to come up with a case insensitive string is as
follows



/// Traits class for a case insensitive string class
struct ci_char_traits : public std::char_traits<char> {

static bool eq( char c1, char c2 )
{ return toupper(c1) == toupper(c2); }

static bool ne( char c1, char c2 )
{ return toupper(c1) != toupper(c2); }

static bool lt( char c1, char c2 )
{ return toupper(c1) < toupper(c2); }

static int compare(const char* s1, const char* s2, size_t n )
{ return memicmp( s1, s2, n ); }

private:
static int memicmp(const void *s1, const void *s2, size_t n) {

if (n != 0) {
const unsigned char *p1 = (const unsigned char *)s1, *p2 =
(const unsigned char *)s2;
do {
if (toupper(*p1) != toupper(*p2))
return (*p1 - *p2);
p1++;
p2++;
} while (--n != 0);
}
return 0;
}
};

/// case insensitive string type definition
typedef std::basic_string<char, ci_char_traits> ci_string;


/// provide standard output for case insensitive string
template<typename char_type, typename traits_type, typename
allocator_type>
inline std::basic_ostream<char_type, traits_type>&
operator<<(std::basic_ostream<char_type, traits_type>& os,
const std::basic_string<char_type, ci_char_traits,
allocator_type>& str) {
return std::__ostream_insert(os, str.data(), str.size());
}


so the type definition is the string. Now the problem I have, is that
I can't get a function to work with this custom string as it works
with a regular string. The following template function gets a value
from the string, but the


template <typename T, class string_type, typename counter_type>
T stream_cast(const string_type& s) {

typedef typename string_type::value_type char_type;
typedef typename string_type::traits_type traits_type;

typename std::basic_istringstream<char_type, traits_type> iss(s);

T x;
char c;
if (!(iss >> x) || iss.get(c))
cout<<"*** ERROR *** Bad Conversion!"<<endl;
return x;
}

I call this function to get a double from a string as follows:

ci_string str("2.0");
double test = stream_cast<double>(str);

But something is wrong with my definition of case insensitive string
because the evaluation of the stream object through operator! always
fails (the line !(iss >> x) is always true for this string type).

Does someone know why I'm having this problem? Thanks in advance for
taking the time to read this long post.

aa



All times are GMT. The time now is 07:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.