00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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_THREAD_H_
00054 #include <cc++/thread.h>
00055 #endif
00056
00057 #include <iostream>
00058 #include <fstream>
00059
00060 #ifndef WIN32
00061 #include <cstdio>
00062 #include <dirent.h>
00063 #include <sys/stat.h>
00064 #include <sys/mman.h>
00065 #else
00066 #include <direct.h>
00067 #endif
00068
00069 #ifdef HAVE_MACH_DYLD
00070 #include <mach-o/dyld.h>
00071 #endif
00072
00073 #ifdef CCXX_NAMESPACES
00074 namespace ost {
00075 #endif
00076
00077 typedef unsigned long pos_t;
00078 #ifndef WIN32
00079
00080
00081 #undef caddr_t
00082 #define caddr_t char *
00083 typedef size_t ccxx_size_t;
00084 #else
00085 typedef LONG off_t;
00086 typedef void* caddr_t;
00087 typedef DWORD ccxx_size_t;
00088 #endif
00089
00090 class File
00091 {
00092 public:
00093 enum Error
00094 {
00095 errSuccess = 0,
00096 errNotOpened,
00097 errMmapFailed,
00098 errInitFailed,
00099 errOpenDenied,
00100 errOpenFailed,
00101 errOpenInUse,
00102 errReadInterrupted,
00103 errReadIncomplete,
00104 errReadFailure,
00105 errWriteInterrupted,
00106 errWriteIncomplete,
00107 errWriteFailure,
00108 errExtended
00109 };
00110 typedef enum Error Error;
00111
00112 enum Access
00113 {
00114 #ifndef WIN32
00115 accessReadOnly = O_RDONLY,
00116 accessWriteOnly= O_WRONLY,
00117 accessReadWrite = O_RDWR
00118 #else
00119 accessReadOnly = GENERIC_READ,
00120 accessWriteOnly = GENERIC_WRITE,
00121 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00122 #endif
00123 };
00124 typedef enum Access Access;
00125
00126 protected:
00127 typedef struct _fcb
00128 {
00129 struct _fcb *next;
00130 caddr_t address;
00131 ccxx_size_t len;
00132 off_t pos;
00133 } fcb_t;
00134
00135 #ifdef WIN32
00136 enum Open
00137 {
00138 openReadOnly,
00139 openWriteOnly,
00140 openReadWrite,
00141 openAppend,
00142 openTruncate
00143 };
00144 #else
00145 enum Open
00146 {
00147 openReadOnly = O_RDONLY,
00148 openWriteOnly = O_WRONLY,
00149 openReadWrite = O_RDWR,
00150 openAppend = O_WRONLY | O_APPEND,
00151 #ifdef O_SYNC
00152 openSync = O_RDWR | O_SYNC,
00153 #else
00154 openSync = O_RDWR,
00155 #endif
00156 openTruncate = O_RDWR | O_TRUNC
00157 };
00158 typedef enum Open Open;
00159
00160
00161
00162 #ifndef S_IRUSR
00163 #define S_IRUSR 0400
00164 #define S_IWUSR 0200
00165 #define S_IRGRP 0040
00166 #define S_IWGRP 0020
00167 #define S_IROTH 0004
00168 #define S_IWOTH 0002
00169 #endif
00170
00171 #endif // !WIN32
00172
00173 #ifndef WIN32
00174 enum Attr
00175 {
00176 attrInvalid = 0,
00177 attrPrivate = S_IRUSR | S_IWUSR,
00178 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
00179 attrPublic = attrGroup | S_IROTH | S_IWOTH
00180 };
00181 #else // defined WIN32
00182 enum Attr {
00183 attrInvalid=0,
00184 attrPrivate,
00185 attrGroup,
00186 attrPublic
00187 };
00188 #endif // !WIN32
00189 typedef enum Attr Attr;
00190
00191 #ifdef WIN32
00192 enum Complete
00193 {
00194 completionImmediate,
00195 completionDelayed,
00196 completionDeferred
00197 };
00198
00199 enum Mapping
00200 {
00201 mappedRead,
00202 mappedWrite,
00203 mappedReadWrite
00204 };
00205 #else
00206 enum Mapping
00207 {
00208 mappedRead = accessReadOnly,
00209 mappedWrite = accessWriteOnly,
00210 mappedReadWrite = accessReadWrite
00211 };
00212 enum Complete
00213 {
00214 completionImmediate,
00215 completionDelayed,
00216 completionDeferred
00217 };
00218 #endif
00219 typedef enum Complete Complete;
00220 typedef enum Mapping Mapping;
00221 };
00222
00223 #ifndef WIN32
00224
00234 class fifostream : public std::fstream, public File
00235 {
00236 private:
00237 char *pathname;
00238
00239 public:
00244 fifostream();
00245
00252 fifostream(const char *fname, long access = (long)attrGroup);
00253
00257 virtual ~fifostream();
00258
00266 void open(const char *fname, long access = (long)attrGroup);
00267
00271 void close(void);
00272 };
00273 #endif // !WIN32
00274
00275 #ifndef WIN32
00276
00282 class FIFOSession : public Thread, public std::fstream, public File
00283 {
00284 private:
00285 char *pathname;
00286
00287 public:
00288 FIFOSession(const char *session, long access = (long)attrGroup, int pri = 0, int stack = 0);
00289 virtual ~FIFOSession();
00290 };
00291 #endif // !WIN32
00292
00301 class CCXX_CLASS_EXPORT Dir
00302 {
00303 private:
00304 #ifndef WIN32
00305 DIR *dir;
00306 #else
00307 HANDLE hDir;
00308 WIN32_FIND_DATA data;
00309 char *name;
00310 #endif
00311
00312 public:
00313 Dir(const char *name);
00314 virtual ~Dir();
00315
00316 char *getName(void);
00317
00318 bool operator!()
00319 #ifndef WIN32
00320 {return !dir;};
00321 #else
00322 {return hDir != INVALID_HANDLE_VALUE;};
00323 #endif
00324
00325 bool isValid(void);
00326 };
00327
00338 class CCXX_CLASS_EXPORT RandomFile : protected Mutex, public File
00339 {
00340 private:
00341 Error errid;
00342 char *errstr;
00343
00344 protected:
00345 #ifndef WIN32
00346 int fd;
00347
00348 Access access;
00349 #else
00350 HANDLE fd;
00351 #endif
00352 char *pathname;
00353
00354 struct
00355 {
00356 unsigned count : 16;
00357 bool thrown : 1;
00358 bool initial : 1;
00359 #ifndef WIN32
00360 bool immediate : 1;
00361 #endif
00362 bool temp : 1;
00363 } flags;
00364
00368 RandomFile();
00369
00373 RandomFile(const RandomFile &rf);
00374
00382 Error error(Error errid, char *errstr = NULL);
00383
00390 inline Error error(char *errstr)
00391 {return error(errExtended, errstr);};
00392
00399 inline void setError(bool enable)
00400 {flags.thrown = !enable;};
00401
00402 #ifndef WIN32
00403
00410 Error setCompletion(Complete mode);
00411 #endif
00412
00419 inline void setTemporary(bool enable)
00420 {flags.temp = enable;};
00421
00433 virtual Attr initialize(void)
00434 {return attrPublic;};
00435
00439 void final(void);
00440
00441 public:
00445 virtual ~RandomFile()
00446 {final();};
00447
00456 bool initial(void);
00457
00463 off_t getCapacity(void);
00464
00470 virtual Error restart(void)
00471 {return errOpenFailed;};
00472
00478 inline Error getErrorNumber(void)
00479 {return errid;};
00480
00486 inline char *getErrorString(void)
00487 {return errstr;};
00488
00489 bool operator!(void);
00490 };
00491
00511 class CCXX_CLASS_EXPORT ThreadFile : public RandomFile
00512 {
00513 private:
00514 ThreadKey state;
00515 fcb_t *first;
00516 fcb_t *getFCB(void);
00517 Error open(const char *path);
00518
00519 public:
00526 ThreadFile(const char *path);
00527
00531 virtual ~ThreadFile();
00532
00538 Error restart(void)
00539 {return open(pathname);};
00540
00550 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00551
00561 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00562
00568 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00569
00575 off_t getPosition(void);
00576
00577 bool operator++(void);
00578 bool operator--(void);
00579 };
00580
00595 class CCXX_CLASS_EXPORT SharedFile : public RandomFile
00596 {
00597 private:
00598 fcb_t fcb;
00599 Error open(const char *path);
00600
00601 public:
00608 SharedFile(const char *path);
00609
00616 SharedFile(const SharedFile &file);
00617
00621 virtual ~SharedFile();
00622
00628 Error restart(void)
00629 {return open(pathname);};
00630
00641 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00642
00653 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
00654
00663 Error clear(ccxx_size_t length = 0, off_t pos = -1);
00664
00671 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
00672
00678 off_t getPosition(void);
00679
00680 bool operator++(void);
00681 bool operator--(void);
00682 };
00683
00684 #ifndef WIN32
00685
00696 class CCXX_CLASS_EXPORT MappedFile : public RandomFile
00697 {
00698 private:
00699 fcb_t fcb;
00700 int prot;
00701
00702 public:
00710 MappedFile(const char *fname, Access mode);
00711
00722 MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
00723
00728 virtual ~MappedFile();
00729
00730
00736 inline void sync(void)
00737 {msync(fcb.address, fcb.len, MS_SYNC);};
00738
00745 void sync(caddr_t address, size_t len);
00746
00755 void update(size_t offset = 0, size_t len = 0);
00756
00764 void update(caddr_t address, size_t len);
00765
00772 void release(caddr_t address, size_t len);
00773
00782 inline caddr_t fetch(size_t offset = 0)
00783 {return ((char *)(fcb.address)) + offset;};
00784
00793 caddr_t fetch(off_t pos, size_t len);
00794 };
00795 #endif // !WIN32
00796
00813 class CCXX_CLASS_EXPORT Pipe
00814 {
00815 protected:
00816 #ifndef WIN32
00817 int fd[2];
00818 #else
00819 HANDLE fd[2];
00820 #endif
00821 int objcount;
00822 int objsize;
00823
00829 inline int getSize(void)
00830 {return objsize;};
00831
00840 inline void endSender(void)
00841 #ifndef WIN32
00842 {close(fd[1]);};
00843 #else
00844 {CloseHandle(fd[1]); fd[1] = NULL; };
00845 #endif
00846
00854 inline void endReceiver(void)
00855 #ifndef WIN32
00856 {close(fd[0]);};
00857 #else
00858 {CloseHandle(fd[0]); fd[0] = NULL; };
00859 #endif
00860
00871 Pipe(int size = 512, int count = 1);
00872
00876 virtual ~Pipe();
00877
00883 Pipe(const Pipe &orig);
00884
00885 Pipe &operator=(const Pipe &orig);
00886
00887
00888 void sender(void)
00889 {endReceiver();};
00890
00891 void receiver(void)
00892 {endSender();};
00893
00894 int read(void *buf, int len)
00895 #ifndef WIN32
00896 {return ::read(fd[0], (char *)buf, len);};
00897 #else
00898 {DWORD dwLen; return ReadFile(fd[0],buf,len,&dwLen,NULL) ? dwLen : (DWORD)-1; };
00899 #endif
00900
00901 int write(void *buf, int len)
00902 #ifndef WIN32
00903 {return ::write(fd[1], (char *)buf, len);};
00904 #else
00905 {DWORD dwLen; return WriteFile(fd[1],buf,len,&dwLen,NULL) ? dwLen : (DWORD)-1; };
00906 #endif
00907
00908 public:
00912 bool operator!();
00913
00920 #ifndef WIN32
00921 inline int receive(void *addr)
00922 {return ::read(fd[0], (char *)addr, objsize);};
00923 #else
00924 int receive(void *addr);
00925 #endif
00926
00933 #ifndef WIN32
00934 inline int send(void *addr)
00935 {return ::write(fd[1], (char *)addr, objsize);};
00936 #else
00937 int send(void *addr);
00938 #endif
00939
00945 bool isValid(void);
00946 };
00947
00956 class DSO
00957 {
00958 private:
00959 #ifdef HAVE_MODULES
00960 static Mutex mutex;
00961 static DSO *first, *last;
00962 DSO *next, *prev;
00963 #if defined(HAVE_MACH_DYLD)
00964 NSModule oModule;
00965 char *err;
00966 #elif defined(HAVE_SHL_LOAD)
00967 char *err;
00968 shl_t image;
00969 #elif defined(WIN32)
00970 char *err;
00971 HINSTANCE hImage;
00972 #else
00973 void *image;
00974 #endif
00975 CCXX_MEMBER_EXPORT(void) loader(const char *filename, bool resolve);
00976 #endif
00977
00978 public:
00984 #ifdef HAVE_MODULES
00985 DSO(const char *filename)
00986 {loader(filename, true);};
00987
00988 DSO(const char *filename, bool resolve)
00989 {loader(filename, resolve);};
00990 #else
00991 DSO(const char *filename)
00992 {throw this;};
00993 DSO(const char *filename, bool resolve)
00994 {throw this;};
00995 #endif
00996
01001 char *getError(void);
01002
01006 #ifdef HAVE_MODULES
01007 CCXX_MEMBER_EXPORT(virtual) ~DSO();
01008 #endif
01009
01013 #ifdef HAVE_MODULES
01014 CCXX_MEMBER_EXPORT(void*) operator[](const char *sym);
01015 #else
01016 void *operator[](const char *)
01017 {return NULL;};
01018 #endif
01019
01020 #ifdef HAVE_MODULES
01021 static CCXX_EXPORT(void) dynunload(void);
01022 #else
01023 static void dynunload(void)
01024 {return;};
01025 #endif
01026
01032 CCXX_MEMBER_EXPORT(bool) isValid(void);
01033 };
01034
01036 bool isDir(const char *path);
01038 bool isFile(const char *path);
01039 #ifndef WIN32
01040
01041 bool isDevice(const char *path);
01042 #else
01043
01044 inline bool isDevice(const char *path)
01045 { return false; }
01046 #endif
01047
01048 bool canAccess(const char *path);
01050 bool canModify(const char *path);
01051
01052 #ifdef COMMON_STD_EXCEPTION
01053
01054 class DirException : public IOException
01055 {
01056 public:
01057 DirException(std::string str) : IOException(str) {};
01058 };
01059
01060 class DSOException : public IOException
01061 {
01062 public:
01063 DSOException(std::string str) : IOException(str) {};
01064 };
01065
01066 class FIFOException : public IOException
01067 {
01068 public:
01069 FIFOException(std::string str) : IOException(str) {};
01070 };
01071
01072 class PipeException : public IOException
01073 {
01074 public:
01075 PipeException(std::string str) : IOException(str) {};
01076 };
01077
01078 class FileException : public IOException
01079 {
01080 public:
01081 FileException(std::string str) : IOException(str) {};
01082 };
01083
01084 #endif
01085
01086 #ifdef CCXX_NAMESPACES
01087 };
01088 #endif
01089
01090 #endif
01091