00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#ifndef WT_COFFEE_UNIX_H
00023
#define WT_COFFEE_UNIX_H
00024
00025
#include <wefts_coffee_base.h>
00026
00027
#include <sys/select.h>
00028
#include <sys/types.h>
00029
#include <sys/stat.h>
00030
#include <fcntl.h>
00031
00032
00033
namespace Wefts {
00034
00035
00052 class OSFileFuncUnix :
public OSFileFuncBase
00053 {
00054
protected:
00055
00056 int m_fd;
00057
00058
#ifndef HAVE_PSELECT
00059 int pselect(
int n, fd_set *r, fd_set *w, fd_set *e,
struct timespec * ti,
void * mask)
00060 {
00061
struct timeval to;
00062
00063 to.tv_sec = ti->tv_sec;
00064 to.tv_usec = ti->tv_nsec / 1000;
00065
return select(n, r, w, e, &to);
00066 }
00067
#endif // ifndef HAVE_PSELECT
00068
00069 bool waitForWrite()
00070 {
00071
if ( m_timeout < 0 )
return true;
00072 fd_set set;
00073
struct timespec timeout;
00074
00075 FD_ZERO( &set );
00076 FD_SET(
m_fd, &set);
00077 timeout.tv_sec = static_cast<long>(m_timeout);
00078 timeout.tv_nsec = static_cast<long>(
00079 (m_timeout - static_cast<long>(m_timeout)) *1000000000);
00080 pselect(
m_fd + 1, NULL, &set, NULL, &timeout, NULL);
00081
00082
return FD_ISSET(
m_fd, &set );
00083 }
00084
00085
00086 bool waitForRead()
00087 {
00088
if ( m_timeout < 0 )
return true;
00089 fd_set set;
00090
struct timespec timeout;
00091
00092 FD_ZERO( &set );
00093 FD_SET(
m_fd, &set);
00094 timeout.tv_sec = static_cast<long>(m_timeout);
00095 timeout.tv_nsec = static_cast<long>(
00096 (m_timeout - static_cast<long>(m_timeout)) *1000000000);
00097 pselect(
m_fd + 1, &set, NULL, NULL, &timeout, NULL);
00098
00099
return FD_ISSET(
m_fd, &set );
00100 }
00101
00102
00103
public:
00104 OSFileFuncUnix(
void *descriptor = 0) :
00105
OSFileFuncBase()
00106 {
00107
if ( descriptor != 0 )
00108
setDescriptor( descriptor );
00109 }
00110
00111
virtual CffStatus open(
00112 std::string filespec,
00113 OpenMode openMode = OM_RO,
00114 LockMode lockMode = LM_SHARED
00115 );
00116
00117
virtual CffStatus create(
00118 std::string filespec,
00119 CreateMode cMode = CM_ARCHIVE,
00120 LockMode lockMode = LM_EXCLUSIVE
00121 );
00122
00123
00127 inline virtual void getDescriptor(
void *data )
const
00128
{
00129 *static_cast<int *>(data) =
m_fd;
00130 }
00131
00134 inline virtual void setDescriptor(
void *data )
00135 {
00136
m_fd = *static_cast<int *>(data);
00137 }
00138
00139
virtual file_size_t read(
void *buffer,
const file_size_t len );
00140
virtual file_size_t write(
void *buffer,
const file_size_t len );
00141
virtual file_size_t seek(
00142
const file_size_t position,
00143
const SeekWhence swFrom= SW_FROM_BEGIN
00144 );
00145
virtual CffStatus close();
00146
00147 inline virtual void setStdIn() {
m_fd = 0; }
00148 inline virtual void setStdOut(){
m_fd = 1; }
00149 inline virtual void setStdErr(){
m_fd = 2; }
00150
00151 };
00152
00154 typedef OSFileFuncUnix OSFileFunc;
00155
00157 }
00158
00159
#endif
00160
00161