Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvfile.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2004 Net Integration Technologies, Inc.
00004  * 
00005  * A simple class to access filesystem files using WvStreams.
00006  */
00007 #include "wvfile.h"
00008 
00009 #ifdef _WIN32
00010 #include <io.h>
00011 #define O_NONBLOCK 0
00012 #define O_LARGEFILE 0
00013 #define fcntl(a,b,c)
00014 #endif
00015 
00016 #ifndef _WIN32 // meaningless to do this on win32
00017 /*
00018  * The Win32 runtime library doesn't provide fcntl so we can't
00019  * set readable and writable reliably. Use the other constructor.
00020  */
00021 WvFile::WvFile(int rwfd) : WvFDStream(rwfd)
00022 {
00023     if (rwfd > -1)
00024     {
00025         /* We have to do it this way since O_RDONLY is defined as 0
00026             in linux. */
00027         mode_t xmode = fcntl(rwfd, F_GETFL);
00028         xmode = xmode & (O_RDONLY | O_WRONLY | O_RDWR);
00029         readable = (xmode == O_RDONLY) || (xmode == O_RDWR);
00030         writable = (xmode == O_WRONLY) || (xmode == O_RDWR);
00031     }
00032     else
00033     {
00034         readable = writable = false;
00035     }
00036 }
00037 #endif
00038 
00039 bool WvFile::open(WvStringParm filename, int mode, int create_mode)
00040 {
00041     errnum = 0;
00042     
00043     /* We have to do it this way since O_RDONLY is defined as 0
00044        in linux. */
00045     int xmode = (mode & (O_RDONLY | O_WRONLY | O_RDWR));
00046     readable = (xmode == O_RDONLY) || (xmode == O_RDWR);
00047     writable = (xmode == O_WRONLY) || (xmode == O_RDWR);
00048 
00049     skip_select = false;
00050     
00051     // don't do the default force_select of read if we're not readable!
00052     if (!readable)
00053         undo_force_select(true, false, false);
00054     
00055     close();
00056     #ifndef _WIN32
00057     int rwfd = ::open(filename, mode | O_NONBLOCK, create_mode);
00058     #else
00059     int rwfd = ::_open(filename, mode | O_NONBLOCK, create_mode);
00060     #endif
00061     if (rwfd < 0)
00062     {
00063         seterr(errno);
00064         return false;
00065     }
00066     setfd(rwfd);
00067     fcntl(rwfd, F_SETFD, 1);
00068     return true;
00069 }
00070 
00071 
00072 // files not open for read are never readable; files not open for write
00073 // are never writable.
00074 bool WvFile::pre_select(SelectInfo &si)
00075 {
00076     bool ret;
00077     
00078     SelectRequest oldwant = si.wants;
00079     
00080     if (!readable) si.wants.readable = false;
00081     if (!writable) si.wants.writable = false;
00082     ret = WvFDStream::pre_select(si);
00083     
00084     si.wants = oldwant;
00085 
00086     // Force select() to always return true by causing it to not wait and
00087     // setting our pre_select() return value to true.
00088     if (skip_select)
00089     {
00090         si.msec_timeout = 0;
00091         ret = true;
00092     }
00093     return ret;
00094 }

Generated on Sat Mar 13 14:55:40 2004 for WvStreams by doxygen 1.3.6-20040222