Skip to content
Snippets Groups Projects
ParseInputFile.cpp 2.34 KiB
Newer Older
#include "ParseInputFile.h"

/* Parsed Data */
ParseInputFile::ParseInputFile(const ParseCommandLine& cmdln)
	: _hasCells(false), _hasRadii(false), _hasCellSize(false), _hasPositions(false), _hasCellType(false),
	_nx(0), _ny(0), _nz(0), _cx(0.0), _cy(0.0), _cz(0.0)
{
	if (cmdln.hasKey("celltype"))
	{
		_cellType = cmdln.getValue("celltype");
		_hasCellType = true;
	}

	if (cmdln.hasKey("input"))
		if (!parseFile(cmdln.getValue("input")))
			return;
	if (!_hasRadii)
		_errors.emplace_back(ErrorCodes::StrutRadiusNotDefined);
	if (!_hasCellSize)
		_errors.emplace_back(ErrorCodes::CellSizeNotDefined);
}

/* Read Input File */
bool ParseInputFile::parseFile(const std::string filepath)
{
	std::fstream file(filepath);
	if (!file.is_open())
	{
		_errors.emplace_back(ErrorCodes::FileNotFound);
	}

	std::string line;
	{
		// Skip commented line
		if (line[0] == '%')
			continue;

		// Read line argument
		std::istringstream iss(line);
		std::string argument;
		iss >> argument;
		if (argument == "Cells" && !_hasCells)
		{
			iss >> _nx >> _ny >> _nz;
			_hasCells = true;
		}
		else if (argument == "Size" && !_hasCellSize)
		{
			iss >> _cx >> _cy >> _cz;
			_hasCellSize = true;
		}
		else if (argument == "Radii")
		{
			size_t nCells;
			iss >> nCells;
		}
		else if (argument == "Positions")
		{
			size_t nPoints;
			iss >> nPoints;
		else if (argument == "CellType" && !_hasCellType)
		{
			iss >> _cellType;
			for (auto& c : _cellType)
				c = tolower(c);
			_hasCellType = true;
		}
void ParseInputFile::parseRadii(std::fstream& file, size_t nCells)
{
	_radii.reserve(nCells);
	for (size_t i = 0; i < nCells; i++)
	{
		std::string line;
		std::getline(file, line);
		std::istringstream iss(line);
		double radius;
		iss >> radius;
		_radii.emplace_back(radius);
	}
	_hasRadii = true;
}

void ParseInputFile::parsePositions(std::fstream& file, size_t nPositions)
{
	_points.reserve(nPositions);
	for (size_t i = 0; i < nPositions; i++)
	{
		std::string line;
		std::getline(file, line);
		std::istringstream iss(line);
		double x, y, z;
		iss >> x >> y >> z;
		_points.emplace_back(x, y, z);
	}

	_hasPositions = true;
}

void ParseInputFile::dumpErrors()
{
	for (const ErrorCodes code : _errors)
	{
		ERROR_MSG("ParseInputFile", code);
	}
}