Skip to content

Commit

Permalink
Fix old compiler support (idaholab#19001)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Oct 25, 2021
1 parent ac1444a commit b059a33
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions framework/contrib/hit/braceexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class BraceExpander;
class Evaler
{
public:
virtual ~Evaler() {}
virtual std::string eval(Field * n, const std::list<std::string> & args, BraceExpander & exp) = 0;
};

Expand Down
56 changes: 23 additions & 33 deletions framework/contrib/hit/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ globCompare(const std::string & candidate,
return globCompare(candidate, pattern, c + 1, p + 1);
}

std::string
readInput(const std::string & fname)
{
if (fname == "-")
return std::string(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
else
{
std::ifstream f(fname);
if (!f)
throw std::runtime_error("Can't open '" + fname + "'");
return std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
}
}

int
main(int argc, char ** argv)
{
Expand Down Expand Up @@ -247,7 +261,7 @@ int
findParam(int argc, char ** argv)
{
Flags flags(
"hit find [flags] <parameter-pathern> <file>...\n Specify '-' as a file name to accept "
"hit find [flags] <parameter-pattern> <file>...\n Specify '-' as a file name to accept "
"input from stdin.\n A pattern has the form param[=value] and wildcards (*,?) may be used");
flags.add("f", "only show file name");
flags.add("i", "case insensitive matches");
Expand Down Expand Up @@ -295,10 +309,7 @@ findParam(int argc, char ** argv)

// load and parse input
std::string fname(positional[i]);
std::istream && f =
(fname == "-" ? (std::istream &&) std::cin : (std::istream &&) std::ifstream(fname));
std::string input((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());

std::string input = readInput(fname);
std::unique_ptr<hit::Node> root;
try
{
Expand Down Expand Up @@ -446,16 +457,8 @@ format(int argc, char ** argv)
int ret = 0;
for (int i = 0; i < positional.size(); i++)
{
std::string fname(positional[i]);
std::istream && f =
(fname == "-" ? (std::istream &&) std::cin : (std::istream &&) std::ifstream(fname));
if (!f)
{
std::cerr << "Can't open '" << fname << "'\n";
return 1;
}
std::string input(std::istreambuf_iterator<char>(f), {});

const std::string fname(positional[i]);
const std::string input = readInput(fname);
try
{
auto fmted = fmt.format(fname, input);
Expand Down Expand Up @@ -485,15 +488,7 @@ readMerged(const std::vector<std::string> & input_filenames)

for (auto & input_filename : input_filenames)
{
std::ifstream f(input_filename);
if (!f)
{
std::cerr << "Can't open '" << input_filename << "'\n";
return nullptr;
}

std::string input((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());

std::string input = readInput(input_filename);
std::unique_ptr<hit::Node> root(hit::parse(input_filename, input));
hit::explode(root.get());

Expand Down Expand Up @@ -532,10 +527,8 @@ merge(int argc, char ** argv)
hit::Node * root = nullptr;
for (int i = 0; i < positional.size(); i++)
{
std::string fname(positional[i]);
std::istream && f =
(fname == "-" ? (std::istream &&) std::cin : (std::istream &&) std::ifstream(fname));
std::string input(std::istreambuf_iterator<char>(f), {});
const std::string fname(positional[i]);
const std::string input = readInput(fname);
if (root)
hit::merge(hit::parse(fname, input), root);
else
Expand Down Expand Up @@ -808,11 +801,8 @@ validate(int argc, char ** argv)
int ret = 0;
for (int i = 0; i < argc; i++)
{
std::string fname(argv[i]);
std::istream && f =
(fname == "-" ? (std::istream &&) std::cin : (std::istream &&) std::ifstream(fname));
std::string input((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());

const std::string fname(argv[i]);
const std::string input = readInput(fname);
std::unique_ptr<hit::Node> root;
try
{
Expand Down
4 changes: 3 additions & 1 deletion framework/contrib/hit/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Error : public std::exception
{
public:
Error(const std::string & msg);
virtual const char * what() const throw() override;
virtual const char * what() const noexcept override;
std::string msg;
};

Expand All @@ -113,6 +113,8 @@ class ParseError : public Error
class Walker
{
public:
virtual ~Walker() {}

/// walk is called when the walker is passed into a Node::walk function for each relevant node in
/// the hit (sub)tree. fullpath is the fully-qualified (absolute) path to the hit node
/// where each section header is a path-element. nodepath is the path for the node of interest -
Expand Down
23 changes: 17 additions & 6 deletions test/tests/misc/hit_cli/hit_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import sys
import os
import shutil
import subprocess

if len(sys.argv) < 2:
print("Usage:\nhit_wrapper.py gold_output [hit arguments]\n")

MOOSE_DIR = os.getenv('MOOSE_DIR', os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', '..', '..')))
CWD = os.path.dirname(os.path.realpath(__file__))
MOOSE_DIR = os.getenv('MOOSE_DIR', os.path.abspath(os.path.join(CWD, '..', '..', '..', '..')))

# in place compiled hit
hit = os.path.join(MOOSE_DIR, 'framework', 'contrib', 'hit', 'hit')
if not os.path.exists(hit):
# installed hit
hit = os.path.abspath(os.path.join(CWD, '../../../../../../bin/hit'))
if not os.path.exists(hit):
print('Failed to locate hit executable.')
sys.exit(1)
Expand All @@ -19,14 +26,18 @@
with open(gold_file, 'rb') as f:
gold = f.read()

try:
out = subprocess.check_output([hit] + hit_args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
return_code = err.returncode
out = err.output
command = [hit] + hit_args
print("Running: ", ' '.join(command))

p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE)
p.wait()
print("hit returned ", p.returncode)
out = p.communicate()[0]

if out == gold:
print("Passed.")
sys.exit(0)

print("Output:\n", out, "\ndoes not match gold:\n", gold)
print(sys.version)
sys.exit(1)

0 comments on commit b059a33

Please sign in to comment.