44 #ifndef COMMONCPP_FILE_H_
45 #define COMMONCPP_FILE_H_
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
51 #ifndef COMMONCPP_THREAD_H_
55 #ifndef COMMONCPP_EXCEPTION_H_
62 # include <sys/types.h>
68 # include <sys/stat.h>
69 # include <sys/mman.h>
71 # if __BORLANDC__ >= 0x0560
73 # include <sys/stat.h>
81 typedef unsigned long pos_t;
86 #define caddr_t char *
87 typedef size_t ccxx_size_t;
89 typedef DWORD ccxx_size_t;
120 typedef enum Error Error;
124 accessReadOnly = O_RDONLY,
125 accessWriteOnly= O_WRONLY,
126 accessReadWrite = O_RDWR
128 accessReadOnly = GENERIC_READ,
129 accessWriteOnly = GENERIC_WRITE,
130 accessReadWrite = GENERIC_READ | GENERIC_WRITE
133 typedef enum Access Access;
136 typedef struct _fcb {
155 openReadOnly = O_RDONLY,
156 openWriteOnly = O_WRONLY,
157 openReadWrite = O_RDWR,
158 openAppend = O_WRONLY | O_APPEND,
160 openSync = O_RDWR | O_SYNC,
164 openTruncate = O_RDWR | O_TRUNC
166 typedef enum Open Open;
184 attrPrivate = S_IRUSR | S_IWUSR,
185 attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
186 attrPublic = attrGroup | S_IROTH | S_IWOTH
196 typedef enum Attr Attr;
212 mappedRead = accessReadOnly,
213 mappedWrite = accessWriteOnly,
214 mappedReadWrite = accessReadWrite
222 typedef enum Complete Complete;
223 typedef enum Mapping Mapping;
226 static const char *getExtension(
const char *path);
227 static const char *getFilename(
const char *path);
228 static char *getFilename(
const char *path,
char *buffer,
size_t size = NAME_MAX);
229 static char *getDirname(
const char *path,
char *buffer,
size_t size = PATH_MAX);
230 static char *getRealpath(
const char *path,
char *buffer,
size_t size = PATH_MAX);
241 class __EXPORT Dir :
public File
247 char save_space[
sizeof(
struct dirent) + PATH_MAX + 1];
248 struct dirent *entry;
251 WIN32_FIND_DATA data, fdata;
258 Dir(
const char *name = NULL);
260 static bool create(
const char *path, Attr attr = attrGroup);
261 static bool remove(
const char *path);
262 static bool setPrefix(
const char *path);
263 static bool getPrefix(
char *path,
size_t size = PATH_MAX);
265 void open(
const char *name);
270 const char *getName(
void);
272 const char *operator++() {
276 const char *operator++(
int) {
280 const char *operator*();
284 bool operator!()
const {
288 return hDir == INVALID_HANDLE_VALUE;
292 operator bool()
const {
296 return hDir != INVALID_HANDLE_VALUE;
300 bool isValid(
void)
const;
309 class __EXPORT DirTree
312 char path[PATH_MAX + 1];
314 unsigned max, current, prefixpos;
316 __DELETE_COPY(DirTree);
328 virtual bool filter(
const char *file,
struct stat *ino);
338 DirTree(
const char *prefix,
unsigned maxdepth);
345 DirTree(
unsigned maxdepth);
354 void open(
const char *prefix);
379 unsigned perform(
const char *prefix);
392 class __EXPORT RandomFile :
protected Mutex,
public File
421 RandomFile(
const char *name = NULL);
426 RandomFile(
const RandomFile &rf);
435 Error error(Error errid,
char *errstr = NULL);
443 inline Error error(
char *err) {
444 return error(errExtended, err);
453 inline void setError(
bool enable) {
454 flags.thrown = !enable;
465 Error setCompletion(Complete mode);
474 inline void setTemporary(
bool enable) {
489 virtual Attr initialize(
void);
500 virtual ~RandomFile();
517 off_t getCapacity(
void);
524 virtual Error restart(
void);
531 inline Error getErrorNumber(
void)
const {
540 inline char *getErrorString(
void)
const {
544 operator bool()
const;
546 bool operator!(
void)
const;
563 class __EXPORT SharedFile :
public RandomFile
567 Error open(
const char *path);
576 SharedFile(
const char *path);
584 SharedFile(
const SharedFile &file);
589 virtual ~SharedFile();
596 Error restart(
void) {
597 return open(pathname);
610 Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
622 Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
632 Error clear(ccxx_size_t length = 0, off_t pos = -1);
640 Error append(caddr_t address = NULL, ccxx_size_t length = 0);
647 off_t getPosition(
void);
649 bool operator++(
void);
650 bool operator--(
void);
663 class __EXPORT MappedFile :
public RandomFile
673 __DELETE_COPY(MappedFile);
683 MappedFile(
const char *fname, Access mode);
693 MappedFile(
const char *fname, Access mode,
size_t size);
705 MappedFile(
const char *fname, pos_t offset,
size_t size, Access mode);
711 virtual ~MappedFile();
727 void sync(caddr_t address,
size_t len);
737 void update(
size_t offset = 0,
size_t len = 0);
746 void update(caddr_t address,
size_t len);
754 void release(caddr_t address,
size_t len);
764 inline caddr_t fetch(
size_t offset = 0) {
765 return ((
char *)(fcb.address)) + offset;
776 caddr_t fetch(off_t pos,
size_t len);
796 size_t pageAligned(
size_t size);
819 typedef ucommon::dso::addr_t addr_t;
824 void loader(
const char *filename,
bool resolve);
832 DSO(
const char *filename) {
833 loader(filename,
true);
836 DSO(
const char *filename,
bool resolve) {
837 loader(filename, resolve);
844 inline const char *getError(
void)
const {
856 addr_t operator[](
const char *sym);
858 static void dynunload(
void);
865 static DSO *getObject(
const char *name);
877 static void setDebug(
void);
881 bool __EXPORT isDir(
const char *path);
883 bool __EXPORT isFile(
const char *path);
886 bool __EXPORT isDevice(
const char *path);
889 inline bool isDevice(
const char *path) {
894 bool __EXPORT canAccess(
const char *path);
896 bool __EXPORT canModify(
const char *path);
898 time_t __EXPORT lastModified(
const char *path);
900 time_t __EXPORT lastAccessed(
const char *path);
902 #ifdef COMMON_STD_EXCEPTION
904 class DirException :
public IOException
907 DirException(
const String &str) : IOException(str) {};
910 class __EXPORT DSOException :
public IOException
913 DSOException(
const String &str) : IOException(str) {};
916 class __EXPORT FileException :
public IOException
919 FileException(
const String &str) : IOException(str) {};
GNU Common C++ exception model base classes.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Common C++ thread class and sychronization objects.