Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/thirdparty/taocpp-pegtl/pegtl/internal/file_opener.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2014-2020 Dr. Colin Hirsch and Daniel Frey
2
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3
4
#ifndef TAO_PEGTL_INTERNAL_FILE_OPENER_HPP
5
#define TAO_PEGTL_INTERNAL_FILE_OPENER_HPP
6
7
#include <fcntl.h>
8
#include <sys/stat.h>
9
#include <sys/types.h>
10
#include <unistd.h>
11
12
#include <utility>
13
14
#include "../config.hpp"
15
#include "../input_error.hpp"
16
17
namespace tao
18
{
19
   namespace TAO_PEGTL_NAMESPACE
20
   {
21
      namespace internal
22
      {
23
         struct file_opener
24
         {
25
            explicit file_opener( const char* filename )
26
               : m_source( filename ),
27
                 m_fd( open() )
28
0
            {
29
0
            }
30
31
            file_opener( const file_opener& ) = delete;
32
            file_opener( file_opener&& ) = delete;
33
34
            ~file_opener() noexcept
35
0
            {
36
0
               ::close( m_fd );
37
0
            }
38
39
            void operator=( const file_opener& ) = delete;
40
            void operator=( file_opener&& ) = delete;
41
42
            std::size_t size() const
43
0
            {
44
0
               struct stat st;  // NOLINT
45
0
               errno = 0;
46
0
               if( ::fstat( m_fd, &st ) < 0 ) {
47
0
                  TAO_PEGTL_THROW_INPUT_ERROR( "unable to fstat() file " << m_source << " descriptor " << m_fd );
48
0
               }
49
0
               return std::size_t( st.st_size );
50
0
            }
51
52
            const char* const m_source;
53
            const int m_fd;
54
55
         private:
56
            int open() const
57
0
            {
58
0
               errno = 0;
59
0
               const int fd = ::open( m_source,  // NOLINT
60
0
                                      O_RDONLY
61
0
#ifdef O_CLOEXEC
62
0
                                         | O_CLOEXEC
63
0
#endif
64
0
               );
65
0
               if( fd >= 0 ) {
66
0
                  return fd;
67
0
               }
68
0
               TAO_PEGTL_THROW_INPUT_ERROR( "unable to open() file " << m_source << " for reading" );
69
0
            }
70
         };
71
72
      }  // namespace internal
73
74
   }  // namespace TAO_PEGTL_NAMESPACE
75
76
}  // namespace tao
77
78
#endif