UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 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 00570 static fd_t input(const char *path); 00571 00577 static fd_t output(const char *path); 00578 00584 static fd_t append(const char *path); 00585 00590 static void release(fd_t descriptor); 00591 00599 inline static void create(fsys& object, const char *path, access_t access, unsigned mode) 00600 {object.create(path, access, mode);}; 00601 00607 static int load(const char *path); 00608 00614 static void load(fsys& module, const char *path); 00615 00620 static void unload(fsys& module); 00621 00628 static void *find(fsys& module, const char *symbol); 00629 00630 static inline bool isfile(struct stat *inode) 00631 {return S_ISREG(inode->st_mode);} 00632 00633 static inline bool isdir(struct stat *inode) 00634 {return S_ISDIR(inode->st_mode);} 00635 00636 static inline bool islink(struct stat *inode) 00637 {return S_ISLNK(inode->st_mode);} 00638 00639 static inline bool isdev(struct stat *inode) 00640 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);} 00641 00642 static inline bool ischar(struct stat *inode) 00643 {return S_ISCHR(inode->st_mode);} 00644 00645 static inline bool isdisk(struct stat *inode) 00646 {return S_ISBLK(inode->st_mode);} 00647 00648 static inline bool issys(struct stat *inode) 00649 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);} 00650 }; 00651 00657 class __EXPORT charfile : public CharacterProtocol 00658 { 00659 private: 00660 FILE *fp; 00661 bool opened; 00662 00663 int _putch(int code); 00664 00665 int _getch(void); 00666 00667 public: 00668 typedef ::fpos_t bookmark_t; 00669 00674 inline charfile(FILE *file) 00675 {fp = file; opened = false;} 00676 00682 charfile(const char *path, const char *mode); 00683 00687 charfile(); 00688 00692 ~charfile(); 00693 00698 inline operator bool() 00699 {return fp != NULL;} 00700 00705 inline bool operator !() 00706 {return fp == NULL;} 00707 00713 void open(const char *path, const char *mode); 00714 00718 void close(void); 00719 00725 size_t put(const char *string); 00726 00736 size_t readline(char *string, size_t size); 00737 00746 size_t readline(string& string); 00747 00748 inline size_t put(const void *data, size_t size) 00749 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);} 00750 00751 size_t get(void *data, size_t size) 00752 { return fp == NULL ? 0 : fread(data, 1, size, fp);} 00753 00754 inline void get(bookmark_t& pos) 00755 { if(fp) fsetpos(fp, &pos);} 00756 00757 inline void set(bookmark_t& pos) 00758 { if(fp) fgetpos(fp, &pos);} 00759 00760 int err(void); 00761 00762 bool eof(void); 00763 00764 inline void seek(long offset) 00765 {if(fp) fseek(fp, offset, SEEK_SET);} 00766 00767 inline void move(long offset) 00768 {if(fp) fseek(fp, offset, SEEK_CUR);} 00769 00770 inline void append(void) 00771 {if (fp) fseek(fp, 0l, SEEK_END);} 00772 00773 inline void rewind(void) 00774 {if(fp) ::rewind(fp);} 00775 00776 size_t printf(const char *format, ...) __PRINTF(2, 3); 00777 00778 bool istty(void); 00779 }; 00780 00781 String str(charfile& fp, strsize_t size); 00782 00786 typedef fsys fsys_t; 00787 00788 extern charfile cstdin, cstdout, cstderr; 00789 00790 END_NAMESPACE 00791 00792 #endif 00793