qtcsv/sources/reader.cpp
bool ReaderPrivate::read(const QString& filePath,
                            QList<QStringList>& list,
                            const QString& separator,
                            const QString& textDelimiter,
                            QTextCodec* codec)
{
    if ( false == checkParams(filePath, separator) )
    {
        return false;
    }

    QFile csvFile(filePath);
    if ( false == csvFile.open(QIODevice::ReadOnly | QIODevice::Text) )
    {
        qDebug() << __FUNCTION__ << "Error - can't open file:" << filePath;
        return false;
    }

    QTextStream stream(&csvFile);
    stream.setCodec(codec);

    // This list will contain elements of the row if its elements
    // are located on several lines
    QStringList row;

    ElementInfo elemInfo;
    while ( false == stream.atEnd() )
    {
        QString line = stream.readLine();
        QStringList elements = ReaderPrivate::splitElements(
                  line, separator, textDelimiter, elemInfo);
        if (elemInfo.isEnded)
        {
            // Current row ends on this line. Check if these elements are
            // end elements of the long row
            if (row.isEmpty())
            {
                // No, these elements constitute the entire row
                list << elements;
            }
            else
            {
                // Yes, these elements should be added to the row
                if (false == elements.isEmpty())
                {
                    row.last().append(elements.takeFirst());
                    row << elements;
                }

                list << row;
                row.clear();
            }
        }
        else
        {
            // These elements constitute long row that lasts on several lines
            if (false == elements.isEmpty())
            {
                if (false == row.isEmpty())
                {
                    row.last().append(elements.takeFirst());
                }

                row << elements;
            }
        }
    }

    csvFile.close();

    if (false == elemInfo.isEnded && false == row.isEmpty())
    {
        list << row;
    }

    return true;
}
// Function that really reads csv-file and save it's data as strings to
// QList<QStringList>
// @input:
// - filePath - string with absolute path to csv-file
// - list - refernce to list container where read data will be saved
// - 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 for file reading
// @output:
// - bool - result of read operation
Function that really reads csv-file and save it's data as strings to
QList
Function that really reads csv - file and save it ' s data as strings to QList
bool ReaderPrivate::read(const QString& filePath,
                            QList<QStringList>& list,
                            const QString& separator,
                            const QString& textDelimiter,
                            QTextCodec* codec)
{
    if ( false == checkParams(filePath, separator) )
    {
        return false;
    }
    QFile csvFile(filePath);
    if ( false == csvFile.open(QIODevice::ReadOnly | QIODevice::Text) )
    {
        qDebug() << __FUNCTION__ << "Error - can't open file:" << filePath;
        return false;
    }
    QTextStream stream(&csvFile);
    stream.setCodec(codec);
    QStringList row;
    ElementInfo elemInfo;
    while ( false == stream.atEnd() )
    {
        QString line = stream.readLine();
        QStringList elements = ReaderPrivate::splitElements(
                  line, separator, textDelimiter, elemInfo);
        if (elemInfo.isEnded)
        {
            if (row.isEmpty())
            {
                list << elements;
            }
            else
            {
                if (false == elements.isEmpty())
                {
                    row.last().append(elements.takeFirst());
                    row << elements;
                }
                list << row;
                row.clear();
            }
        }
        else
        {
            if (false == elements.isEmpty())
            {
                if (false == row.isEmpty())
                {
                    row.last().append(elements.takeFirst());
                }
                row << elements;
            }
        }
    }
    csvFile.close();
    if (false == elemInfo.isEnded && false == row.isEmpty())
    {
        list << row;
    }
    return true;
}
bool ReaderPrivate :: read ( const QString & filePath , QList < QStringList > & list , const QString & separator , const QString & textDelimiter , QTextCodec * codec ) { if ( false == checkParams ( filePath , separator ) ) { return false ; } QFile csvFile ( filePath ) ; if ( false == csvFile . open ( QIODevice :: ReadOnly | QIODevice :: Text ) ) { qDebug ( ) << __FUNCTION__ << " " << filePath ; return false ; } QTextStream stream ( & csvFile ) ; stream . setCodec ( codec ) ; QStringList row ; ElementInfo elemInfo ; while ( false == stream . atEnd ( ) ) { QString line = stream . readLine ( ) ; QStringList elements = ReaderPrivate :: splitElements ( line , separator , textDelimiter , elemInfo ) ; if ( elemInfo . isEnded ) { if ( row . isEmpty ( ) ) { list << elements ; } else { if ( false == elements . isEmpty ( ) ) { row . last ( ) . append ( elements . takeFirst ( ) ) ; row << elements ; } list << row ; row . clear ( ) ; } } else { if ( false == elements . isEmpty ( ) ) { if ( false == row . isEmpty ( ) ) { row . last ( ) . append ( elements . takeFirst ( ) ) ; } row << elements ; } } } csvFile . close ( ) ; if ( false == elemInfo . isEnded && false == row . isEmpty ( ) ) { list << row ; } return true ; }
Function that really reads csv-file and save it's data as strings to
QList<QStringList>
Function that really reads csv - file and save it ' s data as strings to QList<QStringList >
// This list will contain elements of the row if its elements // are located on several lines // Current row ends on this line. Check if these elements are // end elements of the long row // No, these elements constitute the entire row // Yes, these elements should be added to the row // These elements constitute long row that lasts on several lines