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 # 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                 errSuccess = 0,
00121                 errNotOpened,
00122                 errMapFailed,
00123                 errInitFailed,
00124                 errOpenDenied,
00125                 errOpenFailed,
00126                 errOpenInUse,
00127                 errReadInterrupted,
00128                 errReadIncomplete,
00129                 errReadFailure,
00130                 errWriteInterrupted,
00131                 errWriteIncomplete,
00132                 errWriteFailure,
00133                 errLockFailure,
00134                 errExtended
00135         };
00136         typedef enum Error Error;
00137 
00138         enum Access {
00139 #ifndef WIN32
00140                 accessReadOnly = O_RDONLY,
00141                 accessWriteOnly= O_WRONLY,
00142                 accessReadWrite = O_RDWR
00143 #else
00144                 accessReadOnly = GENERIC_READ,
00145                 accessWriteOnly = GENERIC_WRITE,
00146                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00147 #endif
00148         };
00149         typedef enum Access Access;
00150 
00151 protected:
00152         typedef struct _fcb {
00153                 struct _fcb *next;
00154                 caddr_t address;
00155                 ccxx_size_t len;
00156                 off_t pos;
00157                 bool locked;
00158         } fcb_t;
00159 
00160 public:
00161 #ifdef  WIN32
00162         enum Open {
00163                 openReadOnly, // = FILE_OPEN_READONLY,
00164                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00165                 openReadWrite, // = FILE_OPEN_READWRITE,
00166                 openAppend, // = FILE_OPEN_APPEND,
00167                 openTruncate // = FILE_OPEN_TRUNCATE
00168         };
00169         #else
00170         enum Open {
00171                 openReadOnly = O_RDONLY,
00172                 openWriteOnly = O_WRONLY,
00173                 openReadWrite = O_RDWR,
00174                 openAppend = O_WRONLY | O_APPEND,
00175 #ifdef  O_SYNC
00176                 openSync = O_RDWR | O_SYNC,
00177 #else
00178                 openSync = O_RDWR,
00179 #endif
00180                 openTruncate = O_RDWR | O_TRUNC
00181         };
00182         typedef enum Open Open;
00183 
00184 /* to be used in future */
00185 
00186 #ifndef S_IRUSR
00187 #define S_IRUSR 0400
00188 #define S_IWUSR 0200
00189 #define S_IRGRP 0040
00190 #define S_IWGRP 0020
00191 #define S_IROTH 0004
00192 #define S_IWOTH 0002
00193 #endif
00194 
00195 #endif // !WIN32
00196 
00197 #ifndef WIN32
00198         enum Attr {
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                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00217                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00218                 completionDeferred // = FILE_COMPLETION_DEFERRED
00219         };
00220 
00221         enum Mapping {
00222                 mappedRead,
00223                 mappedWrite,
00224                 mappedReadWrite
00225         };
00226 #else
00227         enum Mapping {
00228                 mappedRead = accessReadOnly,
00229                 mappedWrite = accessWriteOnly,
00230                 mappedReadWrite = accessReadWrite
00231         };
00232         enum Complete {
00233                 completionImmediate,
00234                 completionDelayed,
00235                 completionDeferred
00236         };
00237 #endif
00238         typedef enum Complete Complete;
00239         typedef enum Mapping Mapping;
00240 
00241 public:
00242         static const char *getExtension(const char *path);
00243         static const char *getFilename(const char *path);
00244         static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00245         static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00246         static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00247 };
00248 
00257 class __EXPORT Dir : public File
00258 {
00259 private:
00260 #ifndef WIN32
00261         DIR *dir;
00262 #ifdef  HAVE_READDIR_R
00263         struct dirent *save;
00264         char save_space[sizeof(struct dirent) + PATH_MAX + 1];
00265 #endif
00266         struct dirent *entry;
00267 #else
00268         HANDLE hDir;
00269         WIN32_FIND_DATA data, fdata;
00270         char *name;
00271 #endif
00272 
00273 public:
00274         Dir(const char *name = NULL);
00275 
00276         static bool create(const char *path, Attr attr = attrGroup);
00277         static bool remove(const char *path);
00278         static bool setPrefix(const char *path);
00279         static bool getPrefix(char *path, size_t size = PATH_MAX);
00280 
00281         void open(const char *name);
00282         void close(void);
00283 
00284         virtual ~Dir();
00285 
00286         const char *getName(void);
00287 
00288         const char *operator++()
00289                 {return getName();};
00290 
00291         const char *operator++(int)
00292                 {return getName();};
00293 
00294         const char *operator*();
00295 
00296         bool rewind(void);
00297 
00298         bool operator!()
00299 #ifndef WIN32
00300                 {return !dir;};
00301 #else
00302                 {return hDir != INVALID_HANDLE_VALUE;};
00303 #endif
00304 
00305         bool isValid(void);
00306 };
00307 
00314 class __EXPORT  DirTree
00315 {
00316 private:
00317         char path[PATH_MAX + 1];
00318         Dir *dir;
00319         unsigned max, current, prefixpos;
00320 
00321 protected:
00331         virtual bool filter(const char *file, struct stat *ino);
00332 
00333 public:
00341         DirTree(const char *prefix, unsigned maxdepth);
00342 
00348         DirTree(unsigned maxdepth);
00349 
00350         virtual ~DirTree();
00351 
00357         void open(const char *prefix);
00358 
00362         void close(void);
00363 
00371         char *getPath(void);
00372 
00382         unsigned perform(const char *prefix);
00383 };
00384 
00395 class __EXPORT RandomFile : protected Mutex, public File
00396 {
00397 private:
00398         Error errid;
00399         char *errstr;
00400 
00401 protected:
00402 #ifndef WIN32
00403         int fd;
00404         // FIXME: WIN32 as no access member
00405         Access access;
00406 #else
00407         HANDLE fd;
00408 #endif
00409         char *pathname;
00410 
00411         struct {
00412                 unsigned count : 16;
00413                 bool thrown : 1;
00414                 bool initial : 1;
00415 #ifndef WIN32
00416                 bool immediate : 1;
00417 #endif
00418                 bool temp : 1;
00419         } flags;
00420 
00424         RandomFile(const char *name = NULL);
00425 
00429         RandomFile(const RandomFile &rf);
00430 
00438         Error error(Error errid, char *errstr = NULL);
00439 
00446         inline Error error(char *err)
00447                 {return error(errExtended, err);};
00448 
00455         inline void setError(bool enable)
00456                 {flags.thrown = !enable;};
00457 
00458 #ifndef WIN32
00459 
00466         Error setCompletion(Complete mode);
00467 #endif
00468 
00475         inline void setTemporary(bool enable)
00476                 {flags.temp = enable;};
00477 
00489         virtual Attr initialize(void);
00490 
00494         void final(void);
00495 
00496 public:
00500         virtual ~RandomFile();
00501 
00510         bool initial(void);
00511 
00517         off_t getCapacity(void);
00518 
00524         virtual Error restart(void);
00525 
00531         inline Error getErrorNumber(void)
00532                 {return errid;};
00533 
00539         inline char *getErrorString(void)
00540                 {return errstr;};
00541 
00542         bool operator!(void);
00543 };
00544 
00564 class __EXPORT ThreadFile : public RandomFile
00565 {
00566 private:
00567         ThreadKey state;
00568         fcb_t *first;
00569         fcb_t *getFCB(void);
00570         Error open(const char *path);
00571 
00572 public:
00579         ThreadFile(const char *path);
00580 
00584         virtual ~ThreadFile();
00585 
00591         Error restart(void);
00592 
00602         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00603 
00613         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00614 
00620         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00621 
00627         off_t getPosition(void);
00628 
00629         bool operator++(void);
00630         bool operator--(void);
00631 };
00632 
00647 class __EXPORT SharedFile : public RandomFile
00648 {
00649 private:
00650         fcb_t fcb;
00651         Error open(const char *path);
00652 
00653 public:
00660         SharedFile(const char *path);
00661 
00668         SharedFile(const SharedFile &file);
00669 
00673         virtual ~SharedFile();
00674 
00680         Error restart(void)
00681                 {return open(pathname);};
00682 
00693         Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00694 
00705         Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00706 
00715         Error clear(ccxx_size_t length = 0, off_t pos = -1);
00716 
00723         Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00724 
00730         off_t getPosition(void);
00731 
00732         bool operator++(void);
00733         bool operator--(void);
00734 };
00735 
00746 class __EXPORT MappedFile : public RandomFile
00747 {
00748 private:
00749         fcb_t fcb;
00750         int prot;
00751 #ifdef  WIN32
00752         HANDLE map;
00753         char mapname[64];
00754 #endif
00755 
00756 public:
00764         MappedFile(const char *fname, Access mode);
00765 
00774         MappedFile(const char *fname, Access mode, size_t size);
00775 
00786         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00787 
00792         virtual ~MappedFile();
00793 
00794         // FIXME: not use library function in header ??
00800         void sync(void);
00801 
00808         void sync(caddr_t address, size_t len);
00809 
00818         void update(size_t offset = 0, size_t len = 0);
00819 
00827         void update(caddr_t address, size_t len);
00828 
00835         void release(caddr_t address, size_t len);
00836 
00845         inline caddr_t fetch(size_t offset = 0)
00846                 {return ((char *)(fcb.address)) + offset;};
00847 
00856         caddr_t fetch(off_t pos, size_t len);
00857 
00863         bool lock(void);
00864 
00868         void unlock(void);
00869 
00876         size_t pageAligned(size_t size);
00877 };
00878 
00879 
00888 class __EXPORT DSO
00889 {
00890 private:
00891         const char *err;
00892 #ifdef  HAVE_MODULES
00893         static Mutex mutex;
00894         static DSO *first;
00895         static DSO *last;
00896         DSO *next, *prev;
00897         const char *id;
00898 #if     defined(HAVE_MACH_DYLD)
00899         NSModule oModule;
00900 #elif   defined(HAVE_SHL_LOAD)
00901         shl_t image;
00902 #elif   defined(WIN32)
00903         HINSTANCE hImage;
00904 #else
00905         void *image;
00906 #endif
00907         void loader(const char *filename, bool resolve);
00908 #endif
00909 
00910 public:
00916 #ifdef  HAVE_MODULES
00917         DSO(const char *filename)
00918                 {loader(filename, true);};
00919 
00920         DSO(const char *filename, bool resolve)
00921                 {loader(filename, resolve);};
00922 #else
00923         DSO(const char *filename)
00924                 {throw this;};
00925         DSO(const char *filename, bool resolve)
00926                 {throw this;};
00927 #endif
00928 
00933         inline const char *getError(void)
00934                 {return err;};
00935 
00939 #ifdef  HAVE_MODULES
00940         virtual ~DSO();
00941 #endif
00942 
00946 #ifdef  HAVE_MODULES
00947         void* operator[](const char *sym);
00948 #else
00949         void *operator[](const char *)
00950                 {return NULL;};
00951 #endif
00952 
00953 #ifdef  HAVE_MODULES
00954         static void dynunload(void);
00955 #else
00956         static void dynunload(void)
00957                 {return;};
00958 #endif
00959 
00965         static DSO *getObject(const char *name);
00966 
00972         bool isValid(void);
00973 
00977         static void setDebug(void);
00978 };
00979 
00981 bool __EXPORT isDir(const char *path);
00983 bool __EXPORT isFile(const char *path);
00984 #ifndef WIN32
00985 
00986 bool __EXPORT isDevice(const char *path);
00987 #else
00988 
00989 inline bool isDevice(const char *path)
00990 { return false; }
00991 #endif
00992 
00993 bool __EXPORT canAccess(const char *path);
00995 bool __EXPORT canModify(const char *path);
00997 time_t __EXPORT lastModified(const char *path);
00999 time_t __EXPORT lastAccessed(const char *path);
01000 
01001 #ifdef  COMMON_STD_EXCEPTION
01002 
01003 class DirException : public IOException
01004 {
01005 public:
01006         DirException(const String &str) : IOException(str) {};
01007 };
01008 
01009 class __EXPORT DSOException : public IOException
01010 {
01011 public:
01012         DSOException(const String &str) : IOException(str) {};
01013 };
01014 
01015 class __EXPORT FileException : public IOException
01016 {
01017 public:
01018         FileException(const String &str) : IOException(str) {};
01019 };
01020 
01021 #endif
01022 
01023 #ifdef  CCXX_NAMESPACES
01024 }
01025 #endif
01026 
01027 #endif
01028 

Generated on Fri May 30 14:45:03 2008 for GNU CommonC++ by doxygen 1.3.5