00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00027 #ifndef _UCOMMON_FILE_H_
00028 #define _UCOMMON_FILE_H_
00029
00030 #ifndef _UCOMMON_CONFIG_H_
00031 #include <ucommon/platform.h>
00032 #endif
00033
00034 #ifndef _UCOMMON_PROTOCOLS_H_
00035 #include <ucommon/protocols.h>
00036 #endif
00037
00038 #ifndef _UCOMMON_THREAD_H_
00039 #include <ucommon/thread.h>
00040 #endif
00041
00042 #ifndef _UCOMMON_STRING_H_
00043 #include <ucommon/string.h>
00044 #endif
00045
00046 #ifndef _MSWINDOWS_
00047 #include <sys/stat.h>
00048 #else
00049 #include <io.h>
00050 #ifndef R_OK
00051 #define F_OK 0
00052 #define X_OK 1
00053 #define W_OK 2
00054 #define R_OK 4
00055 #endif
00056 #endif
00057
00058 #include <errno.h>
00059 #include <stdio.h>
00060
00061 #ifndef __S_ISTYPE
00062 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
00063 #endif
00064
00065 #if !defined(S_ISDIR) && defined(S_IFDIR)
00066 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
00067 #endif
00068
00069 #if !defined(S_ISCHR) && defined(S_IFCHR)
00070 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
00071 #elif !defined(S_ISCHR)
00072 #define S_ISCHR(mode) 0
00073 #endif
00074
00075 #if !defined(S_ISBLK) && defined(S_IFBLK)
00076 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
00077 #elif !defined(S_ISBLK)
00078 #define S_ISBLK(mode) 0
00079 #endif
00080
00081 #if !defined(S_ISREG) && defined(S_IFREG)
00082 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
00083 #elif !defined(S_ISREG)
00084 #define S_ISREG(mode) 1
00085 #endif
00086
00087 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
00088 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
00089 #elif !defined(S_ISSOCK)
00090 #define S_ISSOCK(mode) (0)
00091 #endif
00092
00093 #if !defined(S_ISFIFO) && defined(S_IFIFO)
00094 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
00095 #elif !defined(S_ISFIFO)
00096 #define S_ISFIFO(mode) (0)
00097 #endif
00098
00099 #if !defined(S_ISLNK) && defined(S_IFLNK)
00100 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
00101 #elif !defined(S_ISLNK)
00102 #define S_ISLNK(mode) (0)
00103 #endif
00104
00105 NAMESPACE_UCOMMON
00106
00110 typedef void *dir_t;
00111
00115 typedef void *mem_t;
00116
00125 class __EXPORT fsys
00126 {
00127 protected:
00128 fd_t fd;
00129 #ifdef _MSWINDOWS_
00130 WIN32_FIND_DATA *ptr;
00131 HINSTANCE mem;
00132 #else
00133 void *ptr;
00134 #endif
00135 int error;
00136
00137 public:
00138 typedef struct stat fileinfo_t;
00139
00140 #ifdef _MSWINDOWS_
00141 static int remapError(void);
00142 #else
00143 inline static int remapError(void)
00144 {return errno;};
00145 #endif
00146
00150 typedef enum {
00151 ACCESS_RDONLY,
00152 ACCESS_WRONLY,
00153 ACCESS_REWRITE,
00154 ACCESS_RDWR = ACCESS_REWRITE,
00155 ACCESS_APPEND,
00156 ACCESS_SHARED,
00157 ACCESS_DIRECTORY,
00158 ACCESS_STREAM,
00159 ACCESS_RANDOM
00160 } access_t;
00161
00165 typedef long offset_t;
00166
00170 static const offset_t end;
00171
00175 fsys();
00176
00180 fsys(fd_t handle);
00181
00186 fsys(const fsys& descriptor);
00187
00193 fsys(const char *path, access_t access);
00194
00201 fsys(const char *path, access_t access, unsigned permission);
00202
00206 ~fsys();
00207
00212 inline fd_t operator*() const
00213 {return fd;};
00214
00219 inline operator fd_t() const
00220 {return fd;};
00221
00226 inline operator bool() const
00227 {return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
00228
00233 inline bool operator!() const
00234 {return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
00235
00240 void operator=(const fsys& descriptor);
00241
00246 void operator=(fd_t descriptor);
00247
00252 inline fd_t getHandle(void) const
00253 {return fd;};
00254
00259 void set(fd_t descriptor);
00260
00265 fd_t release(void);
00266
00272 int seek(offset_t offset);
00273
00279 int drop(offset_t size = 0);
00280
00285 bool istty(void);
00286
00291 static bool istty(fd_t fd);
00292
00299 ssize_t read(void *buffer, size_t count);
00300
00307 ssize_t write(const void *buffer, size_t count);
00308
00314 int stat(struct stat *buffer);
00315
00316 inline int fileinfo(fileinfo_t *buffer)
00317 {return stat(buffer);};
00318
00325 int trunc(offset_t offset);
00326
00331 int sync(void);
00332
00338 static int changeDir(const char *path);
00339
00346 static int getPrefix(char *path, size_t size);
00347
00354 static int stat(const char *path, struct stat *buffer);
00355
00356 static inline int fileinfo(const char *path, fileinfo_t *buffer)
00357 {return stat(path, buffer);};
00358
00364 static int remove(const char *path);
00365
00373 static int copy(const char *source, const char *target, size_t size = 1024);
00374
00381 static int rename(const char *oldpath, const char *newpath);
00382
00389 static int change(const char *path, unsigned mode);
00390
00397 static int access(const char *path, unsigned mode);
00398
00404 static bool isfile(const char *path);
00405
00411 static bool isdir(const char *path);
00412
00418 static bool islink(const char *path);
00419
00425 static bool ishidden(const char *path);
00426
00434 inline static ssize_t read(fsys& descriptor, void *buffer, size_t count)
00435 {return descriptor.read(buffer, count);};
00436
00444 inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count)
00445 {return descriptor.write(buffer, count);};
00446
00453 inline static int seek(fsys& descriptor, offset_t offset)
00454 {return descriptor.seek(offset);};
00455
00462 inline static int drop(fsys& descriptor, offset_t size)
00463 {return descriptor.drop(size);};
00464
00470 void open(const char *path, access_t access);
00471
00476 inline void assign(fd_t descriptor)
00477 {close(); fd = descriptor;};
00478
00484 inline static void assign(fsys& object, fd_t descriptor)
00485 {object.close(); object.fd = descriptor;};
00486
00493 void create(const char *path, access_t access, unsigned mode);
00494
00501 static int createDir(const char *path, unsigned mode);
00502
00508 static int removeDir(const char *path);
00509
00517 static int unlink(const char *path);
00518
00525 static int link(const char *path, const char *target);
00526
00533 static int hardlink(const char *path, const char *target);
00534
00541 static int linkinfo(const char *path, char *buffer, size_t size);
00542
00547 inline static void close(fsys& descriptor)
00548 {descriptor.close();};
00549
00553 void close(void);
00554
00559 inline int err(void) const
00560 {return error;}
00561
00568 inline static void open(fsys& object, const char *path, access_t access)
00569 {object.open(path, access);};
00570
00578 inline static void create(fsys& object, const char *path, access_t access, unsigned mode)
00579 {object.create(path, access, mode);};
00580
00586 static int load(const char *path);
00587
00593 static void load(fsys& module, const char *path);
00594
00599 static void unload(fsys& module);
00600
00607 static void *find(fsys& module, const char *symbol);
00608
00609 static inline bool isfile(struct stat *inode)
00610 {return S_ISREG(inode->st_mode);}
00611
00612 static inline bool isdir(struct stat *inode)
00613 {return S_ISDIR(inode->st_mode);}
00614
00615 static inline bool islink(struct stat *inode)
00616 {return S_ISLNK(inode->st_mode);}
00617
00618 static inline bool isdev(struct stat *inode)
00619 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
00620
00621 static inline bool ischar(struct stat *inode)
00622 {return S_ISCHR(inode->st_mode);}
00623
00624 static inline bool isdisk(struct stat *inode)
00625 {return S_ISBLK(inode->st_mode);}
00626
00627 static inline bool issys(struct stat *inode)
00628 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
00629 };
00630
00636 class __EXPORT charfile : public CharacterProtocol
00637 {
00638 private:
00639 FILE *fp;
00640 bool opened;
00641
00642 int _putch(int code);
00643
00644 int _getch(void);
00645
00646 public:
00647 typedef ::fpos_t bookmark_t;
00648
00653 inline charfile(FILE *file)
00654 {fp = file; opened = false;}
00655
00661 charfile(const char *path, const char *mode);
00662
00666 charfile();
00667
00671 ~charfile();
00672
00677 inline operator bool()
00678 {return fp != NULL;}
00679
00684 inline bool operator !()
00685 {return fp == NULL;}
00686
00692 void open(const char *path, const char *mode);
00693
00697 void close(void);
00698
00704 size_t put(const char *string);
00705
00715 size_t readline(char *string, size_t size);
00716
00725 size_t readline(string& string);
00726
00727 inline size_t put(const void *data, size_t size)
00728 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
00729
00730 size_t get(void *data, size_t size)
00731 { return fp == NULL ? 0 : fread(data, 1, size, fp);}
00732
00733 inline void get(bookmark_t& pos)
00734 { if(fp) fsetpos(fp, &pos);}
00735
00736 inline void set(bookmark_t& pos)
00737 { if(fp) fgetpos(fp, &pos);}
00738
00739 int err(void);
00740
00741 bool eof(void);
00742
00743 inline void seek(long offset)
00744 {if(fp) fseek(fp, offset, SEEK_SET);}
00745
00746 inline void move(long offset)
00747 {if(fp) fseek(fp, offset, SEEK_CUR);}
00748
00749 inline void append(void)
00750 {if (fp) fseek(fp, 0l, SEEK_END);}
00751
00752 inline void rewind(void)
00753 {if(fp) ::rewind(fp);}
00754
00755 size_t printf(const char *format, ...) __PRINTF(2, 3);
00756
00757 bool istty(void);
00758 };
00759
00760 String str(charfile& fp, strsize_t size);
00761
00765 typedef fsys fsys_t;
00766
00767 extern charfile cstdin, cstdout, cstderr;
00768
00769 END_NAMESPACE
00770
00771 #endif
00772