GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
network_file.cpp
Go to the documentation of this file.
2
3#include <string>
4#include <vector>
5#include <algorithm>
6#include <fstream>
7#include <stdexcept>
8
9#include "quill/LogMacros.h"
10
11namespace gridfire::io {
12 namespace {
13 inline void ltrim(std::string &s) {
14 s.erase(
15 s.begin(),
16 std::ranges::find_if(s,
17 [](const unsigned char ch) {
18 return !std::isspace(ch);
19 })
20 );
21 }
22
23 inline void rtrim(std::string &s) {
24 s.erase(
25 std::find_if(
26 s.rbegin(),
27 s.rend(),
28 [](const unsigned char ch) {
29 return !std::isspace(ch);
30 }).base(),
31 s.end()
32 );
33 }
34
35 inline void trim(std::string &s) {
36 ltrim(s);
37 rtrim(s);
38 }
39
40
41 }
43
44 ParsedNetworkData SimpleReactionListFileParser::parse(const std::string& filename) const {
45 LOG_TRACE_L1(m_logger, "Parsing simple reaction list file: {}", filename);
46
47 std::ifstream file(filename);
48 if (!file.is_open()) {
49 LOG_ERROR(m_logger, "Failed to open file: {}", filename);
50 m_logger -> flush_log();
51 throw std::runtime_error("Could not open file: " + filename);
52 }
53
54 ParsedNetworkData parsed;
55 std::string line;
56 int line_number = 0;
57 while (std::getline(file, line)) {
58 line_number++;
59 LOG_TRACE_L3(m_logger, "Parsing reaction list file {}, line {}: {}", filename, line_number, line);
60
61 const size_t comment_pos = line.find('#');
62 if (comment_pos != std::string::npos) {
63 line = line.substr(0, comment_pos);
64 }
65
66 trim(line);
67
68 if (line.empty()) {
69 continue; // Skip empty lines
70 }
71 parsed.reactionPENames.push_back(line);
72 }
73 LOG_TRACE_L1(m_logger, "Parsed {} reactions from file: {}", parsed.reactionPENames.size(), filename);
74 return parsed;
75 }
76
77}
ParsedNetworkData parse(const std::string &filename) const override
Parses a simple reaction list file.
SimpleReactionListFileParser()
Constructs a SimpleReactionListFileParser.
Holds the data parsed from a network file.
std::vector< std::string > reactionPENames
A vector of reaction names in their PEN-style format.