bool text_helper::firstColumnIsIncrimental(QTextStream &stream, const QString &delimiter) { qint64 streamStartingPosition = stream.pos(); double firstColumnValue, prevFirstColumnValue = 0; bool firstColumnIsIncrimental = true; QString line = QString(); QStringList strings; bool ok; //Read out the first line (possibly header) line = QString(stream.readLine()); while (!stream.atEnd()) { line = QString(stream.readLine()); QStringList strings = line.split(delimiter); //Lets determine if we can use the first column as an X-axis firstColumnValue = strings.value(0).toDouble(&ok); if (!ok || (firstColumnValue < prevFirstColumnValue)) { firstColumnIsIncrimental = false; break; } prevFirstColumnValue = firstColumnValue; } //Reposition the stream stream.seek(streamStartingPosition); return firstColumnIsIncrimental; }
void Entity::handleTag( const Comment* openingTag, const Comment* closingTag, QTextStream& file, QIODevice& output) { file.seek(openingTag->getCommentEnd()+1); while (!file.atEnd()) { qint64 pos=file.pos(); if (pos>=closingTag->getCommentStart()){ file.seek(closingTag->getCommentEnd()+1); break; } QChar c1; file >> c1; const Comment* p= isSpecial(pos); if (p!=nullptr){ if (p->isAutoClosing()){ // output.write(p->comment); // output.write("<!--REMOVESTART-->"); p->output(this,output); // output.write("<!--REMOVEEND-->"); } else { const Comment* closingTag=findClosingTag(p,file); if (closingTag != nullptr){ QBuffer buf; buf.open(QBuffer::WriteOnly|QBuffer::Text); handleTag(p,closingTag,file,buf); buf.close(); if (false /*p->getTag()==STYLE_START*/){ //embedded_styles.append(buf.buffer()); } else { output.write(buf.buffer()); } } } } else if (isInOuput(pos)){ if (openingTag->isHTML() && c1=='\n'){ output.write(QString("<BR/>").toUtf8()); } else { if (!c1.isNonCharacter()) output.write(QString(c1).toUtf8()); } } } return ; }
void text_helper::checkAndProcessColumnHeaders( QTextStream &stream, const QString &delimiter, QList<QVariantMap> &metaData, int firstDataColumn ) { qint64 streamStartingPosition = stream.pos(); QString line = QString(stream.readLine()); QStringList strings = line.split(delimiter); bool headerFound = true; int column; QVariantMap variantMap; //Check if the first line is not a header (only contains spaces, numbers, decimal points) QRegExp re("^[ .0-9]*$"); if (re.exactMatch(line)) { //reset stream and return stream.seek(streamStartingPosition); headerFound = false; } for (column = firstDataColumn; column < strings.size(); column++) { variantMap.clear(); if (headerFound) { if (strings.value(column).size() != 0) { variantMap["Key Field"] = strings.value(column); variantMap["Data Source"] = column; } } else { //Use column number as the header variantMap["Key Field"] = QString(tr("Column ")) + QString::number(column); variantMap["Data Source"] = column; } if (!variantMap.isEmpty()) metaData.append(variantMap); } }
QString text_helper::autoDetectDelimiter(QTextStream &stream) { const QString delimiters[] = {"\t", ",", " "}; qint64 streamStartingPosition = stream.pos(); QVector<QString> possibleDelimiters; QString line = QString(); QStringList strings; int i; while (!stream.atEnd() && (possibleDelimiters.size() != 1)) { line = QString(stream.readLine()); possibleDelimiters.clear(); if (line.isEmpty()) continue; for (i = 0; i < 3; i++) { strings.clear(); strings = line.split(delimiters[i]); if ( strings.size() != 1 ) { possibleDelimiters.append(delimiters[i]); } } } //qDebug() << "Best guess from auto delim format->" << possibleDelimiters.first() << possibleDelimiters.size(); //Reposition the stream stream.seek(streamStartingPosition); if (!possibleDelimiters.empty()) { return possibleDelimiters.first(); } //Something went wrong, lets pick whatever is first and run with it. Maybe there is only 1 column return delimiters[0]; }
bool CSVParser::parse(QTextStream& stream, qint64 nMaxRecords) { m_vCSVData.clear(); m_nColumns = 0; ParseStates state = StateNormal; QString fieldbuf; QStringList record; if(m_pCSVProgress) m_pCSVProgress->start(); while(!stream.atEnd()) { QString sBuffer = stream.read(m_nBufferSize); for(QString::iterator it = sBuffer.begin(); it != sBuffer.end(); ++it) { QChar c = *it; switch(state) { case StateNormal: { if(c == m_cFieldSeparator) { addColumn(record, fieldbuf, m_bTrimFields); } else if(c == m_cQuoteChar) { state = StateInQuote; } else if(c == '\r') { // look ahead to check for linefeed QString::iterator nit = it + 1; // In order to check what the next byte is we must make sure that that byte is already loaded. Assume we're at an m_nBufferSize // boundary but not at the end of the file when we hit a \r character. Now we're going to be at the end of the sBuffer string // because of the m_nBufferSize boundary. But this means that the following check won't work properly because we can't check the // next byte when we really should be able to do so because there's more data coming. To fix this we'll check for this particular // case and, if this is what's happening, we'll just load an extra byte. if(nit == sBuffer.end() && !stream.atEnd()) { // Load one more byte sBuffer.append(stream.read(1)); // Restore both iterators. sBuffer.end() points to the imagined char after the last one in the string. So the extra byte we've // just loaded is the one before that, i.e. the actual last one, and the original last char is the one before that. it = sBuffer.end() - 2; nit = sBuffer.end() - 1; } // no linefeed, so assume that CR represents a newline if(nit != sBuffer.end() && *nit != '\n') { addColumn(record, fieldbuf, m_bTrimFields); addRow(record); } } else if(c == '\n') { addColumn(record, fieldbuf, m_bTrimFields); addRow(record); } else { fieldbuf.append(c); } } break; case StateInQuote: { if(c == m_cQuoteChar) { state = StateEndQuote; } else { fieldbuf.append(c); } } break; case StateEndQuote: { if(c == m_cQuoteChar) { state = StateInQuote; fieldbuf.append(c); } else if(c == m_cFieldSeparator) { state = StateNormal; addColumn(record, fieldbuf, m_bTrimFields); } else if(c == '\n') { state = StateNormal; addColumn(record, fieldbuf, m_bTrimFields); addRow(record); } else if(c == '\r') { // look ahead to check for linefeed QString::iterator nit = it + 1; // See above for details on this. if(nit == sBuffer.end() && !stream.atEnd()) { sBuffer.append(stream.read(1)); it = sBuffer.end() - 2; nit = sBuffer.end() - 1; } // no linefeed, so assume that CR represents a newline if(nit != sBuffer.end() && *nit != '\n') { addColumn(record, fieldbuf, m_bTrimFields); addRow(record); } } else { state = StateNormal; fieldbuf.append(c); } } break; } if(nMaxRecords != -1 && m_vCSVData.size() >= nMaxRecords) return true; } if(m_pCSVProgress && m_vCSVData.size() % 100 == 0) { if(!m_pCSVProgress->update(stream.pos())) return false; } } if(!record.isEmpty()) { addColumn(record, fieldbuf, m_bTrimFields); addRow(record); } if(m_pCSVProgress) m_pCSVProgress->end(); return state == StateNormal; }
/*! \brief Load opt file \param in QTextStream which holds the opt.txt file NOTICE(panqing): Before loading opt data, dat and prn data are already loaded. mainEdgeAttrList is filled. TODO(panqing): showNameList and attrNameList are needed to be set */ int DataIO::loadOptData(QTextStream &in) { QString line; // One line of the file QStringList lineList; // Items in one line, seperated by some delimiters const int lengthThreshold = 10; // Recognize one line as a recording by its length quint16 xPos, yPos, halfSceneWidth, halfSceneHeight, maxX=0, maxY=0, minX=50000, minY=50000; quint32 subSegPartID = 0, lastSubSegPartID = 0; while (!(line = in.readLine()).contains("SegmentId")) ; qint64 filePos=in.pos(); // 当前文件位置 while ((line = in.readLine()).length() >= lengthThreshold) { lineList = line.split(",", QString::SkipEmptyParts); // 先遍历所有血管段一遍,得到最大/最小坐标 xPos = lineList.at(5).toInt(); yPos = lineList.at(6).toInt(); if (xPos>maxX) maxX=xPos; if (yPos>maxY) maxY=yPos; if (xPos<minX) minX=xPos; if (yPos<minY) minY=yPos; xPos = lineList.at(9).toInt(); yPos = lineList.at(10).toInt(); if (xPos>maxX) maxX=xPos; if (yPos>maxY) maxY=yPos; if (xPos<minX) minX=xPos; if (yPos<minY) minY=yPos; } halfSceneWidth=(maxX-minX)/2; halfSceneHeight=(maxY-minY)/2; ResManager::getSceneRect().setRect(0,0,halfSceneWidth*2*1.1,halfSceneHeight*2*1.1); in.seek(filePos); while ((line = in.readLine()).length() >= lengthThreshold) { EdgeAttr *edgeAttr = NULL; lineList = line.split(",", QString::SkipEmptyParts); quint16 segName = lineList.at(0).toInt(); for (int i=0;i<edgeAttrList.size();++i) { if (segName==edgeAttrList[i]->getIntAttr(EdgeAttr::SEGNAME)) { edgeAttr=edgeAttrList[i]; break; } } lastSubSegPartID = subSegPartID; // Store the last sub segment part id subSegPartID = lineList.at(1).toInt(); // Acquire the sub segment part id of the new line // The last sub segment part id is 99 // Change the last sub segment part id to its previous id plus 1 // For example, 0, 1, 2, 99 --> 0, 1, 2, 3 if (subSegPartID == 99) subSegPartID = lastSubSegPartID + 1; quint32 startNodeIndex, endNodeIndex; // StartNode // if the node is an subNodeItem, the index of the node is set as coeff*subIndex+nodeIndex if (lineList.at(4).toInt() >= 0) startNodeIndex = lineList.at(4).toInt(); else startNodeIndex = lineList.at(0).toInt() + coeff*subSegPartID; if (!nodeHash.contains(startNodeIndex)) { xPos = lineList.at(5).toInt(); yPos = lineList.at(6).toInt(); if (startNodeIndex < coeff) { MainNodeItem *mainNodeItem = new MainNodeItem(NULL, startNodeIndex); mainNodeItem->setPos(xPos-halfSceneWidth-minX, yPos-halfSceneHeight-minY); nodeHash.insert(startNodeIndex, mainNodeItem); } else { SubNodeItem *subNodeItem = new SubNodeItem(NULL, startNodeIndex); subNodeItem->setPos(xPos-halfSceneWidth-minX, yPos-halfSceneHeight-minY); nodeHash.insert(startNodeIndex, subNodeItem); } } // EndNode if (lineList.at(8).toInt() >= 0) endNodeIndex = lineList.at(8).toInt(); else endNodeIndex = lineList.at(0).toInt() + coeff*(1+subSegPartID); if (!nodeHash.contains(endNodeIndex)) { xPos = lineList.at(9).toInt(); yPos = lineList.at(10).toInt(); if (endNodeIndex < coeff) { MainNodeItem *mainNodeItem = new MainNodeItem(NULL, endNodeIndex); mainNodeItem->setPos(xPos-halfSceneWidth-minX, yPos-halfSceneHeight-minY); nodeHash.insert(endNodeIndex, mainNodeItem); } else { SubNodeItem *subNodeItem = new SubNodeItem(NULL, endNodeIndex); subNodeItem->setPos(xPos-halfSceneWidth-minX, yPos-halfSceneHeight-minY); nodeHash.insert(endNodeIndex, subNodeItem); } } SubEdgeItem *subEdgeItem = new SubEdgeItem(nodeHash[startNodeIndex], nodeHash[endNodeIndex], edgeAttr); edgeItemList.append(subEdgeItem); subEdgeItem->setEdgeAttr(edgeAttr); } return 0; }