Skip to content

Commit

Permalink
Apply clang-format (Google, ColumnLimit:120)
Browse files Browse the repository at this point in the history
Used command:

    clang-format --style="{BasedOnStyle: Google, ColumnLimit: 120}" -i mst.cxx
  • Loading branch information
kgadek committed Apr 16, 2022
1 parent 86d3864 commit 40bca0e
Showing 1 changed file with 36 additions and 47 deletions.
83 changes: 36 additions & 47 deletions mst.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// vim: sw=4 ts=4 expandtab
// vim: sw=4 ts=4 expandtab cc=120

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -16,52 +16,41 @@
#include <string>
#include <utility>

int main(int argc, char *argv[]) {
try {
mstch::config::escape = [](const std::string& str) -> std::string {
return str;
};
std::ifstream tpl_fstream{ argv[1] };
std::string tpl{ std::istreambuf_iterator<char>{ tpl_fstream },
std::istreambuf_iterator<char>{} };
mstch::map ctx{
{"include", mstch::lambda{ [](const std::string &filename) -> mstch::node {
std::ifstream file_fstream{ filename };
if((file_fstream.rdstate() & std::ifstream::failbit) != 0)
throw std::runtime_error("Can't open file: " + filename);
return std::string { std::istreambuf_iterator<char>{ file_fstream },
std::istreambuf_iterator<char>{} };
}}},
{"env", mstch::lambda{ [](const std::string &env_var_name) -> mstch::node {
if(char *env_var_value = std::getenv(env_var_name.c_str()))
return std::string{env_var_value};
throw std::runtime_error("Env var doesn't exist: " + env_var_name);
}}},
{"cmd", mstch::lambda{ [](const std::string &cmd) -> mstch::node {
// https://stackoverflow.com/a/478960/547223
std::array<char, 16384> buffer; // 16 KiB
buffer.fill('\0');
std::string stdout;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if(!pipe)
throw std::runtime_error("Command failed: " + cmd);
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
stdout += buffer.data();
return stdout;
}}}
};
int main(int argc, char* argv[]) {
using namespace std;
try {
mstch::config::escape = [](const string& str) -> string { return str; };
ifstream tpl_fstream{argv[1]};
string tpl{istreambuf_iterator<char>{tpl_fstream}, istreambuf_iterator<char>{}};
mstch::map ctx{{"include", mstch::lambda{[](const string& filename) -> mstch::node {
ifstream file_fstream{filename};
if ((file_fstream.rdstate() & ifstream::failbit) != 0)
throw runtime_error("Can't open file: " + filename);
return string{istreambuf_iterator<char>{file_fstream}, istreambuf_iterator<char>{}};
}}},
{"env", mstch::lambda{[](const string& env_var_name) -> mstch::node {
if (char* env_var_value = getenv(env_var_name.c_str())) return string{env_var_value};
throw runtime_error("Env var doesn't exist: " + env_var_name);
}}},
{"cmd", mstch::lambda{[](const string& cmd) -> mstch::node {
// https://stackoverflow.com/a/478960/547223
array<char, 16384> buffer; // 16 KiB
buffer.fill('\0');
string stdout;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) throw runtime_error("Command failed: " + cmd);
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) stdout += buffer.data();
return stdout;
}}}};

for(int i=2; i+1<argc; i+=2)
ctx.emplace(std::make_pair(std::string(argv[i]), std::string(argv[i+1])));
for (int i = 2; i + 1 < argc; i += 2) ctx.emplace(make_pair(string(argv[i]), string(argv[i + 1])));

std::cout << mstch::render(tpl, ctx);
} catch (...) {
std::cerr << "Invocation:\n" << std::endl;
for(int i=0; i<argc; ++i)
std::cerr << " '" << argv[i] << "'\n";
std::cerr << boost::current_exception_diagnostic_information() << std::endl;
return -1;
}
return 0;
cout << mstch::render(tpl, ctx);
} catch (...) {
cerr << "Invocation:\n" << endl;
for (int i = 0; i < argc; ++i) cerr << " '" << argv[i] << "'\n";
cerr << boost::current_exception_diagnostic_information() << endl;
return -1;
}
return 0;
}

0 comments on commit 40bca0e

Please sign in to comment.