#ifndef GRAPHIO_H #define GRAPHIO_H ///////////////////////////////////////////////////////////////// // GraphIO.h - reads and writes directed graph structures // // ver 1.1 // // ----------------------------------------------------------- // // copyright © Jim Fawcett, 2012 // // All rights granted provided that this notice is retained // // ----------------------------------------------------------- // // Jim Fawcett, CST 4-187, Syracuse University, 315 443 3948 // // Summer Projects, 2012 // // Win7, Visual Studio 2010 // ///////////////////////////////////////////////////////////////// /* * Package Operations: * =================== * This package provides a class to support reading and writing * directed graphs: * - GraphIO: * Provides the static functions: * - ReadGraph reads and parses an XML file to build a * directed graph. * - WriteGraph builds an XML representation of an * existing directed graph. */ #include #include #include #include #include #include "..\graph\Graph.h" #include "..\LibUtils\LibUtils.h" #include "XmlReader.h" #include "XmlWriter.h" namespace DirectedGraph { template class GraphIO { public: static bool ReadGraph(Graph& g, const std::string& fileName); static bool WriteGraph(Graph& g, const std::string& fileName); }; template bool GraphIO::ReadGraph(Graph& g, const std::string& fileName) { // open xml file std::ifstream in(fileName.c_str(), std::ios::in); if(!in.good()) return false; //std::cout << "\n reading " << fileName; // create XmlReader std::ostringstream temp; temp << in.rdbuf(); XmlReader rdr(temp.str()); // add vertices to graph while(rdr.next()) { std::string tag = rdr.tag(); if(tag == "graph") { XmlReader::attribElems attribs = rdr.attributes(); g.name() = attribs[0].second; } if(tag == "v") { XmlReader::attribElems attribs = rdr.attributes(); size_t id = LibUtils::fromString(attribs[0].second); V value = attribs[1].second; Vertex vert(id, value); g.addVertex(vert); } } // add edges to graph rdr.reset(); size_t parentIndex; while(rdr.next()) { std::string tag = rdr.tag(); if(tag == "v") { XmlReader::attribElems attribs = rdr.attributes(); size_t id = LibUtils::fromString(attribs[0].second); parentIndex = g.findVertexById(id); } if(tag == "e") { XmlReader::attribElems attribs = rdr.attributes(); E value = LibUtils::fromString(attribs[0].second); size_t targetVertexId = LibUtils::fromString(rdr.body()); size_t targetVertexIndex = g.findVertexById(targetVertexId); g.addEdge(value, g[parentIndex], g[targetVertexIndex]); } } in.close(); return true; } template bool GraphIO::WriteGraph(Graph& g, const std::string& fileName) { std::ofstream out(fileName.c_str(), std::ios::out); if(!out.good()) return false; //std::cout << "\n writing " << fileName; // create graph XmlWriter XmlWriter wtr; wtr.addDeclaration(); wtr.start("graph"); std::string Vtype = typeid(V).name(); std::string Etype = typeid(E).name(); wtr.addAttribute("name", g.name()); // each vertex needs an XmlWriter to build its element std::vector vertices; for(size_t i=0; i 0) { // each edge needs an XmlWriter to build its element std::vector edges; for(size_t j=0; j