GridFire 0.0.1a General Purpose Nuclear Network

Loading...
Searching...
No Matches
network_file.cpp
Go to the documentation of this file.
1#include "gridfire/io/network_file.h"
2
3#include <string>
4#include <vector>
5#include <algorithm>
6#include <fstream>
7#include <stdexcept>
8
9#include "quill/LogMacros.h"
10
11 namespace 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 }
42 SimpleReactionListFileParser::SimpleReactionListFileParser() {}
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 while (std::getline(file, line)) {
57 LOG_TRACE_L3(m_logger, "Parsing reaction list file {}, line: {}", filename, line);
58
59 const size_t comment_pos = line.find('#');
60 if (comment_pos != std::string::npos) {
61 line = line.substr(0, comment_pos);
62 }
63
64 trim(line);
65
66 if (line.empty()) {
67 continue; // Skip empty lines
68 }
69 parsed.push_back(line);
70 }
71 LOG_TRACE_L1(m_logger, "Parsed {} reactions from file: {}", parsed.size(), filename);
72 return parsed;
73 }
74
75}
gridfire::io::SimpleReactionListFileParser::parse
ParsedNetworkData parse(const std::string &filename) const override
Parses a simple reaction list file.
Definition network_file.cpp:44
gridfire::io::SimpleReactionListFileParser::m_logger
quill::Logger * m_logger
Definition network_file.h:107
gridfire::io::SimpleReactionListFileParser::SimpleReactionListFileParser
SimpleReactionListFileParser()
Constructs a SimpleReactionListFileParser.
Definition network_file.cpp:42
gridfire::io
Definition network_file.h:11
gridfire::io::ParsedNetworkData
std::vector< std::string > ParsedNetworkData
Definition network_file.h:12
network_file.h