void PdmlReader::readField(PdmlProtocol *pdmlProto, OstProto::Protocol *pbProto) { Q_ASSERT(isStartElement() && name() == "field"); // fields with "hide='yes'" are informational and should be skipped if (attributes().value("hide") == "yes") { skipElement(); return; } QString fieldName = attributes().value("name").toString(); qDebug(" fieldName:%s", fieldName.toAscii().constData()); pdmlProto->fieldHandler(fieldName, attributes(), pbProto, currentStream_); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (name() == "proto") { // Since we are in the midst of processing a protocol, we // end it prematurely before we start processing the // embedded protocol // int endPos = -1; if (!attributes().value("pos").isEmpty()) endPos = attributes().value("pos").toString().toInt(); pdmlProto->prematureEndHandler(endPos, pbProto, currentStream_); pdmlProto->postProtocolHandler(pbProto, currentStream_); StreamBase s; s.protoDataCopyFrom(*currentStream_); expPos_ = s.frameProtocolLength(0); readProto(); } else if (name() == "field") readField(pdmlProto, pbProto); else skipElement(); } } }
bool XmlReader::readPlacement(const char* tag, Placement* val) { if (isTag(name(), tag)) { while (read()) { if (nodeType() == XML_READER_TYPE_TEXT) { MString8 t = value(); if (t == "auto") *val = PLACE_AUTO; else if (t == "above") *val = PLACE_ABOVE; else if (t == "below") *val = PLACE_BELOW; else if (t == "left") *val = PLACE_LEFT; skipElement((const xmlChar*)tag); return true; } else if ((nodeType() == XML_READER_TYPE_END_ELEMENT) && isTag(name(), tag)) { *val = PLACE_AUTO; return true; } else printf("?readInt: node type %d <%s>\n", nodeType(), name()); } } return false; }
void XmlReader::unknown() { printf("xmlReader: unknown <%s> at %d\n", name(), line()); xmlChar* dname = (xmlChar*)strdup((const char*)name()); skipElement(dname); free(dname); }
bool PViewData::writeTXT(const std::string &fileName) { FILE *fp = Fopen(fileName.c_str(), "w"); if(!fp){ Msg::Error("Unable to open file '%s'", fileName.c_str()); return false; } for(int step = 0; step < getNumTimeSteps(); step++){ for(int ent = 0; ent < getNumEntities(step); ent++){ for(int ele = 0; ele < getNumElements(step, ent); ele++){ if(skipElement(step, ent, ele)) continue; for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++){ double x, y, z; getNode(step, ent, ele, nod, x, y, z); fprintf(fp, "%d %.16g %d %d %.16g %.16g %.16g ", step, getTime(step), ent, ele, x, y, z); for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++){ double val; getValue(step, ent, ele, nod, comp, val); fprintf(fp, "%.16g ", val); } } fprintf(fp, "\n"); } } } fclose(fp); return true; }
bool PViewData::writeSTL(const std::string &fileName) { FILE *fp = Fopen(fileName.c_str(), "w"); if(!fp){ Msg::Error("Unable to open file '%s'", fileName.c_str()); return false; } if(!getNumTriangles() && !getNumQuadrangles()){ Msg::Error("No surface elements to save"); fclose(fp); return false; } int step = getFirstNonEmptyTimeStep(); fprintf(fp, "solid Created by Gmsh\n"); for(int ent = 0; ent < getNumEntities(step); ent++){ for(int ele = 0; ele < getNumElements(step, ent); ele++){ if(getDimension(step, ent, ele) != 2) continue; if(skipElement(step, ent, ele)) continue; int N = getNumNodes(step, ent, ele); if(N != 3 && N != 4) continue; double x[4], y[4], z[4], n[3]; for(int i = 0; i < N; i++) getNode(step, ent, ele, i, x[i], y[i], z[i]); normal3points(x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2], n); if(N == 3){ fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); } else{ fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]); fprintf(fp, " outer loop\n"); fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]); fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]); fprintf(fp, " vertex %g %g %g\n", x[3], y[3], z[3]); fprintf(fp, " endloop\n"); fprintf(fp, "endfacet\n"); } } } fprintf(fp, "endsolid Created by Gmsh\n"); fclose(fp); return true; }
std::string JSON::toString() const { ElementType type = getType(); if (type == TYPE_STRING) return getString(); else { Pos pos = skipElement(); return std::string(ptr_begin, pos - ptr_begin); } }
qreal XmlReader::readReal(const char* tag) { if (read()) { if (nodeType() == XML_READER_TYPE_TEXT) { qreal val = matof((const char*)value()); skipElement((const xmlChar*)tag); return val; } else if (nodeType() == XML_READER_TYPE_END_ELEMENT) return 0; else printf("?readInt: node type %d <%s>\n", nodeType(), name()); } return false; }
JSON::iterator & JSON::iterator::operator++() { Pos pos = skipElement(); checkPos(pos); if (*pos != ',') ptr_begin = nullptr; else { ++pos; checkPos(pos); ptr_begin = pos; } return *this; }
bool XmlReader::readReal(const char* tag, qreal* val) { if (isTag(name(), tag)) { if (read()) { if (nodeType() == XML_READER_TYPE_TEXT) { *val = (qreal)matof((const char*)value()); skipElement((const xmlChar*)tag); return true; } else if (nodeType() == XML_READER_TYPE_END_ELEMENT) return false; else printf("?readInt: node type %d <%s>\n", nodeType(), name()); } } return false; }
void PdmlReader::skipElement() { Q_ASSERT(isStartElement()); qDebug("skipping element - <%s>", name().toString().toAscii().constData()); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) skipElement(); } }
bool XmlReader::readString(const char* tag, QString* val) { if (isTag(name(), tag)) { while (read()) { if (nodeType() == XML_READER_TYPE_TEXT) { *val = QString::fromUtf8((const char*)value()); skipElement((const xmlChar*)tag); return true; } else if ((nodeType() == XML_READER_TYPE_END_ELEMENT) && isTag(name(), tag)) { *val = QString(); return true; } else printf("?readInt: node type %d <%s>\n", nodeType(), name()); } } return false; }
void PdmlReader::readPdml() { QStringList creator; Q_ASSERT(isStartElement() && name() == "pdml"); isMldSupport_ = true; creator = attributes().value("creator").toString().split('/'); if ((creator.size() >= 2) && (creator.at(0) == "wireshark")) { QList<uint> minMldVer; minMldVer << 1 << 5 << 0; QStringList version = creator.at(1).split('.'); for (int i = 0; i < qMin(version.size(), minMldVer.size()); i++) { if (version.at(i).toUInt() < minMldVer.at(i)) { isMldSupport_ = false; break; } } } packetCount_ = 1; while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (name() == "packet") readPacket(); else skipElement(); } } }
bool PViewData::toVector(std::vector<std::vector<double> > &vec) { vec.resize(getNumTimeSteps()); for(int step = 0; step < getNumTimeSteps(); step++){ vec[step].clear(); for(int ent = 0; ent < getNumEntities(step); ent++){ for(int ele = 0; ele < getNumElements(step, ent); ele++){ if(skipElement(step, ent, ele)) continue; for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++){ for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++){ double val; getValue(step, ent, ele, nod, comp, val); vec[step].push_back(val); } } } } } return true; }
bool PViewData::fromVector(const std::vector<std::vector<double> > &vec) { if(empty() || !getNumTimeSteps()){ Msg::Warning("Cannot import vector in an empty view; skipping"); return false; } if((int)vec.size() != getNumTimeSteps()){ Msg::Error("Incompatible number of steps in vector for view import (%d!=%d)", (int)vec.size(), getNumTimeSteps()); return false; } for(int step = 0; step < getNumTimeSteps(); step++){ int i = 0; for(int ent = 0; ent < getNumEntities(step); ent++){ for(int ele = 0; ele < getNumElements(step, ent); ele++){ if(skipElement(step, ent, ele)) continue; for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++){ double x, y, z; getNode(step, ent, ele, nod, x, y, z); for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++){ if(i < (int)vec[step].size()){ setValue(step, ent, ele, nod, comp, vec[step][i++]); } else{ Msg::Error("Bad index (%d) in vector (%d) for view import", i, (int)vec[step].size()); return false; } } } } } } return true; }
bool PViewData::writePOS(const std::string &fileName, bool binary, bool parsed, bool append) { if(_adaptive){ Msg::Warning("Writing adapted dataset (will only export current time step)"); return _adaptive->getData()->writePOS(fileName, binary, parsed, append); } if(hasMultipleMeshes()){ Msg::Error("Cannot export multi-mesh datasets in .pos format"); return false; } if(haveInterpolationMatrices()) Msg::Warning("Discarding interpolation matrices when saving in .pos format"); if(binary || !parsed) Msg::Warning("Only parsed .pos files can be exported for this view type"); FILE *fp = Fopen(fileName.c_str(), append ? "a" : "w"); if(!fp){ Msg::Error("Unable to open file '%s'", fileName.c_str()); return false; } fprintf(fp, "View \"%s\" {\n", getName().c_str()); int firstNonEmptyStep = getFirstNonEmptyTimeStep(); for(int ent = 0; ent < getNumEntities(firstNonEmptyStep); ent++){ for(int ele = 0; ele < getNumElements(firstNonEmptyStep, ent); ele++){ if(skipElement(firstNonEmptyStep, ent, ele)) continue; int type = getType(firstNonEmptyStep, ent, ele); int numComp = getNumComponents(firstNonEmptyStep, ent, ele); const char *s = 0; switch(type){ case TYPE_PNT: s = (numComp == 9) ? "TP" : (numComp == 3) ? "VP" : "SP"; break; case TYPE_LIN: s = (numComp == 9) ? "TL" : (numComp == 3) ? "VL" : "SL"; break; case TYPE_TRI: s = (numComp == 9) ? "TT" : (numComp == 3) ? "VT" : "ST"; break; case TYPE_QUA: s = (numComp == 9) ? "TQ" : (numComp == 3) ? "VQ" : "SQ"; break; case TYPE_TET: s = (numComp == 9) ? "TS" : (numComp == 3) ? "VS" : "SS"; break; case TYPE_HEX: s = (numComp == 9) ? "TH" : (numComp == 3) ? "VH" : "SH"; break; case TYPE_PRI: s = (numComp == 9) ? "TI" : (numComp == 3) ? "VI" : "SI"; break; case TYPE_PYR: s = (numComp == 9) ? "TY" : (numComp == 3) ? "VY" : "SY"; break; } if(s){ fprintf(fp, "%s(", s); int numNod = getNumNodes(firstNonEmptyStep, ent, ele); for(int nod = 0; nod < numNod; nod++){ double x, y, z; getNode(firstNonEmptyStep, ent, ele, nod, x, y, z); fprintf(fp, "%.16g,%.16g,%.16g", x, y, z); if(nod != numNod - 1) fprintf(fp, ","); } bool first = true; for(int step = 0; step < getNumTimeSteps(); step++){ if(hasTimeStep(step)){ for(int nod = 0; nod < numNod; nod++){ for(int comp = 0; comp < numComp; comp++){ double val; getValue(step, ent, ele, nod, comp, val); if(first){ fprintf(fp, "){%.16g", val); first = false; } else fprintf(fp, ",%.16g", val); } } } } fprintf(fp, "};\n"); } } } fprintf(fp, "};\n"); fclose(fp); return true; }
void PdmlReader::readProto() { PdmlProtocol *pdmlProto = NULL; OstProto::Protocol *pbProto = NULL; Q_ASSERT(isStartElement() && name() == "proto"); QString protoName; int pos = -1; int size = -1; if (!attributes().value("name").isEmpty()) protoName = attributes().value("name").toString(); if (!attributes().value("pos").isEmpty()) pos = attributes().value("pos").toString().toInt(); if (!attributes().value("size").isEmpty()) size = attributes().value("size").toString().toInt(); qDebug("proto: %s, pos = %d, expPos_ = %d, size = %d", protoName.toAscii().constData(), pos, expPos_, size); // This is a heuristic to skip protocols which are not part of // this frame, but of a reassembled segment spanning several frames // 1. Proto starting pos is 0, but we've already seen some protocols // 2. Protocol Size exceeds frame length if (((pos == 0) && (currentStream_->protocol_size() > 0)) || ((pos + size) > int(currentStream_->core().frame_len()))) { skipElement(); return; } if (isDontCareProto()) { skipElement(); return; } // if we detect a gap between subsequent protocols, we "fill-in" // with a "hexdump" from the pcap if (pos > expPos_ && pcap_) { appendHexDumpProto(expPos_, pos - expPos_); expPos_ = pos; } // for unknown protocol, read a hexdump from the pcap if (!factory_.contains(protoName) && pcap_) { int size = -1; if (!attributes().value("size").isEmpty()) size = attributes().value("size").toString().toInt(); // Check if this proto is a subset of previous proto - if so, do nothing if ((pos >= 0) && (size > 0) && ((pos + size) <= expPos_)) { qDebug("subset proto"); skipElement(); return; } if (pos >= 0 && size > 0 && ((pos + size) <= pktBuf_.size())) { appendHexDumpProto(pos, size); expPos_ += size; skipElement(); return; } } pdmlProto = appendPdmlProto(protoName, &pbProto); qDebug("%s: preProtocolHandler(expPos = %d)", protoName.toAscii().constData(), expPos_); pdmlProto->preProtocolHandler(protoName, attributes(), expPos_, pbProto, currentStream_); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (name() == "proto") { // an embedded proto qDebug("embedded proto: %s\n", attributes().value("name") .toString().toAscii().constData()); if (isDontCareProto()) { skipElement(); continue; } // if we are in the midst of processing a protocol, we // end it prematurely before we start processing the // embedded protocol // // XXX: pdmlProto may be NULL for a sequence of embedded protos if (pdmlProto) { int endPos = -1; if (!attributes().value("pos").isEmpty()) endPos = attributes().value("pos").toString().toInt(); pdmlProto->prematureEndHandler(endPos, pbProto, currentStream_); pdmlProto->postProtocolHandler(pbProto, currentStream_); StreamBase s; s.protoDataCopyFrom(*currentStream_); expPos_ = s.frameProtocolLength(0); } readProto(); pdmlProto = NULL; pbProto = NULL; } else if (name() == "field") { if ((protoName == "fake-field-wrapper") && (attributes().value("name") == "tcp.segments")) { skipElement(); qDebug("[skipping reassembled tcp segments]"); skipUntilEnd_ = true; continue; } if (pdmlProto == NULL) { pdmlProto = appendPdmlProto(protoName, &pbProto); qDebug("%s: preProtocolHandler(expPos = %d)", protoName.toAscii().constData(), expPos_); pdmlProto->preProtocolHandler(protoName, attributes(), expPos_, pbProto, currentStream_); } readField(pdmlProto, pbProto); } else skipElement(); } } // Close-off current protocol if (pdmlProto) { pdmlProto->postProtocolHandler(pbProto, currentStream_); freePdmlProtocol(pdmlProto); StreamBase s; s.protoDataCopyFrom(*currentStream_); expPos_ = s.frameProtocolLength(0); } }
void PdmlReader::readPacket() { PcapFileFormat::PcapPacketHeader pktHdr; Q_ASSERT(isStartElement() && name() == "packet"); qDebug("%s: packetNum = %d", __FUNCTION__, packetCount_); skipUntilEnd_ = false; // XXX: we play dumb and convert each packet to a stream, for now prevStream_ = currentStream_; currentStream_ = streams_->add_stream(); currentStream_->mutable_stream_id()->set_id(packetCount_); currentStream_->mutable_core()->set_is_enabled(true); // Set to a high number; will get reset to correct value during parse currentStream_->mutable_core()->set_frame_len(16384); // FIXME: Hard coding! expPos_ = 0; if (pcap_) pcap_->readPacket(pktHdr, pktBuf_); while (!atEnd()) { readNext(); if (isEndElement()) break; if (isStartElement()) { if (skipUntilEnd_) skipElement(); else if (name() == "proto") readProto(); else if (name() == "field") readField(NULL, NULL); // TODO: top level field!!!! else skipElement(); } } currentStream_->mutable_core()->set_name(""); // FIXME // If trailing bytes are missing, add those from the pcap if ((expPos_ < pktBuf_.size()) && pcap_) { OstProto::Protocol *proto = currentStream_->add_protocol(); OstProto::HexDump *hexDump = proto->MutableExtension( OstProto::hexDump); proto->mutable_protocol_id()->set_id( OstProto::Protocol::kHexDumpFieldNumber); qDebug("adding trailing %d bytes starting from %d", pktBuf_.size() - expPos_, expPos_); hexDump->set_content(pktBuf_.constData() + expPos_, pktBuf_.size() - expPos_); hexDump->set_pad_until_end(false); } packetCount_++; emit progress(int(characterOffset()*100/device()->size())); // in % if (prevStream_) prevStream_->mutable_control()->CopyFrom(currentStream_->control()); if (stop_ && *stop_) raiseError("USER-CANCEL"); }