mstch/test/headerize.cpp

80 lines
2.3 KiB
C++
Raw Permalink Normal View History

2015-04-23 12:54:08 +02:00
#include <iostream>
#include <fstream>
#include <boost/algorithm/string/replace.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
void wrap_string(std::istream& input, std::ostream& output,
const std::string& variable_name)
{
output << "const std::string " << variable_name << "{\n";
2015-04-23 12:54:08 +02:00
std::string line;
while (std::getline(input, line)) {
boost::replace_all(line, "\\", "\\\\");
boost::replace_all(line, "\"", "\\\"");
output << " \"" << line;
if (!input.eof())
output << "\\n";
output << "\"\n";
2015-04-23 12:54:08 +02:00
}
output << "};\n";
2015-04-23 12:54:08 +02:00
}
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "show help")
("output", po::value<std::string>(), "output file")
("namespace", po::value<std::string>(), "namespace to use")
("input-string,S", po::value<std::vector<std::string>>(),
"files to parse as strings")
("input-code,C", po::value<std::vector<std::string>>(),
"files to parse as code");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return 1;
}
if (!vm.count("output")) {
std::cerr << "Output file not set" << std::endl;
2015-04-23 12:54:08 +02:00
return 1;
}
std::ofstream output(vm["output"].as<std::string>(), std::ios::out);
if (vm.count("namespace"))
output << "namespace " << vm["namespace"].as<std::string>() << " {\n";
2015-04-23 12:54:08 +02:00
if (vm.count("input-string"))
for (auto& filename: vm["input-string"].as<std::vector<std::string>>()) {
std::ifstream input(filename, std::ios::in);
2015-05-04 16:14:32 +02:00
std::string variable_name = boost::replace_all_copy(filename, ".", "_");
boost::replace_all(variable_name, "~", "");
wrap_string(input, output, variable_name);
2015-04-23 12:54:08 +02:00
input.close();
}
if (vm.count("input-code"))
for (auto& filename: vm["input-code"].as<std::vector<std::string>>()) {
std::ifstream input(filename, std::ios::in);
output << std::string{(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>()} << std::endl;
2015-04-23 12:54:08 +02:00
input.close();
}
if (vm.count("namespace"))
output << "}\n";
2015-04-23 12:54:08 +02:00
output.close();
return 0;
}