Make WvStreams compile with gcc 4.4.
[wvstreams.git] / streams / wvfile.cc
bloba17a1297711bc804645fc542cc6c018d191a11ca
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
4 *
5 * A simple class to access filesystem files using WvStreams.
6 */
7 #include "wvfile.h"
8 #include "wvmoniker.h"
10 WvFile::WvFile()
12 readable = writable = false;
15 #ifndef _WIN32 // meaningless to do this on win32
17 * The Win32 runtime library doesn't provide fcntl so we can't
18 * set readable and writable reliably. Use the other constructor.
20 WvFile::WvFile(int rwfd) : WvFDStream(rwfd)
22 if (rwfd > -1)
24 /* We have to do it this way since O_RDONLY is defined as 0
25 in linux. */
26 mode_t xmode = fcntl(rwfd, F_GETFL);
27 xmode = xmode & (O_RDONLY | O_WRONLY | O_RDWR);
28 readable = (xmode == O_RDONLY) || (xmode == O_RDWR);
29 writable = (xmode == O_WRONLY) || (xmode == O_RDWR);
31 else
32 readable = writable = false;
34 #endif
37 WvFile::WvFile(WvStringParm filename, int mode, int create_mode)
39 #ifdef _WIN32
40 mode |= O_BINARY; // WvStreams users aren't expecting crlf mangling
41 #endif
42 open(filename, mode, create_mode);
46 static IWvStream *increator(WvStringParm s, IObject*)
48 return new WvFile(s, O_RDONLY, 0666);
51 static IWvStream *outcreator(WvStringParm s, IObject*)
53 return new WvFile(s, O_WRONLY|O_CREAT|O_TRUNC, 0666);
56 static IWvStream *creator(WvStringParm s, IObject*)
58 return new WvFile(s, O_RDWR|O_CREAT, 0666);
61 static WvMoniker<IWvStream> reg0("infile", increator);
62 static WvMoniker<IWvStream> reg1("outfile", outcreator);
63 static WvMoniker<IWvStream> reg3("file", creator);
66 bool WvFile::open(WvStringParm filename, int mode, int create_mode)
68 noerr();
70 /* We have to do it this way since O_RDONLY is defined as 0
71 in linux. */
72 int xmode = (mode & (O_RDONLY | O_WRONLY | O_RDWR));
73 readable = (xmode == O_RDONLY) || (xmode == O_RDWR);
74 writable = (xmode == O_WRONLY) || (xmode == O_RDWR);
76 // don't do the default force_select of read if we're not readable!
77 if (!readable)
78 undo_force_select(true, false, false);
80 close();
81 #ifndef _WIN32
82 int rwfd = ::open(filename, mode | O_NONBLOCK, create_mode);
83 #else
84 int rwfd = ::_open(filename, mode | O_NONBLOCK, create_mode);
85 #endif
86 if (rwfd < 0)
88 seterr(errno);
89 return false;
91 setfd(rwfd);
92 fcntl(rwfd, F_SETFD, 1);
94 closed = stop_read = stop_write = false;
95 return true;
99 #ifndef _WIN32 // since win32 doesn't support fcntl
100 bool WvFile::open(int _rwfd)
102 noerr();
103 if (_rwfd < 0)
104 return false;
106 noerr();
107 close();
109 int mode = fcntl(_rwfd, F_GETFL);
110 int xmode = (mode & (O_RDONLY | O_WRONLY | O_RDWR));
111 readable = (xmode == O_RDONLY) || (xmode == O_RDWR);
112 writable = (xmode == O_WRONLY) || (xmode == O_RDWR);
114 if (!readable)
115 undo_force_select(true, false, false);
117 setfd(_rwfd);
118 fcntl(_rwfd, F_SETFL, mode | O_NONBLOCK);
119 fcntl(_rwfd, F_SETFD, 1);
121 closed = stop_read = stop_write = false;
122 return true;
124 #endif
127 // files not open for read are never readable; files not open for write
128 // are never writable.
129 void WvFile::pre_select(SelectInfo &si)
131 if (!readable) si.wants.readable = false;
132 if (!writable) si.wants.writable = false;
134 WvFDStream::pre_select(si);
138 bool WvFile::post_select(SelectInfo &si)
140 return WvFDStream::post_select(si);