xrubywriter.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 <fstream>
15 #include "xrubywriter.h"
16 #include "xitemnode.h"
17 #include "xlistnode.h"
18 
19 XRubyWriter::XRubyWriter(const shared_ptr<XNode> &root, std::ofstream &ofs)
20  : m_root(root), m_ofs(ofs)
21 {
22  assert(ofs.good());
23  ofs << "# KAME2 measurement configuration file" << std::endl
24  << "# Automatically created. KAME version. " << VERSION << std::endl
25  << "# date: " << XTime::now().getTimeStr() << std::endl;
26  ofs << "x = Array.new" << std::endl;
27 }
28 XRubyWriter::~XRubyWriter()
29 {
30  m_ofs.flush();
31 }
32 void
33 XRubyWriter::write()
34 {
35  XString name = m_root->getName();
36  name[0] = toupper(name[0]);
37  m_ofs << "x << "
38  << name
39  << std::endl;
40  Snapshot shot( *m_root);
41  write(m_root, shot, false, 0);
42 }
43 void
44 XRubyWriter::write(
45  const shared_ptr<XNode> &node, const Snapshot &shot,
46  bool ghost, int level)
47 {
48  int size = shot.size(node);
49  ghost = ghost || shot[ *node].isRuntime();
50  auto vnode = dynamic_pointer_cast<XValueNodeBase>(node);
51  if(vnode) {
52  if(size) {
53  for(int j = 0; j < level; j++) m_ofs << "\t";
54  if(ghost)
55  m_ofs << "# ";
56  m_ofs << "x.last";
57  }
58  else {
59  if(ghost)
60  m_ofs << "\t# ";
61  }
62  QString s(shot[ *vnode].to_str());
63  s.replace( QChar('\\'), "\\\\");
64  s.replace( QChar('\n'), "\\n");
65  s.replace( QChar('\r'), "\\r");
66  s.replace( QChar('\t'), "\\t");
67  m_ofs << ".load(\""
68  << (const char *)s.toUtf8().data()
69  << "\")"
70  << std::endl;
71  }
72  else
73  if( ! size) {m_ofs << std::endl;}
74 
75  auto lnode = dynamic_pointer_cast<XListNodeBase>(node);
76  bool write_typename = false;
77  if(lnode) {
78  // XListNode doesn't want typename
79  write_typename = (lnode->getTypename().find("XListNode") != 0);
80  // XAliasListNode doesn't want creation of child
81  if(lnode->getTypename().find( "XAliasListNode") == 0) lnode.reset();
82  }
83  unsigned idx = 0;
84  if(size) {
85  const XNode::NodeList &list( *shot.list(node));
86  for(auto it = list.begin(); it != list.end(); it++) {
87  shared_ptr<XNode> child = *it;
88  for(int j = 0; j < level; j++) m_ofs << "\t";
89  if(ghost)
90  m_ofs << "# ";
91  int child_size = shot.size(child);
92  if(child_size) {
93  m_ofs << "x << ";
94  }
95  if(lnode) {
96  m_ofs << "x.last.create(";
97  if(write_typename || child->getName().length()) {
98  m_ofs << "\""
99  << (write_typename ? child->getTypename().c_str() : "")
100  << "\"";
101  }
102  if(child->getName().length()) {
103  m_ofs << ",\""
104  << child->getName()
105  << "\"";
106  }
107  m_ofs << ")";
108  }
109  else {
110  if(child->getName().length()) {
111  m_ofs << "x.last[\""
112  << child->getName()
113  << "\"]";
114  }
115  else {
116  m_ofs << "x.last["
117  << idx
118  << "]";
119  }
120  }
121  if(child_size) {
122  m_ofs << std::endl;
123  }
124  write(child, shot, ghost, level + 1);
125  if(child_size) {
126  for(int j = 0; j < level; j++) m_ofs << "\t";
127  if(ghost)
128  m_ofs << "# ";
129  m_ofs << "x.pop"
130  << std::endl;
131  }
132 
133  idx++;
134  }
135  }
136 }

Generated for KAME4 by  doxygen 1.8.3