can't stream cast from a case insensitive string

Discussion in 'C++' started by aaragon, Oct 9, 2011.

  1. aaragon

    aaragon Guest

    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
    aaragon, Oct 9, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tee
    Replies:
    3
    Views:
    7,768
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Guoqi Zheng

    string.indexof case insensitive

    Guoqi Zheng, Jul 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    12,882
    bentangle
    Jan 25, 2009
  3. Andrew
    Replies:
    2
    Views:
    1,592
    Mike Schilling
    Aug 15, 2004
  4. Replies:
    1
    Views:
    2,457
    Mark P
    Apr 6, 2007
  5. Xah Lee
    Replies:
    4
    Views:
    933
Loading...

Share This Page