aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/fifo.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/unstable/fifo.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/unstable/fifo.cpp')
-rw-r--r--src/unstable/fifo.cpp162
1 files changed, 162 insertions, 0 deletions
diff --git a/src/unstable/fifo.cpp b/src/unstable/fifo.cpp
new file mode 100644
index 0000000..b0cf1c7
--- /dev/null
+++ b/src/unstable/fifo.cpp
@@ -0,0 +1,162 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/fifo.h"
9#include <errno.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <unistd.h>
14
15#include "bu/config.h"
16
17namespace Bu { subExceptionDef( FifoException ) }
18
19Bu::Fifo::Fifo( const Bu::String &sName, int iFlags, mode_t mAcc ) :
20 iFlags( iFlags ),
21 iIn( -1 ),
22 iOut( -1 )
23{
24#ifndef WIN32
25 if( iFlags&Create )
26 {
27 if( mkfifo( sName.getStr(), mAcc ) )
28 {
29 throw FifoException("Error creating fifo: %s\n", strerror( errno ) );
30 }
31 }
32 if( iFlags&Read )
33 {
34 iIn = ::open(
35 sName.getStr(),
36 O_RDONLY|((iFlags&NonBlock)?O_NONBLOCK:0)
37 );
38 }
39 if( iFlags&Write )
40 {
41 iOut = ::open(
42 sName.getStr(),
43 O_WRONLY
44 );
45 }
46#else
47 #warning Bu::Fifo::Fifo IS A STUB for WIN32!!!!
48#endif
49}
50
51Bu::Fifo::~Fifo()
52{
53 close();
54}
55
56void Bu::Fifo::close()
57{
58 if( iIn > -1 )
59 {
60 ::close( iIn );
61 iIn = -1;
62 }
63 if( iOut > -1 )
64 {
65 ::close( iOut );
66 iOut = -1;
67 }
68}
69
70Bu::size Bu::Fifo::read( void *pBuf, Bu::size nBytes )
71{
72 if( iIn < 0 )
73 throw FifoException("Fifo not open for reading.");
74
75 return TEMP_FAILURE_RETRY( ::read( iIn, pBuf, nBytes ) );
76}
77
78Bu::size Bu::Fifo::write( const void *pBuf, Bu::size nBytes )
79{
80 if( iOut < 0 )
81 throw FifoException("Fifo not open for writing.");
82
83 return TEMP_FAILURE_RETRY( ::write( iOut, pBuf, nBytes ) );
84}
85
86Bu::size Bu::Fifo::tell()
87{
88 return -1;
89}
90
91void Bu::Fifo::seek( Bu::size )
92{
93}
94
95void Bu::Fifo::setPos( Bu::size )
96{
97}
98
99void Bu::Fifo::setPosEnd( Bu::size )
100{
101}
102
103bool Bu::Fifo::isEos()
104{
105 return false;
106}
107
108bool Bu::Fifo::canRead()
109{
110 return (iIn>-1);
111}
112
113bool Bu::Fifo::canWrite()
114{
115 return (iOut>-1);
116}
117
118bool Bu::Fifo::isReadable()
119{
120 return (iIn>-1);
121}
122
123bool Bu::Fifo::isWritable()
124{
125 return (iOut>-1);
126}
127
128bool Bu::Fifo::isSeekable()
129{
130 return false;
131}
132
133bool Bu::Fifo::isBlocking()
134{
135#ifndef WIN32
136 return ((fcntl( iIn, F_GETFL, 0 )&O_NONBLOCK) == O_NONBLOCK);
137#else
138 #warning Bu::Fifo::isBlocking IS A STUB for WIN32!!!!
139#endif
140}
141
142void Bu::Fifo::setBlocking( bool bBlocking )
143{
144#ifndef WIN32
145 if( bBlocking )
146 fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )&(~O_NONBLOCK) );
147 else
148 fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )|O_NONBLOCK );
149#else
150 #warning Bu::Fifo::setBlocking IS A STUB for WIN32!!!!
151#endif
152}
153
154void Bu::Fifo::flush()
155{
156}
157
158bool Bu::Fifo::isOpen()
159{
160 return (iIn > -1) || (iOut > -1);
161}
162