Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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 #ifdef  __BORLANDC__
00067 #include <stdio.h>
00068 #include <sys/types.h>
00069 #else
00070 #include <cstdio>
00071 #endif
00072 #include <dirent.h>
00073 #include <sys/stat.h>
00074 #include <sys/mman.h>
00075 #else
00076 #include <direct.h>
00077 #endif
00078 
00079 #ifdef  HAVE_MACH_DYLD
00080 #include <mach-o/dyld.h>
00081 #endif
00082 
00083 #ifdef  CCXX_NAMESPACES
00084 namespace ost {
00085 #endif
00086 
00087 typedef unsigned long pos_t;
00088 #ifndef WIN32
00089 // use a define so that if the sys/types.h header already defines caddr_t
00090 // as it may on BSD systems, we do not break it by redefining again.
00091 #undef  caddr_t
00092 #define caddr_t char *
00093 typedef size_t ccxx_size_t;
00094 #else
00095 #ifndef __BORLANDC__
00096 typedef LONG off_t;
00097 #endif
00098 typedef void* caddr_t;
00099 typedef DWORD ccxx_size_t;
00100 #endif
00101 
00102 #ifndef PATH_MAX
00103 #define PATH_MAX        256
00104 #endif
00105 
00106 #ifndef NAME_MAX
00107 #define NAME_MAX        64
00108 #endif
00109 
00110 class __EXPORT File
00111 {
00112 public:
00113         enum Error
00114         {
00115                 errSuccess = 0,
00116                 errNotOpened,
00117                 errMapFailed,
00118                 errInitFailed,
00119                 errOpenDenied,
00120                 errOpenFailed,
00121                 errOpenInUse,
00122                 errReadInterrupted,
00123                 errReadIncomplete,
00124                 errReadFailure,
00125                 errWriteInterrupted,
00126                 errWriteIncomplete,
00127                 errWriteFailure,
00128                 errExtended
00129         };
00130         typedef enum Error Error;
00131 
00132         enum Access
00133         {
00134 #ifndef WIN32
00135                 accessReadOnly = O_RDONLY,
00136                 accessWriteOnly= O_WRONLY,
00137                 accessReadWrite = O_RDWR
00138 #else
00139                 accessReadOnly = GENERIC_READ,
00140                 accessWriteOnly = GENERIC_WRITE,
00141                 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00142 #endif
00143         };
00144         typedef enum Access Access;
00145 
00146 protected:
00147         typedef struct _fcb
00148         {
00149                 struct _fcb *next;
00150                 caddr_t address;
00151                 ccxx_size_t len;
00152                 off_t pos;
00153         } fcb_t;
00154 
00155 public:
00156 #ifdef  WIN32
00157         enum Open
00158         {
00159                 openReadOnly, // = FILE_OPEN_READONLY,
00160                 openWriteOnly, // = FILE_OPEN_WRITEONLY,
00161                 openReadWrite, // = FILE_OPEN_READWRITE,
00162                 openAppend, // = FILE_OPEN_APPEND,
00163                 openTruncate // = FILE_OPEN_TRUNCATE
00164         };
00165         #else
00166         enum Open
00167         {
00168                 openReadOnly = O_RDONLY,
00169                 openWriteOnly = O_WRONLY,
00170                 openReadWrite = O_RDWR,
00171                 openAppend = O_WRONLY | O_APPEND,
00172 #ifdef  O_SYNC
00173                 openSync = O_RDWR | O_SYNC,
00174 #else
00175                 openSync = O_RDWR,
00176 #endif
00177                 openTruncate = O_RDWR | O_TRUNC
00178         };
00179         typedef enum Open Open;
00180 
00181 /* to be used in future */
00182 
00183 #ifndef S_IRUSR
00184 #define S_IRUSR 0400
00185 #define S_IWUSR 0200
00186 #define S_IRGRP 0040
00187 #define S_IWGRP 0020
00188 #define S_IROTH 0004
00189 #define S_IWOTH 0002
00190 #endif
00191 
00192 #endif // !WIN32
00193 
00194 #ifndef WIN32
00195         enum Attr
00196         {
00197                 attrInvalid = 0,
00198                 attrPrivate = S_IRUSR | S_IWUSR,
00199                 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00200                 attrPublic = attrGroup | S_IROTH | S_IWOTH
00201         };
00202         #else // defined WIN32
00203         enum Attr {
00204                 attrInvalid=0,
00205                 attrPrivate,
00206                 attrGroup,
00207                 attrPublic
00208         };
00209 #endif // !WIN32
00210         typedef enum Attr Attr;
00211 
00212 #ifdef  WIN32
00213         enum Complete
00214         {
00215                 completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
00216                 completionDelayed, // = FILE_COMPLETION_DELAYED,
00217                 completionDeferred // = FILE_COMPLETION_DEFERRED
00218         };
00219 
00220         enum Mapping
00221         {
00222                 mappedRead,
00223                 mappedWrite,
00224                 mappedReadWrite
00225         };
00226 #else
00227         enum Mapping
00228         {
00229                 mappedRead = accessReadOnly,
00230                 mappedWrite = accessWriteOnly,
00231                 mappedReadWrite = accessReadWrite
00232         };
00233         enum Complete
00234         {
00235                 completionImmediate,
00236                 completionDelayed,
00237                 completionDeferred
00238         };
00239 #endif
00240         typedef enum Complete Complete;
00241         typedef enum Mapping Mapping;
00242 
00243 public:
00244         static const char *getExtension(const char *path);
00245         static const char *getFilename(const char *path);
00246         static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
00247         static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
00248         static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
00249 };
00250 
00251 #ifndef WIN32
00252 
00262 class fifostream : public std::fstream, public File
00263 {
00264 private:
00265         char *pathname;
00266 
00267 public:
00272         fifostream();
00273 
00280         fifostream(const char *fname, long access = (long)attrGroup);
00281 
00285         virtual ~fifostream();
00286 
00294         void open(const char *fname, long access = (long)attrGroup);
00295 
00299         void close(void);
00300 };
00301 #endif // !WIN32
00302 
00303 #ifndef WIN32
00304 
00310 class FIFOSession : public Thread, public std::fstream, public File
00311 {
00312 private:
00313         char *pathname;
00314 
00315 public:
00316         FIFOSession(const char *session, long access = (long)attrGroup, int pri = 0, int stack = 0);
00317         virtual ~FIFOSession();
00318 };
00319 #endif // !WIN32
00320 
00329 class __EXPORT Dir : public File
00330 {
00331 private:
00332 #ifndef WIN32
00333         DIR *dir;
00334 #ifdef  HAVE_READDIR_R
00335         struct dirent *save;
00336         char save_space[sizeof(struct dirent) + PATH_MAX + 1];
00337 #endif
00338         struct dirent *entry;
00339 #else
00340         HANDLE hDir;
00341         WIN32_FIND_DATA data, fdata;
00342         char *name;
00343 #endif
00344 
00345 public:
00346         Dir(const char *name = NULL);
00347 
00348         static bool create(const char *path, Attr attr = attrGroup);
00349         static bool remove(const char *path);
00350         static bool setPrefix(const char *path);
00351         static bool getPrefix(char *path, size_t size = PATH_MAX);
00352 
00353         void open(const char *name);
00354         void close(void);
00355 
00356         virtual ~Dir();
00357 
00358         const char *getName(void);
00359 
00360         const char *operator++()
00361                 {return getName();};
00362 
00363         const char *operator++(int)
00364                 {return getName();};
00365 
00366         const char *operator*();
00367 
00368         bool rewind(void);
00369 
00370         bool operator!()
00371 #ifndef WIN32
00372                 {return !dir;};
00373 #else
00374                 {return hDir != INVALID_HANDLE_VALUE;};
00375 #endif
00376 
00377         bool isValid(void);
00378 };
00379 
00390 class __EXPORT RandomFile : protected Mutex, public File
00391 {
00392 private:
00393         Error errid;
00394         char *errstr;
00395 
00396 protected:
00397 #ifndef WIN32
00398         int fd;
00399         // FIXME: WIN32 as no access member
00400         Access access;
00401 #else
00402         HANDLE fd;
00403 #endif
00404         char *pathname;
00405 
00406         struct
00407         {
00408                 unsigned count : 16;
00409                 bool thrown : 1;
00410                 bool initial : 1;
00411 #ifndef WIN32
00412                 bool immediate : 1;
00413 #endif
00414                 bool temp : 1;
00415         } flags;
00416 
00420         RandomFile(const char *name = NULL);
00421 
00425         RandomFile(const RandomFile &rf);
00426 
00434         Error error(Error errid, char *errstr = NULL);
00435 
00442         inline Error error(char *err)
00443                 {return error(errExtended, err);};
00444 
00451         inline void setError(bool enable)
00452                 {flags.thrown = !enable;};
00453 
00454 #ifndef WIN32
00455 
00462         Error setCompletion(Complete mode);
00463 #endif
00464 
00471         inline void setTemporary(bool enable)
00472                 {flags.temp = enable;};
00473 
00485         virtual Attr initialize(void)
00486                 {return attrPublic;};
00487 
00491         void final(void);
00492 
00493 public:
00497         virtual ~RandomFile()
00498                 {final();};
00499 
00508         bool initial(void);
00509 
00515         off_t getCapacity(void);
00516 
00522         virtual Error restart(void)
00523                 {return errOpenFailed;};
00524 
00530         inline Error getErrorNumber(void)
00531                 {return errid;};
00532 
00538         inline char *getErrorString(void)
00539                 {return errstr;};
00540 
00541         bool operator!(void);
00542 };
00543 
00563 class __EXPORT ThreadFile : public RandomFile
00564 {
00565 private:
00566         ThreadKey state;
00567         fcb_t *first;
00568         fcb_t *getFCB(void);
00569         Error open(const char *path);
00570 
00571 public:
00578         ThreadFile(const char *path);
00579 
00583         virtual ~ThreadFile();
00584 
00590         Error restart(void)
00591                 {return open(pathname);};
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 
00747 class __EXPORT MappedFile : public RandomFile
00748 {
00749 private:
00750         fcb_t fcb;
00751         int prot;
00752 #ifdef  WIN32
00753         HANDLE map;
00754         char mapname[64];
00755 #endif
00756 
00757 public:
00765         MappedFile(const char *fname, Access mode);
00766 
00777         MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00778 
00783         virtual ~MappedFile();
00784 
00785         // FIXME: not use library function in header ??
00791         void sync(void);
00792 
00799         void sync(caddr_t address, size_t len);
00800 
00809         void update(size_t offset = 0, size_t len = 0);
00810 
00818         void update(caddr_t address, size_t len);
00819 
00826         void release(caddr_t address, size_t len);
00827 
00836         inline caddr_t fetch(size_t offset = 0)
00837                 {return ((char *)(fcb.address)) + offset;};
00838 
00847         caddr_t fetch(off_t pos, size_t len);
00848 };
00849 
00850 
00859 class __EXPORT DSO
00860 {
00861 private:
00862         const char *err;
00863 #ifdef  HAVE_MODULES
00864         static Mutex mutex;
00865         static DSO *first;
00866         static DSO *last;
00867         DSO *next, *prev;
00868         const char *id;
00869 #if     defined(HAVE_MACH_DYLD)
00870         NSModule oModule;
00871 #elif   defined(HAVE_SHL_LOAD)
00872         shl_t image;
00873 #elif   defined(WIN32)
00874         HINSTANCE hImage;
00875 #else
00876         void *image;
00877 #endif
00878         void loader(const char *filename, bool resolve);
00879 #endif
00880 
00881 public:
00887 #ifdef  HAVE_MODULES
00888         DSO(const char *filename)
00889                 {loader(filename, true);};
00890 
00891         DSO(const char *filename, bool resolve)
00892                 {loader(filename, resolve);};
00893 #else
00894         DSO(const char *filename)
00895                 {throw this;};
00896         DSO(const char *filename, bool resolve)
00897                 {throw this;};
00898 #endif
00899 
00904         inline const char *getError(void)
00905                 {return err;};
00906 
00910 #ifdef  HAVE_MODULES
00911         virtual ~DSO();
00912 #endif
00913 
00917 #ifdef  HAVE_MODULES
00918         void* operator[](const char *sym);
00919 #else
00920         void *operator[](const char *)
00921                 {return NULL;};
00922 #endif
00923 
00924 #ifdef  HAVE_MODULES
00925         static void dynunload(void);
00926 #else
00927         static void dynunload(void)
00928                 {return;};
00929 #endif
00930 
00936         static DSO *getObject(const char *name);
00937 
00943         bool isValid(void);
00944 };
00945 
00947 bool __EXPORT isDir(const char *path);
00949 bool __EXPORT isFile(const char *path);
00950 #ifndef WIN32
00951 
00952 bool __EXPORT isDevice(const char *path);
00953 #else
00954 
00955 inline bool isDevice(const char *path)
00956 { return false; }
00957 #endif
00958 
00959 bool __EXPORT canAccess(const char *path);
00961 bool __EXPORT canModify(const char *path);
00962 
00963 #ifdef  COMMON_STD_EXCEPTION
00964 
00965 class DirException : public IOException
00966 {
00967 public:
00968         DirException(const String &str) : IOException(str) {};
00969 };
00970 
00971 class __EXPORT DSOException : public IOException
00972 {
00973 public:
00974         DSOException(const String &str) : IOException(str) {};
00975 };
00976 
00977 class __EXPORT FIFOException : public IOException
00978 {
00979 public:
00980         FIFOException(const String &str) : IOException(str) {};
00981 };
00982 
00983 class __EXPORT PipeException : public IOException
00984 {
00985 public:
00986         PipeException(const String &str) : IOException(str) {};
00987 };
00988 
00989 class __EXPORT FileException : public IOException
00990 {
00991 public:
00992         FileException(const String &str) : IOException(str) {};
00993 };
00994 
00995 #endif
00996 
00997 #ifdef  CCXX_NAMESPACES
00998 }
00999 #endif
01000 
01001 #endif
01002 

Generated on Tue Jan 18 14:32:37 2005 for GNU CommonC++ by  doxygen 1.3.9.1