#include #include "port_number.hpp" #include "logger/logger_set.hpp" namespace smtp::checker::errors::settings { bool PortNumber::Check( manage::SettingsFileDataType const& line ) const { static const std::string PORT_FIELD = "port"; static constexpr int MIN_PORT_NUMBER = 0; static constexpr int MAX_PORT_NUMBER = 65535; //TODO общее использование полей auto find = line.find( PORT_FIELD ); if( find == line.end() ) { logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't found" ); return false; } auto host_as_string = find->second; if( host_as_string.empty()) { return true; } int host_as_int{}; try { host_as_int = std::stoi( host_as_string ); } catch( std::invalid_argument const& ex ) { logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't entered by numbers" ); return false; } catch( std::out_of_range const& ex ) { logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port out of range" ); return false; } catch( ... ) { logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't entered by numbers" ); return false; } auto result = host_as_int >= MIN_PORT_NUMBER && host_as_int <= MAX_PORT_NUMBER; if( !result ) { logger::LoggerSet::GetInstance()->LogError( GetMethodName(), "Port doesn't entered by numbers" ); } return result; } }