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 fileinfo(fileinfo_t *buffer);
00315
00322 int trunc(offset_t offset);
00323
00328 int sync(void);
00329
00335 static int changeDir(const char *path);
00336
00343 static int getPrefix(char *path, size_t size);
00344
00351 static int fileinfo(const char *path, fileinfo_t *buffer);
00352
00358 static int remove(const char *path);
00359
00367 static int copy(const char *source, const char *target, size_t size = 1024);
00368
00375 static int rename(const char *oldpath, const char *newpath);
00376
00383 static int change(const char *path, unsigned mode);
00384
00391 static int access(const char *path, unsigned mode);
00392
00398 static bool isfile(const char *path);
00399
00405 static bool isdir(const char *path);
00406
00412 static bool islink(const char *path);
00413
00419 static bool ishidden(const char *path);
00420
00428 inline static ssize_t read(fsys& descriptor, void *buffer, size_t count)
00429 {return descriptor.read(buffer, count);};
00430
00438 inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count)
00439 {return descriptor.write(buffer, count);};
00440
00447 inline static int seek(fsys& descriptor, offset_t offset)
00448 {return descriptor.seek(offset);};
00449
00456 inline static int drop(fsys& descriptor, offset_t size)
00457 {return descriptor.drop(size);};
00458
00464 void open(const char *path, access_t access);
00465
00470 inline void assign(fd_t descriptor)
00471 {close(); fd = descriptor;};
00472
00478 inline static void assign(fsys& object, fd_t descriptor)
00479 {object.close(); object.fd = descriptor;};
00480
00487 void create(const char *path, access_t access, unsigned mode);
00488
00495 static int createDir(const char *path, unsigned mode);
00496
00502 static int removeDir(const char *path);
00503
00511 static int unlink(const char *path);
00512
00519 static int link(const char *path, const char *target);
00520
00527 static int hardlink(const char *path, const char *target);
00528
00535 static int linkinfo(const char *path, char *buffer, size_t size);
00536
00541 inline static void close(fsys& descriptor)
00542 {descriptor.close();};
00543
00547 void close(void);
00548
00553 inline int err(void) const
00554 {return error;}
00555
00562 inline static void open(fsys& object, const char *path, access_t access)
00563 {object.open(path, access);};
00564
00572 inline static void create(fsys& object, const char *path, access_t access, unsigned mode)
00573 {object.create(path, access, mode);};
00574
00580 static int load(const char *path);
00581
00587 static void load(fsys& module, const char *path);
00588
00593 static void unload(fsys& module);
00594
00601 static void *find(fsys& module, const char *symbol);
00602
00603 static inline bool isfile(struct stat *inode)
00604 {return S_ISREG(inode->st_mode);}
00605
00606 static inline bool isdir(struct stat *inode)
00607 {return S_ISDIR(inode->st_mode);}
00608
00609 static inline bool islink(struct stat *inode)
00610 {return S_ISLNK(inode->st_mode);}
00611
00612 static inline bool isdev(struct stat *inode)
00613 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
00614
00615 static inline bool ischar(struct stat *inode)
00616 {return S_ISCHR(inode->st_mode);}
00617
00618 static inline bool isdisk(struct stat *inode)
00619 {return S_ISBLK(inode->st_mode);}
00620
00621 static inline bool issys(struct stat *inode)
00622 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
00623 };
00624
00630 class __EXPORT charfile : public CharacterProtocol
00631 {
00632 private:
00633 FILE *fp;
00634 bool opened;
00635
00636 int _putch(int code);
00637
00638 int _getch(void);
00639
00640 public:
00641 typedef ::fpos_t bookmark_t;
00642
00647 inline charfile(FILE *file)
00648 {fp = file; opened = false;}
00649
00655 charfile(const char *path, const char *mode);
00656
00660 charfile();
00661
00665 ~charfile();
00666
00671 inline operator bool()
00672 {return fp != NULL;}
00673
00678 inline bool operator !()
00679 {return fp == NULL;}
00680
00686 void open(const char *path, const char *mode);
00687
00691 void close(void);
00692
00698 size_t put(const char *string);
00699
00709 size_t readline(char *string, size_t size);
00710
00719 size_t readline(string& string);
00720
00721 inline size_t put(const void *data, size_t size)
00722 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
00723
00724 size_t get(void *data, size_t size)
00725 { return fp == NULL ? 0 : fread(data, 1, size, fp);}
00726
00727 inline void get(bookmark_t& pos)
00728 { if(fp) fsetpos(fp, &pos);}
00729
00730 inline void set(bookmark_t& pos)
00731 { if(fp) fgetpos(fp, &pos);}
00732
00733 int err(void);
00734
00735 bool eof(void);
00736
00737 inline void seek(long offset)
00738 {if(fp) fseek(fp, offset, SEEK_SET);}
00739
00740 inline void move(long offset)
00741 {if(fp) fseek(fp, offset, SEEK_CUR);}
00742
00743 inline void append(void)
00744 {if (fp) fseek(fp, 0l, SEEK_END);}
00745
00746 inline void rewind(void)
00747 {if(fp) ::rewind(fp);}
00748
00749 size_t printf(const char *format, ...) __PRINTF(2, 3);
00750
00751 bool istty(void);
00752 };
00753
00754 String str(charfile& fp, strsize_t size);
00755
00759 typedef fsys fsys_t;
00760
00761 extern charfile cstdin, cstdout, cstderr;
00762
00763 END_NAMESPACE
00764
00765 #endif
00766