Определение параметров текущей ОС во время выполнения
This commit is contained in:
parent
d159417bbb
commit
2261982479
51
examples/core/02_current-system/CMakeLists.txt
Normal file
51
examples/core/02_current-system/CMakeLists.txt
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# Название основной цели в текущем каталоге
|
||||||
|
set(TRGT example-core-current-system)
|
||||||
|
|
||||||
|
# Список файлов исходных текстов
|
||||||
|
set(TRGT_cpp ${CMAKE_CURRENT_SOURCE_DIR}/current_system.cpp)
|
||||||
|
|
||||||
|
if(MYXLIB_BUILD_EXAMPLES)
|
||||||
|
# Путь поиска библиотек внутри проекта
|
||||||
|
link_directories(${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
|
||||||
|
|
||||||
|
# Цель для создания исполняемого файла
|
||||||
|
add_executable(${TRGT} ${TRGT_cpp} ${TRGT_qrc})
|
||||||
|
common_target_properties(${TRGT})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clang-tidy
|
||||||
|
add_clang_tidy_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clang-analyze
|
||||||
|
add_clang_analyze_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clazy
|
||||||
|
add_clazy_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой pvs-studio
|
||||||
|
add_pvs_check(${TRGT})
|
||||||
|
|
||||||
|
# Создание цели для автоматического форматирования кода
|
||||||
|
add_format_sources(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Qt5
|
||||||
|
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||||
|
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||||
|
add_dependencies(${TRGT} core)
|
||||||
|
|
||||||
|
target_link_libraries(${TRGT} Qt5::Core)
|
||||||
|
target_link_libraries(${TRGT} Threads::Threads)
|
||||||
|
|
||||||
|
# Имя выходного файла для цели
|
||||||
|
set_target_properties(${TRGT} PROPERTIES OUTPUT_NAME current-system-minimal)
|
||||||
|
|
||||||
|
add_sanitizers(${TRGT})
|
||||||
|
|
||||||
|
cotire(${TRGT})
|
||||||
|
|
||||||
|
add_dependencies(${TRGT} create_auxilary_symlinks)
|
||||||
|
|
||||||
|
# Правила для установки
|
||||||
|
# install(TARGETS ${TRGT} COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
endif()
|
23
examples/core/02_current-system/current_system.cpp
Normal file
23
examples/core/02_current-system/current_system.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <myx/core/config.hpp>
|
||||||
|
#include <myx/core/current_system.hpp>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
//NOLINTNEXTLINE
|
||||||
|
#define CMLIB_PROJECT_NAME "myxlib"
|
||||||
|
|
||||||
|
namespace MC = myx::core;
|
||||||
|
|
||||||
|
int main( int argc, char** argv )
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
|
||||||
|
MC::CurrentSystem& currentSystem = MC::CurrentSystem::instance();
|
||||||
|
qDebug() << "Current OS: " << QString::fromStdString( currentSystem.os() );
|
||||||
|
qDebug() << "OS distrib: " << QString::fromStdString( currentSystem.distribution() );
|
||||||
|
qDebug() << "OS variant: " << QString::fromStdString( currentSystem.variant() );
|
||||||
|
qDebug() << "OS version: " << QString::fromStdString( currentSystem.version() );
|
||||||
|
|
||||||
|
return( 0 );
|
||||||
|
} // main
|
@ -1 +1,2 @@
|
|||||||
add_subdirectory(01_endian)
|
add_subdirectory(01_endian)
|
||||||
|
add_subdirectory(02_current-system)
|
||||||
|
@ -9,6 +9,7 @@ set(TRGT_cpp)
|
|||||||
set(TRGT_hpp
|
set(TRGT_hpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
|
${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/limits.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}/endian_types.hpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/enum_bitmask_operations.hpp)
|
${CMAKE_CURRENT_SOURCE_DIR}/enum_bitmask_operations.hpp)
|
||||||
|
|
||||||
|
98
src/myx/core/current_system.hpp
Normal file
98
src/myx/core/current_system.hpp
Normal 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_
|
Loading…
x
Reference in New Issue
Block a user