* \brief Parses 'input' and stores the CSV data into the given `CsvDataStorage` object. * 'definition' tells what CSV dialect is used. * The vec-of-vec gets enlarged while ensuring all vectors have the same length. * IMPORTANT: Update any variables afterwards that store the dimension of the table. * * \param input The stream that should get parsed * \param storage The `CsvDataStorage` obj
| 56 | |
| 57 | */ |
| 58 | std::map<long,long> CsvParser::parseCsvStream( std::istream *input, CsvDataStorage &storage, CsvDefinition *definition, int maxLines, bool resizeRows ) { |
| 59 | std::string line; |
| 60 | std::stringstream sstr; |
| 61 | long act_rows = 0; |
| 62 | long act_cols = 0; |
| 63 | std::vector<std::string> vec; |
| 64 | std::map<long,long> rowLengths; |
| 65 | |
| 66 | // skip bomBytes |
| 67 | input->ignore(definition->bomBytes); |
| 68 | |
| 69 | // read lines from istream `input` into `line` |
| 70 | while( myGetlineEncodings( *input, line, definition->encoding) ) { |
| 71 | if( parseCsvState == CSVPARSER_CONST_NOT_ENCLOSED ) { |
| 72 | // if this line has been fully parsed, clear previous line vector |
| 73 | vec.clear(); |
| 74 | } |
| 75 | |
| 76 | // translate encodings to valid UTF-8 |
| 77 | switch( definition->encoding ) { |
| 78 | case CsvDefinition::ENC_NONE: |
| 79 | case CsvDefinition::ENC_UTF8: |
| 80 | Helper::fixUtf8(line); // replaces invalid chars with replacement char |
| 81 | break; |
| 82 | case CsvDefinition::ENC_Latin1: |
| 83 | line = Helper::latin1toutf8(line); |
| 84 | break; |
| 85 | case CsvDefinition::ENC_Win1252: |
| 86 | line = Helper::win1252toutf8(line); |
| 87 | break; |
| 88 | case CsvDefinition::ENC_UTF16LE: // translation happens in myGetlineEncodings() |
| 89 | case CsvDefinition::ENC_UTF16BE: // translation happens in myGetlineEncodings() |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | // parse `line` and put cells into `vec` |
| 94 | parseCsvLine(vec, line, definition); |
| 95 | if( parseCsvState == CSVPARSER_CONST_NOT_ENCLOSED ) { |
| 96 | // if this line has been fully parsed |
| 97 | |
| 98 | // calculate histogram data |
| 99 | size_t vecSize = vec.size(); |
| 100 | if( input->rdstate() != std::ios_base::eofbit ) { |
| 101 | // only record when we're not at the last line |
| 102 | rowLengths[vecSize]++; |
| 103 | } |
| 104 | |
| 105 | // resize rows |
| 106 | if( resizeRows && vec.size() ) { |
| 107 | if( act_cols < (long)vec.size() ) { |
| 108 | // parsed line is longer than columns(): resize storage |
| 109 | storage.resize(0, (long)vec.size()); |
| 110 | } else if( act_cols > (long)vec.size() ) { |
| 111 | // die aktuelle Zeile ist kürzer als die bisherigen Zeilen: die aktuelle Zeile verlängern |
| 112 | vec.resize( act_cols ); |
| 113 | } |
| 114 | act_cols = std::max( act_cols, (long)vec.size() ); |
| 115 | } |
no test coverage detected