Program Listing for File SourceReader.h¶
↰ Return to documentation for file (include/file_manager/SourceReader.h)
// _______. ______ __ ___ .__ __. .___________. __ ___ ___ //
// / | / || | / \ | \ | | | || | \ \ / / //
// | (----`| ,----'| | / ^ \ | \| | `---| |----`| | \ V / //
// \ \ | | | | / /_\ \ | . ` | | | | | > < //
// .----) | | `----.| | / _____ \ | |\ | | | | | / . \ //
// |_______/ \______||__| /__/ \__\ |__| \__| |__| |__| /__/ \__\ //
// //
// Originally developed by D. Pizzocri & T. Barani //
// //
// Version: 2.1 //
// Year: 2024 //
// Authors: D. Pizzocri, G. Zullo. //
// //
#ifndef SOURCE_READER_H
#define SOURCE_READER_H
#include "Source.h"
#include <fstream>
#include <iostream>
#include <string>
void ReadSource(double& Source_slope_input, double& Source_intercept_input)
{
// Initialize variables
Source_slope_input = 0.0;
Source_intercept_input = 0.0;
// Attempt to open the file
std::ifstream source_file(TestPath + "source.txt", std::ios::in);
if (source_file.is_open())
{
double slope, intercept;
std::string line;
while (std::getline(source_file, line))
{
// Skip comments and empty lines
if (line.empty() || line[0] == '#')
{
continue;
}
std::istringstream iss(line);
if (iss >> slope >> intercept) // Read two values from the line
{
Source_slope_input = slope;
Source_intercept_input = intercept;
break; // Stop reading after the first valid line
}
}
// Close the file
source_file.close();
}
}
void ReadNonUniformSource(Source& non_uniform_source)
{
// Attempt to open the file
std::ifstream source_file(TestPath + "non_uniform_source.txt", std::ios::in);
// Missing file
if (!source_file.is_open())
{
std::cerr << "Error: Unable to open file | File is missing, cannot run: " << TestPath + "general_source.txt"
<< "\n";
exit(EXIT_FAILURE); // Terminate the program with an error code
}
std::string line, token;
std::getline(source_file, line); // Read the entire file into a single string
std::stringstream ss(line);
// Parse the time
std::getline(ss, token, '#');
non_uniform_source.time = std::stod(token);
// Parse Normalized Domain
std::getline(ss, token, '#');
std::stringstream NormalizedDomainStream(token);
double value;
while (NormalizedDomainStream >> value)
{
non_uniform_source.NormalizedDomain.push_back(value);
}
// Parse Slopes
std::getline(ss, token, '#');
std::stringstream SlopesStream(token);
while (SlopesStream >> value)
{
non_uniform_source.Slopes.push_back(value);
}
// Parse Intercepts
std::getline(ss, token, '#');
std::stringstream InterceptsStream(token);
while (InterceptsStream >> value)
{
non_uniform_source.Intercepts.push_back(value);
}
}
#endif // SOURCE_READER_H