QString QCSVParser::columnName(int column) const { if (column >= this->columnCount()) throw QException(QObject::tr("Indice trop grand")); return this->header().at(column); }
void QCSVParser::parse(QFile &file, const QChar &separator, QString::SplitBehavior behavior) { /* Try to open the file * The QIODevice::Text flag passed to open() tells Qt to convert Windows- * style line terminators ("\r\n") into C++-style terminators ("\n") */ if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) throw QException(QObject::tr("Impossible d'ouvrir le fichier ") + file.fileName()); // Open succed this->_fileName = file.fileName(); this->_separator = separator; this->_behavior = behavior; this->_content.clear(); // Read file line by line QString line; while(!file.atEnd()) { line = file.readLine(); this->_content.push_back( line.split(this->_separator, this->_behavior).toVector()); } file.close(); }
QCSVRow& QCSVParser::row(int index) { if (index >= this->rowCount()) throw QException(QObject::tr("Indice trop grand")); return this->_content[index]; }
int QCSVParser::columnIndice(const QString& columnName) const { QCSVRow header = this->header(); for (int i(0); i < header.count(); ++i) if (header.at(i) == columnName) return i; throw QException(QObject::tr("Column not found")); }
void QCSVParser::save(void) const { QFile file(this->_fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) throw QException(QObject::tr("Impossible d'ouvrir le fichier")); // Write all the line QTextStream out(&file); foreach (QCSVRow row, this->_content) this->writeRow(out, row); file.close(); }
QCSVColumn& QCSVParser::column(int index) { if (index >= this->columnCount()) throw QException(QObject::tr("Indice trop grand")); this->_column.clear(); for (int i(1); i < this->rowCount(); ++i) // skeep the header this->_column.push_back(this->_content[i][index]); return this->_column; /* QCSVColumn col; foreach (QCSVRow row, this->_content) col.push_back(row.at(index)); return col; */ }
static QDomDocument *loadXml(const QString &path, const QString &docName) { QFile file(path); if (file.open(QIODevice::ReadOnly | QIODevice::Text) == false) { printf("Error opening file %s: %s \n",path.toUtf8().constData(),strerror(errno)); throw QException(); return NULL; } QDomDocument *doc = new QDomDocument(docName); bool setContentResult = doc->setContent(&file); file.close(); if (setContentResult == false) { return NULL; } return doc; }
//------------------------------------------------------------------------------ static void foo() { throw QException("MODULE", "a problem", "a cause"); }