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 00287 ssize_t read(void *buffer, size_t count); 00288 00295 ssize_t write(const void *buffer, size_t count); 00296 00302 int stat(struct stat *buffer); 00303 00304 inline int fileinfo(fileinfo_t *buffer) 00305 {return stat(buffer);}; 00306 00313 int trunc(offset_t offset); 00314 00319 int sync(void); 00320 00326 static int changeDir(const char *path); 00327 00334 static int getPrefix(char *path, size_t size); 00335 00342 static int stat(const char *path, struct stat *buffer); 00343 00344 static inline int fileinfo(const char *path, fileinfo_t *buffer) 00345 {return stat(path, buffer);}; 00346 00352 static int remove(const char *path); 00353 00360 static int rename(const char *oldpath, const char *newpath); 00361 00368 static int change(const char *path, unsigned mode); 00369 00376 static int access(const char *path, unsigned mode); 00377 00383 static bool isfile(const char *path); 00384 00390 static bool isdir(const char *path); 00391 00397 static bool islink(const char *path); 00398 00404 static bool ishidden(const char *path); 00405 00413 inline static ssize_t read(fsys& descriptor, void *buffer, size_t count) 00414 {return descriptor.read(buffer, count);}; 00415 00423 inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count) 00424 {return descriptor.write(buffer, count);}; 00425 00432 inline static int seek(fsys& descriptor, offset_t offset) 00433 {return descriptor.seek(offset);}; 00434 00441 inline static int drop(fsys& descriptor, offset_t size) 00442 {return descriptor.drop(size);}; 00443 00449 void open(const char *path, access_t access); 00450 00455 inline void assign(fd_t descriptor) 00456 {close(); fd = descriptor;}; 00457 00463 inline static void assign(fsys& object, fd_t descriptor) 00464 {object.close(); object.fd = descriptor;}; 00465 00472 void create(const char *path, access_t access, unsigned mode); 00473 00480 static int createDir(const char *path, unsigned mode); 00481 00487 static int removeDir(const char *path); 00488 00496 static int unlink(const char *path); 00497 00504 static int link(const char *path, const char *target); 00505 00512 static int hardlink(const char *path, const char *target); 00513 00520 static int linkinfo(const char *path, char *buffer, size_t size); 00521 00526 inline static void close(fsys& descriptor) 00527 {descriptor.close();}; 00528 00532 void close(void); 00533 00538 inline int err(void) const 00539 {return error;} 00540 00547 inline static void open(fsys& object, const char *path, access_t access) 00548 {object.open(path, access);}; 00549 00557 inline static void create(fsys& object, const char *path, access_t access, unsigned mode) 00558 {object.create(path, access, mode);}; 00559 00565 static int load(const char *path); 00566 00572 static void load(fsys& module, const char *path); 00573 00578 static void unload(fsys& module); 00579 00586 static void *find(fsys& module, const char *symbol); 00587 00588 static inline bool isfile(struct stat *inode) 00589 {return S_ISREG(inode->st_mode);} 00590 00591 static inline bool isdir(struct stat *inode) 00592 {return S_ISDIR(inode->st_mode);} 00593 00594 static inline bool islink(struct stat *inode) 00595 {return S_ISLNK(inode->st_mode);} 00596 00597 static inline bool isdev(struct stat *inode) 00598 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);} 00599 00600 static inline bool isdisk(struct stat *inode) 00601 {return S_ISBLK(inode->st_mode);} 00602 00603 static inline bool issys(struct stat *inode) 00604 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);} 00605 }; 00606 00612 class __EXPORT charfile : public CharacterProtocol 00613 { 00614 private: 00615 FILE *fp; 00616 bool opened; 00617 00618 int _putch(int code); 00619 00620 int _getch(void); 00621 00622 public: 00627 inline charfile(FILE *file) 00628 {fp = file; opened = false;} 00629 00635 charfile(const char *path, const char *mode); 00636 00640 charfile(); 00641 00645 ~charfile(); 00646 00651 inline operator bool() 00652 {return fp != NULL;} 00653 00658 inline bool operator !() 00659 {return fp == NULL;} 00660 00666 void open(const char *path, const char *mode); 00667 00671 void close(void); 00672 00678 size_t put(const char *string); 00679 00689 size_t readline(char *string, size_t size); 00690 00699 size_t readline(string& string); 00700 00701 inline size_t put(const void *data, size_t size) 00702 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);} 00703 00704 size_t get(void *data, size_t size) 00705 { return fp == NULL ? 0 : fread(data, 1, size, fp);} 00706 00707 inline void get(fpos_t& pos) 00708 { if(fp) fsetpos(fp, &pos);} 00709 00710 inline void set(fpos_t& pos) 00711 { if(fp) fgetpos(fp, &pos);} 00712 00713 int err(void); 00714 00715 bool eof(void); 00716 00717 inline void seek(long offset) 00718 {if(fp) fseek(fp, offset, SEEK_SET);} 00719 00720 inline void move(long offset) 00721 {if(fp) fseek(fp, offset, SEEK_CUR);} 00722 00723 inline void append(void) 00724 {if (fp) fseek(fp, 0l, SEEK_END);} 00725 00726 inline void rewind(void) 00727 {if(fp) ::rewind(fp);} 00728 00729 size_t printf(const char *format, ...) __PRINTF(2, 3); 00730 }; 00731 00732 String str(charfile& fp, strsize_t size); 00733 00737 typedef fsys fsys_t; 00738 00739 END_NAMESPACE 00740 00741 #endif 00742