LCOV - code coverage report
Current view: top level - cppprocess/cppprocess - io_file.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 58 56.9 %
Date: 2022-06-18 10:10:36 Functions: 9 16 56.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/eventdispatcher
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : // self
      21             : //
      22             : #include    <cppprocess/io_file.h>
      23             : 
      24             : #include    <cppprocess/exception.h>
      25             : 
      26             : 
      27             : // last include
      28             : //
      29             : #include    <snapdev/poison.h>
      30             : 
      31             : 
      32             : 
      33             : namespace cppprocess
      34             : {
      35             : 
      36             : 
      37             : 
      38           2 : io_file::io_file(io_flags_t flags)
      39           2 :     : io(flags)
      40             : {
      41           2 : }
      42             : 
      43             : 
      44           2 : void io_file::set_filename(std::string const & filename)
      45             : {
      46           2 :     if(f_file != nullptr)
      47             :     {
      48           0 :         throw cppprocess_in_use("io_file is already in use, filename cannot be updated.");
      49             :     }
      50             : 
      51           2 :     f_filename = filename;
      52           2 : }
      53             : 
      54             : 
      55           0 : std::string const & io_file::get_filename() const
      56             : {
      57           0 :     return f_filename;
      58             : }
      59             : 
      60             : 
      61           1 : void io_file::set_truncate(bool truncate)
      62             : {
      63           1 :     if(f_file != nullptr)
      64             :     {
      65           0 :         throw cppprocess_in_use("io_file is already in use, truncate flag cannot be updated.");
      66             :     }
      67             : 
      68           1 :     f_truncate = truncate;
      69           1 : }
      70             : 
      71             : 
      72           0 : bool io_file::get_truncate() const
      73             : {
      74           0 :     return f_truncate;
      75             : }
      76             : 
      77             : 
      78           0 : void io_file::set_append(bool append)
      79             : {
      80           0 :     if(f_file != nullptr)
      81             :     {
      82           0 :         throw cppprocess_in_use("io_file is already in use, append flag cannot be updated.");
      83             :     }
      84             : 
      85           0 :     f_append = append;
      86           0 : }
      87             : 
      88             : 
      89           0 : bool io_file::get_append() const
      90             : {
      91           0 :     return f_append;
      92             : }
      93             : 
      94             : 
      95           0 : void io_file::set_mode(int mode)
      96             : {
      97           0 :     if(f_file != nullptr)
      98             :     {
      99           0 :         throw cppprocess_in_use("io_file is already in use, mode cannot be updated.");
     100             :     }
     101             : 
     102           0 :     f_mode = mode;
     103           0 : }
     104             : 
     105             : 
     106           0 : int io_file::get_mode() const
     107             : {
     108           0 :     return f_mode;
     109             : }
     110             : 
     111             : 
     112           2 : int io_file::get_fd()
     113             : {
     114           2 :     return f_file.get();
     115             : }
     116             : 
     117             : 
     118           2 : int io_file::get_other_fd()
     119             : {
     120           2 :     return get_fd();
     121             : }
     122             : 
     123             : 
     124           0 : void io_file::close_both()
     125             : {
     126           0 :     f_file.reset();
     127           0 : }
     128             : 
     129             : 
     130           2 : void io_file::close_other()
     131             : {
     132             :     // TBD: do we have to do something here?
     133           2 : }
     134             : 
     135             : 
     136           2 : void io_file::process_starting()
     137             : {
     138           2 :     if(f_file == nullptr)
     139             :     {
     140           2 :         int flags(0);
     141           2 :         io_flags_t const io_flags(get_flags());
     142           2 :         if((io_flags & (IO_FLAG_INPUT | IO_FLAG_OUTPUT))
     143             :                                 == (IO_FLAG_INPUT | IO_FLAG_OUTPUT))
     144             :         {
     145           0 :             flags |= O_RDWR;
     146             :         }
     147           2 :         else if((io_flags & IO_FLAG_INPUT) != 0)
     148             :         {
     149           1 :             flags |= O_RDONLY;
     150             :         }
     151           1 :         else if((io_flags & IO_FLAG_OUTPUT) != 0)
     152             :         {
     153           1 :             flags |= O_WRONLY | O_CREAT;
     154             :         }
     155             : 
     156           2 :         if((io_flags & IO_FLAG_OUTPUT) != 0)
     157             :         {
     158           1 :             if(f_truncate)
     159             :             {
     160           1 :                 flags |= O_TRUNC;
     161             :             }
     162           1 :             if(f_append)
     163             :             {
     164           0 :                 flags |= O_APPEND;
     165             :             }
     166             :         }
     167             : 
     168           2 :         f_file.reset(open(f_filename.c_str(), flags, f_mode));
     169             :     }
     170           2 : }
     171             : 
     172             : 
     173             : 
     174           6 : } // namespace cppprocess
     175             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13