2025-02-11 18:02:07 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <fstream>
|
2025-02-12 12:53:50 -05:00
|
|
|
#include <sstream>
|
2025-02-11 18:02:07 -05:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
#include "const.h"
|
|
|
|
|
#include "embedded_constants.h" // Generated at build time by meson
|
2025-02-11 18:02:07 -05:00
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
Constants::Constants() {
|
|
|
|
|
loaded_ = initialize();
|
2025-02-11 18:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
bool Constants::initialize() {
|
|
|
|
|
return load();
|
2025-02-11 18:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
Constant Constants::get(const std::string& name) const {
|
2025-02-11 18:02:07 -05:00
|
|
|
auto it = constants_.find(name);
|
|
|
|
|
if (it != constants_.end()) {
|
|
|
|
|
return it->second;
|
|
|
|
|
} else {
|
|
|
|
|
throw std::out_of_range("Constant '" + name + "' not found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
Constant Constants::operator[](const std::string& name) const {
|
2025-02-12 11:16:40 -05:00
|
|
|
return this->get(name);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
bool Constants::has(const std::string& name) const {
|
2025-02-11 18:02:07 -05:00
|
|
|
return constants_.find(name) != constants_.end();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
std::set<std::string> Constants::keys() const {
|
2025-02-11 18:02:07 -05:00
|
|
|
std::set<std::string> keys;
|
|
|
|
|
for (const auto& pair : constants_) {
|
|
|
|
|
keys.insert(pair.first);
|
|
|
|
|
}
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
std::string Constants::trim(const std::string& str) {
|
2025-02-11 18:02:07 -05:00
|
|
|
size_t first = str.find_first_not_of(" \t");
|
|
|
|
|
if (first == std::string::npos) return "";
|
|
|
|
|
size_t last = str.find_last_not_of(" \t");
|
|
|
|
|
return str.substr(first, last - first + 1);
|
|
|
|
|
}
|
2025-02-12 11:16:40 -05:00
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
bool Constants::load() {
|
|
|
|
|
std::istringstream fileStream(embeddedConstants);
|
2025-02-11 18:02:07 -05:00
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
|
bool data_section = false;
|
|
|
|
|
int line_count = 0;
|
|
|
|
|
|
2025-02-12 12:53:50 -05:00
|
|
|
while (std::getline(fileStream, line)) {
|
2025-02-11 18:02:07 -05:00
|
|
|
line_count++;
|
|
|
|
|
|
|
|
|
|
// Detect start of data section (double divider line)
|
|
|
|
|
if (!data_section) {
|
|
|
|
|
if (line.find("Symbol") != std::string::npos) { // Find header row
|
2025-02-12 12:53:50 -05:00
|
|
|
std::getline(fileStream, line); // Skip dashed divider
|
|
|
|
|
std::getline(fileStream, line); // Skip second dashed divider
|
2025-02-11 18:02:07 -05:00
|
|
|
data_section = true;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure the line is long enough to contain all fields
|
|
|
|
|
if (line.length() < 125) continue;
|
|
|
|
|
|
|
|
|
|
// Define exact column widths from Python script
|
|
|
|
|
int start = 0;
|
|
|
|
|
|
2025-02-12 11:16:40 -05:00
|
|
|
const std::string symbol = trim(line.substr(start, col_widths_[0])); start += col_widths_[0];
|
|
|
|
|
const std::string name = trim(line.substr(start, col_widths_[1])); start += col_widths_[1];
|
|
|
|
|
const std::string valueStr = line.substr(start, col_widths_[2]); start += col_widths_[2];
|
|
|
|
|
const std::string unit = trim(line.substr(start, col_widths_[3])); start += col_widths_[3]; // Only trim the unit
|
|
|
|
|
const std::string uncertaintyStr = line.substr(start, col_widths_[4]); start += col_widths_[4];
|
|
|
|
|
const std::string reference = trim(line.substr(start, col_widths_[5])); // Only trim reference
|
2025-02-11 18:02:07 -05:00
|
|
|
|
|
|
|
|
// Convert numerical fields safely
|
|
|
|
|
double value = 0.0, uncertainty = 0.0;
|
|
|
|
|
try {
|
|
|
|
|
value = std::stod(valueStr);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
std::cerr << "Warning: Invalid value in line " << line_count << ": " << valueStr << std::endl;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
uncertainty = std::stod(uncertaintyStr);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
std::cerr << "Warning: Invalid uncertainty in line " << line_count << ": " << uncertaintyStr << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store in map
|
2025-02-12 12:53:50 -05:00
|
|
|
constants_.emplace(symbol, Constant{name, value, uncertainty, unit, reference});
|
2025-02-11 18:02:07 -05:00
|
|
|
}
|
|
|
|
|
loaded_ = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|