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-2005 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, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however    
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.    
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef CCXX_FILE_H_
00044 #define CCXX_FILE_H_
00045 
00046 #ifndef CCXX_CONFIG_H_
00047 #include <cc++/config.h>
00048 #endif
00049 
00050 #ifndef CCXX_MISSING_H_
00051 #include <cc++/missing.h>
00052 #endif
00053 
00054 #ifndef CCXX_THREAD_H_
00055 #include <cc++/thread.h>
00056 #endif
00057 
00058 #ifndef CCXX_EXCEPTION_H_
00059 #include <cc++/exception.h>
00060 #endif
00061 
00062 #ifndef WIN32
00063 #ifdef  __BORLANDC__
00064 #include <stdio.h>
00065 #include <sys/types.h>
00066 #else
00067 #include <cstdio>
00068 #endif
00069 #include <dirent.h>
00070 #include <sys/stat.h>
00071 #include <sys/mman.h>
00072 #else
00073 #include <direct.h>
00074 #endif
00075 
00076 #ifdef  HAVE_SHL_LOAD
00077 #include <dl.h>
00078 #endif
00079 
00080 #ifdef  HAVE_MACH_DYLD
00081 #include <mach-o/dyld.h>
00082 #endif
00083 
00084 #ifdef  CCXX_NAMESPACES
00085 namespace ost {
00086 #endif
00087 
00088 typedef unsigned long pos_t;
00089 #ifndef WIN32
00090 // use a define so that if the sys/types.h header already defines caddr_t
00091 // as it may on BSD systems, we do not break it by redefining again.
00092 #undef  caddr_t
00093 #define caddr_t char *
00094 typedef size_t ccxx_size_t;
00095 #else
00096 #ifndef __BORLANDC__
00097 typedef LONG off_t;
00098 #endif
00099 typedef void* caddr_t;
00100 typedef DWORD ccxx_size_t;
00101 #endif
00102 
00103 #ifndef PATH_MAX
00104 #define PATH_MAX        256
00105 #endif
00106 
00107 #ifndef NAME_MAX
00108 #define NAME_MAX        64
00109 #endif
00110 
00111 class __EXPORT File
00112 {
00113 public:
00114         enum Error
00115         {
00116                 errSuccess = 0,
00117                 errNotOpened,
00118                 errMapFailed,
00119                 errInitFailed,
00120                 errOpenDenied,
00121                 errOpenFailed,
00122                 errOpenInUse,
00123                 errReadInterrupted,
00124                 errReadIncomplete,
00125                 errReadFailure,
00126                 errWriteInterrupted,
00127                 errWriteIncomplete,
00128                 errWriteFailure,
00129                 errExtended
00130         };
00131         typedef enum Error Error;
00132 
00133         enum Access
00134         {
00135 #ifndef WIN32
00136                 accessReadOnly = O_RDONLY,
00137                 accessWriteOnly= O_WRONLY,
00138                 accessReadWrite = O_RDWR
00139 #else
00140                 accessReadOnly = GENERIC_READ,
00141                 accessWriteOnly = GENERIC_WRITE,
00142                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00143 #endif
00144         };
00145         typedef enum Access Access;
00146 
00147 protected:
00148         typedef struct _fcb
00149         {
00150                 struct _fcb *next;
00151                 caddr_t address;
00152                 ccxx_size_t len;
00153                 off_t pos;
00154                 bool locked;
00155         } fcb_t;
00156 
00157 public:
00158 #ifdef  WIN32
00159         enum Open
00160         {
00161                 openReadOnly, // = FILE_OPEN_READONLY,
00162                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00163                 openReadWrite, // = FILE_OPEN_READWRITE,
00164                 openAppend, // = FILE_OPEN_APPEND,
00165                 openTruncate // = FILE_OPEN_TRUNCATE
00166         };
00167         #else
00168         enum Open
00169         {
00170                 openReadOnly = O_RDONLY,
00171                 openWriteOnly = O_WRONLY,
00172                 openReadWrite = O_RDWR,
00173                 openAppend = O_WRONLY | O_APPEND,
00174 #ifdef  O_SYNC
00175                 openSync = O_RDWR | O_SYNC,
00176 #else
00177                 openSync = O_RDWR,
00178 #endif
00179                 openTruncate = O_RDWR | O_TRUNC
00180         };
00181         typedef enum Open Open;
00182 
00183 /* to be used in future */
00184 
00185 #ifndef S_IRUSR
00186 #define S_IRUSR 0400
00187 #define S_IWUSR 0200
00188 #define S_IRGRP 0040
00189 #define S_IWGRP 0020
00190 #define S_IROTH 0004
00191 #define S_IWOTH 0002
00192 #endif
00193 
00194 #endif // !WIN32
00195 
00196 #ifndef WIN32
00197         enum Attr
00198         {
00199                 attrInvalid = 0,
00200                 attrPrivate = S_IRUSR | S_IWUSR,
00201                 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00202                 attrPublic = attrGroup | S_IROTH | S_IWOTH
00203         };
00204         #else // defined WIN32
00205         enum Attr {
00206                 attrInvalid=0,
00207                 attrPrivate,
00208                 attrGroup,
00209                 attrPublic
00210         };
00211 #endif // !WIN32
00212         typedef enum Attr Attr;
00213 
00214 #ifdef  WIN32
00215         enum Complete
00216         {
00217                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00218                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00219                 completionDeferred // = FILE_COMPLETION_DEFERRED
00220         };
00221 
00222         enum Mapping
00223         {
00224                 mappedRead,
00225                 mappedWrite,
00226                 mappedReadWrite
00227         };
00228 #else
00229         enum Mapping
00230         {
00231                 mappedRead = accessReadOnly,
00232                 mappedWrite = accessWriteOnly,
00233                 mappedReadWrite = accessReadWrite
00234         };
00235         enum Complete
00236         {
00237                 completionImmediate,
00238                 completionDelayed,
00239                 completionDeferred
00240         };
00241 #endif
00242         typedef enum Complete Complete;
00243         typedef enum Mapping Mapping;
00244 
00245 public:
00246         static const char *getExtension(const char *path);
00247         static const char *getFilename(const char *path);
00248         static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00249         static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00250         static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00251 };
00252 
00261 class __EXPORT Dir : public File
00262 {
00263 private:
00264 #ifndef WIN32
00265         DIR *dir;
00266 #ifdef  HAVE_READDIR_R
00267         struct dirent *save;
00268         char save_space[sizeof(struct dirent) + PATH_MAX + 1];
00269 #endif
00270         struct dirent *entry;
00271 #else
00272         HANDLE hDir;
00273         WIN32_FIND_DATA data, fdata;
00274         char *name;
00275 #endif
00276 
00277 public:
00278         Dir(const char *name = NULL);
00279 
00280         static bool create(const char *path, Attr attr = attrGroup);
00281         static bool remove(const char *path);
00282         static bool setPrefix(const char *path);
00283         static bool getPrefix(char *path, size_t size = PATH_MAX);
00284 
00285         void open(const char *name);
00286         void close(void);
00287 
00288         virtual ~Dir();
00289 
00290         const char *getName(void);
00291 
00292         const char *operator++()
00293                 {return getName();};
00294 
00295         const char *operator++(int)
00296                 {return getName();};
00297 
00298         const char *operator*();
00299 
00300         bool rewind(void);
00301 
00302         bool operator!()
00303 #ifndef WIN32
00304                 {return !dir;};
00305 #else
00306                 {return hDir != INVALID_HANDLE_VALUE;};
00307 #endif
00308 
00309         bool isValid(void);
00310 };
00311 
00318 class __EXPORT  DirTree 
00319 {
00320 private:
00321         char path[PATH_MAX + 1];
00322         Dir *dir;
00323         unsigned max, current, prefixpos;
00324 
00325 protected:
00335         virtual bool filter(const char *path, struct stat *ino);
00336 
00337 public:
00345         DirTree(const char *prefix, unsigned depth);
00346 
00352         DirTree(unsigned depth);
00353 
00354         virtual ~DirTree();
00355 
00361         void open(const char *prefix);
00362 
00366         void close(void);
00367 
00375         char *getPath(void);
00376 
00386         unsigned perform(const char *prefix);
00387 };
00388 
00399 class __EXPORT RandomFile : protected Mutex, public File
00400 {
00401 private:
00402         Error errid;
00403         char *errstr;
00404 
00405 protected:
00406 #ifndef WIN32
00407         int fd;
00408         // FIXME: WIN32 as no access member
00409         Access access;
00410 #else
00411         HANDLE fd;
00412 #endif
00413         char *pathname;
00414 
00415         struct
00416         {
00417                 unsigned count : 16;
00418                 bool thrown : 1;
00419                 bool initial : 1;
00420 #ifndef WIN32
00421                 bool immediate : 1;
00422 #endif
00423                 bool temp : 1;
00424         } flags;
00425 
00429         RandomFile(const char *name = NULL);
00430 
00434         RandomFile(const RandomFile &rf);
00435 
00443         Error error(Error errid, char *errstr = NULL);
00444 
00451         inline Error error(char *err)
00452                 {return error(errExtended, err);};
00453 
00460         inline void setError(bool enable)
00461                 {flags.thrown = !enable;};
00462 
00463 #ifndef WIN32
00464 
00471         Error setCompletion(Complete mode);
00472 #endif
00473 
00480         inline void setTemporary(bool enable)
00481                 {flags.temp = enable;};
00482 
00494         virtual Attr initialize(void);
00495 
00499         void final(void);
00500 
00501 public:
00505         virtual ~RandomFile();
00506 
00515         bool initial(void);
00516 
00522         off_t getCapacity(void);
00523 
00529         virtual Error restart(void);
00530 
00536         inline Error getErrorNumber(void)
00537                 {return errid;};
00538 
00544         inline char *getErrorString(void)
00545                 {return errstr;};
00546 
00547         bool operator!(void);
00548 };
00549 
00569 class __EXPORT ThreadFile : public RandomFile
00570 {
00571 private:
00572         ThreadKey state;
00573         fcb_t *first;
00574         fcb_t *getFCB(void);
00575         Error open(const char *path);
00576 
00577 public:
00584         ThreadFile(const char *path);
00585 
00589         virtual ~ThreadFile();
00590 
00596         Error restart(void);
00597 
00607         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00608 
00618         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00619 
00625         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00626 
00632         off_t getPosition(void);
00633 
00634         bool operator++(void);
00635         bool operator--(void);
00636 };
00637 
00652 class __EXPORT SharedFile : public RandomFile
00653 {
00654 private:
00655         fcb_t fcb;
00656         Error open(const char *path);
00657 
00658 public:
00665         SharedFile(const char *path);
00666 
00673         SharedFile(const SharedFile &file);
00674 
00678         virtual ~SharedFile();
00679 
00685         Error restart(void)
00686                 {return open(pathname);};
00687 
00698         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00699 
00710         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00711 
00720         Error clear(ccxx_size_t length = 0, off_t pos = -1);
00721 
00728         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00729 
00735         off_t getPosition(void);
00736 
00737         bool operator++(void);
00738         bool operator--(void);
00739 };
00740 
00751 class __EXPORT MappedFile : public RandomFile
00752 {
00753 private:
00754         fcb_t fcb;
00755         int prot;
00756 #ifdef  WIN32
00757         HANDLE map;
00758         char mapname[64];
00759 #endif
00760 
00761 public:
00769         MappedFile(const char *fname, Access mode);
00770 
00779         MappedFile(const char *fname, Access mode, size_t size);
00780 
00791         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00792 
00797         virtual ~MappedFile();
00798 
00799         // FIXME: not use library function in header ??
00805         void sync(void);
00806 
00813         void sync(caddr_t address, size_t len);
00814 
00823         void update(size_t offset = 0, size_t len = 0);
00824 
00832         void update(caddr_t address, size_t len);
00833 
00840         void release(caddr_t address, size_t len);
00841 
00850         inline caddr_t fetch(size_t offset = 0)
00851                 {return ((char *)(fcb.address)) + offset;};
00852 
00861         caddr_t fetch(off_t pos, size_t len);
00862 
00868         bool lock(void);
00869 
00873         void unlock(void);
00874 
00881         size_t pageAligned(size_t size);
00882 };
00883 
00884 
00893 class __EXPORT DSO
00894 {
00895 private:
00896         const char *err;
00897 #ifdef  HAVE_MODULES
00898         static Mutex mutex;
00899         static DSO *first;
00900         static DSO *last;
00901         DSO *next, *prev;
00902         const char *id;
00903 #if     defined(HAVE_MACH_DYLD)
00904         NSModule oModule;
00905 #elif   defined(HAVE_SHL_LOAD)
00906         shl_t image;
00907 #elif   defined(WIN32)
00908         HINSTANCE hImage;
00909 #else
00910         void *image;
00911 #endif
00912         void loader(const char *filename, bool resolve);
00913 #endif
00914 
00915 public:
00921 #ifdef  HAVE_MODULES
00922         DSO(const char *filename)
00923                 {loader(filename, true);};
00924 
00925         DSO(const char *filename, bool resolve)
00926                 {loader(filename, resolve);};
00927 #else
00928         DSO(const char *filename)
00929                 {throw this;};
00930         DSO(const char *filename, bool resolve)
00931                 {throw this;};
00932 #endif
00933 
00938         inline const char *getError(void)
00939                 {return err;};
00940 
00944 #ifdef  HAVE_MODULES
00945         virtual ~DSO();
00946 #endif
00947 
00951 #ifdef  HAVE_MODULES
00952         void* operator[](const char *sym);
00953 #else
00954         void *operator[](const char *)
00955                 {return NULL;};
00956 #endif
00957 
00958 #ifdef  HAVE_MODULES
00959         static void dynunload(void);
00960 #else
00961         static void dynunload(void)
00962                 {return;};
00963 #endif
00964 
00970         static DSO *getObject(const char *name);
00971 
00977         bool isValid(void);
00978 
00982         static void setDebug(void);
00983 };
00984 
00986 bool __EXPORT isDir(const char *path);
00988 bool __EXPORT isFile(const char *path);
00989 #ifndef WIN32
00990 
00991 bool __EXPORT isDevice(const char *path);
00992 #else
00993 
00994 inline bool isDevice(const char *path)
00995 { return false; }
00996 #endif
00997 
00998 bool __EXPORT canAccess(const char *path);
01000 bool __EXPORT canModify(const char *path);
01002 time_t __EXPORT lastModified(const char *path);
01004 time_t __EXPORT lastAccessed(const char *path);
01005 
01006 #ifdef  COMMON_STD_EXCEPTION
01007 
01008 class DirException : public IOException
01009 {
01010 public:
01011         DirException(const String &str) : IOException(str) {};
01012 };
01013 
01014 class __EXPORT DSOException : public IOException
01015 {
01016 public:
01017         DSOException(const String &str) : IOException(str) {};
01018 };
01019 
01020 class __EXPORT FileException : public IOException
01021 {
01022 public:
01023         FileException(const String &str) : IOException(str) {};
01024 };
01025 
01026 #endif
01027 
01028 #ifdef  CCXX_NAMESPACES
01029 }
01030 #endif
01031 
01032 #endif
01033 

Generated on Sat Jul 23 19:28:36 2005 for GNU CommonC++ by  doxygen 1.4.3-20050530