summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bactivepipe.h
blob: fc2a32d854fcd04b5d7929087013700c8d4c4891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#ifndef _K3B_ACTIVE_PIPE_H_
#define _K3B_ACTIVE_PIPE_H_

#include <tqcstring.h>

#include <k3b_export.h>


class TQIODevice;


/**
 * The active pipe pumps data from a source to a sink using an
 * additional thread.
 */
class LIBK3B_EXPORT K3bActivePipe
{
 public:
  K3bActivePipe();
  virtual ~K3bActivePipe();

  /**
   * Opens the pipe and thus starts the 
   * pumping.
   *
   * \param closeWhenDone If true the pipes will be closed
   *        once all data has been read.
   */
  virtual bool open( bool closeWhenDone = false );

  /**
   * Opens the pipe syncroneously and blocks until all data has been
   * pumped through.
   * The pipe is closed afterwards.
   */
  bool pumpSync();

  /**
   * Close the pipe
   */
  virtual void close();

  /**
   * Set the file descriptor to read from. If this is -1 (the default) then
   * data has to be piped into the in() file descriptor.
   *
   * \param fd The file descriptor to read from.
   * \param close If true the reading file descriptor will be closed on a call to close()
   */
  void readFromFd( int fd, bool close = false );

  /**
   * Set the file descriptor to write to. If this is -1 (the default) then
   * data has to read from the out() file descriptor.
   *
   * \param fd The file descriptor to write to.
   * \param close If true the reading file descriptor will be closed on a call to close()
   */
  void writeToFd( int fd, bool close = false );

  /**
   * Read from a TQIODevice instead of a file descriptor.
   * The device will be opened IO_ReadOnly and closed
   * afterwards.
   */
  void readFromIODevice( TQIODevice* dev );

  /**
   * Write to a TQIODevice instead of a file descriptor.
   * The device will be opened IO_WriteOnly and closed
   * afterwards.
   */
  void writeToIODevice( TQIODevice* dev );

  /**
   * The file descriptor to write into
   * Only valid if no source has been set
   */
  int in() const;

  /**
   * The file descriptor to read from
   * Only valid if no sink has been set
   */
  int out() const;

  /**
   * The number of bytes that have been read.
   */
  TQ_UINT64 bytesRead() const;

  /**
   * The number of bytes that have been written.
   */
  TQ_UINT64 bytesWritten() const;

 protected:
  /**
   * Reads the data from the source.
   * The default implementation reads from the file desc
   * set via readFromFd or from in()
   */
  virtual int read( char* data, int max );

  /**
   * Write the data to the sink.
   * The default implementation writes to the file desc
   * set via writeToFd or out()
   *
   * Can be reimplememented to further process the data.
   */
  virtual int write( char* data, int max );

 private:
  class Private;
  Private* d;
};

#endif