⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlfile.cpp

📁 利用C
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  fprintf(fp, "  </meshfunction>\n");   // Close file  closeFile(fp);    message(1, "Saved mesh function to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(MeshFunction<unsigned int>& meshfunction){  // Open file  FILE *fp = openFile();    // Write mesh in XML format  fprintf(fp, "  <meshfunction type=\"uint\" dim=\"%u\" size=\"%u\">\n",          meshfunction.dim(), meshfunction.size());    Mesh& mesh = meshfunction.mesh();  for(MeshEntityIterator e(mesh, meshfunction.dim()); !e.end(); ++e)  {      fprintf(fp, "    <entity index=\"%u\" value=\"%d\"/>\n",              e->index(), meshfunction(*e));  }  fprintf(fp, "  </meshfunction>\n");   // Close file  closeFile(fp);    message(1, "Saved mesh function to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(MeshFunction<double>& meshfunction){  // Open file  FILE *fp = openFile();    // Write mesh in XML format  fprintf(fp, "  <meshfunction type=\"double\" dim=\"%u\" size=\"%u\">\n",          meshfunction.dim(), meshfunction.size());  Mesh& mesh = meshfunction.mesh();  for(MeshEntityIterator e(mesh, meshfunction.dim()); !e.end(); ++e)  {      fprintf(fp, "    <entity index=\"%u\" value=\"%g\"/>\n",              e->index(), meshfunction(*e));  }  fprintf(fp, "  </meshfunction>\n");   // Close file  closeFile(fp);    message(1, "Saved mesh function to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(MeshFunction<bool>& meshfunction){  // Open file  FILE *fp = openFile();    // Write mesh in XML format  fprintf(fp, "  <meshfunction type=\"bool\" dim=\"%u\" size=\"%u\">\n",          meshfunction.dim(), meshfunction.size());  Mesh& mesh = meshfunction.mesh();  std::string value;  for (MeshEntityIterator e(mesh, meshfunction.dim()); !e.end(); ++e)  {    value = (meshfunction(*e) ? "true" : "false");    fprintf(fp, "    <entity index=\"%u\" value=\"%s\"/>\n",              e->index(), value.c_str());  }  fprintf(fp, "  </meshfunction>\n");   // Close file  closeFile(fp);    message(1, "Saved mesh function to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(Function& f){  // Can only save discrete functions  if ( f.type() != Function::discrete )    error("Only discrete functions can be saved in XML format.");  // Get discrete function (we're all friends here)  DiscreteFunction* df = static_cast<DiscreteFunction*>(f.f);    // Begin function  FILE *fp = openFile();  fprintf(fp, "  <function> \n");  closeFile(fp);  // Write the mesh  *this << df->mesh;    // Write the vector  *this << *df->x;  // Write the finite element  fp = openFile();  fprintf(fp, "  <finiteelement signature=\"%s\"/>\n", df->finite_element->signature());  closeFile(fp);  // Write the dof map  fp = openFile();  fprintf(fp, "  <dofmap signature=\"%s\"/>\n", df->dof_map->signature());  closeFile(fp);  // End function  fp = openFile();  fprintf(fp, "  </function> \n");  closeFile(fp);  message(1, "Saved function to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(ParameterList& parameters){  // Open file  FILE *fp = openFile();  // Write parameter list in XML format  fprintf(fp, "  <parameters>\n" );  for (ParameterList::const_iterator it = parameters.parameters.begin(); it != parameters.parameters.end(); ++it)  {    const Parameter parameter = it->second;    switch ( parameter.type() )    {    case Parameter::type_int:      fprintf(fp, "    <parameter name=\"%s\" type=\"int\" value=\"%d\"/>\n",	      it->first.c_str(), static_cast<int>(parameter));      break;    case Parameter::type_real:      fprintf(fp, "    <parameter name=\"%s\" type=\"real\" value=\"%.16e\"/>\n",	      it->first.c_str(), static_cast<real>(parameter));      break;    case Parameter::type_bool:      if ( static_cast<bool>(parameter) )	fprintf(fp, "    <parameter name=\"%s\" type=\"bool\" value=\"true\"/>\n",		it->first.c_str());      else	fprintf(fp, "    <parameter name=\"%s\" type=\"bool\" value=\"false\"/>\n",		it->first.c_str());      break;    case Parameter::type_string:      fprintf(fp, "    <parameter name=\"%s\" type=\"string\" value=\"%s\"/>\n",	      it->first.c_str(), static_cast<std::string>(parameter).c_str());      break;    default:      ; // Do nothing    }  }    fprintf(fp, "  </parameters>\n" );  // Close file  closeFile(fp);  message(1, "Saved parameters to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------void XMLFile::operator<<(Graph& graph){  // Open file  FILE *fp = openFile();    // Get graph type and number of vertices, edges and arches  uint num_vertices = graph.numVertices();  // Write graph in XML format  fprintf(fp, "  <graph type=\"%s\">\n", graph.typestr().c_str());  // Get connections (outgoing edges), offsets and weigts  const uint* connections = graph.connectivity();  const uint* offsets = graph.offsets();  const uint* edge_weights = graph.edgeWeights();  const uint* vertex_weights = graph.vertexWeights();  dolfin_assert(connections);  dolfin_assert(offsets);  dolfin_assert(edge_weights);  dolfin_assert(vertex_weights);    // Write vertice header   fprintf(fp, "    <vertices size=\"%u\">\n", graph.numVertices());  // Vertices  for(uint i=0; i<num_vertices; ++i)  {	  fprintf(fp,           "      <vertex index=\"%u\" num_edges=\"%u\" weight=\"%u\"/>\n", i,          graph.numEdges(i), vertex_weights[i]);	    }  fprintf(fp, "    </vertices>\n");  fprintf(fp, "    <edges size=\"%u\">\n", graph.numEdges());  // Edges  for(uint i=0; i<num_vertices; ++i)  {    for(uint j=offsets[i]; j<offsets[i] + graph.numEdges(i); ++j)    {      // In undirected graphs an edge (v1, v2) is the same as edge (v2, v1)      // and should not be stored twice      if ( graph.type() == Graph::directed || i < connections[j] )        fprintf(fp,         "      <edge v1=\"%u\" v2=\"%u\" weight=\"%u\"/>\n",        i, connections[j], edge_weights[j]);    }  }  fprintf(fp, "    </edges>\n");  fprintf(fp, "  </graph>\n");    // Close file  closeFile(fp);  message(1, "Saved graph to file %s in DOLFIN XML format.", filename.c_str());}//-----------------------------------------------------------------------------FILE* XMLFile::openFile(){  // Open file  FILE *fp = fopen(filename.c_str(), "r+");  // Step to position before previously written footer  //printf("Stepping to position: %ld\n", mark);  fseek(fp, mark, SEEK_SET);  fflush(fp);    // Write DOLFIN XML format header  if ( !header_written )  {    fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );    fprintf(fp, "<dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\">\n" );        header_written = true;  }  return fp;}//-----------------------------------------------------------------------------void XMLFile::closeFile(FILE* fp){  // Get position in file before writing footer  mark = ftell(fp);  //printf("Position in file before writing footer: %ld\n", mark);  // Write DOLFIN XML format footer  if ( header_written )    fprintf(fp, "</dolfin>\n");  // Close file  fclose(fp);}//-----------------------------------------------------------------------------void XMLFile::parseFile(){  // Notify that file is being opened  xmlObject->open(filename);  // Parse file using the SAX interface  parseSAX();    // Notify that file is being closed  if ( !xmlObject->close() )    error("Unable to find data in XML file.");}//-----------------------------------------------------------------------------void XMLFile::parseSAX(){  // Set up the sax handler. Note that it is important that we initialise  // all (24) fields, even the ones we don't use!  xmlSAXHandler sax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};    // Set up handlers for parser events  sax.startDocument = sax_start_document;  sax.endDocument   = sax_end_document;  sax.startElement  = sax_start_element;  sax.endElement    = sax_end_element;  sax.warning       = sax_warning;  sax.error         = sax_error;  sax.fatalError    = sax_fatal_error;    // Parse file  xmlSAXUserParseFile(&sax, (void *) xmlObject, filename.c_str());}//-----------------------------------------------------------------------------// Callback functions for the SAX interface//-----------------------------------------------------------------------------void dolfin::sax_start_document(void *ctx){  // Do nothing}//-----------------------------------------------------------------------------void dolfin::sax_end_document(void *ctx){  // Do nothing}//-----------------------------------------------------------------------------void dolfin::sax_start_element(void *ctx,			       const xmlChar *name, const xmlChar **attrs){  ( (XMLObject *) ctx )->startElement(name, attrs);}//-----------------------------------------------------------------------------void dolfin::sax_end_element(void *ctx, const xmlChar *name){  ( (XMLObject *) ctx )->endElement(name);}//-----------------------------------------------------------------------------void dolfin::sax_warning(void *ctx, const char *msg, ...){  va_list args;  va_start(args, msg);  char buffer[DOLFIN_LINELENGTH];  vsnprintf(buffer, DOLFIN_LINELENGTH, msg, args);  warning("Incomplete XML data: " + std::string(buffer));  va_end(args);}//-----------------------------------------------------------------------------void dolfin::sax_error(void *ctx, const char *msg, ...){  va_list args;  va_start(args, msg);  char buffer[DOLFIN_LINELENGTH];  vsnprintf(buffer, DOLFIN_LINELENGTH, msg, args);  error("Illegal XML data: " + std::string(buffer));  va_end(args);}//-----------------------------------------------------------------------------void dolfin::sax_fatal_error(void *ctx, const char *msg, ...){  va_list args;  va_start(args, msg);  char buffer[DOLFIN_LINELENGTH];  vsnprintf(buffer, DOLFIN_LINELENGTH, msg, args);  error("Illegal XML data: " + std::string(buffer));  va_end(args);}//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -