Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class 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_MISSING_H_
00054 #include <cc++/missing.h>
00055 #endif
00056 
00057 #ifndef CCXX_THREAD_H_
00058 #include <cc++/thread.h>
00059 #endif
00060 
00061 #ifndef CCXX_EXCEPTION_H_
00062 #include <cc++/exception.h>
00063 #endif
00064 
00065 #ifndef WIN32
00066 #include <cstdio>
00067 #include <dirent.h>
00068 #include <sys/stat.h>
00069 #include <sys/mman.h>
00070 #else
00071 #include <direct.h>
00072 #endif
00073 
00074 #ifdef  HAVE_MACH_DYLD
00075 #include <mach-o/dyld.h>
00076 #endif
00077 
00078 #ifdef  CCXX_NAMESPACES
00079 namespace ost {
00080 #endif
00081 
00082 typedef unsigned long pos_t;
00083 #ifndef WIN32
00084 // use a define so that if the sys/types.h header already defines caddr_t
00085 // as it may on BSD systems, we do not break it by redefining again.
00086 #undef  caddr_t
00087 #define caddr_t char *
00088 typedef size_t ccxx_size_t;
00089 #else
00090 typedef LONG off_t;
00091 typedef void* caddr_t;
00092 typedef DWORD ccxx_size_t;
00093 #endif
00094 
00095 #ifndef PATH_MAX
00096 #define PATH_MAX        256
00097 #endif
00098 
00099 #ifndef NAME_MAX
00100 #define NAME_MAX        64
00101 #endif
00102 
00103 class __EXPORT File
00104 {
00105 public:
00106         enum Error
00107         {
00108                 errSuccess = 0,
00109                 errNotOpened,
00110                 errMapFailed,
00111                 errInitFailed,
00112                 errOpenDenied,
00113                 errOpenFailed,
00114                 errOpenInUse,
00115                 errReadInterrupted,
00116                 errReadIncomplete,
00117                 errReadFailure,
00118                 errWriteInterrupted,
00119                 errWriteIncomplete,
00120                 errWriteFailure,
00121                 errExtended
00122         };
00123         typedef enum Error Error;
00124 
00125         enum Access
00126         {
00127 #ifndef WIN32
00128                 accessReadOnly = O_RDONLY,
00129                 accessWriteOnly= O_WRONLY,
00130                 accessReadWrite = O_RDWR
00131 #else
00132                 accessReadOnly = GENERIC_READ,
00133                 accessWriteOnly = GENERIC_WRITE,
00134                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00135 #endif
00136         };
00137         typedef enum Access Access;
00138 
00139 protected:
00140         typedef struct _fcb
00141         {
00142                 struct _fcb *next;
00143                 caddr_t address;
00144                 ccxx_size_t len;
00145                 off_t pos;
00146         } fcb_t;
00147 
00148 public:
00149 #ifdef  WIN32
00150         enum Open
00151         {
00152                 openReadOnly, // = FILE_OPEN_READONLY,
00153                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00154                 openReadWrite, // = FILE_OPEN_READWRITE,
00155                 openAppend, // = FILE_OPEN_APPEND,
00156                 openTruncate // = FILE_OPEN_TRUNCATE
00157         };
00158         #else
00159         enum Open
00160         {
00161                 openReadOnly = O_RDONLY,
00162                 openWriteOnly = O_WRONLY,
00163                 openReadWrite = O_RDWR,
00164                 openAppend = O_WRONLY | O_APPEND,
00165 #ifdef  O_SYNC
00166                 openSync = O_RDWR | O_SYNC,
00167 #else
00168                 openSync = O_RDWR,
00169 #endif
00170                 openTruncate = O_RDWR | O_TRUNC
00171         };
00172         typedef enum Open Open;
00173 
00174 /* to be used in future */
00175 
00176 #ifndef S_IRUSR
00177 #define S_IRUSR 0400
00178 #define S_IWUSR 0200
00179 #define S_IRGRP 0040
00180 #define S_IWGRP 0020
00181 #define S_IROTH 0004
00182 #define S_IWOTH 0002
00183 #endif
00184 
00185 #endif // !WIN32
00186 
00187 #ifndef WIN32
00188         enum Attr
00189         {
00190                 attrInvalid = 0,
00191                 attrPrivate = S_IRUSR | S_IWUSR,
00192                 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00193                 attrPublic = attrGroup | S_IROTH | S_IWOTH
00194         };
00195         #else // defined WIN32
00196         enum Attr {
00197                 attrInvalid=0,
00198                 attrPrivate,
00199                 attrGroup,
00200                 attrPublic
00201         };
00202 #endif // !WIN32
00203         typedef enum Attr Attr;
00204 
00205 #ifdef  WIN32
00206         enum Complete
00207         {
00208                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00209                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00210                 completionDeferred // = FILE_COMPLETION_DEFERRED
00211         };
00212 
00213         enum Mapping
00214         {
00215                 mappedRead,
00216                 mappedWrite,
00217                 mappedReadWrite
00218         };
00219 #else
00220         enum Mapping
00221         {
00222                 mappedRead = accessReadOnly,
00223                 mappedWrite = accessWriteOnly,
00224                 mappedReadWrite = accessReadWrite
00225         };
00226         enum Complete
00227         {
00228                 completionImmediate,
00229                 completionDelayed,
00230                 completionDeferred
00231         };
00232 #endif
00233         typedef enum Complete Complete;
00234         typedef enum Mapping Mapping;
00235 
00236 public:
00237         static const char *getExtension(const char *path);
00238         static const char *getFilename(const char *path);
00239         static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00240         static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00241         static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00242 };
00243 
00244 #ifndef WIN32
00245 
00255 class fifostream : public std::fstream, public File
00256 {
00257 private:
00258         char *pathname;
00259 
00260 public:
00265         fifostream();
00266 
00273         fifostream(const char *fname, long access = (long)attrGroup);
00274 
00278         virtual ~fifostream();
00279 
00287         void open(const char *fname, long access = (long)attrGroup);
00288 
00292         void close(void);
00293 };
00294 #endif // !WIN32
00295 
00296 #ifndef WIN32
00297 
00303 class FIFOSession : public Thread, public std::fstream, public File
00304 {
00305 private:
00306         char *pathname;
00307 
00308 public:
00309         FIFOSession(const char *session, long access = (long)attrGroup, int pri = 0, int stack = 0);
00310         virtual ~FIFOSession();
00311 };
00312 #endif // !WIN32
00313 
00322 class __EXPORT Dir : public File
00323 {
00324 private:
00325 #ifndef WIN32
00326         DIR *dir;
00327 #ifdef  HAVE_READDIR_R
00328         struct dirent save;
00329 #endif
00330         struct dirent *entry;
00331 #else
00332         HANDLE hDir;
00333         WIN32_FIND_DATA data, fdata;
00334         char *name;
00335 #endif
00336 
00337 public:
00338         Dir(const char *name = NULL);
00339 
00340         static bool create(const char *path, Attr attr = attrGroup);
00341         static bool remove(const char *path);
00342         static bool setPrefix(const char *path);
00343         static bool getPrefix(char *path, size_t size = PATH_MAX);
00344 
00345         void open(const char *name);
00346         void close(void);
00347 
00348         virtual ~Dir();
00349 
00350         const char *getName(void);
00351 
00352         const char *operator++()
00353                 {return getName();};
00354 
00355         const char *operator++(int)
00356                 {return getName();};
00357 
00358         const char *operator*();
00359 
00360         bool rewind(void);
00361 
00362         bool operator!()
00363 #ifndef WIN32
00364                 {return !dir;};
00365 #else
00366                 {return hDir != INVALID_HANDLE_VALUE;};
00367 #endif
00368 
00369         bool isValid(void);
00370 };
00371 
00382 class __EXPORT RandomFile : protected Mutex, public File
00383 {
00384 private:
00385         Error errid;
00386         char *errstr;
00387 
00388 protected:
00389 #ifndef WIN32
00390         int fd;
00391         // FIXME: WIN32 as no access member
00392         Access access;
00393 #else
00394         HANDLE fd;
00395 #endif
00396         char *pathname;
00397 
00398         struct
00399         {
00400                 unsigned count : 16;
00401                 bool thrown : 1;
00402                 bool initial : 1;
00403 #ifndef WIN32
00404                 bool immediate : 1;
00405 #endif
00406                 bool temp : 1;
00407         } flags;
00408 
00412         RandomFile(const char *name = NULL);
00413 
00417         RandomFile(const RandomFile &rf);
00418 
00426         Error error(Error errid, char *errstr = NULL);
00427 
00434         inline Error error(char *errstr)
00435                 {return error(errExtended, errstr);};
00436 
00443         inline void setError(bool enable)
00444                 {flags.thrown = !enable;};
00445 
00446 #ifndef WIN32
00447 
00454         Error setCompletion(Complete mode);
00455 #endif
00456 
00463         inline void setTemporary(bool enable)
00464                 {flags.temp = enable;};
00465 
00477         virtual Attr initialize(void)
00478                 {return attrPublic;};
00479 
00483         void final(void);
00484 
00485 public:
00489         virtual ~RandomFile()
00490                 {final();};
00491 
00500         bool initial(void);
00501 
00507         off_t getCapacity(void);
00508 
00514         virtual Error restart(void)
00515                 {return errOpenFailed;};
00516 
00522         inline Error getErrorNumber(void)
00523                 {return errid;};
00524 
00530         inline char *getErrorString(void)
00531                 {return errstr;};
00532 
00533         bool operator!(void);
00534 };
00535 
00555 class __EXPORT ThreadFile : public RandomFile
00556 {
00557 private:
00558         ThreadKey state;
00559         fcb_t *first;
00560         fcb_t *getFCB(void);
00561         Error open(const char *path);
00562 
00563 public:
00570         ThreadFile(const char *path);
00571 
00575         virtual ~ThreadFile();
00576 
00582         Error restart(void)
00583                 {return open(pathname);};
00584 
00594         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00595 
00605         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00606 
00612         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00613 
00619         off_t getPosition(void);
00620 
00621         bool operator++(void);
00622         bool operator--(void);
00623 };
00624 
00639 class __EXPORT SharedFile : public RandomFile
00640 {
00641 private:
00642         fcb_t fcb;
00643         Error open(const char *path);
00644 
00645 public:
00652         SharedFile(const char *path);
00653 
00660         SharedFile(const SharedFile &file);
00661 
00665         virtual ~SharedFile();
00666 
00672         Error restart(void)
00673                 {return open(pathname);};
00674 
00685         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00686 
00697         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00698 
00707         Error clear(ccxx_size_t length = 0, off_t pos = -1);
00708 
00715         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00716 
00722         off_t getPosition(void);
00723 
00724         bool operator++(void);
00725         bool operator--(void);
00726 };
00727 
00739 class __EXPORT MappedFile : public RandomFile
00740 {
00741 private:
00742         fcb_t fcb;
00743         int prot;
00744 #ifdef  WIN32
00745         HANDLE map;
00746         char mapname[64];
00747 #endif
00748 
00749 public:
00757         MappedFile(const char *fname, Access mode);
00758 
00769         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00770 
00775         virtual ~MappedFile();
00776 
00777         // FIXME: not use library function in header ??
00783         void sync(void);
00784 
00791         void sync(caddr_t address, size_t len);
00792 
00801         void update(size_t offset = 0, size_t len = 0);
00802 
00810         void update(caddr_t address, size_t len);
00811 
00818         void release(caddr_t address, size_t len);
00819 
00828         inline caddr_t fetch(size_t offset = 0)
00829                 {return ((char *)(fcb.address)) + offset;};
00830 
00839         caddr_t fetch(off_t pos, size_t len);
00840 };
00841 
00842 
00851 class __EXPORT DSO
00852 {
00853 private:
00854         const char *err;
00855 #ifdef  HAVE_MODULES
00856         static Mutex mutex;
00857         static DSO *first;
00858         static DSO *last;
00859         DSO *next, *prev;
00860         const char *id;
00861 #if     defined(HAVE_MACH_DYLD)
00862         NSModule oModule;
00863 #elif   defined(HAVE_SHL_LOAD)
00864         shl_t image;
00865 #elif   defined(WIN32)
00866         HINSTANCE hImage;
00867 #else
00868         void *image;
00869 #endif
00870         void loader(const char *filename, bool resolve);
00871 #endif
00872 
00873 public:
00879 #ifdef  HAVE_MODULES
00880         DSO(const char *filename)
00881                 {loader(filename, true);};
00882 
00883         DSO(const char *filename, bool resolve)
00884                 {loader(filename, resolve);};
00885 #else
00886         DSO(const char *filename)
00887                 {throw this;};
00888         DSO(const char *filename, bool resolve)
00889                 {throw this;};
00890 #endif
00891 
00896         inline const char *getError(void)
00897                 {return err;};
00898 
00902 #ifdef  HAVE_MODULES
00903         virtual ~DSO();
00904 #endif
00905 
00909 #ifdef  HAVE_MODULES
00910         void* operator[](const char *sym);
00911 #else
00912         void *operator[](const char *)
00913                 {return NULL;};
00914 #endif
00915 
00916 #ifdef  HAVE_MODULES
00917         static void dynunload(void);
00918 #else
00919         static void dynunload(void)
00920                 {return;};
00921 #endif
00922 
00928         static DSO *getObject(const char *name);
00929 
00935         bool isValid(void);
00936 };
00937 
00939 bool __EXPORT isDir(const char *path);
00941 bool __EXPORT isFile(const char *path);
00942 #ifndef WIN32
00943 
00944 bool __EXPORT isDevice(const char *path);
00945 #else
00946 
00947 inline bool isDevice(const char *path)
00948 { return false; }
00949 #endif
00950 
00951 bool __EXPORT canAccess(const char *path);
00953 bool __EXPORT canModify(const char *path);
00954 
00955 #ifdef  COMMON_STD_EXCEPTION
00956 
00957 class DirException : public IOException
00958 {
00959 public:
00960         DirException(String str) : IOException(str) {};
00961 };
00962 
00963 class __EXPORT DSOException : public IOException
00964 {
00965 public:
00966         DSOException(String str) : IOException(str) {};
00967 };
00968 
00969 class __EXPORT FIFOException : public IOException
00970 {
00971 public:
00972         FIFOException(String str) : IOException(str) {};
00973 };
00974 
00975 class __EXPORT PipeException : public IOException
00976 {
00977 public:
00978         PipeException(String str) : IOException(str) {};
00979 };
00980 
00981 class __EXPORT FileException : public IOException
00982 {
00983 public:
00984         FileException(String str) : IOException(str) {};
00985 };
00986 
00987 #endif
00988 
00989 #ifdef  CCXX_NAMESPACES
00990 };
00991 #endif
00992 
00993 #endif
00994 

Generated on Fri Feb 27 11:37:10 2004 for GNU CommonC++ by doxygen 1.3.5