ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InputsName.H
Go to the documentation of this file.
1 #ifndef ERF_INPUTS_NAME_H_
2 #define ERF_INPUTS_NAME_H_
3 
4 #include <AMReX.H>
5 #include <AMReX_Print.H>
6 
7 #include <fstream>
8 #include <string>
9 #include <unordered_map>
10 
11 extern std::string inputs_name;
12 
13 namespace
14 {
15  std::string trim_copy (std::string s)
16  {
17  auto b = s.find_first_not_of(" \t\r");
18  if (b == std::string::npos) return "";
19 
20  auto e = s.find_last_not_of(" \t\r");
21  return s.substr(b, e - b + 1);
22  }
23 }
24 
25 inline void
26 CheckForDuplicateInputs (const std::string& inputs_file)
27 {
28  std::ifstream ifs(inputs_file);
29 
30  if (!ifs.is_open()) {
31  amrex::Abort("Could not open inputs file: " + inputs_file);
32  }
33 
34  struct EntryInfo {
35  int line_number;
36  std::string value;
37  };
38 
39  std::unordered_map<std::string, EntryInfo> seen;
40 
41  std::string line;
42  int line_number = 0;
43  bool found_duplicates = false;
44 
45  while (std::getline(ifs, line))
46  {
47  ++line_number;
48 
49  // Remove comments
50  auto hash_pos = line.find('#');
51  if (hash_pos != std::string::npos) {
52  line = line.substr(0, hash_pos);
53  }
54 
55  line = trim_copy(line);
56 
57  // Skip blank lines
58  if (line.empty()) continue;
59 
60  // Skip lines that are not assignments
61  auto eq_pos = line.find('=');
62  if (eq_pos == std::string::npos) continue;
63 
64  std::string key = trim_copy(line.substr(0, eq_pos));
65  std::string value = trim_copy(line.substr(eq_pos + 1));
66 
67  auto it = seen.find(key);
68 
69  if (it != seen.end())
70  {
71  found_duplicates = true;
72 
73  amrex::Print()
74  << "\nDuplicate input detected:\n"
75  << " Key : " << key << "\n"
76  << " First line : " << it->second.line_number
77  << " Value : " << it->second.value << "\n"
78  << " Second line : " << line_number
79  << " Value : " << value << "\n";
80  }
81  else
82  {
83  seen.emplace(key, EntryInfo{line_number, value});
84  }
85  }
86 
87  if (found_duplicates)
88  {
89  amrex::Abort("Duplicate inputs detected in inputs file");
90  }
91 }
92 
93 #endif
amrex::Real value
Definition: ERF_HurricaneDiagnostics.H:20
void CheckForDuplicateInputs(const std::string &inputs_file)
Definition: ERF_InputsName.H:26
std::string inputs_name
Definition: main.cpp:15