Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

file.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2001 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 
00041 #ifndef __CCXX_FILE_H__
00042 #define __CCXX_FILE_H__
00043 
00044 #ifndef __CCXX_CONFIG_H__
00045 #include <cc++/config.h>
00046 #endif
00047 
00048 #ifndef __CCXX_THREAD_H__
00049 #include <cc++/thread.h>
00050 #endif
00051 
00052 #include <iostream>
00053 #include <fstream>
00054 
00055 #ifndef WIN32
00056 #include <cstdio>
00057 #include <dirent.h>
00058 #include <sys/stat.h>
00059 #include <sys/mman.h>
00060 #else
00061 #include <direct.h>
00062 #endif
00063 
00064 #ifdef  __NAMESPACES__
00065 namespace ost {
00066 #endif
00067 
00068 typedef unsigned long pos_t;
00069 #ifndef WIN32
00070 // use a define so that if the sys/types.h header already defines caddr_t
00071 // as it may on BSD systems, we do not break it by redefining again.
00072 #undef  caddr_t
00073 #define caddr_t char *
00074 typedef size_t ccxx_size_t;
00075 #else
00076 typedef LONG off_t;
00077 typedef void* caddr_t;
00078 typedef DWORD ccxx_size_t;
00079 #endif
00080 
00081 typedef struct _fcb
00082 {
00083         struct _fcb *next;
00084         caddr_t address;
00085         ccxx_size_t len;
00086         off_t pos;
00087 } fcb_t;
00088 
00089 #ifndef WIN32
00090 enum
00091 {
00092         FILE_OPEN_READONLY = O_RDONLY,
00093         FILE_OPEN_WRITEONLY = O_WRONLY,
00094         FILE_OPEN_READWRITE = O_RDWR,
00095         FILE_OPEN_APPEND = O_WRONLY | O_APPEND,
00096 
00097 #ifdef  O_SYNC
00098         FILE_OPEN_SYNC = O_RDWR | O_SYNC,
00099 #else
00100         FILE_OPEN_SYNC = O_RDWR,
00101 #endif
00102         FILE_OPEN_TRUNCATE = O_RDWR | O_TRUNC
00103 };
00104 
00105 /* to be used in future */
00106 
00107 #ifndef S_IRUSR
00108 #define S_IRUSR 0400
00109 #define S_IWUSR 0200
00110 #define S_IRGRP 0040
00111 #define S_IWGRP 0020
00112 #define S_IROTH 0004
00113 #define S_IWOTH 0002
00114 #endif
00115 
00116 #endif // !WIN32
00117 
00118 #ifndef WIN32
00119 enum fileattr_t
00120 {
00121         FILE_ATTR_INVALID = 0,
00122         FILE_ATTR_PRIVATE = S_IRUSR | S_IWUSR,
00123         FILE_ATTR_GROUP = FILE_ATTR_PRIVATE | S_IRGRP | S_IWGRP,
00124         FILE_ATTR_PUBLIC = FILE_ATTR_GROUP | S_IROTH | S_IWOTH
00125 };
00126 typedef enum fileattr_t fileattr_t;
00127 #else // defined WIN32
00128 typedef enum{
00129         FILE_ATTR_INVALID=0,
00130         FILE_ATTR_PRIVATE,
00131         FILE_ATTR_GROUP,
00132         FILE_ATTR_PUBLIC
00133 } fileattr_t;
00134 #endif // !WIN32
00135 
00136 enum fileerror_t
00137 {
00138         FILE_SUCCESS = 0,
00139         FILE_NOT_OPENED,
00140         FILE_MAP_FAILED,
00141         FILE_INIT_FAILED,
00142         FILE_OPEN_DENIED,
00143         FILE_OPEN_FAILED,
00144         FILE_OPEN_INUSE,
00145         FILE_READ_INTERRUPTED,
00146         FILE_READ_INCOMPLETE,
00147         FILE_READ_FAILURE,
00148         FILE_WRITE_INTERRUPTED,
00149         FILE_WRITE_INCOMPLETE,
00150         FILE_WRITE_FAILURE,
00151         FILE_EXTENDED_ERROR
00152 };
00153 typedef enum fileerror_t fileerror_t;
00154 
00155 enum fileaccess_t
00156 {
00157 #ifndef WIN32
00158         FILE_ACCESS_READONLY = O_RDONLY,
00159         FILE_ACCESS_WRITEONLY= O_WRONLY,
00160         FILE_ACCESS_READWRITE = O_RDWR
00161 #else
00162         FILE_ACCESS_READONLY = GENERIC_READ,
00163         FILE_ACCESS_WRITEONLY = GENERIC_WRITE,
00164         FILE_ACCESS_READWRITE = GENERIC_READ | GENERIC_WRITE
00165 #endif
00166 };
00167 typedef enum fileaccess_t fileaccess_t;
00168 
00169 #ifndef WIN32
00170 #define FILE_MAPPED_READ        FILE_ACCESS_READONLY
00171 #define FILE_MAPPED_WRITE       FILE_ACCESS_WRITEONLY
00172 #define FILE_MAPPED_RDWR        FILE_ACCESS_READWRITE
00173 
00174 enum filecomplete_t
00175 {
00176         FILE_COMPLETION_IMMEDIATE,
00177         FILE_COMPLETION_DELAYED,
00178         FILE_COMPLETION_DEFERRED
00179 };
00180 typedef enum filecomplete_t filecomplete_t;
00181 #endif
00182 
00183 // FIXME: add class fifostream  for posix compatibility
00184 #ifndef WIN32
00185 
00193 class fifostream : public std::fstream
00194 {
00195 private:
00196         char *pathname;
00197 
00198 public:
00203         fifostream();
00204 
00210         fifostream(const char *fname, long access = (long)FILE_ATTR_GROUP);
00211 
00215         virtual ~fifostream();
00216 
00223         void open(const char *fname, long access = (long)FILE_ATTR_GROUP);
00224 
00228         void close(void);
00229 };
00230 #endif // !WIN32
00231 
00232 // FIXME: add class FIFOSession for posix compatibility
00233 #ifndef WIN32
00234 
00239 class FIFOSession : public Thread, public std::fstream
00240 {
00241 private:
00242         char *pathname;
00243 
00244 public:
00245         FIFOSession(const char *session, long access = (long)FILE_ATTR_GROUP, int pri = 0, int stack = 0);
00246         virtual ~FIFOSession();
00247 };
00248 #endif // !WIN32
00249 
00258 class CCXX_CLASS_EXPORT Dir
00259 {
00260 private:
00261 #ifndef WIN32
00262         DIR *dir;
00263 #else
00264         HANDLE hDir;
00265         WIN32_FIND_DATA data;
00266         char *name;
00267 #endif
00268 
00269 public:
00270         Dir(const char *name);
00271         virtual ~Dir();
00272 
00273         char *getName(void);
00274 
00275         bool operator!()
00276 #ifndef WIN32
00277                 {return !dir;};
00278 #else
00279                 {return hDir != INVALID_HANDLE_VALUE;};
00280 #endif
00281 
00282         bool isValid(void);
00283 };
00284 
00295 class CCXX_CLASS_EXPORT RandomFile : public Mutex
00296 {
00297 private:
00298         fileerror_t errid;
00299         char *errstr;
00300 
00301 protected:
00302 #ifndef WIN32
00303         int fd;
00304         // FIXME: WIN32 as no access member
00305         fileaccess_t access;
00306 #else
00307         HANDLE fd;
00308 #endif
00309         char *pathname;
00310 
00311         struct
00312         {
00313                 unsigned count : 16;
00314                 bool thrown : 1;
00315                 bool initial : 1;
00316                 // FIXME: win32 don't use immediate
00317                 bool immediate : 1;
00318                 bool temp : 1;
00319         } flags;
00320 
00324         RandomFile();
00325 
00329         RandomFile(const RandomFile &rf);
00330 
00338         fileerror_t Error(fileerror_t errid, char *errstr = NULL);
00339         
00346         inline fileerror_t Error(char *errstr)
00347                 {return Error(FILE_EXTENDED_ERROR, errstr);};
00348 
00355         inline void setError(bool enable)
00356                 {flags.thrown = !enable;};
00357 
00358         // FIXME: add fileerror_t setCompletion(filecomplete_t mode); for posix compatibility
00359 #ifndef WIN32
00360 
00366         fileerror_t setCompletion(filecomplete_t mode);
00367 #endif
00368 
00375         inline void setTemporary(bool enable)
00376                 {flags.temp = enable;};
00377 
00389         virtual fileattr_t Initialize(void)
00390                 {return FILE_ATTR_PUBLIC;};
00391 
00395         void Final(void);
00396 
00397 public:
00401         virtual ~RandomFile()
00402                 {Final();};
00403 
00412         bool Initial(void);
00413 
00419         off_t getCapacity(void);
00420 
00426         virtual fileerror_t Restart(void)
00427                 {return FILE_OPEN_FAILED;};
00428 
00434         inline fileerror_t getErrorNumber(void)
00435                 {return errid;};
00436 
00442         inline char *getErrorString(void)
00443                 {return errstr;};
00444 
00445         bool operator!(void);
00446 };
00447 
00467 class CCXX_CLASS_EXPORT ThreadFile : public RandomFile
00468 {
00469 private:
00470         ThreadKey state;
00471         fcb_t *first;
00472         fcb_t *getFCB(void);
00473         fileerror_t Open(const char *path);
00474 
00475 public:
00482         ThreadFile(const char *path);
00483 
00487         virtual ~ThreadFile();
00488 
00494         fileerror_t Restart(void)
00495                 {return Open(pathname);};
00496 
00506         fileerror_t Fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00507 
00517         fileerror_t Update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00518 
00524         fileerror_t Append(caddr_t address = NULL, ccxx_size_t length = 0);
00525 
00531         off_t getPosition(void);
00532 
00533         bool operator++(void);
00534         bool operator--(void);
00535 };
00536 
00551 class CCXX_CLASS_EXPORT SharedFile : public RandomFile
00552 {
00553 private:
00554         fcb_t fcb;
00555         fileerror_t Open(const char *path);
00556 
00557 public:
00565         SharedFile(const char *path);
00566 
00573         SharedFile(const SharedFile &file);
00574 
00578         virtual ~SharedFile();
00579 
00585         fileerror_t Restart(void)
00586                 {return Open(pathname);};
00587 
00598         fileerror_t Fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00599 
00610         fileerror_t Update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00611 
00620         fileerror_t Clear(ccxx_size_t length = 0, off_t pos = -1);
00621 
00628         fileerror_t Append(caddr_t address = NULL, ccxx_size_t length = 0);
00629 
00635         off_t getPosition(void);
00636 
00637         bool operator++(void);
00638         bool operator--(void);
00639 };
00640 
00641 // FIXME: add MappedFile for posix compatibility
00642 #ifndef WIN32
00643 
00653 class CCXX_CLASS_EXPORT MappedFile : public RandomFile
00654 {
00655 private:
00656         fcb_t fcb;
00657         int prot;
00658 
00659 public:
00667         MappedFile(const char *fname, fileaccess_t mode);
00668 
00679         MappedFile(const char *fname, pos_t offset, size_t size, fileaccess_t mode);    
00680         
00685         virtual ~MappedFile();
00686 
00692         inline void Sync(void)
00693                 {msync(fcb.address, fcb.len, MS_SYNC);};
00694 
00701         void Sync(caddr_t address, size_t len);
00702 
00711         void Update(size_t offset = 0, size_t len = 0);
00712 
00720         void Update(caddr_t address, size_t len);
00721 
00728         void Release(caddr_t address, size_t len);
00729 
00738         inline caddr_t Fetch(size_t offset = 0)
00739                 {return ((char *)(fcb.address)) + offset;};
00740 
00749         caddr_t Fetch(off_t pos, size_t len);
00750 };
00751 #endif // !WIN32
00752 
00769 class CCXX_CLASS_EXPORT Pipe
00770 {
00771 private:
00772 #ifndef WIN32
00773         int fd[2];
00774 #else
00775         HANDLE fd[2];
00776 #endif
00777         int objcount;
00778         int objsize;
00779 
00780 protected:
00786         inline int getSize(void)
00787                 {return objsize;};
00788 
00797         inline void endSender(void)
00798 #ifndef WIN32
00799                 {close(fd[1]);};
00800 #else
00801                 {CloseHandle(fd[1]); fd[1] = NULL; };
00802 #endif
00803 
00811         inline void endReceiver(void)
00812 #ifndef WIN32
00813                 {close(fd[0]);};
00814 #else
00815                 {CloseHandle(fd[0]); fd[0] = NULL; };
00816 #endif
00817 
00828         Pipe(int size = 512, int count = 1);
00829 
00833         virtual ~Pipe();
00834 
00840         Pipe(const Pipe &orig);
00841 
00842         Pipe &operator=(const Pipe &orig);
00843 
00844         /* depreciated methods */
00845         void Sender(void)
00846                 {endReceiver();};
00847 
00848         void Receiver(void)
00849                 {endSender();};
00850 
00851         int Read(void *buf, int len)
00852 #ifndef WIN32
00853                 {return ::read(fd[0], (char *)buf, len);};
00854 #else
00855                 {DWORD dwLen; return ReadFile(fd[0],buf,len,&dwLen,NULL) ? dwLen : -1; };
00856 #endif
00857 
00858         int Write(void *buf, int len)
00859 #ifndef WIN32
00860                 {return ::write(fd[1], (char *)buf, len);};
00861 #else
00862                 {DWORD dwLen; return WriteFile(fd[1],buf,len,&dwLen,NULL) ? dwLen : -1; };
00863 #endif
00864 
00865 public:
00869         bool operator!();
00870 
00878 #ifndef WIN32
00879         inline int Recv(void *addr)
00880                 {return ::read(fd[0], (char *)addr, objsize);};
00881 #else
00882         int Recv(void *addr);
00883 #endif
00884 
00892 #ifndef WIN32
00893         inline int Send(void *addr)
00894                 {return ::write(fd[1], (char *)addr, objsize);};
00895 #else
00896         int Send(void *addr);
00897 #endif
00898 
00904         bool isValid(void);
00905 };
00906 
00915 class DSO
00916 {
00917 private:
00918 #ifdef  HAVE_MODULES
00919         static Mutex mutex;
00920         static DSO *first, *last;
00921         DSO *next, *prev;
00922 #ifndef WIN32
00923         void *image;
00924 #else
00925         char *err;
00926         HINSTANCE hImage;
00927 #endif
00928         CCXX_MEMBER_EXPORT(void) loader(const char *filename, bool resolve);
00929 #endif
00930 
00931 public:
00937 #ifdef  HAVE_MODULES
00938         DSO(char *filename)
00939                 {loader(filename, true);};
00940 
00941         DSO(char *filename, bool resolve)
00942                 {loader(filename, resolve);};
00943 #else
00944         DSO(char *filename)
00945                 {throw this;};
00946         DSO(char *filename, bool resolve)
00947                 {throw this;};
00948 #endif
00949 
00954 #ifndef WIN32
00955         char *getError(void);
00956 #else
00957         inline char *getError(void)
00958                 {return err;};
00959 #endif
00960 
00964 #ifdef  HAVE_MODULES
00965         CCXX_MEMBER_EXPORT(virtual) ~DSO();
00966 #endif
00967 
00971 #ifdef  HAVE_MODULES
00972         CCXX_MEMBER_EXPORT(void*) operator[](const char *sym);
00973 #else
00974         void *operator[](const char *)
00975                 {return NULL;};
00976 #endif  
00977 
00978 #ifdef  HAVE_MODULES
00979         friend CCXX_EXPORT(void) dynunload(void);
00980 #else
00981         friend void dynunload(void)
00982                 {return;};
00983 #endif
00984 
00990         CCXX_MEMBER_EXPORT(bool) isValid(void);
00991 };
00992 
00993 bool isDir(const char *path);
00994 bool isFile(const char *path);
00995 #ifndef WIN32
00996 bool isDevice(const char *path);
00997 #else
00998 inline bool isDevice(const char *path)
00999 { return false; }
01000 #endif
01001 bool canAccess(const char *path);
01002 bool canModify(const char *path);
01003 
01004 #ifdef  COMMON_STD_EXCEPTION
01005 
01006 class DirException : public IOException
01007 {
01008 public:
01009         DirException(std::string str) : IOException(str) {};
01010 };
01011 
01012 class DSOException : public IOException
01013 {
01014 public:
01015         DSOException(std::string str) : IOException(str) {};
01016 };
01017 
01018 class FIFOException : public IOException
01019 {
01020 public:
01021         FIFOException(std::string str) : IOException(str) {};
01022 };
01023 
01024 class PipeException : public IOException
01025 {
01026 public:
01027         PipeException(std::string str) : IOException(str) {};
01028 };
01029 
01030 class FileException : public IOException
01031 {
01032 public:
01033         FileException(std::string str) : IOException(str) {};
01034 };
01035 
01036 #endif
01037 
01038 #ifdef  __NAMESPACES__
01039 };
01040 #endif
01041 
01042 #endif
01043 

Generated at Tue Nov 20 12:34:33 2001 for CommonC++ by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001