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_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_SHL_LOAD
00080
#include <dl.h>
00081
#endif
00082
00083
#ifdef HAVE_MACH_DYLD
00084
#include <mach-o/dyld.h>
00085
#endif
00086
00087
#ifdef CCXX_NAMESPACES
00088
namespace ost {
00089
#endif
00090
00091 typedef unsigned long pos_t;
00092
#ifndef WIN32
00093
00094
00095
#undef caddr_t
00096 #define caddr_t char *
00097 typedef size_t
ccxx_size_t;
00098
#else
00099
#ifndef __BORLANDC__
00100
typedef LONG off_t;
00101
#endif
00102
typedef void*
caddr_t;
00103
typedef DWORD
ccxx_size_t;
00104
#endif
00105
00106
#ifndef PATH_MAX
00107 #define PATH_MAX 256
00108
#endif
00109
00110
#ifndef NAME_MAX
00111 #define NAME_MAX 64
00112
#endif
00113
00114 class __EXPORT File
00115 {
00116
public:
00117 enum Error
00118 {
00119 errSuccess = 0,
00120 errNotOpened,
00121 errMapFailed,
00122 errInitFailed,
00123 errOpenDenied,
00124 errOpenFailed,
00125 errOpenInUse,
00126 errReadInterrupted,
00127 errReadIncomplete,
00128 errReadFailure,
00129 errWriteInterrupted,
00130 errWriteIncomplete,
00131 errWriteFailure,
00132 errExtended
00133 };
00134 typedef enum Error Error;
00135
00136 enum Access
00137 {
00138
#ifndef WIN32
00139
accessReadOnly = O_RDONLY,
00140 accessWriteOnly= O_WRONLY,
00141 accessReadWrite = O_RDWR
00142
#else
00143
accessReadOnly = GENERIC_READ,
00144 accessWriteOnly = GENERIC_WRITE,
00145 accessReadWrite = GENERIC_READ | GENERIC_WRITE
00146
#endif
00147
};
00148 typedef enum Access Access;
00149
00150
protected:
00151 typedef struct _fcb
00152 {
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 {
00164 openReadOnly,
00165 openWriteOnly,
00166 openReadWrite,
00167 openAppend,
00168 openTruncate
00169 };
00170
#else
00171 enum Open
00172 {
00173 openReadOnly = O_RDONLY,
00174 openWriteOnly = O_WRONLY,
00175 openReadWrite = O_RDWR,
00176 openAppend = O_WRONLY | O_APPEND,
00177
#ifdef O_SYNC
00178
openSync = O_RDWR | O_SYNC,
00179
#else
00180
openSync = O_RDWR,
00181
#endif
00182
openTruncate = O_RDWR | O_TRUNC
00183 };
00184 typedef enum Open Open;
00185
00186
00187
00188
#ifndef S_IRUSR
00189 #define S_IRUSR 0400
00190 #define S_IWUSR 0200
00191 #define S_IRGRP 0040
00192 #define S_IWGRP 0020
00193 #define S_IROTH 0004
00194 #define S_IWOTH 0002
00195
#endif
00196
00197
#endif // !WIN32
00198
00199
#ifndef WIN32
00200 enum Attr
00201 {
00202 attrInvalid = 0,
00203 attrPrivate =
S_IRUSR |
S_IWUSR,
00204 attrGroup = attrPrivate |
S_IRGRP |
S_IWGRP,
00205 attrPublic = attrGroup |
S_IROTH |
S_IWOTH
00206 };
00207
#else // defined WIN32
00208
enum Attr {
00209 attrInvalid=0,
00210 attrPrivate,
00211 attrGroup,
00212 attrPublic
00213 };
00214
#endif // !WIN32
00215 typedef enum Attr Attr;
00216
00217
#ifdef WIN32
00218
enum Complete
00219 {
00220 completionImmediate,
00221 completionDelayed,
00222 completionDeferred
00223 };
00224
00225
enum Mapping
00226 {
00227 mappedRead,
00228 mappedWrite,
00229 mappedReadWrite
00230 };
00231
#else
00232 enum Mapping
00233 {
00234 mappedRead = accessReadOnly,
00235 mappedWrite = accessWriteOnly,
00236 mappedReadWrite = accessReadWrite
00237 };
00238 enum Complete
00239 {
00240 completionImmediate,
00241 completionDelayed,
00242 completionDeferred
00243 };
00244
#endif
00245 typedef enum Complete Complete;
00246 typedef enum Mapping Mapping;
00247
00248
public:
00249
static const char *getExtension(
const char *path);
00250
static const char *getFilename(
const char *path);
00251
static char *getFilename(
const char *path,
char *buffer, size_t size = NAME_MAX);
00252
static char *getDirname(
const char *path,
char *buffer, size_t size = PATH_MAX);
00253
static char *getRealpath(
const char *path,
char *buffer, size_t size = PATH_MAX);
00254 };
00255
00264 class __EXPORT Dir :
public File
00265 {
00266
private:
00267
#ifndef WIN32
00268
DIR *dir;
00269
#ifdef HAVE_READDIR_R
00270
struct dirent *save;
00271
char save_space[
sizeof(
struct dirent) +
PATH_MAX + 1];
00272
#endif
00273
struct dirent *entry;
00274
#else
00275
HANDLE hDir;
00276 WIN32_FIND_DATA data, fdata;
00277
char *name;
00278
#endif
00279
00280
public:
00281 Dir(
const char *name = NULL);
00282
00283
static bool create(
const char *path, Attr attr = attrGroup);
00284
static bool remove(
const char *path);
00285
static bool setPrefix(
const char *path);
00286
static bool getPrefix(
char *path, size_t size =
PATH_MAX);
00287
00288
void open(
const char *name);
00289
void close(
void);
00290
00291
virtual ~Dir();
00292
00293
const char *getName(
void);
00294
00295 const char *operator++()
00296 {
return getName();};
00297
00298 const char *operator++(
int)
00299 {
return getName();};
00300
00301
const char *operator*();
00302
00303
bool rewind(
void);
00304
00305 bool operator!()
00306 #ifndef WIN32
00307 {
return !dir;};
00308
#else
00309
{
return hDir !=
INVALID_HANDLE_VALUE;};
00310
#endif
00311
00312
bool isValid(
void);
00313 };
00314
00321 class __EXPORT DirTree
00322 {
00323
private:
00324
char path[
PATH_MAX + 1];
00325 Dir *dir;
00326
unsigned max, current, prefixpos;
00327
00328
protected:
00338
virtual bool filter(
const char *path,
struct stat *ino);
00339
00340
public:
00348 DirTree(
const char *prefix,
unsigned depth);
00349
00355 DirTree(
unsigned depth);
00356
00357
virtual ~DirTree();
00358
00364
void open(
const char *prefix);
00365
00369
void close(
void);
00370
00378
char *getPath(
void);
00379
00389
unsigned perform(
const char *prefix);
00390 };
00391
00402 class __EXPORT RandomFile :
protected Mutex,
public File
00403 {
00404
private:
00405 Error errid;
00406
char *errstr;
00407
00408
protected:
00409
#ifndef WIN32
00410 int fd;
00411
00412 Access access;
00413
#else
00414
HANDLE fd;
00415
#endif
00416 char *pathname;
00417
00418
struct
00419
{
00420 unsigned count : 16;
00421 bool thrown : 1;
00422 bool initial : 1;
00423
#ifndef WIN32
00424 bool immediate : 1;
00425
#endif
00426 bool temp : 1;
00427 } flags;
00428
00432 RandomFile(
const char *name = NULL);
00433
00437 RandomFile(
const RandomFile &rf);
00438
00446 Error error(Error errid,
char *errstr = NULL);
00447
00454 inline Error error(
char *err)
00455 {
return error(errExtended, err);};
00456
00463 inline void setError(
bool enable)
00464 {flags.thrown = !enable;};
00465
00466
#ifndef WIN32
00467
00474 Error setCompletion(Complete mode);
00475
#endif
00476
00483 inline void setTemporary(
bool enable)
00484 {flags.temp = enable;};
00485
00497 virtual Attr initialize(
void)
00498 {
return attrPublic;};
00499
00503
void final(
void);
00504
00505
public:
00509 virtual ~RandomFile()
00510 {
final();};
00511
00520
bool initial(
void);
00521
00527 off_t getCapacity(
void);
00528
00534 virtual Error restart(
void)
00535 {
return errOpenFailed;};
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 {
return open(pathname);};
00604
00614 Error fetch(caddr_t address = NULL,
ccxx_size_t length = 0, off_t position = -1);
00615
00625 Error update(caddr_t address = NULL,
ccxx_size_t length = 0, off_t position = -1);
00626
00632 Error append(caddr_t address = NULL,
ccxx_size_t length = 0);
00633
00639 off_t getPosition(
void);
00640
00641
bool operator++(
void);
00642
bool operator--(
void);
00643 };
00644
00659 class __EXPORT SharedFile :
public RandomFile
00660 {
00661
private:
00662 fcb_t fcb;
00663 Error open(
const char *path);
00664
00665
public:
00672 SharedFile(
const char *path);
00673
00680 SharedFile(
const SharedFile &file);
00681
00685
virtual ~SharedFile();
00686
00692 Error restart(
void)
00693 {
return open(pathname);};
00694
00705 Error fetch(caddr_t address = NULL,
ccxx_size_t length = 0, off_t position = -1);
00706
00717 Error update(caddr_t address = NULL,
ccxx_size_t length = 0, off_t position = -1);
00718
00727 Error clear(
ccxx_size_t length = 0, off_t pos = -1);
00728
00735 Error append(caddr_t address = NULL,
ccxx_size_t length = 0);
00736
00742 off_t getPosition(
void);
00743
00744
bool operator++(
void);
00745
bool operator--(
void);
00746 };
00747
00759 class __EXPORT MappedFile :
public RandomFile
00760 {
00761
private:
00762 fcb_t fcb;
00763
int prot;
00764
#ifdef WIN32
00765
HANDLE map;
00766
char mapname[64];
00767
#endif
00768
00769
public:
00777 MappedFile(
const char *fname, Access mode);
00778
00787 MappedFile(
const char *fname, Access mode, size_t size);
00788
00799 MappedFile(
const char *fname,
pos_t offset, size_t size, Access mode);
00800
00805
virtual ~MappedFile();
00806
00807
00813
void sync(
void);
00814
00821
void sync(
caddr_t address, size_t len);
00822
00831
void update(size_t offset = 0, size_t len = 0);
00832
00840
void update(
caddr_t address, size_t len);
00841
00848
void release(
caddr_t address, size_t len);
00849
00858 inline caddr_t fetch(size_t offset = 0)
00859 {
return ((
char *)(fcb.address)) + offset;};
00860
00869
caddr_t fetch(off_t pos, size_t len);
00870
00876
bool lock(
void);
00877
00881
void unlock(
void);
00882
00889 size_t pageAligned(size_t size);
00890 };
00891
00892
00901 class __EXPORT DSO
00902 {
00903
private:
00904
const char *err;
00905
#ifdef HAVE_MODULES
00906
static Mutex mutex;
00907
static DSO *first;
00908
static DSO *last;
00909 DSO *next, *prev;
00910
const char *
id;
00911
#if defined(HAVE_MACH_DYLD)
00912
NSModule oModule;
00913
#elif defined(HAVE_SHL_LOAD)
00914
shl_t image;
00915
#elif defined(WIN32)
00916
HINSTANCE hImage;
00917
#else
00918
void *image;
00919
#endif
00920
void loader(
const char *filename,
bool resolve);
00921
#endif
00922
00923
public:
00929
#ifdef HAVE_MODULES
00930 DSO(
const char *filename)
00931 {loader(filename,
true);};
00932
00933 DSO(
const char *filename,
bool resolve)
00934 {loader(filename, resolve);};
00935
#else
00936
DSO(
const char *filename)
00937 {
throw this;};
00938 DSO(
const char *filename,
bool resolve)
00939 {
throw this;};
00940
#endif
00941
00946 inline const char *getError(
void)
00947 {
return err;};
00948
00952
#ifdef HAVE_MODULES
00953
virtual ~DSO();
00954
#endif
00955
00959
#ifdef HAVE_MODULES
00960
void* operator[](
const char *sym);
00961
#else
00962
void *operator[](
const char *)
00963 {
return NULL;};
00964
#endif
00965
00966
#ifdef HAVE_MODULES
00967
static void dynunload(
void);
00968
#else
00969
static void dynunload(
void)
00970 {
return;};
00971
#endif
00972
00978
static DSO *getObject(
const char *name);
00979
00985
bool isValid(
void);
00986 };
00987
00989
bool __EXPORT isDir(
const char *path);
00991
bool __EXPORT isFile(
const char *path);
00992
#ifndef WIN32
00993
00994
bool __EXPORT isDevice(
const char *path);
00995
#else
00996
00997
inline bool isDevice(
const char *path)
00998 {
return false; }
00999
#endif
01000
01001
bool __EXPORT canAccess(
const char *path);
01003
bool __EXPORT canModify(
const char *path);
01005 time_t __EXPORT lastModified(
const char *path);
01007 time_t __EXPORT lastAccessed(
const char *path);
01008
01009
#ifdef COMMON_STD_EXCEPTION
01010
01011
class DirException :
public IOException
01012 {
01013
public:
01014 DirException(
const String &str) : IOException(str) {};
01015 };
01016
01017
class __EXPORT DSOException :
public IOException
01018 {
01019
public:
01020 DSOException(
const String &str) : IOException(str) {};
01021 };
01022
01023
class __EXPORT FileException :
public IOException
01024 {
01025
public:
01026 FileException(
const String &str) : IOException(str) {};
01027 };
01028
01029
#endif
01030
01031
#ifdef CCXX_NAMESPACES
01032
}
01033
#endif
01034
01035
#endif
01036