Commit 3156477f authored by aggsol's avatar aggsol 🙉

improved graph

parent 18180697
...@@ -11,4 +11,15 @@ Helper tool for gamebooks written in Asciidoc ...@@ -11,4 +11,15 @@ Helper tool for gamebooks written in Asciidoc
3. Meta data in graph 3. Meta data in graph
4. How to add labels in dot file 4. How to add labels in dot file
5. Output final result 5. Output final result
6. Shuffle of sections 6. Shuffle of sections, verify shuffle
\ No newline at end of file 7. Better test cases
## Format
### Book
### Sections
### Metadata
## Best Practices
add_library( add_library(
gamebook gamebook
STATIC STATIC
Graph.cpp
Parser.cpp Parser.cpp
) )
......
/*
Copyright (C) 2018 Kim HOANG <foss@aggsol.de>
This file is part of gamebook-checker.
xollox 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "Graph.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
namespace bodhi
{
void Graph::createDotFile(const std::map<int, Section>& sections)
{
std::ofstream dotfile("graph.dot");
if(not dotfile.is_open())
{
throw std::runtime_error("Cannot write dot file");
}
dotfile << "digraph G {\n"
<< "forcelabels=true;"
<< " node [style=filled fillcolor=white];\n";
for(auto& s: sections)
{
int sectionNum = s.second.m_number;
std::ostringstream label;
label << " " << sectionNum << " [label=\"";
unsigned counter = 0;
for(auto& attr: s.second.m_classes)
{
if(counter > 0)
{
label << ", ";
}
label << attr;
counter++;
}
label << "\"];\n";
auto labelStr = label.str();
if(counter > 0)
{
dotfile << label.str();
}
for(auto& c: s.second.m_children)
{
dotfile << " " << sectionNum << " -> " << c << ";\n";
}
if(s.second.m_children.size() == 0)
{
dotfile << " " << sectionNum << " [peripheries=2];\n";
}
}
dotfile << "}\n";
}
}
\ No newline at end of file
/*
Copyright (C) 2018 Kim HOANG <foss@aggsol.de>
This file is part of gamebook-checker.
xollox 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef BODHI_GRAPH_HPP
#define BODHI_GRAPH_HPP
#include "Section.hpp"
#include <map>
namespace bodhi
{
class Graph
{
public:
void createDotFile(const std::map<int, Section>& sections);
private:
};
}
#endif
\ No newline at end of file
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "CliArguments.hpp" #include "CliArguments.hpp"
#include "Graph.hpp"
#include "Helpers.hpp" #include "Helpers.hpp"
#include "Parser.hpp" #include "Parser.hpp"
#include "Shuffle.hpp" #include "Shuffle.hpp"
...@@ -77,6 +78,12 @@ int main(int argc, char* argv[]) ...@@ -77,6 +78,12 @@ int main(int argc, char* argv[])
auto dot = args.getOpt("d", "dot-file"); auto dot = args.getOpt("d", "dot-file");
auto verbose = args.getOpt("v", "verbose"); auto verbose = args.getOpt("v", "verbose");
if(mix && dot)
{
std::cerr << "Error : Cannot combine --mix-sections and --dot-file\n";
return 101;
}
std::map<int, bodhi::Section> sections; std::map<int, bodhi::Section> sections;
try try
...@@ -155,27 +162,9 @@ int main(int argc, char* argv[]) ...@@ -155,27 +162,9 @@ int main(int argc, char* argv[])
if(dot) if(dot)
{ {
std::ofstream dotfile("graph.dot"); bodhi::Graph graph;
if(not dotfile.is_open()) graph.createDotFile(sections);
{ return 0;
throw std::runtime_error("Cannot write dot file");
}
dotfile << "digraph G {\n"
<< " node [style=filled fillcolor=white];\n";
for(auto& s: sections)
{
for(auto& c: s.second.m_children)
{
dotfile << " " << s.second.m_number << " -> " << c << ";\n";
}
if(s.second.m_children.size() == 0)
{
dotfile << " " << s.second.m_number << " [peripheries=2];\n";
}
}
dotfile << "}\n";
} }
if(mix) if(mix)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment