LCOV - code coverage report
Current view: top level - source/lib/file - file.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 23 25 92.0 %
Date: 2026-12-03 01:19:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * simple POSIX file wrapper.
      25             :  */
      26             : 
      27             : #ifndef INCLUDED_FILE
      28             : #define INCLUDED_FILE
      29             : 
      30             : #include "lib/os_path.h"
      31             : #include "lib/status.h"
      32             : 
      33             : #include <memory>
      34             : 
      35             : namespace ERR
      36             : {
      37             :     const Status FILE_ACCESS = -110300;
      38             :     const Status FILE_NOT_FOUND = -110301;
      39             : }
      40             : 
      41             : // @param oflag: either O_RDONLY or O_WRONLY (in which case O_CREAT and
      42             : //   O_TRUNC are added), plus O_DIRECT if aio is desired
      43             : // @return file descriptor or a negative Status
      44             : Status FileOpen(const OsPath& pathname, int oflag);
      45             : void FileClose(int& fd);
      46             : 
      47             : class File
      48             : {
      49             : public:
      50       16203 :     File()
      51       16203 :         : m_PathName(), m_FileDescriptor(-1)
      52             :     {
      53             :     }
      54             : 
      55           1 :     File(const OsPath& pathname, int oflag)
      56           1 :     {
      57           1 :         THROW_STATUS_IF_ERR(Open(pathname, oflag));
      58           1 :     }
      59             : 
      60       16204 :     ~File()
      61             :     {
      62       32408 :         Close();
      63       16204 :     }
      64             : 
      65       16204 :     Status Open(const OsPath& pathName, int openFlag)
      66             :     {
      67       16204 :         Status ret = FileOpen(pathName, openFlag);
      68       16204 :         RETURN_STATUS_IF_ERR(ret);
      69       16204 :         m_PathName = pathName;
      70       16204 :         m_FileDescriptor = static_cast<int>(ret);
      71       16204 :         m_OpenFlag = openFlag;
      72       16204 :         return INFO::OK;
      73             :     }
      74             : 
      75       18319 :     void Close()
      76             :     {
      77       18319 :         FileClose(m_FileDescriptor);
      78        2115 :     }
      79             : 
      80           0 :     const OsPath& Pathname() const
      81             :     {
      82           0 :         return m_PathName;
      83             :     }
      84             : 
      85       16205 :     int Descriptor() const
      86             :     {
      87       16203 :         return m_FileDescriptor;
      88             :     }
      89             : 
      90       16205 :     int Flags() const
      91             :     {
      92       16205 :         return m_OpenFlag;
      93             :     }
      94             : 
      95             : private:
      96             :     OsPath m_PathName;
      97             :     int m_FileDescriptor;
      98             :     int m_OpenFlag;
      99             : };
     100             : 
     101             : typedef std::shared_ptr<File> PFile;
     102             : 
     103             : #endif  // #ifndef INCLUDED_FILE

Generated by: LCOV version 1.16