Function that really reads csv-data and transfer it's data to AbstractProcessor-based processor @input: - ioDevice - IO Device containing the csv-formatted data - processor - refernce to AbstractProcessor-based object - separator - string or character that separate values in a row - textDelimiter - string or character that enclose row elements - codec - pointer to codec object that would be used f
| 80 | // @output: |
| 81 | // - bool - result of read operation |
| 82 | bool ReaderPrivate::read( |
| 83 | QIODevice& ioDevice, |
| 84 | Reader::AbstractProcessor& processor, |
| 85 | const QString& separator, |
| 86 | const QString& textDelimiter, |
| 87 | const QStringConverter::Encoding codec) |
| 88 | { |
| 89 | if (!checkParams(separator)) { return false; } |
| 90 | |
| 91 | // Open IO Device if it was not opened |
| 92 | if (!ioDevice.isOpen() && !ioDevice.open(QIODevice::ReadOnly)) { |
| 93 | qDebug() << __FUNCTION__ << "Error - failed to open IO Device"; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | QTextStream stream(&ioDevice); |
| 98 | stream.setEncoding(codec); |
| 99 | |
| 100 | // This list will contain elements of the row if its elements |
| 101 | // are located on several lines |
| 102 | QList<QString> row; |
| 103 | |
| 104 | ElementInfo elemInfo; |
| 105 | auto result = true; |
| 106 | while (!stream.atEnd()) { |
| 107 | auto line = stream.readLine(); |
| 108 | processor.preProcessRawLine(line); |
| 109 | auto elements = ReaderPrivate::splitElements( |
| 110 | line, separator, textDelimiter, elemInfo); |
| 111 | if (elemInfo.isEnded) { |
| 112 | // Current row ends on this line. Check if these elements are |
| 113 | // end elements of the long row |
| 114 | if (row.isEmpty()) { |
| 115 | // No, these elements constitute the entire row |
| 116 | if (!processor.processRowElements(elements)) { |
| 117 | result = false; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | else { |
| 122 | // Yes, these elements should be added to the row |
| 123 | if (!elements.isEmpty()) { |
| 124 | row.last().append(elements.takeFirst()); |
| 125 | row << elements; |
| 126 | } |
| 127 | |
| 128 | if (!processor.processRowElements(row)) { |
| 129 | result = false; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | row.clear(); |
| 134 | } |
| 135 | } |
| 136 | else { |
| 137 | // These elements constitute long row that lasts on several lines |
| 138 | if (!elements.isEmpty()) { |
| 139 | if (!row.isEmpty()) { |
no test coverage detected