Author: Kagula
Env: VS2013Update4 QT5.4
#ifndef _ProductInfoTableStruct_H_
#define _ProductInfoTableStruct_H_
#pragma once
#include <QtCore>
#include <map>
#include <vector>
namespace kagula
{
struct ProdInfoTableCSVStruct
{
void SetData(QString fileName);
QString _version;
std::vector<QString> _title;
std::vector<QString> _titleDesp;
std::vector<std::vector<QString>> _body;
};
}
#endif
#include "ProdInfoTableCSVStruct.h"
namespace kagula
{
void ProdInfoTableCSVStruct::SetData(QString fileName)
{
//read data from readonly file
QString data;
QStringList rowOfData;
QStringList colOfData;
qDebug() << "CSV filename is " << fileName;
rowOfData.clear();
colOfData.clear();
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
in.setCodec("GBK");
while (!in.atEnd())
{
//Get line
QString line = in.readLine();
line = line + "\n";
data = data + line;
}
file.close();
//get data from local file end!
//extract rowOfData.begin
int doubleQuote = 0;
for (long long i = 0; i < data.size(); i++)
{
if (data.at(i) == "\"")
{
doubleQuote++;
continue;
}
if (data.at(i) == "\n" && doubleQuote % 2 == 0)
{
QString line = data.mid(0, i);
data.remove(0, i);
rowOfData.push_back(line);
i = 0;
}
}
//comment below, because linefeed symbol may be between a pair of double quotes.
//rowOfData = data.split("\n");
//extract rowOfData.end
//how many records
if (rowOfData.size() > 3)
{
_body.resize(rowOfData.size() - 3);
}
//parse body.
for (int rowIndex = 0; rowIndex < rowOfData.size(); rowIndex++)
{
//extract to colOfData.begin
{
colOfData.clear();
int doubleQuote = 0;
//colOfData = rowOfData.at(rowIndex).split(";");
QString data = rowOfData.at(rowIndex);
for (int i = 0; i < data.size(); i++)
{
if (data.at(i) == "\"")
{
doubleQuote++;
continue;
}
if ( (data.at(i) == "\t" || data.at(i)==",") && doubleQuote % 2 == 0)
{
QString col = data.mid(0, i);
data.remove(0, i);
col = col.trimmed();
if (col.length() > 1 && col[0] == ',')
{
col.remove(0, 1);
}
colOfData.push_back(col);
i = 0;
}
}
}
//extract to colOfData.end
//get column number
if (rowIndex >= 3)
{
_body[rowIndex - 3].resize(colOfData.size());
}
//
for (int colIndex = 0; colIndex < colOfData.size(); colIndex++)
{
if (rowIndex == 0)
{
//first line, read version info.
if (colIndex == 0)
{
_version = colOfData[colIndex];
}
}
else if (rowIndex == 1)
{
//second line, read field name.
QString fieldName = colOfData[colIndex];
fieldName = fieldName.trimmed();
_title.push_back(fieldName);
}
else if (rowIndex == 2)
{
//third line, read field name's description.
QString fieldV = colOfData[colIndex];
_titleDesp.push_back(fieldV);
}
else if (rowIndex >= 3)
{
//if more than 3, read data body.
//remove quote symbol at head and tail if it's exist.
QString fieldV = colOfData[colIndex];
if (fieldV.size() > 1)
{
if (fieldV.at(0) == "\"")
{
fieldV.remove(0, 1);
}
if (fieldV.at(fieldV.size() - 1) == "\"")
{
fieldV.remove(fieldV.size() - 1, 1);
}
}
_body[rowIndex - 3][colIndex] = fieldV;
}//end else if
}//end for
}//end for
}//end func
}//end namespace