Adding common functions (read file)
This commit is contained in:
		
					parent
					
						
							
								ec4d60142d
							
						
					
				
			
			
				commit
				
					
						1681640aaa
					
				
			
		
					 4 changed files with 63 additions and 0 deletions
				
			
		
							
								
								
									
										9
									
								
								common/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								common/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
project(common_lib CXX)
 | 
			
		||||
 | 
			
		||||
add_library(common SHARED common_functions.cpp common_functions.hpp)
 | 
			
		||||
 | 
			
		||||
target_compile_definitions(common PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
 | 
			
		||||
 | 
			
		||||
target_compile_options(common PUBLIC ${COMPILE_FLAGS})
 | 
			
		||||
 | 
			
		||||
target_link_options(common PUBLIC ${LINKER_OPTIONS})
 | 
			
		||||
							
								
								
									
										34
									
								
								common/common_functions.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								common/common_functions.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
//
 | 
			
		||||
// Created by postaron on 31/07/24.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include "common_functions.hpp"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <format>
 | 
			
		||||
 | 
			
		||||
std::optional<std::ifstream> common::read_file(const std::filesystem::path &problemFile) noexcept {
 | 
			
		||||
    std::ifstream file(problemFile);
 | 
			
		||||
    if (!file.is_open()) {
 | 
			
		||||
        const auto error_state = file.rdstate();
 | 
			
		||||
        switch (error_state) {
 | 
			
		||||
            case std::ios::badbit:
 | 
			
		||||
                std::cerr << "Fatal I/O error occurred.\n";
 | 
			
		||||
                break;
 | 
			
		||||
            case std::ios::eofbit:
 | 
			
		||||
                std::cerr << "End of file reached.\n";
 | 
			
		||||
                break;
 | 
			
		||||
            case std::ios::failbit:
 | 
			
		||||
                std::cerr << "Non-fatal I/O error occurred.\n";
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                std::cerr << "impossible to reach.\n";
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
        const auto path_string = problemFile.string();
 | 
			
		||||
        const auto msg = std::format("Failed to open file {}: ", path_string);
 | 
			
		||||
        std::perror(msg.c_str());
 | 
			
		||||
        return std::nullopt;
 | 
			
		||||
    }
 | 
			
		||||
    return file;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								common/common_functions.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								common/common_functions.hpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
//
 | 
			
		||||
// Created by postaron on 31/07/24.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef ADVENTOFCODE2023_COMMON_FUNCTIONS_HPP
 | 
			
		||||
#define ADVENTOFCODE2023_COMMON_FUNCTIONS_HPP
 | 
			
		||||
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <filesystem>
 | 
			
		||||
#include <optional>
 | 
			
		||||
 | 
			
		||||
namespace common {
 | 
			
		||||
    namespace fs = std::filesystem;
 | 
			
		||||
 | 
			
		||||
    std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //ADVENTOFCODE2023_COMMON_FUNCTIONS_HPP
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue