Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

file.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2002 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception to the GNU General Public License, permission is
00018 // granted for additional uses of the text contained in its release
00019 // of Common C++.
00020 //
00021 // The exception is that, if you link the Common C++ library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the Common C++ library code into it.
00026 //
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 //
00030 // This exception applies only to the code released under the
00031 // name Common C++.  If you copy code from other releases into a copy of
00032 // Common C++, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 //
00037 // If you write modifications of your own for Common C++, it is your
00038 // choice whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.
00040 
00046 #ifndef CCXX_FILE_H_
00047 #define CCXX_FILE_H_
00048 
00049 #ifndef CCXX_CONFIG_H_
00050 #include <cc++/config.h>
00051 #endif
00052 
00053 #ifndef CCXX_THREAD_H_
00054 #include <cc++/thread.h>
00055 #endif
00056 
00057 #include <iostream>
00058 #include <fstream>
00059 
00060 #ifndef WIN32
00061 #include <cstdio>
00062 #include <dirent.h>
00063 #include <sys/stat.h>
00064 #include <sys/mman.h>
00065 #else
00066 #include <direct.h>
00067 #endif
00068 
00069 #ifdef  CCXX_NAMESPACES
00070 namespace ost {
00071 #endif
00072 
00073 typedef unsigned long pos_t;
00074 #ifndef WIN32
00075 // use a define so that if the sys/types.h header already defines caddr_t
00076 // as it may on BSD systems, we do not break it by redefining again.
00077 #undef  caddr_t
00078 #define caddr_t char *
00079 typedef size_t ccxx_size_t;
00080 #else
00081 typedef LONG off_t;
00082 typedef void* caddr_t;
00083 typedef DWORD ccxx_size_t;
00084 #endif
00085 
00086 class File
00087 {
00088 protected:
00089         typedef struct _fcb
00090         {
00091                 struct _fcb *next;
00092                 caddr_t address;
00093                 ccxx_size_t len;
00094                 off_t pos;
00095         } fcb_t;
00096 
00097 #ifdef  WIN32
00098         enum Open
00099         {
00100                 openReadOnly, // = FILE_OPEN_READONLY,
00101                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00102                 openReadWrite, // = FILE_OPEN_READWRITE,
00103                 openAppend, // = FILE_OPEN_APPEND,
00104                 openTruncate // = FILE_OPEN_TRUNCATE
00105         };
00106         #else
00107         enum Open
00108         {
00109                 openReadOnly = O_RDONLY,
00110                 openWriteOnly = O_WRONLY,
00111                 openReadWrite = O_RDWR,
00112                 openAppend = O_WRONLY | O_APPEND,
00113 #ifdef  O_SYNC
00114                 openSync = O_RDWR | O_SYNC,
00115 #else
00116                 openSync = O_RDWR,
00117 #endif
00118                 openTruncate = O_RDWR | O_TRUNC
00119         };
00120         typedef enum Open Open;
00121 
00122 /* to be used in future */
00123 
00124 #ifndef S_IRUSR
00125 #define S_IRUSR 0400
00126 #define S_IWUSR 0200
00127 #define S_IRGRP 0040
00128 #define S_IWGRP 0020
00129 #define S_IROTH 0004
00130 #define S_IWOTH 0002
00131 #endif
00132 
00133 #endif // !WIN32
00134 
00135 #ifndef WIN32
00136         enum Attr
00137         {
00138                 attrInvalid = 0,
00139                 attrPrivate = S_IRUSR | S_IWUSR,
00140                 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00141                 attrPublic = attrGroup | S_IROTH | S_IWOTH
00142         };
00143         #else // defined WIN32
00144         enum Attr {
00145                 attrInvalid=0,
00146                 attrPrivate,
00147                 attrGroup,
00148                 attrPublic
00149         };
00150 #endif // !WIN32
00151         typedef enum Attr Attr;
00152 
00153         enum Error
00154         {
00155                 errSuccess = 0,
00156                 errNotOpened,
00157                 errMmapFailed,
00158                 errInitFailed,
00159                 errOpenDenied,
00160                 errOpenFailed,
00161                 errOpenInUse,
00162                 errReadInterrupted,
00163                 errReadIncomplete,
00164                 errReadFailure,
00165                 errWriteInterrupted,
00166                 errWriteIncomplete,
00167                 errWriteFailure,
00168                 errExtended
00169         };
00170         typedef enum Error Error;
00171 
00172         enum Access
00173         {
00174 #ifndef WIN32
00175                 accessReadOnly = O_RDONLY,
00176                 accessWriteOnly= O_WRONLY,
00177                 accessReadWrite = O_RDWR
00178 #else
00179                 accessReadOnly = GENERIC_READ,
00180                 accessWriteOnly = GENERIC_WRITE,
00181                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00182 #endif
00183         };
00184         typedef enum Access Access;
00185 
00186 #ifdef  WIN32
00187         enum Complete
00188         {
00189                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00190                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00191                 completionDeferred // = FILE_COMPLETION_DEFERRED
00192         };
00193 
00194         enum Mapping
00195         {
00196                 mappedRead, // = FILE_MAPPED_READ
00197                 mappedWrite, // = FILE_MAPPED_WRITE,
00198                 mappedReadWrite // = FILE_MAPPED_RDWR
00199         };
00200 #else
00201         enum Mapping
00202         {
00203                 mappedRead = accessReadOnly,
00204                 mappedWrite = accessWriteOnly,
00205                 mappedReadWrite = accessReadWrite
00206         };
00207         enum Complete
00208         {
00209                 completionImmediate,
00210                 completionDelayed,
00211                 completionDeferred
00212         };
00213 #endif
00214         typedef enum Complete Complete;
00215         typedef enum Mapping Mapping;
00216 };
00217 
00218 #ifndef WIN32
00219 
00229 class fifostream : public std::fstream, public File
00230 {
00231 private:
00232         char *pathname;
00233 
00234 public:
00239         fifostream();
00240 
00247         fifostream(const char *fname, long access = (long)attrGroup);
00248 
00252         virtual ~fifostream();
00253 
00261         void open(const char *fname, long access = (long)attrGroup);
00262 
00266         void close(void);
00267 };
00268 #endif // !WIN32
00269 
00270 #ifndef WIN32
00271 
00277 class FIFOSession : public Thread, public std::fstream, public File
00278 {
00279 private:
00280         char *pathname;
00281 
00282 public:
00283         FIFOSession(const char *session, long access = (long)attrGroup, int pri = 0, int stack = 0);
00284         virtual ~FIFOSession();
00285 };
00286 #endif // !WIN32
00287 
00296 class CCXX_CLASS_EXPORT Dir
00297 {
00298 private:
00299 #ifndef WIN32
00300         DIR *dir;
00301 #else
00302         HANDLE hDir;
00303         WIN32_FIND_DATA data;
00304         char *name;
00305 #endif
00306 
00307 public:
00308         Dir(const char *name);
00309         virtual ~Dir();
00310 
00311         char *getName(void);
00312 
00313         bool operator!()
00314 #ifndef WIN32
00315                 {return !dir;};
00316 #else
00317                 {return hDir != INVALID_HANDLE_VALUE;};
00318 #endif
00319 
00320         bool isValid(void);
00321 };
00322 
00333 class CCXX_CLASS_EXPORT RandomFile : protected Mutex, public File
00334 {
00335 private:
00336         Error errid;
00337         char *errstr;
00338 
00339 protected:
00340 #ifndef WIN32
00341         int fd;
00342         // FIXME: WIN32 as no access member
00343         Access access;
00344 #else
00345         HANDLE fd;
00346 #endif
00347         char *pathname;
00348 
00349         struct
00350         {
00351                 unsigned count : 16;
00352                 bool thrown : 1;
00353                 bool initial : 1;
00354 #ifndef WIN32
00355                 bool immediate : 1;
00356 #endif
00357                 bool temp : 1;
00358         } flags;
00359 
00363         RandomFile();
00364 
00368         RandomFile(const RandomFile &rf);
00369 
00377         Error error(Error errid, char *errstr = NULL);
00378 
00385         inline Error error(char *errstr)
00386                 {return error(errExtended, errstr);};
00387 
00394         inline void setError(bool enable)
00395                 {flags.thrown = !enable;};
00396 
00397 #ifndef WIN32
00398 
00405         Error setCompletion(Complete mode);
00406 #endif
00407 
00414         inline void setTemporary(bool enable)
00415                 {flags.temp = enable;};
00416 
00428         virtual Attr initialize(void)
00429                 {return attrPublic;};
00430 
00434         void final(void);
00435 
00436 public:
00440         virtual ~RandomFile()
00441                 {final();};
00442 
00451         bool initial(void);
00452 
00458         off_t getCapacity(void);
00459 
00465         virtual Error restart(void)
00466                 {return errOpenFailed;};
00467 
00473         inline Error getErrorNumber(void)
00474                 {return errid;};
00475 
00481         inline char *getErrorString(void)
00482                 {return errstr;};
00483 
00484         bool operator!(void);
00485 };
00486 
00506 class CCXX_CLASS_EXPORT ThreadFile : public RandomFile
00507 {
00508 private:
00509         ThreadKey state;
00510         fcb_t *first;
00511         fcb_t *getFCB(void);
00512         Error open(const char *path);
00513 
00514 public:
00521         ThreadFile(const char *path);
00522 
00526         virtual ~ThreadFile();
00527 
00533         Error restart(void)
00534                 {return open(pathname);};
00535 
00545         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00546 
00556         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00557 
00563         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00564 
00570         off_t getPosition(void);
00571 
00572         bool operator++(void);
00573         bool operator--(void);
00574 };
00575 
00590 class CCXX_CLASS_EXPORT SharedFile : public RandomFile
00591 {
00592 private:
00593         fcb_t fcb;
00594         Error open(const char *path);
00595 
00596 public:
00603         SharedFile(const char *path);
00604 
00611         SharedFile(const SharedFile &file);
00612 
00616         virtual ~SharedFile();
00617 
00623         Error restart(void)
00624                 {return open(pathname);};
00625 
00636         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00637 
00648         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00649 
00658         Error clear(ccxx_size_t length = 0, off_t pos = -1);
00659 
00666         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00667 
00673         off_t getPosition(void);
00674 
00675         bool operator++(void);
00676         bool operator--(void);
00677 };
00678 
00679 #ifndef WIN32
00680 
00691 class CCXX_CLASS_EXPORT MappedFile : public RandomFile
00692 {
00693 private:
00694         fcb_t fcb;
00695         int prot;
00696 
00697 public:
00705         MappedFile(const char *fname, Access mode);
00706 
00717         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);  
00718         
00723         virtual ~MappedFile();
00724 
00725         // FIXME: not use library function in header ??
00731         inline void sync(void)
00732                 {msync(fcb.address, fcb.len, MS_SYNC);};
00733 
00740         void sync(caddr_t address, size_t len);
00741 
00750         void update(size_t offset = 0, size_t len = 0);
00751 
00759         void update(caddr_t address, size_t len);
00760 
00767         void release(caddr_t address, size_t len);
00768 
00777         inline caddr_t fetch(size_t offset = 0)
00778                 {return ((char *)(fcb.address)) + offset;};
00779 
00788         caddr_t fetch(off_t pos, size_t len);
00789 };
00790 #endif // !WIN32
00791 
00808 class CCXX_CLASS_EXPORT Pipe
00809 {
00810 protected:
00811 #ifndef WIN32
00812         int fd[2];
00813 #else
00814         HANDLE fd[2];
00815 #endif
00816         int objcount;
00817         int objsize;
00818 
00824         inline int getSize(void)
00825                 {return objsize;};
00826 
00835         inline void endSender(void)
00836 #ifndef WIN32
00837                 {close(fd[1]);};
00838 #else
00839                 {CloseHandle(fd[1]); fd[1] = NULL; };
00840 #endif
00841 
00849         inline void endReceiver(void)
00850 #ifndef WIN32
00851                 {close(fd[0]);};
00852 #else
00853                 {CloseHandle(fd[0]); fd[0] = NULL; };
00854 #endif
00855 
00866         Pipe(int size = 512, int count = 1);
00867 
00871         virtual ~Pipe();
00872 
00878         Pipe(const Pipe &orig);
00879 
00880         Pipe &operator=(const Pipe &orig);
00881 
00882         /* depreciated methods */
00883         void sender(void)
00884                 {endReceiver();};
00885 
00886         void receiver(void)
00887                 {endSender();};
00888 
00889         int read(void *buf, int len)
00890 #ifndef WIN32
00891                 {return ::read(fd[0], (char *)buf, len);};
00892 #else
00893                 {DWORD dwLen; return ReadFile(fd[0],buf,len,&dwLen,NULL) ? dwLen : (DWORD)-1; };
00894 #endif
00895 
00896         int write(void *buf, int len)
00897 #ifndef WIN32
00898                 {return ::write(fd[1], (char *)buf, len);};
00899 #else
00900                 {DWORD dwLen; return WriteFile(fd[1],buf,len,&dwLen,NULL) ? dwLen : (DWORD)-1; };
00901 #endif
00902 
00903 public:
00907         bool operator!();
00908 
00915 #ifndef WIN32
00916         inline int receive(void *addr)
00917                 {return ::read(fd[0], (char *)addr, objsize);};
00918 #else
00919         int receive(void *addr);
00920 #endif
00921 
00928 #ifndef WIN32
00929         inline int send(void *addr)
00930                 {return ::write(fd[1], (char *)addr, objsize);};
00931 #else
00932         int send(void *addr);
00933 #endif
00934 
00940         bool isValid(void);
00941 };
00942 
00951 class DSO
00952 {
00953 private:
00954 #ifdef  HAVE_MODULES
00955         static Mutex mutex;
00956         static DSO *first, *last;
00957         DSO *next, *prev;
00958 #ifndef WIN32
00959         void *image;
00960 #else
00961         char *err;
00962         HINSTANCE hImage;
00963 #endif
00964         CCXX_MEMBER_EXPORT(void) loader(const char *filename, bool resolve);
00965 #endif
00966 
00967 public:
00973 #ifdef  HAVE_MODULES
00974         DSO(const char *filename)
00975                 {loader(filename, true);};
00976 
00977         DSO(const char *filename, bool resolve)
00978                 {loader(filename, resolve);};
00979 #else
00980         DSO(const char *filename)
00981                 {throw this;};
00982         DSO(const char *filename, bool resolve)
00983                 {throw this;};
00984 #endif
00985 
00990 #ifndef WIN32
00991         char *getError(void);
00992 #else
00993         inline char *getError(void)
00994                 {return err;};
00995 #endif
00996 
01000 #ifdef  HAVE_MODULES
01001         CCXX_MEMBER_EXPORT(virtual) ~DSO();
01002 #endif
01003 
01007 #ifdef  HAVE_MODULES
01008         CCXX_MEMBER_EXPORT(void*) operator[](const char *sym);
01009 #else
01010         void *operator[](const char *)
01011                 {return NULL;};
01012 #endif
01013 
01014 #ifdef  HAVE_MODULES
01015         static CCXX_EXPORT(void) dynunload(void);
01016 #else
01017         static void dynunload(void)
01018                 {return;};
01019 #endif
01020 
01026         CCXX_MEMBER_EXPORT(bool) isValid(void);
01027 };
01028 
01030 bool isDir(const char *path);
01032 bool isFile(const char *path);
01033 #ifndef WIN32
01034 
01035 bool isDevice(const char *path);
01036 #else
01037 
01038 inline bool isDevice(const char *path)
01039 { return false; }
01040 #endif
01041 
01042 bool canAccess(const char *path);
01044 bool canModify(const char *path);
01045 
01046 #ifdef  COMMON_STD_EXCEPTION
01047 
01048 class DirException : public IOException
01049 {
01050 public:
01051         DirException(std::string str) : IOException(str) {};
01052 };
01053 
01054 class DSOException : public IOException
01055 {
01056 public:
01057         DSOException(std::string str) : IOException(str) {};
01058 };
01059 
01060 class FIFOException : public IOException
01061 {
01062 public:
01063         FIFOException(std::string str) : IOException(str) {};
01064 };
01065 
01066 class PipeException : public IOException
01067 {
01068 public:
01069         PipeException(std::string str) : IOException(str) {};
01070 };
01071 
01072 class FileException : public IOException
01073 {
01074 public:
01075         FileException(std::string str) : IOException(str) {};
01076 };
01077 
01078 #endif
01079 
01080 #ifdef  CCXX_NAMESPACES
01081 };
01082 #endif
01083 
01084 #endif
01085 

Generated on Thu Nov 21 12:28:31 2002 for GNU CommonC++ by doxygen1.2.18