Определение параметров текущей ОС во время выполнения

This commit is contained in:
2021-07-05 13:11:00 +03:00
parent d159417bbb
commit 2261982479
5 changed files with 174 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ set(TRGT_cpp)
set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
${CMAKE_CURRENT_SOURCE_DIR}/limits.hpp
${CMAKE_CURRENT_SOURCE_DIR}/current_system.hpp
${CMAKE_CURRENT_SOURCE_DIR}/endian_types.hpp
${CMAKE_CURRENT_SOURCE_DIR}/enum_bitmask_operations.hpp)

View File

@@ -0,0 +1,98 @@
#ifndef MYX_CORE_CURRENT_SYSTEM_HPP_
#define MYX_CORE_CURRENT_SYSTEM_HPP_
#pragma once
#include <algorithm>
#include <limits>
#include <fstream>
#include <string>
namespace myx {
namespace core {
class CurrentSystem
{
public:
CurrentSystem( const CurrentSystem& ) = delete;
CurrentSystem& operator=( const CurrentSystem& ) = delete;
CurrentSystem( CurrentSystem&& ) = delete;
CurrentSystem& operator=( CurrentSystem&& ) = delete;
/**
* @brief instance
* @return Уникальный экземпляр класса CurrentSystem
*/
static CurrentSystem& instance()
{
static CurrentSystem sCurrentSystem;
return( sCurrentSystem );
}
std::string os() const { return( m_os ); }
std::string distribution() const { return( m_distribution ); }
std::string variant() const { return( m_variant ); }
std::string version() const { return( m_version ); }
protected:
CurrentSystem() :
m_os
(
#if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __NT__ )
"windows"
#elif __linux__
"linux"
#else
#error "Unknown OS"
#endif
)
{
std::ifstream file( "/etc/os-release" );
if ( file.is_open() )
{
std::string line;
while ( std::getline( file, line ) )
{
std::size_t pos = line.find( "ID=" );
if ( pos == 0 )
{
m_distribution = line.replace( pos, sizeof( "ID=" ) - 1, "" );
}
pos = line.find( "VARIANT_ID=" );
if ( pos != std::string::npos )
{
m_variant = line.replace( pos, sizeof( "VARIANT_ID=" ) - 1, "" );
}
pos = line.find( "VERSION_ID=" );
if ( pos != std::string::npos )
{
m_version = line.replace( pos, sizeof( "VERSION_ID=" ) - 1, "" );
while ( ( pos = m_version.find( '"' ) ) != std::string::npos )
{
m_version.erase( pos, sizeof( '"' ) );
}
}
}
file.close();
}
}
~CurrentSystem() = default;
private:
std::string m_os;
std::string m_distribution;
std::string m_variant;
std::string m_version;
}; // class CurrentSystem
// class CurrentSystem
} // namespace core
} // namespace myx
#endif // MYX_CORE_CURRENT_SYSTEM_HPP_