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 # if __BORLANDC__ >= 0x0560
00074 #  include <dirent.h>
00075 #  include <sys/stat.h>
00076 # else
00077 #  include <direct.h>
00078 # endif
00079 #endif
00080 
00081 #ifdef  HAVE_SHL_LOAD
00082 #include <dl.h>
00083 #endif
00084 
00085 #ifdef  HAVE_MACH_DYLD
00086 #include <mach-o/dyld.h>
00087 #endif
00088 
00089 #ifdef  CCXX_NAMESPACES
00090 namespace ost {
00091 #endif
00092 
00093 typedef unsigned long pos_t;
00094 #ifndef WIN32
00095 // use a define so that if the sys/types.h header already defines caddr_t
00096 // as it may on BSD systems, we do not break it by redefining again.
00097 #undef  caddr_t
00098 #define caddr_t char *
00099 typedef size_t ccxx_size_t;
00100 #else
00101 #if     !defined(__BORLANDC__) || __BORLANDC__ >= 0x0560
00102 typedef LONG off_t;
00103 #endif
00104 typedef void* caddr_t;
00105 typedef DWORD ccxx_size_t;
00106 #endif
00107 
00108 #ifndef PATH_MAX
00109 #define PATH_MAX        256
00110 #endif
00111 
00112 #ifndef NAME_MAX
00113 #define NAME_MAX        64
00114 #endif
00115 
00116 class __EXPORT File
00117 {
00118 public:
00119         enum Error
00120         {
00121                 errSuccess = 0,
00122                 errNotOpened,
00123                 errMapFailed,
00124                 errInitFailed,
00125                 errOpenDenied,
00126                 errOpenFailed,
00127                 errOpenInUse,
00128                 errReadInterrupted,
00129                 errReadIncomplete,
00130                 errReadFailure,
00131                 errWriteInterrupted,
00132                 errWriteIncomplete,
00133                 errWriteFailure,
00134                 errLockFailure,
00135                 errExtended
00136         };
00137         typedef enum Error Error;
00138 
00139         enum Access
00140         {
00141 #ifndef WIN32
00142                 accessReadOnly = O_RDONLY,
00143                 accessWriteOnly= O_WRONLY,
00144                 accessReadWrite = O_RDWR
00145 #else
00146                 accessReadOnly = GENERIC_READ,
00147                 accessWriteOnly = GENERIC_WRITE,
00148                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00149 #endif
00150         };
00151         typedef enum Access Access;
00152 
00153 protected:
00154         typedef struct _fcb
00155         {
00156                 struct _fcb *next;
00157                 caddr_t address;
00158                 ccxx_size_t len;
00159                 off_t pos;
00160                 bool locked;
00161         } fcb_t;
00162 
00163 public:
00164 #ifdef  WIN32
00165         enum Open
00166         {
00167                 openReadOnly, // = FILE_OPEN_READONLY,
00168                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00169                 openReadWrite, // = FILE_OPEN_READWRITE,
00170                 openAppend, // = FILE_OPEN_APPEND,
00171                 openTruncate // = FILE_OPEN_TRUNCATE
00172         };
00173         #else
00174         enum Open
00175         {
00176                 openReadOnly = O_RDONLY,
00177                 openWriteOnly = O_WRONLY,
00178                 openReadWrite = O_RDWR,
00179                 openAppend = O_WRONLY | O_APPEND,
00180 #ifdef  O_SYNC
00181                 openSync = O_RDWR | O_SYNC,
00182 #else
00183                 openSync = O_RDWR,
00184 #endif
00185                 openTruncate = O_RDWR | O_TRUNC
00186         };
00187         typedef enum Open Open;
00188 
00189 /* to be used in future */
00190 
00191 #ifndef S_IRUSR
00192 #define S_IRUSR 0400
00193 #define S_IWUSR 0200
00194 #define S_IRGRP 0040
00195 #define S_IWGRP 0020
00196 #define S_IROTH 0004
00197 #define S_IWOTH 0002
00198 #endif
00199 
00200 #endif // !WIN32
00201 
00202 #ifndef WIN32
00203         enum Attr
00204         {
00205                 attrInvalid = 0,
00206                 attrPrivate = S_IRUSR | S_IWUSR,
00207                 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00208                 attrPublic = attrGroup | S_IROTH | S_IWOTH
00209         };
00210         #else // defined WIN32
00211         enum Attr {
00212                 attrInvalid=0,
00213                 attrPrivate,
00214                 attrGroup,
00215                 attrPublic
00216         };
00217 #endif // !WIN32
00218         typedef enum Attr Attr;
00219 
00220 #ifdef  WIN32
00221         enum Complete
00222         {
00223                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00224                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00225                 completionDeferred // = FILE_COMPLETION_DEFERRED
00226         };
00227 
00228         enum Mapping
00229         {
00230                 mappedRead,
00231                 mappedWrite,
00232                 mappedReadWrite
00233         };
00234 #else
00235         enum Mapping
00236         {
00237                 mappedRead = accessReadOnly,
00238                 mappedWrite = accessWriteOnly,
00239                 mappedReadWrite = accessReadWrite
00240         };
00241         enum Complete
00242         {
00243                 completionImmediate,
00244                 completionDelayed,
00245                 completionDeferred
00246         };
00247 #endif
00248         typedef enum Complete Complete;
00249         typedef enum Mapping Mapping;
00250 
00251 public:
00252         static const char *getExtension(const char *path);
00253         static const char *getFilename(const char *path);
00254         static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00255         static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00256         static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00257 };
00258 
00267 class __EXPORT Dir : public File
00268 {
00269 private:
00270 #ifndef WIN32
00271         DIR *dir;
00272 #ifdef  HAVE_READDIR_R
00273         struct dirent *save;
00274         char save_space[sizeof(struct dirent) + PATH_MAX + 1];
00275 #endif
00276         struct dirent *entry;
00277 #else
00278         HANDLE hDir;
00279         WIN32_FIND_DATA data, fdata;
00280         char *name;
00281 #endif
00282 
00283 public:
00284         Dir(const char *name = NULL);
00285 
00286         static bool create(const char *path, Attr attr = attrGroup);
00287         static bool remove(const char *path);
00288         static bool setPrefix(const char *path);
00289         static bool getPrefix(char *path, size_t size = PATH_MAX);
00290 
00291         void open(const char *name);
00292         void close(void);
00293 
00294         virtual ~Dir();
00295 
00296         const char *getName(void);
00297 
00298         const char *operator++()
00299                 {return getName();};
00300 
00301         const char *operator++(int)
00302                 {return getName();};
00303 
00304         const char *operator*();
00305 
00306         bool rewind(void);
00307 
00308         bool operator!()
00309 #ifndef WIN32
00310                 {return !dir;};
00311 #else
00312                 {return hDir != INVALID_HANDLE_VALUE;};
00313 #endif
00314 
00315         bool isValid(void);
00316 };
00317 
00324 class __EXPORT  DirTree 
00325 {
00326 private:
00327         char path[PATH_MAX + 1];
00328         Dir *dir;
00329         unsigned max, current, prefixpos;
00330 
00331 protected:
00341         virtual bool filter(const char *file, struct stat *ino);
00342 
00343 public:
00351         DirTree(const char *prefix, unsigned maxdepth);
00352 
00358         DirTree(unsigned maxdepth);
00359 
00360         virtual ~DirTree();
00361 
00367         void open(const char *prefix);
00368 
00372         void close(void);
00373 
00381         char *getPath(void);
00382 
00392         unsigned perform(const char *prefix);
00393 };
00394 
00405 class __EXPORT RandomFile : protected Mutex, public File
00406 {
00407 private:
00408         Error errid;
00409         char *errstr;
00410 
00411 protected:
00412 #ifndef WIN32
00413         int fd;
00414         // FIXME: WIN32 as no access member
00415         Access access;
00416 #else
00417         HANDLE fd;
00418 #endif
00419         char *pathname;
00420 
00421         struct
00422         {
00423                 unsigned count : 16;
00424                 bool thrown : 1;
00425                 bool initial : 1;
00426 #ifndef WIN32
00427                 bool immediate : 1;
00428 #endif
00429                 bool temp : 1;
00430         } flags;
00431 
00435         RandomFile(const char *name = NULL);
00436 
00440         RandomFile(const RandomFile &rf);
00441 
00449         Error error(Error errid, char *errstr = NULL);
00450 
00457         inline Error error(char *err)
00458                 {return error(errExtended, err);};
00459 
00466         inline void setError(bool enable)
00467                 {flags.thrown = !enable;};
00468 
00469 #ifndef WIN32
00470 
00477         Error setCompletion(Complete mode);
00478 #endif
00479 
00486         inline void setTemporary(bool enable)
00487                 {flags.temp = enable;};
00488 
00500         virtual Attr initialize(void);
00501 
00505         void final(void);
00506 
00507 public:
00511         virtual ~RandomFile();
00512 
00521         bool initial(void);
00522 
00528         off_t getCapacity(void);
00529 
00535         virtual Error restart(void);
00536 
00542         inline Error getErrorNumber(void)
00543                 {return errid;};
00544 
00550         inline char *getErrorString(void)
00551                 {return errstr;};
00552 
00553         bool operator!(void);
00554 };
00555 
00575 class __EXPORT ThreadFile : public RandomFile
00576 {
00577 private:
00578         ThreadKey state;
00579         fcb_t *first;
00580         fcb_t *getFCB(void);
00581         Error open(const char *path);
00582 
00583 public:
00590         ThreadFile(const char *path);
00591 
00595         virtual ~ThreadFile();
00596 
00602         Error restart(void);
00603 
00613         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00614 
00624         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00625 
00631         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00632 
00638         off_t getPosition(void);
00639 
00640         bool operator++(void);
00641         bool operator--(void);
00642 };
00643 
00658 class __EXPORT SharedFile : public RandomFile
00659 {
00660 private:
00661         fcb_t fcb;
00662         Error open(const char *path);
00663 
00664 public:
00671         SharedFile(const char *path);
00672 
00679         SharedFile(const SharedFile &file);
00680 
00684         virtual ~SharedFile();
00685 
00691         Error restart(void)
00692                 {return open(pathname);};
00693 
00704         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00705 
00716         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00717 
00726         Error clear(ccxx_size_t length = 0, off_t pos = -1);
00727 
00734         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00735 
00741         off_t getPosition(void);
00742 
00743         bool operator++(void);
00744         bool operator--(void);
00745 };
00746 
00757 class __EXPORT MappedFile : public RandomFile
00758 {
00759 private:
00760         fcb_t fcb;
00761         int prot;
00762 #ifdef  WIN32
00763         HANDLE map;
00764         char mapname[64];
00765 #endif
00766 
00767 public:
00775         MappedFile(const char *fname, Access mode);
00776 
00785         MappedFile(const char *fname, Access mode, size_t size);
00786 
00797         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00798 
00803         virtual ~MappedFile();
00804 
00805         // FIXME: not use library function in header ??
00811         void sync(void);
00812 
00819         void sync(caddr_t address, size_t len);
00820 
00829         void update(size_t offset = 0, size_t len = 0);
00830 
00838         void update(caddr_t address, size_t len);
00839 
00846         void release(caddr_t address, size_t len);
00847 
00856         inline caddr_t fetch(size_t offset = 0)
00857                 {return ((char *)(fcb.address)) + offset;};
00858 
00867         caddr_t fetch(off_t pos, size_t len);
00868 
00874         bool lock(void);
00875 
00879         void unlock(void);
00880 
00887         size_t pageAligned(size_t size);
00888 };
00889 
00890 
00899 class __EXPORT DSO
00900 {
00901 private:
00902         const char *err;
00903 #ifdef  HAVE_MODULES
00904         static Mutex mutex;
00905         static DSO *first;
00906         static DSO *last;
00907         DSO *next, *prev;
00908         const char *id;
00909 #if     defined(HAVE_MACH_DYLD)
00910         NSModule oModule;
00911 #elif   defined(HAVE_SHL_LOAD)
00912         shl_t image;
00913 #elif   defined(WIN32)
00914         HINSTANCE hImage;
00915 #else
00916         void *image;
00917 #endif
00918         void loader(const char *filename, bool resolve);
00919 #endif
00920 
00921 public:
00927 #ifdef  HAVE_MODULES
00928         DSO(const char *filename)
00929                 {loader(filename, true);};
00930 
00931         DSO(const char *filename, bool resolve)
00932                 {loader(filename, resolve);};
00933 #else
00934         DSO(const char *filename)
00935                 {throw this;};
00936         DSO(const char *filename, bool resolve)
00937                 {throw this;};
00938 #endif
00939 
00944         inline const char *getError(void)
00945                 {return err;};
00946 
00950 #ifdef  HAVE_MODULES
00951         virtual ~DSO();
00952 #endif
00953 
00957 #ifdef  HAVE_MODULES
00958         void* operator[](const char *sym);
00959 #else
00960         void *operator[](const char *)
00961                 {return NULL;};
00962 #endif
00963 
00964 #ifdef  HAVE_MODULES
00965         static void dynunload(void);
00966 #else
00967         static void dynunload(void)
00968                 {return;};
00969 #endif
00970 
00976         static DSO *getObject(const char *name);
00977 
00983         bool isValid(void);
00984 
00988         static void setDebug(void);
00989 };
00990 
00992 bool __EXPORT isDir(const char *path);
00994 bool __EXPORT isFile(const char *path);
00995 #ifndef WIN32
00996 
00997 bool __EXPORT isDevice(const char *path);
00998 #else
00999 
01000 inline bool isDevice(const char *path)
01001 { return false; }
01002 #endif
01003 
01004 bool __EXPORT canAccess(const char *path);
01006 bool __EXPORT canModify(const char *path);
01008 time_t __EXPORT lastModified(const char *path);
01010 time_t __EXPORT lastAccessed(const char *path);
01011 
01012 #ifdef  COMMON_STD_EXCEPTION
01013 
01014 class DirException : public IOException
01015 {
01016 public:
01017         DirException(const String &str) : IOException(str) {};
01018 };
01019 
01020 class __EXPORT DSOException : public IOException
01021 {
01022 public:
01023         DSOException(const String &str) : IOException(str) {};
01024 };
01025 
01026 class __EXPORT FileException : public IOException
01027 {
01028 public:
01029         FileException(const String &str) : IOException(str) {};
01030 };
01031 
01032 #endif
01033 
01034 #ifdef  CCXX_NAMESPACES
01035 }
01036 #endif
01037 
01038 #endif
01039 

Generated on Sun Jul 30 23:52:06 2006 for GNU CommonC++ by  doxygen 1.4.7