xdotwriter.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #include "xdotwriter.h"
15 #include <fstream>
16 #include "xitemnode.h"
17 #include "xlistnode.h"
18 
19 XDotWriter::XDotWriter(const shared_ptr<XNode> &root, std::ofstream &ofs)
20  : m_root(root), m_ofs(ofs), m_unnamedcnt(0) {
21  assert(ofs.good());
22  ofs << "/* KAME2 measurement configuration file" << std::endl
23  << "* Automatically created. KAME version. " << VERSION << std::endl
24  << "* date: " << XTime::now().getTimeStr() << std::endl
25  << "*/" << std::endl;
26 }
27 XDotWriter::~XDotWriter() {
28  m_ofs.flush();
29 }
30 void
31 XDotWriter::write() {
32  m_ofs << "digraph "
33  << "G" //(const char *)m_root->getName().c_str()
34  << " {"
35  << std::endl;
36  m_ofs << "node [shape=box,style=filled,color=green];" << std::endl;
37 
38  Snapshot shot(*m_root);
39  write(m_root, shot);
40  m_ofs << "}"
41  << std::endl;
42 }
43 void
44 XDotWriter::write(const shared_ptr<XNode> &node, const Snapshot &shot) {
45  if(std::find(m_nodes.begin(), m_nodes.end(), node) == m_nodes.end()) {
46  m_ofs << "obj_" << (uintptr_t)node.get()
47  << " [label=\"" << node->getName()
48  << "\"]" << std::endl;
49  m_nodes.push_back(node);
50  }
51 // shared_ptr<XValueNodeBase> vnode = dynamic_pointer_cast<XValueNodeBase>(node);
52 
53 // shared_ptr<XListNodeBase> lnode = dynamic_pointer_cast<XListNodeBase>(node);
54  int unnamed = 0;
55  if(shot.size(node)) {
56  for(auto it = shot.list(node)->begin(); it != shot.list(node)->end(); it++) {
57  shared_ptr<XNode> child = *it;
58 
59  if(child->getName().empty()) {
60  unnamed++;
61  }
62  else {
63  m_ofs << "obj_" << (uintptr_t)child.get()
64  << " -> "
65  << "obj_" << (uintptr_t)node.get()
66  << std::endl;
67  write(child, shot);
68  }
69  }
70  }
71  if(unnamed) {
72  m_unnamedcnt++;
73  m_ofs << "unnamedobj_" << (int)m_unnamedcnt
74  << " [label=\"" << (const char*)QString("%1 obj.").arg(unnamed).toUtf8().data()
75  << "\"]" << std::endl;
76  m_ofs << "unnamedobj_" << (int)m_unnamedcnt
77  << " -> "
78  << "obj_" << (uintptr_t)node.get()
79  << std::endl;
80  }
81 }

Generated for KAME4 by  doxygen 1.8.3