Commit 48174f35 authored by aggsol's avatar aggsol 🙉

fix section shuffle

parent 35c1e0af
......@@ -49,7 +49,7 @@ namespace bodhi
}
/* Make the replacement. */
str.replace(index, sub.size(), sub);
str.replace(index, target.size(), sub);
/* Advance index forward so the next iteration doesn't pick it up as well. */
index += sub.size();
......
......@@ -66,7 +66,7 @@ namespace bodhi
}
if(counter >= maxAttempts)
{
std::cout << "Warning: Could not map section " << a << " farther away\n";
std::cerr << "Warning: Could not map section " << a << " farther away\n";
}
numbers.erase(numbers.begin() + index);
......
......@@ -22,8 +22,9 @@
#include "Shuffle.hpp"
#include <cassert>
#include <istream>
#include <iostream>
#include <sstream>
#include <fstream>
#include <deque>
#include <map>
......@@ -54,6 +55,78 @@ namespace
<< std::endl;
}
void writeSections(std::map<int, bodhi::Section>& sections, const std::map<int, int> resultMapping,
std::ostream& strm)
{
std::map<int, bodhi::Section> final;
for(auto& s: sections)
{
assert(s.first == s.second.m_number);
int shuffled = resultMapping.at(s.first);
s.second.m_number = shuffled;
for(auto& c: s.second.m_children)
{
std::string before = "<<section-";
before += std::to_string(c);
before += ">>";
std::string after = "<<section-";
after += std::to_string(resultMapping.at(c));
after += ">>";
for(auto& l: s.second.m_lines)
{
l = bodhi::replaceSubStr(l, before, after);
}
}
final[shuffled] = s.second;
}
for(auto& f: final)
{
strm << "[[section-" << f.second.m_number << "]]\n"
<< "== " << f.second.m_number << "\n";
for(auto& l: f.second.m_lines)
{
strm << l << "\n";
}
}
// for(auto& s: sections)
// {
// assert(s.first == s.second.m_number);
// int shuffled = resultMapping.at(s.first);
// strm << "[[section-" << shuffled << "]]\n"
// << "== " << shuffled << "\n";
// std::string line;
// for(auto& l: s.second.m_lines)
// {
// line = l;
// for(auto& c: s.second.m_children)
// {
// std::string before = "<<section-";
// before += std::to_string(c);
// before += ">>";
// std::string after = "<<section-";
// after += std::to_string(resultMapping.at(c));
// after += ">>";
// line = bodhi::replaceSubStr(line, before, after);
// }
// strm << line << "\n";
// }
// }
}
}
int main(int argc, char* argv[])
......@@ -128,18 +201,13 @@ int main(int argc, char* argv[])
{
if(s.first - last != 1)
{
std::cerr << "Warning: Missing section(s): ";
for(int i=last+1; i<s.first; ++i)
{
std::cerr << i<< " ";
}
std::cerr << "\n";
std::cerr << "Error : Missing section(s) " << last+1 << "-" << s.first-1 << "\n";
}
if(s.second.m_parents.size() == 0)
{
assert(s.first == s.second.m_number);
std::cerr << "Warning: No reference to section: " << s.first << "\n";
std::cerr << "Error : No reference to section: " << s.first << "\n";
}
}
last = s.first;
......@@ -173,37 +241,16 @@ int main(int argc, char* argv[])
if(output == "stdout")
{
for(auto& s: sections)
writeSections(sections, resultMapping, std::cout);
}
else
{
std::ofstream mixFile(output);
if(not mixFile.is_open())
{
assert(s.first == s.second.m_number);
int shuffled = resultMapping.at(s.first);
std::cout << "[[section-" << shuffled << "]]\n"
<< "== " << shuffled << "\n";
for(auto& c: s.second.m_children)
{
std::string before = "<<section-";
before += std::to_string(c);
before += ">>";
std::string after = "<<section-";
after += std::to_string(resultMapping.at(c));
after += ">>";
std::cout << "Mapping " << before << " to " << after << "\n";
for(auto& l: s.second.m_lines)
{
l = bodhi::replaceSubStr(l, before, after);
}
}
for(auto& l: s.second.m_lines)
{
std::cout << l << "\n";
}
throw std::runtime_error("Cannot open output file.");
}
writeSections(sections, resultMapping, mixFile);
}
}
}
......
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