Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
tuyau.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: tuyau.hpp,v 1.27 2011/04/19 14:57:24 edrusb Rel $
22 //
23 /*********************************************************************/
24 
33 
34 #ifndef TUYAU_HPP
35 #define TUYAU_HPP
36 
37 #include "../my_config.h"
38 #include "infinint.hpp"
39 #include "generic_file.hpp"
40 #include "thread_cancellation.hpp"
41 
42 namespace libdar
43 {
44 
46 
48 
49  class tuyau : public generic_file, public thread_cancellation, protected mem_ui
50  {
51  public:
52  tuyau(user_interaction & dialog, //< for user interaction
53  int fd); //< fd is the filedescriptor of a pipe extremity already openned
54  tuyau(user_interaction & dialog,
55  int fd, //< fd is the filedescriptor of a pipe extremity already openned
56  gf_mode mode); //< forces the mode if possible
57  tuyau(user_interaction & dialog, //< for user interaction
58  const std::string &filename, //< named pipe to open
59  gf_mode mode); //< forces the mode if possible
60  tuyau(user_interaction & dialog); //< creates a anonymous pipe and bind itself to the writing end. The reading end can be obtained by get_read_side() method
61  ~tuyau();
62 
63  // provides the reading end of the anonymous pipe when the current object has created it (no filedesc, no path given to constructor).
64  // it cannot be called more than once.
65  int get_read_fd() const;
66 
68 
72  void close_read_fd();
73 
75  void do_not_close_read_fd();
76 
77  // inherited from generic_file
78  bool skip(const infinint & pos);
79  bool skip_to_eof();
80  bool skip_relative(signed int x);
81  infinint get_position() { return position; };
82 
83  bool has_next_to_read();
84 
85  protected:
86  virtual U_I inherited_read(char *a, U_I size);
87  virtual void inherited_write(const char *a, U_I size);
89  void inherited_terminate();
90 
91  private:
92  enum
93  {
94  pipe_fd, //< holds a single file descriptor for the pipe
95  pipe_path, //< holds a filename to be openned (named pipe)
96  pipe_both //< holds a pair of file descriptors
97  }
98  pipe_mode; //< defines how the object's status (which possible values defined by the anonymous enum above)
99  infinint position; //< recorded position in the stream
100  int filedesc; //< file descriptors of the pipe
101  int other_end_fd; //< in pipe_both mode, this holds the reading side of the anonymous pipe
102  std::string chemin; //< in pipe_path mode only, this holds the named pipe to be open
103  bool has_one_to_read; //< if true, the next char to read is placed in "next_to_read"
104  char next_to_read; //< when has_one_to_read is true, contains the next to read byte
105 
106  void ouverture();
107 
109 
112  bool read_and_drop(infinint byte);
113 
115  bool read_to_eof();
116  };
117 
118 } // end of namespace
119 
120 #endif