LCOV - code coverage report
Current view: top level - spb-proto-compiler/io - file.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 47.1 % 17 8
Test Date: 2025-05-23 14:18:14 Functions: 50.0 % 2 1

            Line data    Source code
       1              : /***************************************************************************\
       2              : * Name              : file io                                               *
       3              : * Description       : basic file io                                         *
       4              : * Author            : antonin.kriz@gmail.com                                *
       5              : * ------------------------------------------------------------------------- *
       6              : * This is free software; you can redistribute it and/or modify it under the *
       7              : * terms of the MIT license. A copy of the license can be found in the file  *
       8              : * "LICENSE" at the root of this distribution.                               *
       9              : \***************************************************************************/
      10              : 
      11              : #include "file.h"
      12              : #include <cstdio>
      13              : #include <cstring>
      14              : #include <stdexcept>
      15              : 
      16           14 : auto load_file( const std::filesystem::path & file_path ) -> std::string
      17              : {
      18           14 :     const auto file_size = std::filesystem::file_size( file_path );
      19           14 :     auto file_content    = std::string( file_size, '\0' );
      20              : 
      21           14 :     if( auto * p_file = fopen( file_path.string( ).c_str( ), "rb" ); p_file )
      22              :     {
      23           14 :         const auto read = fread( file_content.data( ), 1, file_content.size( ), p_file );
      24           14 :         fclose( p_file );
      25           14 :         file_content.resize( read );
      26           14 :         return file_content;
      27              :     }
      28            0 :     throw std::runtime_error( std::string( " " ) + strerror( errno ) );
      29            0 : }
      30              : 
      31            0 : void save_file( const std::filesystem::path & file_path, std::string_view file_content )
      32              : {
      33            0 :     if( auto * p_file = fopen( file_path.string( ).c_str( ), "wb" ); p_file )
      34              :     {
      35            0 :         const auto written = fwrite( file_content.data( ), 1, file_content.size( ), p_file );
      36            0 :         fclose( p_file );
      37            0 :         if( written == file_content.size( ) )
      38              :         {
      39            0 :             return;
      40              :         }
      41              :     }
      42            0 :     throw std::runtime_error( std::string( " " ) + strerror( errno ) );
      43              : }
        

Generated by: LCOV version 2.0-1