reformat
This commit is contained in:
parent
29980e299c
commit
f4dd438fcc
@ -5,6 +5,7 @@
|
|||||||
#include "visitor/has_token.hpp"
|
#include "visitor/has_token.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
const mstch::node render_context::null_node;
|
const mstch::node render_context::null_node;
|
||||||
|
|
||||||
@ -44,9 +45,9 @@ const mstch::node& render_context::find_node(
|
|||||||
token.substr(token.rfind('.') + 1),
|
token.substr(token.rfind('.') + 1),
|
||||||
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
||||||
else
|
else
|
||||||
for (auto& i: current_nodes)
|
for (auto& node: current_nodes)
|
||||||
if (boost::apply_visitor(visitor::has_token(token), i))
|
if (visit(has_token(token), node))
|
||||||
return boost::apply_visitor(visitor::get_token(token, i), i);
|
return visit(get_token(token, node), node);
|
||||||
return null_node;
|
return null_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "template_type.hpp"
|
#include "template_type.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
|
||||||
class render_context {
|
class render_context {
|
||||||
public:
|
public:
|
||||||
class push {
|
class push {
|
||||||
@ -20,6 +21,7 @@ namespace mstch {
|
|||||||
private:
|
private:
|
||||||
render_context& context;
|
render_context& context;
|
||||||
};
|
};
|
||||||
|
|
||||||
render_context(
|
render_context(
|
||||||
const mstch::node& node,
|
const mstch::node& node,
|
||||||
const std::map<std::string, template_type>& partials);
|
const std::map<std::string, template_type>& partials);
|
||||||
@ -31,6 +33,7 @@ namespace mstch {
|
|||||||
state.top() = std::unique_ptr<state::render_state>(
|
state.top() = std::unique_ptr<state::render_state>(
|
||||||
new T(std::forward<Args>(args)...));
|
new T(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const mstch::node null_node;
|
static const mstch::node null_node;
|
||||||
const mstch::node& find_node(
|
const mstch::node& find_node(
|
||||||
@ -40,4 +43,5 @@ namespace mstch {
|
|||||||
std::deque<mstch::node> nodes;
|
std::deque<mstch::node> nodes;
|
||||||
std::stack<std::unique_ptr<state::render_state>> state;
|
std::stack<std::unique_ptr<state::render_state>> state;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
#include "outside_section.hpp"
|
#include "outside_section.hpp"
|
||||||
#include "visitor/is_node_empty.hpp"
|
#include "visitor/is_node_empty.hpp"
|
||||||
#include "visitor/render_section.hpp"
|
#include "visitor/render_section.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
state::in_section::in_section(type type, const std::string& section_name):
|
state::in_section::in_section(type type, const std::string& section_name):
|
||||||
m_type(type), section_name(section_name), skipped_openings(0)
|
m_type(type), section_name(section_name), skipped_openings(0)
|
||||||
@ -13,18 +15,17 @@ state::in_section::in_section(type type, const std::string& section_name):
|
|||||||
std::string state::in_section::render(render_context& ctx, const token& token) {
|
std::string state::in_section::render(render_context& ctx, const token& token) {
|
||||||
if (token.token_type() == token::type::section_close)
|
if (token.token_type() == token::type::section_close)
|
||||||
if (token.name() == section_name && skipped_openings == 0) {
|
if (token.name() == section_name && skipped_openings == 0) {
|
||||||
auto& sn = ctx.get_node(section_name);
|
auto& node = ctx.get_node(section_name);
|
||||||
std::string out;
|
std::string out;
|
||||||
if(m_type == type::normal &&
|
if (m_type == type::normal && !visit(is_node_empty(), node))
|
||||||
!boost::apply_visitor(visitor::is_node_empty(), sn))
|
out = visit(render_section(ctx, section), node);
|
||||||
out = boost::apply_visitor(visitor::render_section(ctx, section), sn);
|
else if (m_type == type::inverted && visit(is_node_empty(), node))
|
||||||
else if(m_type == type::inverted &&
|
|
||||||
boost::apply_visitor(visitor::is_node_empty(), sn))
|
|
||||||
out = render_context::push(ctx).render(section);
|
out = render_context::push(ctx).render(section);
|
||||||
ctx.set_state<outside_section>();
|
ctx.set_state<outside_section>();
|
||||||
return out;
|
return out;
|
||||||
} else
|
} else {
|
||||||
skipped_openings--;
|
skipped_openings--;
|
||||||
|
}
|
||||||
else if (token.token_type() == token::type::inverted_section_open ||
|
else if (token.token_type() == token::type::inverted_section_open ||
|
||||||
token.token_type() == token::type::section_open)
|
token.token_type() == token::type::section_open)
|
||||||
skipped_openings++;
|
skipped_openings++;
|
||||||
|
@ -1,23 +1,28 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "render_state.hpp"
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "render_state.hpp"
|
||||||
#include "template_type.hpp"
|
#include "template_type.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace state {
|
namespace state {
|
||||||
|
|
||||||
class in_section: public render_state {
|
class in_section: public render_state {
|
||||||
public:
|
public:
|
||||||
enum class type { inverted, normal };
|
enum class type {
|
||||||
|
inverted, normal
|
||||||
|
};
|
||||||
in_section(type type, const std::string §ion_name);
|
in_section(type type, const std::string §ion_name);
|
||||||
std::string render(
|
std::string render(render_context &context, const token &token) override;
|
||||||
render_context& context, const token& token) override;
|
|
||||||
private:
|
private:
|
||||||
const type m_type;
|
const type m_type;
|
||||||
const std::string section_name;
|
const std::string section_name;
|
||||||
template_type section;
|
template_type section;
|
||||||
int skipped_openings;
|
int skipped_openings;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
#include "visitor/render_node.hpp"
|
|
||||||
#include "outside_section.hpp"
|
#include "outside_section.hpp"
|
||||||
|
|
||||||
|
#include "visitor/render_node.hpp"
|
||||||
#include "in_section.hpp"
|
#include "in_section.hpp"
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
std::string state::outside_section::render(
|
std::string state::outside_section::render(
|
||||||
render_context& ctx, const token& token)
|
render_context& ctx, const token& token)
|
||||||
{
|
{
|
||||||
|
using flag = render_node::flag;
|
||||||
switch (token.token_type()) {
|
switch (token.token_type()) {
|
||||||
case token::type::section_open:
|
case token::type::section_open:
|
||||||
ctx.set_state<in_section>(in_section::type::normal, token.name());
|
ctx.set_state<in_section>(in_section::type::normal, token.name());
|
||||||
@ -16,18 +20,15 @@ std::string state::outside_section::render(
|
|||||||
ctx.set_state<in_section>(in_section::type::inverted, token.name());
|
ctx.set_state<in_section>(in_section::type::inverted, token.name());
|
||||||
break;
|
break;
|
||||||
case token::type::variable:
|
case token::type::variable:
|
||||||
return boost::apply_visitor(
|
return visit(render_node(flag::escape_html), ctx.get_node(token.name()));
|
||||||
visitor::render_node(visitor::render_node::flag::escape_html),
|
|
||||||
ctx.get_node(token.name()));
|
|
||||||
case token::type::unescaped_variable:
|
case token::type::unescaped_variable:
|
||||||
return boost::apply_visitor(
|
return visit(render_node(flag::none), ctx.get_node(token.name()));
|
||||||
visitor::render_node(visitor::render_node::flag::none),
|
|
||||||
ctx.get_node(token.name()));
|
|
||||||
case token::type::text:
|
case token::type::text:
|
||||||
return token.raw();
|
return token.raw();
|
||||||
case token::type::partial:
|
case token::type::partial:
|
||||||
return ctx.render_partial(token.name());
|
return ctx.render_partial(token.name());
|
||||||
default: break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,11 @@
|
|||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace state {
|
namespace state {
|
||||||
|
|
||||||
class outside_section: public render_state {
|
class outside_section: public render_state {
|
||||||
public:
|
public:
|
||||||
std::string render(
|
std::string render(render_context& context, const token& token) override;
|
||||||
render_context& context, const token& token) override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "token.hpp"
|
#include "token.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
|
||||||
class render_context;
|
class render_context;
|
||||||
|
|
||||||
namespace state {
|
namespace state {
|
||||||
|
|
||||||
class render_state {
|
class render_state {
|
||||||
public:
|
public:
|
||||||
virtual std::string render(
|
virtual std::string render(render_context& context, const token& token) = 0;
|
||||||
render_context& context, const token& token) = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "token.hpp"
|
#include "token.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
|
||||||
class template_type {
|
class template_type {
|
||||||
public:
|
public:
|
||||||
template_type() = default;
|
template_type() = default;
|
||||||
@ -13,6 +14,7 @@ namespace mstch {
|
|||||||
std::vector<token>::const_iterator begin() const { return tokens.begin(); }
|
std::vector<token>::const_iterator begin() const { return tokens.begin(); }
|
||||||
std::vector<token>::const_iterator end() const { return tokens.end(); }
|
std::vector<token>::const_iterator end() const { return tokens.end(); }
|
||||||
void operator<<(const token& token) { tokens.push_back(token); }
|
void operator<<(const token& token) { tokens.push_back(token); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class parse_state {
|
enum class parse_state {
|
||||||
start, in_del_start, in_del, in_content, in_esccontent, in_del_end
|
start, in_del_start, in_del, in_content, in_esccontent, in_del_end
|
||||||
@ -21,4 +23,5 @@ namespace mstch {
|
|||||||
void strip_whitespace();
|
void strip_whitespace();
|
||||||
std::vector<token> tokens;
|
std::vector<token> tokens;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,11 +24,11 @@ token::token(const std::string& str, std::size_t left, std::size_t right):
|
|||||||
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
|
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
|
||||||
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
|
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
|
||||||
} else {
|
} else {
|
||||||
auto first = first_not_ws(str.begin() + left, str.end() - right);
|
auto c = first_not_ws(str.begin() + left, str.end() - right);
|
||||||
m_type = token_info(*first);
|
m_type = token_info(*c);
|
||||||
if (m_type != type::variable)
|
if (m_type != type::variable)
|
||||||
first = first_not_ws(first + 1, str.end() - right);
|
c = first_not_ws(c + 1, str.end() - right);
|
||||||
m_name = {first, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
|
m_name = {c, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m_type = type::text;
|
m_type = type::text;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
|
||||||
class token {
|
class token {
|
||||||
public:
|
public:
|
||||||
enum class type {
|
enum class type {
|
||||||
@ -15,6 +16,7 @@ namespace mstch {
|
|||||||
const std::string& name() const { return m_name; };
|
const std::string& name() const { return m_name; };
|
||||||
bool eol() const { return m_eol; }
|
bool eol() const { return m_eol; }
|
||||||
bool ws_only() const { return m_ws_only; }
|
bool ws_only() const { return m_ws_only; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
type m_type;
|
type m_type;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
@ -23,4 +25,5 @@ namespace mstch {
|
|||||||
bool m_ws_only;
|
bool m_ws_only;
|
||||||
type token_info(char c);
|
type token_info(char c);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <boost/variant/apply_visitor.hpp>
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
|
||||||
using citer = std::string::const_iterator;
|
using citer = std::string::const_iterator;
|
||||||
using criter = std::string::const_reverse_iterator;
|
using criter = std::string::const_reverse_iterator;
|
||||||
|
|
||||||
citer first_not_ws(citer begin, citer end);
|
citer first_not_ws(citer begin, citer end);
|
||||||
citer first_not_ws(criter begin, criter end);
|
citer first_not_ws(criter begin, criter end);
|
||||||
std::string html_escape(std::string str);
|
std::string html_escape(std::string str);
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
auto visit(Args&& ... args) -> decltype(boost::apply_visitor(
|
||||||
|
std::forward<Args>(args)...))
|
||||||
|
{
|
||||||
|
return boost::apply_visitor(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
#include <boost/blank.hpp>
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
#include <boost/blank.hpp>
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace visitor {
|
namespace visitor {
|
||||||
@ -49,8 +50,7 @@ inline std::string render_section::operator()<array>(const array& array) const {
|
|||||||
out += render_context::push(ctx, array).render(section);
|
out += render_context::push(ctx, array).render(section);
|
||||||
else
|
else
|
||||||
for (auto& item: array)
|
for (auto& item: array)
|
||||||
out += boost::apply_visitor(
|
out += visit(render_section(ctx, section, flag::keep_array), item);
|
||||||
render_section(ctx, section, flag::keep_array), item);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,9 @@ include_directories(
|
|||||||
add_executable(benchmark benchmark_main.cpp)
|
add_executable(benchmark benchmark_main.cpp)
|
||||||
target_link_libraries(benchmark mstch)
|
target_link_libraries(benchmark mstch)
|
||||||
|
|
||||||
add_executable(filetoheader filetoheader.cpp)
|
add_executable(headerize headerize.cpp)
|
||||||
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
target_link_libraries(headerize ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
||||||
set(filetoheader_exe ${CMAKE_CURRENT_BINARY_DIR}/filetoheader${CMAKE_EXECUTABLE_SUFFIX})
|
set(headerize_exe ${CMAKE_CURRENT_BINARY_DIR}/headerize${CMAKE_EXECUTABLE_SUFFIX})
|
||||||
|
|
||||||
file(GLOB data_files RELATIVE
|
file(GLOB data_files RELATIVE
|
||||||
"${CMAKE_SOURCE_DIR}/test/data"
|
"${CMAKE_SOURCE_DIR}/test/data"
|
||||||
@ -27,8 +27,7 @@ file(GLOB string_files RELATIVE
|
|||||||
"${CMAKE_SOURCE_DIR}/test/data"
|
"${CMAKE_SOURCE_DIR}/test/data"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
|
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.txt"
|
"${CMAKE_SOURCE_DIR}/test/data/*.txt"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.partial"
|
"${CMAKE_SOURCE_DIR}/test/data/*.partial")
|
||||||
)
|
|
||||||
|
|
||||||
foreach(string_file ${string_files})
|
foreach(string_file ${string_files})
|
||||||
list(APPEND genargs "-S${string_file}")
|
list(APPEND genargs "-S${string_file}")
|
||||||
@ -36,8 +35,8 @@ endforeach(string_file)
|
|||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp
|
||||||
COMMAND ${filetoheader_exe} --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp --namespace mstchtest ${genargs}
|
COMMAND ${headerize_exe} --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp --namespace mstchtest ${genargs}
|
||||||
DEPENDS ${filetoheader_exe}
|
DEPENDS ${headerize_exe}
|
||||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
|
||||||
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp PROPERTIES GENERATED TRUE)
|
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp PROPERTIES GENERATED TRUE)
|
||||||
add_custom_target(test_data_hpp DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp)
|
add_custom_target(test_data_hpp DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp)
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
unsigned long current_msec() {
|
||||||
|
return std::chrono::system_clock::now().time_since_epoch() /
|
||||||
|
std::chrono::milliseconds(1);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::string comment_tmp{
|
std::string comment_tmp{
|
||||||
"<div class=\"comments\"><h3>{{header}}</h3><ul>"
|
"<div class=\"comments\"><h3>{{header}}</h3><ul>"
|
||||||
@ -20,20 +25,17 @@ int main() {
|
|||||||
}}
|
}}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<int> times;
|
std::vector<unsigned long> times;
|
||||||
for (int j = 0; j < 10; j++) {
|
for (int j = 0; j < 10; j++) {
|
||||||
unsigned long start =
|
unsigned long start = current_msec();
|
||||||
std::chrono::system_clock::now().time_since_epoch() /
|
for (int i = 0; i < 5000; i++)
|
||||||
std::chrono::milliseconds(1);
|
|
||||||
for(int i = 0; i < 5000; i++) {
|
|
||||||
mstch::render(comment_tmp, comment_view);
|
mstch::render(comment_tmp, comment_view);
|
||||||
}
|
times.push_back(current_msec() - start);
|
||||||
times.push_back((std::chrono::system_clock::now().time_since_epoch() /
|
|
||||||
std::chrono::milliseconds(1)) - start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float avg = 0;
|
float avg = 0;
|
||||||
for(int i: times) avg += i;
|
for (auto i: times)
|
||||||
|
avg += i;
|
||||||
avg /= times.size();
|
avg /= times.size();
|
||||||
|
|
||||||
std::cout << avg << std::endl;
|
std::cout << avg << std::endl;
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
#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_code(std::istream& input, std::ostream& output) {
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(input, line)) {
|
|
||||||
output << line;
|
|
||||||
if(!input.eof()) output << std::endl;
|
|
||||||
}
|
|
||||||
output << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wrap_string(std::istream& input, std::ostream& output, const std::string& variable_name) {
|
|
||||||
output << "const std::string " << variable_name << "{" << std::endl;;
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(input, line)) {
|
|
||||||
boost::replace_all(line, "\\", "\\\\");
|
|
||||||
boost::replace_all(line, "\"", "\\\"");
|
|
||||||
output << " \"" << line;
|
|
||||||
if(!input.eof()) output << "\\n";
|
|
||||||
output << "\"" << std::endl;
|
|
||||||
}
|
|
||||||
output << "};" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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::cout << "Output file not set" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ofstream output(vm["output"].as<std::string>(), std::ios::out);
|
|
||||||
|
|
||||||
if(vm.count("namespace"))
|
|
||||||
output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl;
|
|
||||||
|
|
||||||
if(vm.count("input-string")) {
|
|
||||||
for (auto &string_filename: vm["input-string"].as<std::vector<std::string>>()) {
|
|
||||||
std::ifstream input(string_filename, std::ios::in);
|
|
||||||
wrap_string(input, output, boost::replace_all_copy(string_filename, ".", "_"));
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(vm.count("input-code")) {
|
|
||||||
for(auto& data_filename: vm["input-code"].as<std::vector<std::string>>()) {
|
|
||||||
std::ifstream input(data_filename, std::ios::in);
|
|
||||||
wrap_code(input, output);
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(vm.count("namespace"))
|
|
||||||
output << "}" << std::endl;
|
|
||||||
|
|
||||||
output.close();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
89
test/headerize.cpp
Normal file
89
test/headerize.cpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#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_code(std::istream& input, std::ostream& output) {
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(input, line)) {
|
||||||
|
output << line;
|
||||||
|
if (!input.eof())
|
||||||
|
output << std::endl;
|
||||||
|
}
|
||||||
|
output << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wrap_string(std::istream& input, std::ostream& output,
|
||||||
|
const std::string& variable_name)
|
||||||
|
{
|
||||||
|
output << "const std::string " << variable_name << "{" << std::endl;;
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(input, line)) {
|
||||||
|
boost::replace_all(line, "\\", "\\\\");
|
||||||
|
boost::replace_all(line, "\"", "\\\"");
|
||||||
|
output << " \"" << line;
|
||||||
|
if (!input.eof())
|
||||||
|
output << "\\n";
|
||||||
|
output << "\"" << std::endl;
|
||||||
|
}
|
||||||
|
output << "};" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
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::cout << "Output file not set" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream output(vm["output"].as<std::string>(), std::ios::out);
|
||||||
|
|
||||||
|
if (vm.count("namespace"))
|
||||||
|
output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl;
|
||||||
|
|
||||||
|
if (vm.count("input-string")) {
|
||||||
|
for (auto& string_filename: vm["input-string"].as<std::vector<std::string>>()) {
|
||||||
|
std::ifstream input(string_filename, std::ios::in);
|
||||||
|
wrap_string(input, output,
|
||||||
|
boost::replace_all_copy(string_filename, ".", "_"));
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vm.count("input-code")) {
|
||||||
|
for (auto& data_filename: vm["input-code"].as<std::vector<std::string>>()) {
|
||||||
|
std::ifstream input(data_filename, std::ios::in);
|
||||||
|
wrap_code(input, output);
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vm.count("namespace"))
|
||||||
|
output << "}" << std::endl;
|
||||||
|
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user