jbattini / fast-cpp-csv-parser

Automatically exported from code.google.com/p/fast-cpp-csv-parser
0 stars 0 forks source link

new line character as single line comment do not work. #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I have a CSV file with some extra new lines (i.e. the lines with data are 
separated with an empty line), so I wanted to use the single line comment 
feature to ignore the lines starting with '\n'
The files look like this

header1,header2
2,2

3,3

4,4

instead of 

header1,header2
2,2
3,3
4,4

2. io::CSVReader<NCOL,io::trim_chars<' 
'>,io::no_quote_escape<','>,io::single_line_comment<'\r','\n'> >in(filename);
3. try reading the file in question

What is the expected output? What do you see instead?

I would expect the empty lines to be ignored, but I get "Too few columns in 
line 3 in file" as an error

What version of the product are you using? On what operating system?

Current version, on Mac OS X mountain lion

Please provide any additional information below.

Original issue reported on code.google.com by pier.mo...@gmail.com on 17 Dec 2013 at 9:39

GoogleCodeExporter commented 8 years ago
The parameter to the single_line_comment template is the character with which 
the comment line should start. Your lines are empty and thus do not start with 
'\r' nor with '\n'. Because internal C-strings are used, the first char in the 
line is a '\0' and thus

io::CSVReader<NCOL,io::trim_chars<' 
'>,io::no_quote_escape<','>,io::throw_on_overflow,io::single_line_comment<'\0'> 
>in(filename);

does the trick if your lines are perfectly empty. If you also want to ignore 
lines that contain only spaces then you have to write your own policy:

struct empty_line_comment{

  static bool is_comment(const char*line){
    while(*line == ' ' || *line == '\t'){
      ++line;
      if(*line == 0)
        return true;
    }
    return false;
  }
};

and use it as following:

io::CSVReader<NCOL,io::trim_chars<' 
'>,io::no_quote_escape<','>,io::throw_on_overflow,empty_line_comment>in(filename
);

Original comment by strasser...@gmail.com on 8 Apr 2014 at 1:45

GoogleCodeExporter commented 8 years ago
Typo:

struct empty_line_comment{
  static bool is_comment(const char*line){
    if(*line == '\0')
      return true;
    while(*line == ' ' || *line == '\t'){
      ++line;
      if(*line == 0)
        return true;
    }
    return false;
  }
};

Original comment by strasser...@gmail.com on 8 Apr 2014 at 1:48