Advertisement
Guest User

test.cpp

a guest
Dec 24th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <boost/graph/graphml.hpp>
  3. #include <boost/graph/graph_utility.hpp>
  4.  
  5. #include <fstream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace boost;
  10. using namespace std;
  11.  
  12. struct VertexProperties { string url, description; };
  13. struct EdgeProperties { string url, description; };
  14. struct GraphProperties { string title; };
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.   string path(argc == 2 ? argv[1] : "simple_graph.graphml");
  19.   typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties, GraphProperties> DirectedGraph;
  20.   typedef dynamic_properties Properties;
  21.   DirectedGraph graph(0);
  22.  
  23.   Properties props(ignore_other_properties);
  24.   props.property("url", get(&VertexProperties::url, graph));
  25.   props.property("description", get(&VertexProperties::description, graph));
  26.   props.property("url", get(&EdgeProperties::url, graph));
  27.   props.property("description", get(&EdgeProperties::description, graph));
  28.   map<string, string> attribute_name2name;
  29.   associative_property_map<map<string, string>> graphname_map(attribute_name2name);
  30.   props.property("title", graphname_map);
  31.  
  32.   ifstream file(path.c_str());
  33.   if (!file.is_open()) {
  34.       cout << "loading file failed." << endl;
  35.       return 1;
  36.   }
  37.  
  38.   // This fixes incompatibilities in yEd's .graphml export:
  39.   string line;
  40.   stringstream validated;
  41.   while (getline(file, line)) {
  42.     auto np = string::npos;
  43.     if (line.find("yfiles.type=\"") != np && (line.find("attr.name=\"") == np ||
  44.         line.find("attr.type=") == np)) {
  45.       auto p = line.find("/>");
  46.       if (p == np) {
  47.         cerr  << "parse error" << endl;
  48.         return 1;
  49.       }
  50.       line.insert(p, " attr.name=\"dummy\" attr.type=\"string\"");
  51.       validated << line << '\n';
  52.     } else {
  53.       validated << line << '\n';
  54.     }
  55.   }
  56.   file.close();
  57.  
  58.   try {
  59.     read_graphml(validated, graph, props);
  60.     graph[graph_bundle].title = get(graphname_map, "title");
  61.     cout << "\"" << graph[graph_bundle].title << "\"" << endl;
  62.   } catch(parse_error& e) {
  63.     cerr << e.what() << endl;
  64.     return 1;
  65.   }
  66.  
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement