How can i add a delimeter to boost::escaped_list_separator?

Viewed 35

I don't what to copy string to trim it later. I parse csv file, my code:

while(std::getline(stream, line))
    {
        boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
        std::for_each(tok.begin(), tok.end(), handler);
        
    }

parseCSV(file, [](const std::string& tok)
    {
        std::vector<SpiceSimulation::DataVector*> arrays;
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        std::cout << "\t-->" << tok << std::endl;
        //std::string cptoken = boost::trim_copy(tok);
        //Read Header Titles
        if(boost::starts_with(tok, "v"))
        {
            std::cout << "START WITH\n";
        }
        
    }); 

My file.csv:

time, vtime2, vtime3, vtime4 ...   

I get results with whitespaces Result: ["time"," vtime2"," vtime3"," vtime4"]

How can I rid of these whitespaces without copying? If I understand right tokenizer return result as basic_string it isn't a copy of original string

1 Answers

The tokenizer function has constructors

explicit escaped_list_separator(Char  e = '\\',
                                Char c = ',',Char  q = '\"')
  : escape_(1,e), c_(1,c), quote_(1,q), last_(false) { }

escaped_list_separator(string_type e, string_type c, string_type q)
  : escape_(e), c_(c), quote_(q), last_(false) { }

You can pass those:

    boost::escaped_list_separator<char> tf("\\", ", ", "\"");
    boost::tokenizer<boost::escaped_list_separator<char>> tok(line, tf);
    std::for_each(tok.begin(), tok.end(), handler);

But it doesn't exactly do what you expect:

Line: "time, vtime2, vtime3, vtime4 ...   "
        -->"time"
        -->""
        -->"vtime2"
START WITH
        -->""
        -->"vtime3"
START WITH
        -->""
        -->"vtime4"
START WITH
        -->"..."
        -->""
        -->""
        -->""

I would do this another way. Parsing != tokenizing. See e.g. https://stackoverflow.com/search?tab=newest&q=user%3a85371%20csv%20parser

Related