views:

49

answers:

2

Is it possibile to ignore punctuacion using std manipulator on cin? For example suppose you have an input stream (in the actual case a file) like: "one, two three". I want to be able to do:

f >> ignore_punct >> a;
f >> ignore_punct >> b;
f >> ignore_punct >> c;

at the end a=="one", b=="two", c=="three".

A: 

There's no standard-library way to do it, but it's pretty easy to do, if I understand you correctly. If you want to read a string until some punctuation as though it were a newline, then you could use a version of getline that accepts a predicate instead of a single delimiter:

template<class F>
std::istream& getline(std::istream& stream, std::string& string, F delim) {

    string.clear();

    // Get characters while the stream is valid and the next character is not the terminating delimiter.
    while (stream && !delim(stream.peek()))
        string += stream.get();

    // Discard delimiter.
    stream.ignore(1);

    return stream;

};

Usage example:

#include <iostream>
#include <cctype>

int main(int argc, char** argv) {

    std::string s;
    getline(std::cin, s, ::ispunct);

    std::cout << s << '\n';
    return 0;

}

If you also want to break on newlines, then you can write a functor:

struct punct_or_newline {
    bool operator()(char c) const { return ::ispunct(c) || c == '\n'; }
};

And invoke as getline(std::cin, my_string, punct_or_newline()) instead. Hope this helps!

Jon Purdy
I would argue that there is a standard way to do it. You would use the codecvt facet on the locale to filter the punctuation before it even got to the main application.
Martin York
That's a fair assessment, but probably overkill. And the OP asked (I think) for solutions based on or similar to `iomanip`.
Jon Purdy
+1  A: 

Try this:

It uses the local to filter out punctuation.
This allows the rest of the code to remain unchanged.

#include <locale>
#include <string>
#include <iostream>
#include <fstream>
#include <cctype>

class PunctRemove: public std::codecvt<char,char,std::char_traits<char>::state_type>
{
    bool do_always_noconv() const throw()  { return false;}
    int do_encoding()       const throw()  { return true; }

    typedef std::codecvt<char,char,std::char_traits<char>::state_type> MyType;
    typedef MyType::state_type          state_type;
    typedef MyType::result              result;


    virtual result  do_in(state_type& s,
            const char* from,const char* from_end,const char*& from_next,
                  char* to,        char* to_limit,      char*& to_next  ) const
    {
        /*
         * This function is used to filter the input
         */
        for(from_next = from, to_next = to;from_next != from_end;++from_next)
        {
            if (!std::ispunct(*from_next))
            {
                *to_next = *from_from;
                ++to_next;
            }
        }
        return ok;
    }

    /*
     * This function is used to filter the output
     */
    virtual result do_out(state_type& state,
            const char* from, const char* from_end, const char*& from_next,
                  char* to,         char* to_limit,       char*& to_next  ) const
    { /* I think you can guess this */ }
};


int main()
{
    // stream must be imbued before they are opened.
    // Otherwise the imbing is ignored.
    //
    std::ifstream   data;
    data.imbue(std::locale(std::locale(), new PunctRemove));
    data.open("plop");
    if (!data)
    {
        std::cout << "Failed to open plop\n";
        return 1;
    }

    std::string         line;
    std::getline(data, line);
    std::cout << "L(" << line << ")\n";
}
Martin York