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: 25 27 92.6 %
Date: 2023-01-19 00:18:29 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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/sysdep/filesystem.h"    // O_*, S_*
      32             : 
      33             : namespace ERR
      34             : {
      35             :     const Status FILE_ACCESS = -110300;
      36             :     const Status FILE_NOT_FOUND = -110301;
      37             : }
      38             : 
      39             : // @param oflag: either O_RDONLY or O_WRONLY (in which case O_CREAT and
      40             : //   O_TRUNC are added), plus O_DIRECT if aio is desired
      41             : // @return file descriptor or a negative Status
      42             : Status FileOpen(const OsPath& pathname, int oflag);
      43             : void FileClose(int& fd);
      44             : 
      45             : class File
      46             : {
      47             : public:
      48        2933 :     File()
      49        2933 :         : m_PathName(), m_FileDescriptor(-1)
      50             :     {
      51        2933 :     }
      52             : 
      53           1 :     File(const OsPath& pathname, int oflag)
      54           1 :     {
      55           1 :         THROW_STATUS_IF_ERR(Open(pathname, oflag));
      56           1 :     }
      57             : 
      58        2934 :     ~File()
      59        2934 :     {
      60        2934 :         Close();
      61        2934 :     }
      62             : 
      63        2934 :     Status Open(const OsPath& pathName, int openFlag)
      64             :     {
      65        2934 :         Status ret = FileOpen(pathName, openFlag);
      66        2934 :         RETURN_STATUS_IF_ERR(ret);
      67        2934 :         m_PathName = pathName;
      68        2934 :         m_FileDescriptor = static_cast<int>(ret);
      69        2934 :         m_OpenFlag = openFlag;
      70        2934 :         return INFO::OK;
      71             :     }
      72             : 
      73        3031 :     void Close()
      74             :     {
      75        3031 :         FileClose(m_FileDescriptor);
      76        3031 :     }
      77             : 
      78           0 :     const OsPath& Pathname() const
      79             :     {
      80           0 :         return m_PathName;
      81             :     }
      82             : 
      83        2935 :     int Descriptor() const
      84             :     {
      85        2935 :         return m_FileDescriptor;
      86             :     }
      87             : 
      88        2935 :     int Flags() const
      89             :     {
      90        2935 :         return m_OpenFlag;
      91             :     }
      92             : 
      93             : private:
      94             :     OsPath m_PathName;
      95             :     int m_FileDescriptor;
      96             :     int m_OpenFlag;
      97             : };
      98             : 
      99             : typedef std::shared_ptr<File> PFile;
     100             : 
     101             : #endif  // #ifndef INCLUDED_FILE

Generated by: LCOV version 1.13