From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/dictionary/DicCsvWriter.cpp | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/dictionary/DicCsvWriter.cpp (limited to 'src/dictionary/DicCsvWriter.cpp') diff --git a/src/dictionary/DicCsvWriter.cpp b/src/dictionary/DicCsvWriter.cpp new file mode 100644 index 0000000..f2a5e2e --- /dev/null +++ b/src/dictionary/DicCsvWriter.cpp @@ -0,0 +1,110 @@ +#include "DicCsvWriter.h" +#include "Dictionary.h" +#include "DicRecord.h" +#include "Field.h" + +#include + +DicCsvWriter::DicCsvWriter( const Dictionary* aDict ): + m_dict( aDict ) + { + } + +DicCsvWriter::DicCsvWriter( const QList aEntries ): + m_dict( NULL ), m_entries( aEntries ) + { + } + +QString DicCsvWriter::toCsvString( const CsvExportData& aExportData ) + { + m_params = aExportData; + if( m_params.quoteAllFields ) + m_fieldSepRegExp = QRegExp("."); // Any character + else + m_fieldSepRegExp = QRegExp( m_params.fieldSeparators ); // Exact string of separators + + QChar delimiter = m_params.textDelimiter; + QString outStr; + QTextStream outStream( &outStr ); + + // Generate list of selected fields + m_selectedFieldNames.clear(); + if( !m_dict ) // from entries + { + foreach(DicRecord* entry, m_entries ) + foreach( QString fieldName, entry->getFields().keys() ) + if( !m_selectedFieldNames.contains( fieldName ) ) + m_selectedFieldNames << fieldName; + } + else // from dictionary + { + if( !m_params.usedCols.isEmpty() ) + foreach( int col, m_params.usedCols ) + m_selectedFieldNames << m_dict->field(col)->name(); + else // All fields + foreach( Field* field, m_dict->fields() ) + m_selectedFieldNames << field->name(); + + } + + // Write column names + if( m_params.writeColumnNames ) + { + QStringList escapedNames; + foreach( QString name, m_selectedFieldNames ) + { + if( !delimiter.isNull() && name.contains( m_fieldSepRegExp ) ) + name = delimiter + name + delimiter; + escapedNames << name; + } + QString header = QString( m_params.commentChar ) + " "; + header += escapedNames.join( m_params.fieldSeparators ); + outStream << header << endl; + } + + // For dictionary, copy entries into the local list. + if( m_dict ) + m_entries = m_dict->getRecords(); + + // Write entries + bool lastLineWasEmpty = false; + for( int i = 0; i < m_entries.size(); i++ ) + { + QString curLine = dicEntryToString( m_entries.value( i ) ); + if( !(lastLineWasEmpty && curLine.isEmpty()) ) // Don't print several empty lines in a row + outStream << curLine << endl; + lastLineWasEmpty = curLine.isEmpty(); + } + return outStr; + } + +QString DicCsvWriter::dicEntryToString(const DicRecord* aEntry ) const + { + if( !aEntry ) + return QString(); + + QStringList fieldValues; // Convert the fields map into string list. If needed, delimit text ("" or '). + QChar delimiter = m_params.textDelimiter; + foreach( QString key, m_selectedFieldNames ) + { + QString value = aEntry->getFieldValue( key ); + /* If the field has embedded field separator or text delimiter (quote), + * it must be quoted. */ + if( !delimiter.isNull() && ( value.contains( m_fieldSepRegExp ) || value.contains(delimiter) ) ) + { + if( value.contains(delimiter) ) // Embedded text delimiter (") + value.replace( delimiter, QString(delimiter) + delimiter ); // Escape it with double delimiter ("") + value = delimiter + value + delimiter; + } + fieldValues << value; + } + // Remove all last empty fields + while( !fieldValues.isEmpty() && fieldValues.last().isEmpty() ) + fieldValues.removeLast(); + QString res = fieldValues.join( m_params.fieldSeparators ); + + /* FUTURE FEATURE: if( iIsCommented ) + csv.insert( 0, m_params.iCommentChar ); // Insert the comment character */ + return res; + } + -- cgit