~cypheon/elfelli

ref: d62f0945c78677b2fe75fe2a71484afb4875f9a2 elfelli/src/XmlWriter.cpp -rw-r--r-- 2.0 KiB
d62f0945 — Johann Rudloff Try enabling Travis macOS build env. 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * XmlWriter.cpp
 * Copyright (C) 2006  Johann Rudloff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <vector>
#include <fstream>

#include "XmlWriter.h"

namespace Elfelli
{

namespace XmlWriter
{

const char *version_string = "elfelli-xml-1";

bool write(const std::string& filename, const Simulation *sim)
{
  std::ofstream out(filename.c_str(), std::ios::out);

  const std::vector<Body>& bodies = sim->get_bodies();
  const std::vector<PlateBody>& plates = sim->get_plates();

  out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  out << "<scene version=\"" << version_string << "\">\n";

  std::vector<Body>::const_iterator b_iter;
  for(b_iter = bodies.begin(); b_iter != bodies.end(); b_iter++)
  {
    out << "  <point x=\"" << (*b_iter).pos.get_x() << "\" "
        << "y=\"" << (*b_iter).pos.get_y() << "\" "
        << "charge=\"" << (*b_iter).charge << "\" />\n";
  }

  std::vector<PlateBody>::const_iterator p_iter;
  for(p_iter = plates.begin(); p_iter != plates.end(); p_iter++)
  {
    out << "  <plate x1=\"" << (*p_iter).pos_a.get_x() << "\" "
        << "y1=\"" << (*p_iter).pos_a.get_y() << "\" "
        << "x2=\"" << (*p_iter).pos_b.get_x() << "\" "
        << "y2=\"" << (*p_iter).pos_b.get_y() << "\" "
        << "charge=\"" << (*p_iter).charge << "\" />\n";
  }

  out << "</scene>\n";
  out.close();
  return true;
}

}

}