diff --git a/examples/core/02_current-system/CMakeLists.txt b/examples/core/02_current-system/CMakeLists.txt new file mode 100644 index 0000000..623cb69 --- /dev/null +++ b/examples/core/02_current-system/CMakeLists.txt @@ -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() diff --git a/examples/core/02_current-system/current_system.cpp b/examples/core/02_current-system/current_system.cpp new file mode 100644 index 0000000..45a122a --- /dev/null +++ b/examples/core/02_current-system/current_system.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include + +//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 diff --git a/examples/core/CMakeLists.txt b/examples/core/CMakeLists.txt index 01da3ee..808a6b3 100644 --- a/examples/core/CMakeLists.txt +++ b/examples/core/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(01_endian) +add_subdirectory(02_current-system) diff --git a/src/myx/core/CMakeLists.txt b/src/myx/core/CMakeLists.txt index 17a50f5..78e5285 100644 --- a/src/myx/core/CMakeLists.txt +++ b/src/myx/core/CMakeLists.txt @@ -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) diff --git a/src/myx/core/current_system.hpp b/src/myx/core/current_system.hpp new file mode 100644 index 0000000..4ba452a --- /dev/null +++ b/src/myx/core/current_system.hpp @@ -0,0 +1,98 @@ +#ifndef MYX_CORE_CURRENT_SYSTEM_HPP_ +#define MYX_CORE_CURRENT_SYSTEM_HPP_ + +#pragma once + +#include +#include +#include +#include + +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_