~ubuntu-branches/ubuntu/breezy/psi/breezy

« back to all changes in this revision

Viewing changes to cutestuff/openpgp/gpgproc/qpipe.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include"qpipe.h"
 
2
 
 
3
#ifdef Q_WS_WIN
 
4
 
 
5
static uint readFromHandle(HANDLE h, char *buf, uint size)
 
6
{
 
7
        if(size > 0) {
 
8
                ulong r;
 
9
                if(ReadFile(h, buf, size, &r, 0))
 
10
                        return r;
 
11
        }
 
12
        return 0;
 
13
}
 
14
 
 
15
#else
 
16
 
 
17
#include<unistd.h>
 
18
#include<fcntl.h>
 
19
#include<errno.h>
 
20
 
 
21
#endif
 
22
 
 
23
//----------------------------------------------------------------------------
 
24
// QPipeEnd
 
25
//----------------------------------------------------------------------------
 
26
QPipeEnd::QPipeEnd()
 
27
{
 
28
        reset();
 
29
}
 
30
 
 
31
QPipeEnd::~QPipeEnd()
 
32
{
 
33
        close();
 
34
}
 
35
 
 
36
void QPipeEnd::reset()
 
37
{
 
38
#ifdef Q_WS_WIN
 
39
        p = 0;
 
40
#else
 
41
        p = -1;
 
42
#endif
 
43
}
 
44
 
 
45
bool QPipeEnd::isOpen() const
 
46
{
 
47
#ifdef Q_WS_WIN
 
48
        return (p ? true: false);
 
49
#else
 
50
        return (p != -1 ? true: false);
 
51
#endif
 
52
}
 
53
 
 
54
const QPipeId & QPipeEnd::id() const
 
55
{
 
56
        return p;
 
57
}
 
58
 
 
59
void QPipeEnd::setId(const QPipeId &x)
 
60
{
 
61
        p = x;
 
62
}
 
63
 
 
64
void QPipeEnd::close()
 
65
{
 
66
        if(!isOpen())
 
67
                return;
 
68
#ifdef Q_WS_WIN
 
69
        CloseHandle(p);
 
70
#else
 
71
        ::close(p);
 
72
#endif
 
73
        reset();
 
74
}
 
75
 
 
76
void QPipeEnd::release()
 
77
{
 
78
        reset();
 
79
}
 
80
 
 
81
QString QPipeEnd::toString() const
 
82
{
 
83
#ifdef Q_WS_WIN
 
84
        DWORD dw;
 
85
        memcpy(&dw, &p, sizeof(DWORD));
 
86
        return QString::number(dw);
 
87
#else
 
88
        return QString::number(p);
 
89
#endif
 
90
}
 
91
 
 
92
int QPipeEnd::write(const QByteArray &buf)
 
93
{
 
94
#ifdef Q_WS_WIN
 
95
        DWORD written;
 
96
        if(!WriteFile(p, buf.data(), buf.size(), &written, 0))
 
97
                return -1;
 
98
        return written;
 
99
#else
 
100
        return (::write(p, buf.data(), buf.size()));
 
101
#endif
 
102
}
 
103
 
 
104
QByteArray QPipeEnd::readAll(bool *done)
 
105
{
 
106
#ifdef Q_WS_WIN
 
107
        if(done)
 
108
                *done = false;
 
109
        unsigned long i, r;
 
110
        char dummy;
 
111
        if(!PeekNamedPipe(p, &dummy, 1, &r, &i, 0))
 
112
                return QByteArray();
 
113
        if(i <= 0)
 
114
                return QByteArray();
 
115
 
 
116
        QByteArray buf;
 
117
        buf.resize(i);
 
118
        int size = readFromHandle(p, buf.data(), i);
 
119
        if(size == 0)
 
120
                return QByteArray();
 
121
        return buf;
 
122
#else
 
123
        if(done)
 
124
                *done = false;
 
125
        QByteArray buf;
 
126
        while(1) {
 
127
                char block[1024];
 
128
                int n = read(p, block, 1024);
 
129
                if(n < 0) {
 
130
                        if(errno == EAGAIN) {
 
131
                                break;
 
132
                        }
 
133
                        else {
 
134
                                if(done)
 
135
                                        *done = true;
 
136
                                break;
 
137
                        }
 
138
                }
 
139
                else if(n == 0) {
 
140
                        if(done)
 
141
                                *done = true;
 
142
                        break;
 
143
                }
 
144
 
 
145
                int oldsize = buf.size();
 
146
                buf.resize(oldsize + n);
 
147
                memcpy(buf.data() + oldsize, block, n);
 
148
        }
 
149
 
 
150
        return buf;
 
151
#endif
 
152
}
 
153
 
 
154
#ifdef Q_WS_WIN
 
155
bool QPipeEnd::winDupHandle()
 
156
{
 
157
        HANDLE h;
 
158
        if(!DuplicateHandle(GetCurrentProcess(), p, GetCurrentProcess(), &h, 0, FALSE, DUPLICATE_SAME_ACCESS))
 
159
                return false;
 
160
        CloseHandle(p);
 
161
        p = h;
 
162
        return true;
 
163
}
 
164
 
 
165
#else
 
166
 
 
167
bool QPipeEnd::setBlock(bool b)
 
168
{
 
169
        int flags = fcntl(p, F_GETFL);
 
170
        if(!b)
 
171
                flags |= O_NONBLOCK;
 
172
        else
 
173
                flags &= ~O_NONBLOCK;
 
174
        if(fcntl(p, F_SETFL, flags) == -1)
 
175
                return false;
 
176
        return true;
 
177
}
 
178
#endif
 
179
 
 
180
 
 
181
//----------------------------------------------------------------------------
 
182
// QPipe
 
183
//----------------------------------------------------------------------------
 
184
QPipe::QPipe()
 
185
{
 
186
}
 
187
 
 
188
QPipe::~QPipe()
 
189
{
 
190
        close();
 
191
}
 
192
 
 
193
void QPipe::closeReadEnd()
 
194
{
 
195
        i.close();
 
196
}
 
197
 
 
198
void QPipe::closeWriteEnd()
 
199
{
 
200
        o.close();
 
201
}
 
202
 
 
203
int QPipe::write(const QByteArray &buf)
 
204
{
 
205
        return o.write(buf);
 
206
}
 
207
 
 
208
QByteArray QPipe::readAll(bool *done)
 
209
{
 
210
        return i.readAll(done);
 
211
}
 
212
 
 
213
bool QPipe::open()
 
214
{
 
215
        close();
 
216
 
 
217
#ifdef Q_WS_WIN
 
218
        SECURITY_ATTRIBUTES secAttr;
 
219
        memset(&secAttr, 0, sizeof secAttr);
 
220
        secAttr.nLength = sizeof secAttr;
 
221
        secAttr.bInheritHandle = TRUE;
 
222
 
 
223
        HANDLE r, w;
 
224
        if(!CreatePipe(&r, &w, &secAttr, 0))
 
225
                return false;
 
226
        i.setId(r);
 
227
        o.setId(w);
 
228
#else
 
229
        int p[2];
 
230
        if(pipe(p) == -1)
 
231
                return false;
 
232
        i.setId(p[0]);
 
233
        o.setId(p[1]);
 
234
#endif
 
235
        return true;
 
236
}
 
237
 
 
238
void QPipe::close()
 
239
{
 
240
        i.close();
 
241
        o.close();
 
242
}
 
243
 
 
244
void QPipe::release()
 
245
{
 
246
        i.release();
 
247
        o.release();
 
248
}