Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Source/ERF_InputsName.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,84 @@

extern std::string inputs_name;

namespace
{
std::string trim_copy (std::string s)
{
auto b = s.find_first_not_of(" \t\r");
if (b == std::string::npos) return "";

auto e = s.find_last_not_of(" \t\r");
return s.substr(b, e - b + 1);
}
}

inline void
CheckForDuplicateInputs (const std::string& inputs_file)
{
std::ifstream ifs(inputs_file);

if (!ifs.is_open()) {
amrex::Abort("Could not open inputs file: " + inputs_file);
}

struct EntryInfo {
int line_number;
std::string value;
};

std::unordered_map<std::string, EntryInfo> seen;

std::string line;
int line_number = 0;
bool found_duplicates = false;

while (std::getline(ifs, line))
{
++line_number;

// Remove comments
auto hash_pos = line.find('#');
if (hash_pos != std::string::npos) {
line = line.substr(0, hash_pos);
}

line = trim_copy(line);

// Skip blank lines
if (line.empty()) continue;

// Skip lines that are not assignments
auto eq_pos = line.find('=');
if (eq_pos == std::string::npos) continue;

std::string key = trim_copy(line.substr(0, eq_pos));
std::string value = trim_copy(line.substr(eq_pos + 1));

auto it = seen.find(key);

if (it != seen.end())
{
found_duplicates = true;

amrex::Print()
<< "\nDuplicate input detected:\n"
<< " Key : " << key << "\n"
<< " First line : " << it->second.line_number
<< " Value : " << it->second.value << "\n"
<< " Second line : " << line_number
<< " Value : " << value << "\n";
}
else
{
seen.emplace(key, EntryInfo{line_number, value});
}
}

if (found_duplicates)
{
amrex::Abort("Duplicate inputs detected in inputs file");
}
}

#endif
1 change: 1 addition & 0 deletions Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ return code;
#else
amrex::Initialize(argc,argv,true,MPI_COMM_WORLD,add_par);
#endif
CheckForDuplicateInputs(argv[1]);

#ifdef ERF_USE_KOKKOS
// Initialize kokkos
Expand Down
Loading