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 #ifdef _MSWINDOWS_ 00139 static int remapError(void); 00140 #else 00141 inline static int remapError(void) 00142 {return errno;}; 00143 #endif 00144 00148 typedef enum { 00149 ACCESS_RDONLY, 00150 ACCESS_WRONLY, 00151 ACCESS_REWRITE, 00152 ACCESS_RDWR = ACCESS_REWRITE, 00153 ACCESS_APPEND, 00154 ACCESS_SHARED, 00155 ACCESS_DIRECTORY, 00156 ACCESS_STREAM, 00157 ACCESS_RANDOM 00158 } access_t; 00159 00163 typedef long offset_t; 00164 00168 static const offset_t end; 00169 00173 fsys(); 00174 00179 fsys(const fsys& descriptor); 00180 00186 fsys(const char *path, access_t access); 00187 00194 fsys(const char *path, access_t access, unsigned permission); 00195 00199 ~fsys(); 00200 00205 inline fd_t operator*() const 00206 {return fd;}; 00207 00212 inline operator fd_t() const 00213 {return fd;}; 00214 00219 inline operator bool() const 00220 {return fd != INVALID_HANDLE_VALUE || ptr != NULL;}; 00221 00226 inline bool operator!() const 00227 {return fd == INVALID_HANDLE_VALUE && ptr == NULL;}; 00228 00233 void operator=(const fsys& descriptor); 00234 00239 void operator=(fd_t descriptor); 00240 00245 inline fd_t getHandle(void) const 00246 {return fd;}; 00247 00253 int seek(offset_t offset); 00254 00260 int drop(offset_t size = 0); 00261 00268 ssize_t read(void *buffer, size_t count); 00269 00276 ssize_t write(const void *buffer, size_t count); 00277 00283 int stat(struct stat *buffer); 00284 00291 int trunc(offset_t offset); 00292 00297 int sync(void); 00298 00304 static int changeDir(const char *path); 00305 00312 static int getPrefix(char *path, size_t size); 00313 00320 static int stat(const char *path, struct stat *buffer); 00321 00327 static int remove(const char *path); 00328 00335 static int rename(const char *oldpath, const char *newpath); 00336 00343 static int change(const char *path, unsigned mode); 00344 00351 static int access(const char *path, unsigned mode); 00352 00358 static bool isfile(const char *path); 00359 00365 static bool isdir(const char *path); 00366 00367 00375 inline static ssize_t read(fsys& descriptor, void *buffer, size_t count) 00376 {return descriptor.read(buffer, count);}; 00377 00385 inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count) 00386 {return descriptor.write(buffer, count);}; 00387 00394 inline static int seek(fsys& descriptor, offset_t offset) 00395 {return descriptor.seek(offset);}; 00396 00403 inline static int drop(fsys& descriptor, offset_t size) 00404 {return descriptor.drop(size);}; 00405 00411 void open(const char *path, access_t access); 00412 00417 inline void assign(fd_t descriptor) 00418 {close(); fd = descriptor;}; 00419 00425 inline static void assign(fsys& object, fd_t descriptor) 00426 {object.close(); object.fd = descriptor;}; 00427 00434 void create(const char *path, access_t access, unsigned mode); 00435 00442 static int createDir(const char *path, unsigned mode); 00443 00449 static int removeDir(const char *path); 00450 00455 inline static void close(fsys& descriptor) 00456 {descriptor.close();}; 00457 00461 void close(void); 00462 00467 inline int err(void) const 00468 {return error;} 00469 00476 inline static void open(fsys& object, const char *path, access_t access) 00477 {object.open(path, access);}; 00478 00486 inline static void create(fsys& object, const char *path, access_t access, unsigned mode) 00487 {object.create(path, access, mode);}; 00488 00494 static int load(const char *path); 00495 00501 static void load(fsys& module, const char *path); 00502 00507 static void unload(fsys& module); 00508 00515 static void *find(fsys& module, const char *symbol); 00516 00517 static inline bool isfile(struct stat *inode) 00518 {return S_ISREG(inode->st_mode);} 00519 00520 static inline bool isdir(struct stat *inode) 00521 {return S_ISDIR(inode->st_mode);} 00522 00523 static inline bool islink(struct stat *inode) 00524 {return S_ISLNK(inode->st_mode);} 00525 00526 static inline bool isdev(struct stat *inode) 00527 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);} 00528 00529 static inline bool isdisk(struct stat *inode) 00530 {return S_ISBLK(inode->st_mode);} 00531 00532 static inline bool issys(struct stat *inode) 00533 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);} 00534 }; 00535 00541 class __EXPORT charfile : public CharacterProtocol 00542 { 00543 private: 00544 FILE *fp; 00545 bool opened; 00546 00547 int _putch(int code); 00548 00549 int _getch(void); 00550 00551 public: 00556 inline charfile(FILE *file) 00557 {fp = file; opened = false;} 00558 00564 charfile(const char *path, const char *mode); 00565 00569 charfile(); 00570 00574 ~charfile(); 00575 00580 inline operator bool() 00581 {return fp != NULL;} 00582 00587 inline bool operator !() 00588 {return fp == NULL;} 00589 00595 void open(const char *path, const char *mode); 00596 00600 void close(void); 00601 00607 size_t put(const char *string); 00608 00618 size_t readline(char *string, size_t size); 00619 00628 size_t readline(string& string); 00629 00630 inline size_t put(const void *data, size_t size) 00631 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);} 00632 00633 size_t get(void *data, size_t size) 00634 { return fp == NULL ? 0 : fread(data, 1, size, fp);} 00635 00636 inline void get(fpos_t& pos) 00637 { if(fp) fsetpos(fp, &pos);} 00638 00639 inline void set(fpos_t& pos) 00640 { if(fp) fgetpos(fp, &pos);} 00641 00642 int err(void); 00643 00644 bool eof(void); 00645 00646 inline void seek(long offset) 00647 {if(fp) fseek(fp, offset, SEEK_SET);} 00648 00649 inline void move(long offset) 00650 {if(fp) fseek(fp, offset, SEEK_CUR);} 00651 00652 inline void append(void) 00653 {if (fp) fseek(fp, 0l, SEEK_END);} 00654 00655 inline void rewind(void) 00656 {if(fp) ::rewind(fp);} 00657 00658 size_t printf(const char *format, ...) __PRINTF(2, 3); 00659 }; 00660 00661 String str(charfile& fp, strsize_t size); 00662 00666 typedef fsys fsys_t; 00667 00668 END_NAMESPACE 00669 00670 #endif 00671