aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exceptions.cpp1
-rw-r--r--src/exceptions.h1
-rw-r--r--src/fifo.cpp146
-rw-r--r--src/fifo.h69
-rw-r--r--src/file.cpp12
-rw-r--r--src/process.cpp11
-rw-r--r--src/process.h1
-rw-r--r--src/util.h18
8 files changed, 257 insertions, 2 deletions
diff --git a/src/exceptions.cpp b/src/exceptions.cpp
index 24c1a93..5d6deeb 100644
--- a/src/exceptions.cpp
+++ b/src/exceptions.cpp
@@ -13,6 +13,7 @@ namespace Bu
13 subExceptionDef( XmlException ) 13 subExceptionDef( XmlException )
14 subExceptionDef( TafException ) 14 subExceptionDef( TafException )
15 subExceptionDef( FileException ) 15 subExceptionDef( FileException )
16 subExceptionDef( FifoException )
16 subExceptionDef( SocketException ) 17 subExceptionDef( SocketException )
17 subExceptionDef( ConnectionException ) 18 subExceptionDef( ConnectionException )
18 subExceptionDef( PluginException ) 19 subExceptionDef( PluginException )
diff --git a/src/exceptions.h b/src/exceptions.h
index cef682f..91e0e6d 100644
--- a/src/exceptions.h
+++ b/src/exceptions.h
@@ -16,6 +16,7 @@ namespace Bu
16 subExceptionDecl( XmlException ) 16 subExceptionDecl( XmlException )
17 subExceptionDecl( TafException ) 17 subExceptionDecl( TafException )
18 subExceptionDecl( FileException ) 18 subExceptionDecl( FileException )
19 subExceptionDecl( FifoException )
19 subExceptionDecl( SocketException ) 20 subExceptionDecl( SocketException )
20 subExceptionDecl( ConnectionException ) 21 subExceptionDecl( ConnectionException )
21 subExceptionDecl( PluginException ) 22 subExceptionDecl( PluginException )
diff --git a/src/fifo.cpp b/src/fifo.cpp
new file mode 100644
index 0000000..2321cb6
--- /dev/null
+++ b/src/fifo.cpp
@@ -0,0 +1,146 @@
1/*
2 * Copyright (C) 2007-2008 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 "fifo.h"
9#include "exceptions.h"
10#include <errno.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14
15Bu::Fifo::Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc ) :
16 iFlags( iFlags ),
17 iIn( -1 ),
18 iOut( -1 )
19{
20 if( iFlags&Create )
21 {
22 if( mkfifo( sName.getStr(), mAcc ) )
23 {
24 throw FifoException("Error creating fifo: %s\n", strerror( errno ) );
25 }
26 }
27 if( iFlags&Read )
28 {
29 iIn = ::open(
30 sName.getStr(),
31 O_RDONLY|((iFlags&NonBlock)?O_NONBLOCK:0)
32 );
33 }
34 if( iFlags&Write )
35 {
36 iOut = ::open(
37 sName.getStr(),
38 O_WRONLY
39 );
40 }
41}
42
43Bu::Fifo::~Fifo()
44{
45 close();
46}
47
48void Bu::Fifo::close()
49{
50 if( iIn > -1 )
51 {
52 ::close( iIn );
53 iIn = -1;
54 }
55 if( iOut > -1 )
56 {
57 ::close( iOut );
58 iOut = -1;
59 }
60}
61
62size_t Bu::Fifo::read( void *pBuf, size_t nBytes )
63{
64 if( iIn < 0 )
65 throw FifoException("Fifo not open for reading.");
66
67 return TEMP_FAILURE_RETRY( ::read( iIn, pBuf, nBytes ) );
68}
69
70size_t Bu::Fifo::write( const void *pBuf, size_t nBytes )
71{
72 if( iOut < 0 )
73 throw FifoException("Fifo not open for writing.");
74
75 return TEMP_FAILURE_RETRY( ::write( iOut, pBuf, nBytes ) );
76}
77
78long Bu::Fifo::tell()
79{
80 return -1;
81}
82
83void Bu::Fifo::seek( long offset )
84{
85}
86
87void Bu::Fifo::setPos( long pos )
88{
89}
90
91void Bu::Fifo::setPosEnd( long pos )
92{
93}
94
95bool Bu::Fifo::isEOS()
96{
97 return false;
98}
99
100bool Bu::Fifo::canRead()
101{
102 return (iIn>-1);
103}
104
105bool Bu::Fifo::canWrite()
106{
107 return (iOut>-1);
108}
109
110bool Bu::Fifo::isReadable()
111{
112 return (iIn>-1);
113}
114
115bool Bu::Fifo::isWritable()
116{
117 return (iOut>-1);
118}
119
120bool Bu::Fifo::isSeekable()
121{
122 return false;
123}
124
125bool Bu::Fifo::isBlocking()
126{
127 return ((fcntl( iIn, F_GETFL, 0 )&O_NONBLOCK) == O_NONBLOCK);
128}
129
130void Bu::Fifo::setBlocking( bool bBlocking )
131{
132 if( bBlocking )
133 fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )&(~O_NONBLOCK) );
134 else
135 fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )|O_NONBLOCK );
136}
137
138void Bu::Fifo::flush()
139{
140}
141
142bool Bu::Fifo::isOpen()
143{
144 return (iIn > -1) || (iOut > -1);
145}
146
diff --git a/src/fifo.h b/src/fifo.h
new file mode 100644
index 0000000..4989839
--- /dev/null
+++ b/src/fifo.h
@@ -0,0 +1,69 @@
1/*
2 * Copyright (C) 2007-2008 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#ifndef BU_FIFO_H
9#define BU_FIFO_H
10
11#include <stdint.h>
12#include <sys/types.h>
13#include <stdlib.h>
14
15#include "bu/stream.h"
16#include "bu/fstring.h"
17
18namespace Bu
19{
20 /**
21 * A fifo stream.
22 *@ingroup Streams
23 */
24 class Fifo : public Bu::Stream
25 {
26 public:
27 Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc=-1 );
28 virtual ~Fifo();
29
30 virtual void close();
31 virtual size_t read( void *pBuf, size_t nBytes );
32 virtual size_t write( const void *pBuf, size_t nBytes );
33 using Stream::write;
34
35 virtual long tell();
36 virtual void seek( long offset );
37 virtual void setPos( long pos );
38 virtual void setPosEnd( long pos );
39 virtual bool isEOS();
40 virtual bool isOpen();
41
42 virtual void flush();
43
44 virtual bool canRead();
45 virtual bool canWrite();
46
47 virtual bool isReadable();
48 virtual bool isWritable();
49 virtual bool isSeekable();
50
51 virtual bool isBlocking();
52 virtual void setBlocking( bool bBlocking=true );
53
54 enum {
55 Read = 0x01,
56 Write = 0x02,
57 Create = 0x04,
58 Delete = 0x08,
59 NonBlock = 0x10
60 };
61
62 private:
63 int iFlags;
64 int iIn;
65 int iOut;
66 };
67}
68
69#endif
diff --git a/src/file.cpp b/src/file.cpp
index 3896005..83d76c5 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -10,6 +10,7 @@
10#include <errno.h> 10#include <errno.h>
11#include <sys/types.h> 11#include <sys/types.h>
12#include <sys/stat.h> 12#include <sys/stat.h>
13#include <fcntl.h>
13 14
14Bu::File::File( const char *sName, const char *sFlags ) 15Bu::File::File( const char *sName, const char *sFlags )
15{ 16{
@@ -138,7 +139,16 @@ bool Bu::File::isBlocking()
138 139
139void Bu::File::setBlocking( bool bBlocking ) 140void Bu::File::setBlocking( bool bBlocking )
140{ 141{
141 return; 142 if( bBlocking )
143 fcntl(
144 fileno( fh ),
145 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )&(~O_NONBLOCK)
146 );
147 else
148 fcntl(
149 fileno( fh ),
150 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )|O_NONBLOCK
151 );
142} 152}
143 153
144#ifndef WIN32 154#ifndef WIN32
diff --git a/src/process.cpp b/src/process.cpp
index e71b782..5781600 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -12,6 +12,7 @@
12#include <stdarg.h> 12#include <stdarg.h>
13#include <signal.h> 13#include <signal.h>
14#include <fcntl.h> 14#include <fcntl.h>
15#include <errno.h>
15 16
16Bu::Process::Process( const char *sName, char *const argv[] ) 17Bu::Process::Process( const char *sName, char *const argv[] )
17{ 18{
@@ -69,6 +70,7 @@ void Bu::Process::gexec( const char *sName, char *const argv[] )
69 dup2( iaStdOut[1], 1 ); 70 dup2( iaStdOut[1], 1 );
70// dup2( iaStdErr[1], 2 ); 71// dup2( iaStdErr[1], 2 );
71 execvp( sName, argv ); 72 execvp( sName, argv );
73 throw Bu::ExceptionBase("Hey, execvp failed!");
72 } 74 }
73 ::close( iaStdIn[0] ); 75 ::close( iaStdIn[0] );
74 ::close( iaStdOut[1] ); 76 ::close( iaStdOut[1] );
@@ -81,6 +83,8 @@ void Bu::Process::close()
81 83
82size_t Bu::Process::read( void *pBuf, size_t nBytes ) 84size_t Bu::Process::read( void *pBuf, size_t nBytes )
83{ 85{
86 return TEMP_FAILURE_RETRY( ::read( iStdOut, pBuf, nBytes ) );
87 /*
84 size_t iTotal = 0; 88 size_t iTotal = 0;
85 for(;;) 89 for(;;)
86 { 90 {
@@ -91,6 +95,7 @@ size_t Bu::Process::read( void *pBuf, size_t nBytes )
91 if( iTotal == nBytes ) 95 if( iTotal == nBytes )
92 return iTotal; 96 return iTotal;
93 } 97 }
98 */
94 /* 99 /*
95 size_t iTotal = 0; 100 size_t iTotal = 0;
96 fd_set rfs; 101 fd_set rfs;
@@ -124,7 +129,7 @@ size_t Bu::Process::readErr( void *pBuf, size_t nBytes )
124 129
125size_t Bu::Process::write( const void *pBuf, size_t nBytes ) 130size_t Bu::Process::write( const void *pBuf, size_t nBytes )
126{ 131{
127 return ::write( iStdIn, pBuf, nBytes ); 132 return TEMP_FAILURE_RETRY( ::write( iStdIn, pBuf, nBytes ) );
128} 133}
129 134
130long Bu::Process::tell() 135long Bu::Process::tell()
@@ -190,5 +195,9 @@ bool Bu::Process::isBlocking()
190 195
191void Bu::Process::setBlocking( bool bBlocking ) 196void Bu::Process::setBlocking( bool bBlocking )
192{ 197{
198 if( bBlocking )
199 fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )&(~O_NONBLOCK) );
200 else
201 fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK );
193} 202}
194 203
diff --git a/src/process.h b/src/process.h
index 27e2a32..a63fbf5 100644
--- a/src/process.h
+++ b/src/process.h
@@ -32,6 +32,7 @@ namespace Bu
32 virtual size_t read( void *pBuf, size_t nBytes ); 32 virtual size_t read( void *pBuf, size_t nBytes );
33 virtual size_t readErr( void *pBuf, size_t nBytes ); 33 virtual size_t readErr( void *pBuf, size_t nBytes );
34 virtual size_t write( const void *pBuf, size_t nBytes ); 34 virtual size_t write( const void *pBuf, size_t nBytes );
35 using Stream::write;
35 36
36 virtual long tell(); 37 virtual long tell();
37 virtual void seek( long offset ); 38 virtual void seek( long offset );
diff --git a/src/util.h b/src/util.h
index b55ceee..efbfb26 100644
--- a/src/util.h
+++ b/src/util.h
@@ -28,18 +28,36 @@ namespace Bu
28 } 28 }
29 29
30 template<typename item> 30 template<typename item>
31 const item &min( const item &a, const item &b )
32 {
33 return a<b?a:b;
34 }
35
36 template<typename item>
31 item &min( item &a, item &b ) 37 item &min( item &a, item &b )
32 { 38 {
33 return a<b?a:b; 39 return a<b?a:b;
34 } 40 }
35 41
36 template<typename item> 42 template<typename item>
43 const item &max( const item &a, const item &b )
44 {
45 return a>b?a:b;
46 }
47
48 template<typename item>
37 item &max( item &a, item &b ) 49 item &max( item &a, item &b )
38 { 50 {
39 return a>b?a:b; 51 return a>b?a:b;
40 } 52 }
41 53
42 template<typename item> 54 template<typename item>
55 const item &mid( const item &a, const item &b, const item &c )
56 {
57 return min( max( a, b ), c );
58 }
59
60 template<typename item>
43 item &mid( item &a, item &b, item &c ) 61 item &mid( item &a, item &b, item &c )
44 { 62 {
45 return min( max( a, b ), c ); 63 return min( max( a, b ), c );