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  CCXX_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 
00687         // FIXME: not use library function in header ??
00693         inline void Sync(void)
00694                 {msync(fcb.address, fcb.len, MS_SYNC);};
00695 
00702         void Sync(caddr_t address, size_t len);
00703 
00712         void Update(size_t offset = 0, size_t len = 0);
00713 
00721         void Update(caddr_t address, size_t len);
00722 
00729         void Release(caddr_t address, size_t len);
00730 
00739         inline caddr_t Fetch(size_t offset = 0)
00740                 {return ((char *)(fcb.address)) + offset;};
00741 
00750         caddr_t Fetch(off_t pos, size_t len);
00751 };
00752 #endif // !WIN32
00753 
00770 class CCXX_CLASS_EXPORT Pipe
00771 {
00772 private:
00773 #ifndef WIN32
00774         int fd[2];
00775 #else
00776         HANDLE fd[2];
00777 #endif
00778         int objcount;
00779         int objsize;
00780 
00781 protected:
00787         inline int getSize(void)
00788                 {return objsize;};
00789 
00798         inline void endSender(void)
00799 #ifndef WIN32
00800                 {close(fd[1]);};
00801 #else
00802                 {CloseHandle(fd[1]); fd[1] = NULL; };
00803 #endif
00804 
00812         inline void endReceiver(void)
00813 #ifndef WIN32
00814                 {close(fd[0]);};
00815 #else
00816                 {CloseHandle(fd[0]); fd[0] = NULL; };
00817 #endif
00818 
00829         Pipe(int size = 512, int count = 1);
00830 
00834         virtual ~Pipe();
00835 
00841         Pipe(const Pipe &orig);
00842 
00843         Pipe &operator=(const Pipe &orig);
00844 
00845         /* depreciated methods */
00846         void Sender(void)
00847                 {endReceiver();};
00848 
00849         void Receiver(void)
00850                 {endSender();};
00851 
00852         int Read(void *buf, int len)
00853 #ifndef WIN32
00854                 {return ::read(fd[0], (char *)buf, len);};
00855 #else
00856                 {DWORD dwLen; return ReadFile(fd[0],buf,len,&dwLen,NULL) ? dwLen : -1; };
00857 #endif
00858 
00859         int Write(void *buf, int len)
00860 #ifndef WIN32
00861                 {return ::write(fd[1], (char *)buf, len);};
00862 #else
00863                 {DWORD dwLen; return WriteFile(fd[1],buf,len,&dwLen,NULL) ? dwLen : -1; };
00864 #endif
00865 
00866 public:
00870         bool operator!();
00871 
00879 #ifndef WIN32
00880         inline int Recv(void *addr)
00881                 {return ::read(fd[0], (char *)addr, objsize);};
00882 #else
00883         int Recv(void *addr);
00884 #endif
00885 
00893 #ifndef WIN32
00894         inline int Send(void *addr)
00895                 {return ::write(fd[1], (char *)addr, objsize);};
00896 #else
00897         int Send(void *addr);
00898 #endif
00899 
00905         bool isValid(void);
00906 };
00907 
00916 class DSO
00917 {
00918 private:
00919 #ifdef  HAVE_MODULES
00920         static Mutex mutex;
00921         static DSO *first, *last;
00922         DSO *next, *prev;
00923 #ifndef WIN32
00924         void *image;
00925 #else
00926         char *err;
00927         HINSTANCE hImage;
00928 #endif
00929         CCXX_MEMBER_EXPORT(void) loader(const char *filename, bool resolve);
00930 #endif
00931 
00932 public:
00938 #ifdef  HAVE_MODULES
00939         DSO(const char *filename)
00940                 {loader(filename, true);};
00941 
00942         DSO(const char *filename, bool resolve)
00943                 {loader(filename, resolve);};
00944 #else
00945         DSO(const char *filename)
00946                 {throw this;};
00947         DSO(const char *filename, bool resolve)
00948                 {throw this;};
00949 #endif
00950 
00955 #ifndef WIN32
00956         char *getError(void);
00957 #else
00958         inline char *getError(void)
00959                 {return err;};
00960 #endif
00961 
00965 #ifdef  HAVE_MODULES
00966         CCXX_MEMBER_EXPORT(virtual) ~DSO();
00967 #endif
00968 
00972 #ifdef  HAVE_MODULES
00973         CCXX_MEMBER_EXPORT(void*) operator[](const char *sym);
00974 #else
00975         void *operator[](const char *)
00976                 {return NULL;};
00977 #endif  
00978 
00979 #ifdef  HAVE_MODULES
00980         static CCXX_EXPORT(void) dynunload(void);
00981 #else
00982         static void dynunload(void)
00983                 {return;};
00984 #endif
00985 
00991         CCXX_MEMBER_EXPORT(bool) isValid(void);
00992 };
00993 
00994 bool isDir(const char *path);
00995 bool isFile(const char *path);
00996 #ifndef WIN32
00997 bool isDevice(const char *path);
00998 #else
00999 inline bool isDevice(const char *path)
01000 { return false; }
01001 #endif
01002 bool canAccess(const char *path);
01003 bool canModify(const char *path);
01004 
01005 #ifdef  COMMON_STD_EXCEPTION
01006 
01007 class DirException : public IOException
01008 {
01009 public:
01010         DirException(std::string str) : IOException(str) {};
01011 };
01012 
01013 class DSOException : public IOException
01014 {
01015 public:
01016         DSOException(std::string str) : IOException(str) {};
01017 };
01018 
01019 class FIFOException : public IOException
01020 {
01021 public:
01022         FIFOException(std::string str) : IOException(str) {};
01023 };
01024 
01025 class PipeException : public IOException
01026 {
01027 public:
01028         PipeException(std::string str) : IOException(str) {};
01029 };
01030 
01031 class FileException : public IOException
01032 {
01033 public:
01034         FileException(std::string str) : IOException(str) {};
01035 };
01036 
01037 #endif
01038 
01039 #ifdef  CCXX_NAMESPACES
01040 };
01041 #endif
01042 
01043 #endif
01044 

Generated at Fri Jan 4 18:51:45 2002 for CommonC++ by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001