|
file.hGo to the documentation of this file.00001 // Copyright (C) 1999-2000 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 choice 00038 // 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_THREAD_H__ 00045 #include <cc++/thread.h> 00046 #else 00047 #ifdef __CCXX_NAMESPACE_H__ 00048 #include <cc++/macros.h> 00049 #endif 00050 #endif 00051 00052 #include <stdio.h> 00053 #include <dirent.h> 00054 #include <sys/stat.h> 00055 #include <sys/mman.h> 00056 #include <iostream.h> 00057 #include <fstream.h> 00058 00059 typedef unsigned long pos_t; 00060 00061 typedef struct _fcb 00062 { 00063 struct _fcb *next; 00064 caddr_t address; 00065 size_t len; 00066 off_t pos; 00067 } fcb_t; 00068 00069 enum 00070 { 00071 FILE_OPEN_READONLY = O_RDONLY, 00072 FILE_OPEN_WRITEONLY = O_WRONLY, 00073 FILE_OPEN_READWRITE = O_RDWR, 00074 FILE_OPEN_APPEND = O_WRONLY | O_APPEND, 00075 00076 #ifdef O_SYNC 00077 FILE_OPEN_SYNC = O_RDWR | O_SYNC, 00078 #else 00079 FILE_OPEN_SYNC = O_RDWR, 00080 #endif 00081 FILE_OPEN_TRUNCATE = O_RDWR | O_TRUNC 00082 }; 00083 00084 /* to be used in future */ 00085 00086 #ifndef S_IRUSR 00087 #define S_IRUSR 0400 00088 #define S_IWUSR 0200 00089 #define S_IRGRP 0040 00090 #define S_IWGRP 0020 00091 #define S_IROTH 0004 00092 #define S_IWOTH 0002 00093 #endif 00094 00095 typedef enum 00096 { 00097 FILE_ATTR_INVALID = 0, 00098 FILE_ATTR_PRIVATE = S_IRUSR | S_IWUSR, 00099 FILE_ATTR_GROUP = FILE_ATTR_PRIVATE | S_IRGRP | S_IWGRP, 00100 FILE_ATTR_PUBLIC = FILE_ATTR_GROUP | S_IROTH | S_IWOTH 00101 } fileattr_t; 00102 00103 typedef enum 00104 { 00105 FILE_SUCCESS = 0, 00106 FILE_NOT_OPENED, 00107 FILE_MAP_FAILED, 00108 FILE_INIT_FAILED, 00109 FILE_OPEN_DENIED, 00110 FILE_OPEN_FAILED, 00111 FILE_OPEN_INUSE, 00112 FILE_READ_INTERRUPTED, 00113 FILE_READ_INCOMPLETE, 00114 FILE_READ_FAILURE, 00115 FILE_WRITE_INTERRUPTED, 00116 FILE_WRITE_INCOMPLETE, 00117 FILE_WRITE_FAILURE, 00118 FILE_EXTENDED_ERROR 00119 } fileerror_t; 00120 00121 typedef enum 00122 { 00123 FILE_ACCESS_READONLY = O_RDONLY, 00124 FILE_ACCESS_WRITEONLY= O_WRONLY, 00125 FILE_ACCESS_READWRITE = O_RDWR 00126 } fileaccess_t; 00127 00128 #define FILE_MAPPED_READ FILE_ACCESS_READONLY 00129 #define FILE_MAPPED_WRITE FILE_ACCESS_WRITEONLY 00130 #define FILE_MAPPED_RDWR FILE_ACCESS_READWRITE 00131 00132 typedef enum 00133 { 00134 FILE_COMPLETION_IMMEDIATE, 00135 FILE_COMPLETION_DELAYED, 00136 FILE_COMPLETION_DEFERRED 00137 } filecomplete_t; 00138 00147 class fifostream : public fstream 00148 { 00149 private: 00150 char *pathname; 00151 00152 public: 00157 fifostream(); 00158 00164 fifostream(const char *fname, long access = (long)FILE_ATTR_GROUP); 00165 00169 ~fifostream(); 00170 00177 void open(const char *fname, long access = (long)FILE_ATTR_GROUP); 00178 00182 void close(void); 00183 }; 00184 00190 class FIFOSession : public Thread, public fstream 00191 { 00192 private: 00193 char *pathname; 00194 00195 public: 00196 FIFOSession(const char *session, long access = (long)FILE_ATTR_GROUP, Semaphore *start = NULL, int pri = 0, int stack = 0); 00197 ~FIFOSession(); 00198 }; 00199 00208 class Dir 00209 { 00210 private: 00211 DIR *dir; 00212 00213 public: 00214 Dir(const char *name); 00215 ~Dir(); 00216 00217 char *getName(void); 00218 00219 bool operator!() 00220 {return !dir;}; 00221 00222 bool isValid(void); 00223 }; 00224 00235 class RandomFile : public Mutex 00236 { 00237 private: 00238 fileerror_t errid; 00239 char *errstr; 00240 00241 protected: 00242 int fd; 00243 fileaccess_t access; 00244 char *pathname; 00245 00246 struct 00247 { 00248 unsigned count : 16; 00249 bool thrown : 1; 00250 bool initial : 1; 00251 bool immediate : 1; 00252 bool temp : 1; 00253 } flags; 00254 00258 RandomFile(); 00259 00263 RandomFile(const RandomFile &rf); 00264 00272 fileerror_t Error(fileerror_t errid, char *errstr = NULL); 00273 00280 inline fileerror_t Error(char *errstr) 00281 {return Error(FILE_EXTENDED_ERROR, errstr);}; 00282 00289 inline void setError(bool enable) 00290 {flags.thrown = !enable;}; 00291 00298 fileerror_t setCompletion(filecomplete_t mode); 00299 00306 inline void setTemporary(bool enable) 00307 {flags.temp = enable;}; 00308 00320 virtual fileattr_t Initialize(void) 00321 {return FILE_ATTR_PUBLIC;}; 00322 00326 void Final(void); 00327 00328 public: 00332 virtual ~RandomFile() 00333 {Final();}; 00334 00343 bool Initial(void); 00344 00350 off_t getCapacity(void); 00351 00357 virtual fileerror_t Restart(void) 00358 {return FILE_OPEN_FAILED;}; 00359 00365 inline fileerror_t getErrorNumber(void) 00366 {return errid;}; 00367 00373 inline char *getErrorString(void) 00374 {return errstr;}; 00375 00376 bool operator!(void); 00377 }; 00378 00398 class ThreadFile : public RandomFile 00399 { 00400 private: 00401 ThreadKey state; 00402 fcb_t *first; 00403 fcb_t *getFCB(void); 00404 fileerror_t Open(const char *path); 00405 00406 public: 00413 ThreadFile(const char *path); 00414 00418 ~ThreadFile(); 00419 00425 fileerror_t Restart(void) 00426 {return Open(pathname);}; 00427 00437 fileerror_t Fetch(caddr_t address = NULL, size_t length = 0, off_t position = - 1); 00438 00448 fileerror_t Update(caddr_t address = NULL, size_t length = 0, off_t position = -1); 00449 00455 fileerror_t Append(caddr_t address = NULL, size_t length = 0); 00456 00462 off_t getPosition(void); 00463 00464 bool operator++(void); 00465 bool operator--(void); 00466 }; 00467 00482 class SharedFile : public RandomFile 00483 { 00484 private: 00485 fcb_t fcb; 00486 fileerror_t Open(const char *path); 00487 00488 public: 00496 SharedFile(const char *path); 00497 00504 SharedFile(const SharedFile &file); 00505 00509 ~SharedFile(); 00510 00516 fileerror_t Restart(void) 00517 {return Open(pathname);}; 00518 00529 fileerror_t Fetch(caddr_t address = NULL, size_t length = 0, off_t position = - 1); 00530 00541 fileerror_t Update(caddr_t address = NULL, size_t length = 0, off_t position = -1); 00542 00551 fileerror_t Clear(size_t length = 0, off_t pos = -1); 00552 00559 fileerror_t Append(caddr_t address = NULL, size_t length = 0); 00560 00566 off_t getPosition(void); 00567 00568 bool operator++(void); 00569 bool operator--(void); 00570 }; 00571 00582 class MappedFile : public RandomFile 00583 { 00584 private: 00585 fcb_t fcb; 00586 int prot; 00587 00588 public: 00596 MappedFile(const char *fname, fileaccess_t mode); 00597 00608 MappedFile(const char *fname, pos_t offset, size_t size, fileaccess_t mode); 00609 00614 virtual ~MappedFile(); 00615 00621 inline void Sync(void) 00622 {msync(fcb.address, fcb.len, MS_SYNC);}; 00623 00630 void Sync(caddr_t address, size_t len); 00631 00640 void Update(size_t offset = 0, size_t len = 0); 00641 00649 void Update(caddr_t address, size_t len); 00650 00657 void Release(caddr_t address, size_t len); 00658 00667 inline caddr_t Fetch(size_t offset = 0) 00668 {return ((char *)(fcb.address)) + offset;}; 00669 00678 caddr_t Fetch(off_t pos, size_t len); 00679 }; 00680 00697 class Pipe 00698 { 00699 private: 00700 int fd[2]; 00701 int objcount; 00702 int objsize; 00703 00704 protected: 00710 inline int getSize(void) 00711 {return objsize;}; 00712 00721 inline void endSender(void) 00722 {close(fd[1]);}; 00731 inline void endReceiver(void) 00732 {close(fd[0]);}; 00733 00744 Pipe(int size = 512, int count = 1); 00745 00749 ~Pipe(); 00750 00756 Pipe(const Pipe &orig); 00757 00758 Pipe &operator=(const Pipe &orig); 00759 00760 /* depreciated methods */ 00761 void Sender(void) 00762 {endReceiver();}; 00763 00764 void Receiver(void) 00765 {endSender();}; 00766 00767 int Read(void *buf, int len) 00768 {return ::read(fd[0], (char *)buf, len);}; 00769 00770 int Write(void *buf, int len) 00771 {return ::write(fd[1], (char *)buf, len);}; 00772 00773 public: 00777 bool operator!(); 00778 00786 inline int Recv(void *addr) 00787 {return ::read(fd[0], (char *)addr, objsize);}; 00788 00796 inline int Send(void *addr) 00797 {return ::write(fd[1], (char *)addr, objsize);}; 00798 00804 bool isValid(void); 00805 }; 00806 00816 class DSO 00817 { 00818 private: 00819 #ifdef HAVE_MODULES 00820 static Mutex mutex; 00821 static DSO *first, *last; 00822 DSO *next, *prev; 00823 void *image; 00824 #endif 00825 00826 public: 00832 #ifdef HAVE_MODULES 00833 DSO(char *filename); 00834 #else 00835 DSO(char *filename) 00836 {throw this;}; 00837 #endif 00838 00843 char *getError(void); 00844 00848 #ifdef HAVE_MODULES 00849 ~DSO(); 00850 #endif 00851 00855 #ifdef HAVE_MODULES 00856 void *operator[](const char *); 00857 #else 00858 void *operator[](const char *) 00859 {return NULL;}; 00860 #endif 00861 00862 #ifdef HAVE_MODULES 00863 friend void dynunload(void); 00864 #else 00865 friend void dynunload(void) 00866 {return;}; 00867 #endif 00868 00874 bool isValid(void); 00875 }; 00876 00877 bool isDir(const char *path); 00878 bool isFile(const char *path); 00879 bool isDevice(const char *path); 00880 bool canAccess(const char *path); 00881 bool canModify(const char *path); 00882 00883 #ifdef __CCXX_NAMESPACE_H__ 00884 #undef __CCXX_NAMESPACE_H__ 00885 #include <cc++/namespace.h> 00886 #endif 00887 00888 #endif 00889 Generated at Fri Mar 23 10:47:54 2001 for CommonC++ by 1.2.1 written by Dimitri van Heesch, © 1997-2000 |