update dev300-m58
[ooovba.git] / vos / source / pipe.cxx
blob0a64438e94c75b0bb6f81c3962d38709ea944f4b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pipe.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <vos/pipe.hxx>
33 #include <vos/diagnose.hxx>
35 using namespace vos;
37 ///////////////////////////////////////////////////////////////////////////////
38 // Pipe
41 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OPipe, vos),
42 VOS_NAMESPACE(OPipe, vos),
43 VOS_NAMESPACE(OObject, vos), 0);
45 /*****************************************************************************/
46 // OPipe()
47 /*****************************************************************************/
48 OPipe::OPipe()
50 m_pPipeRef= 0;
53 /*****************************************************************************/
54 // OPipe()
55 /*****************************************************************************/
57 OPipe::OPipe( const rtl::OUString& strName, TPipeOption Options)
59 m_pPipeRef =
60 new PipeRef( osl_createPipe(strName.pData,
61 (oslPipeOptions)Options,
62 NULL) );
64 VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
65 VOS_POSTCOND((*m_pPipeRef)(), "OPipe(): creation of pipe failed!\n");
68 /*****************************************************************************/
69 // OPipe()
70 /*****************************************************************************/
72 OPipe::OPipe( const rtl::OUString& strName,
73 TPipeOption Options,
74 const OSecurity& rSecurity)
76 m_pPipeRef=
77 new PipeRef(osl_createPipe(strName.pData,
78 (oslPipeOptions)Options,
79 (oslSecurity)rSecurity));
81 VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
82 VOS_POSTCOND((*m_pPipeRef)(), "OPipe(): creation of pipe failed!\n");
85 /*****************************************************************************/
86 // OPipe()
87 /*****************************************************************************/
88 OPipe::OPipe(const OPipe& pipe) :
89 OReference(), OObject()
92 VOS_ASSERT(pipe.m_pPipeRef != 0);
94 m_pPipeRef= pipe.m_pPipeRef;
96 m_pPipeRef->acquire();
99 /*****************************************************************************/
100 // OPipe()
101 /*****************************************************************************/
102 OPipe::OPipe(oslPipe Pipe)
104 m_pPipeRef = new PipeRef(Pipe);
108 /*****************************************************************************/
109 // ~OPipe()
110 /*****************************************************************************/
111 OPipe::~OPipe()
113 close();
116 /*****************************************************************************/
117 // create
118 /*****************************************************************************/
119 sal_Bool OPipe::create( const rtl::OUString& strName, TPipeOption Options )
121 // if this was a valid pipe, decrease reference
122 if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
124 osl_releasePipe((*m_pPipeRef)());
125 delete m_pPipeRef;
126 m_pPipeRef= 0;
129 m_pPipeRef=
130 new PipeRef(osl_createPipe(strName.pData,
131 (oslPipeOptions)Options,
132 NULL));
134 VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
136 return (*m_pPipeRef)() != 0;
139 /*****************************************************************************/
140 // create
141 /*****************************************************************************/
142 sal_Bool OPipe::create( const rtl::OUString& strName,
143 TPipeOption Options,
144 const NAMESPACE_VOS(OSecurity)& rSecurity )
146 // if this was a valid pipe, decrease reference
147 if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
149 osl_releasePipe((*m_pPipeRef)());
150 delete m_pPipeRef;
151 m_pPipeRef= 0;
154 m_pPipeRef=
155 new PipeRef(osl_createPipe(strName.pData,
156 (oslPipeOptions)Options,
157 (oslSecurity)rSecurity));
159 VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
161 return (*m_pPipeRef)() != 0;
164 /*****************************************************************************/
165 // operator=
166 /*****************************************************************************/
167 OPipe& OPipe::operator= (const OPipe& pipe)
169 VOS_PRECOND(pipe.m_pPipeRef != 0, "OPipe::operator=: tried to assign an empty/invalid pipe\n");
171 if (m_pPipeRef == pipe.m_pPipeRef)
172 return *this;
174 // if this was a valid pipe, decrease reference
175 if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
177 osl_releasePipe((*m_pPipeRef)());
178 delete m_pPipeRef;
179 m_pPipeRef= 0;
182 m_pPipeRef= pipe.m_pPipeRef;
184 m_pPipeRef->acquire();
186 return *this;
189 /*****************************************************************************/
190 // operator oslPipe()
191 /*****************************************************************************/
192 OPipe::operator oslPipe() const
194 VOS_ASSERT(m_pPipeRef);
195 return (*m_pPipeRef)();
198 /*****************************************************************************/
199 // isValid()
200 /*****************************************************************************/
201 sal_Bool OPipe::isValid() const
203 return m_pPipeRef != 0 && (*m_pPipeRef)() != 0;
207 /*****************************************************************************/
208 // close
209 /*****************************************************************************/
210 void OPipe::close()
212 if (m_pPipeRef && (m_pPipeRef->release() == 0))
214 osl_releasePipe((*m_pPipeRef)());
215 delete m_pPipeRef;
217 m_pPipeRef= 0;
220 /*****************************************************************************/
221 // accept
222 /*****************************************************************************/
223 OPipe::TPipeError OPipe::accept(OStreamPipe& Connection)
225 if ( isValid() )
227 Connection = osl_acceptPipe((*m_pPipeRef)());
229 if(Connection.isValid())
230 return E_None;
233 return getError();
236 /*****************************************************************************/
237 // recv
238 /*****************************************************************************/
239 sal_Int32 OPipe::recv(void* pBuffer, sal_uInt32 BytesToRead)
241 if ( isValid() )
242 return osl_receivePipe((*m_pPipeRef)(),
243 pBuffer,
244 BytesToRead);
245 else
246 return -1;
250 /*****************************************************************************/
251 // send
252 /*****************************************************************************/
253 sal_Int32 OPipe::send(const void* pBuffer, sal_uInt32 BytesToSend)
255 if ( isValid() )
256 return osl_sendPipe((*m_pPipeRef)(),
257 pBuffer,
258 BytesToSend);
259 else
260 return -1;
263 /*****************************************************************************/
264 // getError
265 /*****************************************************************************/
266 OPipe::TPipeError OPipe::getError() const
268 if (m_pPipeRef)
269 return (TPipeError)osl_getLastPipeError((*m_pPipeRef)());
270 else
271 return (TPipeError)osl_getLastPipeError(NULL);
276 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamPipe, vos),
277 VOS_NAMESPACE(OStreamPipe, vos),
278 VOS_NAMESPACE(OPipe, vos), 0);
282 /*****************************************************************************/
283 // OStreamPipe
284 /*****************************************************************************/
285 OStreamPipe::OStreamPipe()
289 /*****************************************************************************/
290 // OStreamPipe
291 /*****************************************************************************/
292 OStreamPipe::OStreamPipe(oslPipe Pipe) :
293 OPipe(Pipe)
297 /*****************************************************************************/
298 // OStreamPipe
299 // copy constructor
300 /*****************************************************************************/
301 OStreamPipe::OStreamPipe(const OStreamPipe& pipe) :
302 OPipe(), IStream()
304 VOS_ASSERT(pipe.m_pPipeRef != 0);
306 m_pPipeRef= pipe.m_pPipeRef;
308 m_pPipeRef->acquire();
311 /*****************************************************************************/
312 // ~OStreamPipe
313 /*****************************************************************************/
314 OStreamPipe::~OStreamPipe()
318 /*****************************************************************************/
319 // operator=(oslPipe)
320 /*****************************************************************************/
321 OStreamPipe& OStreamPipe::operator=(oslPipe Pipe)
324 // if this was a valid pipe, decrease reference
325 if (m_pPipeRef && (m_pPipeRef->release() == 0))
327 osl_releasePipe((*m_pPipeRef)());
328 delete m_pPipeRef;
329 m_pPipeRef= 0;
332 m_pPipeRef= new PipeRef(Pipe);
334 VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
336 return *this;
339 /*****************************************************************************/
340 // operator=OPipe
341 /*****************************************************************************/
343 OStreamPipe& OStreamPipe::operator= (const OPipe& pipe)
345 OPipe::operator= ( pipe );
346 return *this;
349 /*****************************************************************************/
350 // read
351 /*****************************************************************************/
352 sal_Int32 OStreamPipe::read(void* pBuffer, sal_uInt32 n) const
354 VOS_ASSERT(m_pPipeRef && (*m_pPipeRef)());
356 /* loop until all desired bytes were read or an error occured */
357 sal_Int32 BytesRead= 0;
358 sal_Int32 BytesToRead= n;
359 while (BytesToRead > 0)
361 sal_Int32 RetVal;
362 RetVal= osl_receivePipe((*m_pPipeRef)(),
363 pBuffer,
364 BytesToRead);
366 /* error occured? */
367 if(RetVal <= 0)
369 break;
372 BytesToRead -= RetVal;
373 BytesRead += RetVal;
374 pBuffer= (sal_Char*)pBuffer + RetVal;
377 return BytesRead;
380 /*****************************************************************************/
381 // write
382 /*****************************************************************************/
383 sal_Int32 OStreamPipe::write(const void* pBuffer, sal_uInt32 n)
385 VOS_ASSERT(m_pPipeRef && (*m_pPipeRef)());
387 /* loop until all desired bytes were send or an error occured */
388 sal_Int32 BytesSend= 0;
389 sal_Int32 BytesToSend= n;
390 while (BytesToSend > 0)
392 sal_Int32 RetVal;
394 RetVal= osl_sendPipe((*m_pPipeRef)(),
395 pBuffer,
396 BytesToSend);
398 /* error occured? */
399 if(RetVal <= 0)
401 break;
404 BytesToSend -= RetVal;
405 BytesSend += RetVal;
406 pBuffer= (sal_Char*)pBuffer + RetVal;
409 return BytesSend;
412 sal_Bool OStreamPipe::isEof() const
414 return isValid();