31 lines
616 B
C++
31 lines
616 B
C++
|
|
#include "csvwriter.h"
|
|||
|
|
|
|||
|
|
CsvWriter::CsvWriter()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//int complexField = (strchr(field, csvWriter->delimiter_) || strchr(field, '\n') || strchr(field, '\"')) ? 1 : 0;
|
|||
|
|
|
|||
|
|
void CsvWriter::writeField(QString field) {
|
|||
|
|
// if field contains any of seperator quote or newline then it needs to be quoted
|
|||
|
|
// when quoted quotes need to be doubled to escape them
|
|||
|
|
bool needs_quotes = false;
|
|||
|
|
int new_len = field.length();
|
|||
|
|
for (auto ch : field) {
|
|||
|
|
if (ch == m_quote) {
|
|||
|
|
++new_len;
|
|||
|
|
}
|
|||
|
|
if (ch == '\n' || ch == m_seperator || ch == m_quote) {
|
|||
|
|
needs_quotes = true;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (needs_quotes) {
|
|||
|
|
new_len += 2;
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|