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 #endif
00049
00050 #include <errno.h>
00051 #include <stdio.h>
00052
00053 #ifndef S_ISREG
00054
00055 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
00056
00057 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
00058 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
00059 #ifdef S_IFBLK
00060 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
00061 #else
00062 #define S_ISBLK(mode) 0
00063 #endif
00064 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
00065 #ifdef S_IFSOCK
00066 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
00067 #else
00068 #define S_ISSOCK(mode) 0
00069 #endif
00070 #ifdef S_IFIFO
00071 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
00072 #else
00073 #define S_ISFIFO(mode) 0
00074 #endif
00075 #ifdef S_IFLNK
00076 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
00077 #else
00078 #define S_ISLNK(mode) 0
00079 #endif
00080
00081 #endif
00082
00083 NAMESPACE_UCOMMON
00084
00088 typedef void *dir_t;
00089
00093 typedef void *mem_t;
00094
00103 class __EXPORT fsys
00104 {
00105 protected:
00106 fd_t fd;
00107 #ifdef _MSWINDOWS_
00108 WIN32_FIND_DATA *ptr;
00109 HINSTANCE mem;
00110 #else
00111 void *ptr;
00112 #endif
00113 int error;
00114
00115 public:
00116 #ifdef _MSWINDOWS_
00117 static int remapError(void);
00118 #else
00119 inline static int remapError(void)
00120 {return errno;};
00121 #endif
00122
00126 typedef enum {
00127 ACCESS_RDONLY,
00128 ACCESS_WRONLY,
00129 ACCESS_REWRITE,
00130 ACCESS_RDWR = ACCESS_REWRITE,
00131 ACCESS_APPEND,
00132 ACCESS_SHARED,
00133 ACCESS_DIRECTORY,
00134 ACCESS_STREAM,
00135 ACCESS_RANDOM
00136 } access_t;
00137
00141 typedef long offset_t;
00142
00146 static const offset_t end;
00147
00151 fsys();
00152
00157 fsys(const fsys& descriptor);
00158
00164 fsys(const char *path, access_t access);
00165
00172 fsys(const char *path, access_t access, unsigned permission);
00173
00177 ~fsys();
00178
00183 inline fd_t operator*() const
00184 {return fd;};
00185
00190 inline operator fd_t() const
00191 {return fd;};
00192
00197 inline operator bool() const
00198 {return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
00199
00204 inline bool operator!() const
00205 {return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
00206
00211 void operator=(const fsys& descriptor);
00212
00217 void operator=(fd_t descriptor);
00218
00223 inline fd_t getHandle(void) const
00224 {return fd;};
00225
00231 int seek(offset_t offset);
00232
00238 int drop(offset_t size = 0);
00239
00246 ssize_t read(void *buffer, size_t count);
00247
00254 ssize_t write(const void *buffer, size_t count);
00255
00261 int stat(struct stat *buffer);
00262
00269 int trunc(offset_t offset);
00270
00275 int sync(void);
00276
00282 static int changeDir(const char *path);
00283
00290 static int getPrefix(char *path, size_t size);
00291
00298 static int stat(const char *path, struct stat *buffer);
00299
00305 static int remove(const char *path);
00306
00313 static int rename(const char *oldpath, const char *newpath);
00314
00321 static int change(const char *path, unsigned mode);
00322
00329 static int access(const char *path, unsigned mode);
00330
00336 static bool isfile(const char *path);
00337
00343 static bool isdir(const char *path);
00344
00345
00353 inline static ssize_t read(fsys& descriptor, void *buffer, size_t count)
00354 {return descriptor.read(buffer, count);};
00355
00363 inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count)
00364 {return descriptor.write(buffer, count);};
00365
00372 inline static int seek(fsys& descriptor, offset_t offset)
00373 {return descriptor.seek(offset);};
00374
00381 inline static int drop(fsys& descriptor, offset_t size)
00382 {return descriptor.drop(size);};
00383
00389 void open(const char *path, access_t access);
00390
00395 inline void assign(fd_t descriptor)
00396 {close(); fd = descriptor;};
00397
00403 inline static void assign(fsys& object, fd_t descriptor)
00404 {object.close(); object.fd = descriptor;};
00405
00412 void create(const char *path, access_t access, unsigned mode);
00413
00420 static int createDir(const char *path, unsigned mode);
00421
00427 static int removeDir(const char *path);
00428
00433 inline static void close(fsys& descriptor)
00434 {descriptor.close();};
00435
00439 void close(void);
00440
00445 inline int err(void) const
00446 {return error;}
00447
00454 inline static void open(fsys& object, const char *path, access_t access)
00455 {object.open(path, access);};
00456
00464 inline static void create(fsys& object, const char *path, access_t access, unsigned mode)
00465 {object.create(path, access, mode);};
00466
00472 static int load(const char *path);
00473
00479 static void load(fsys& module, const char *path);
00480
00485 static void unload(fsys& module);
00486
00493 static void *find(fsys& module, const char *symbol);
00494
00495 static inline bool isfile(struct stat *inode)
00496 {return S_ISREG(inode->st_mode);}
00497
00498 static inline bool isdir(struct stat *inode)
00499 {return S_ISDIR(inode->st_mode);}
00500
00501 static inline bool islink(struct stat *inode)
00502 {return S_ISLNK(inode->st_mode);}
00503
00504 static inline bool isdev(struct stat *inode)
00505 {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
00506
00507 static inline bool isdisk(struct stat *inode)
00508 {return S_ISBLK(inode->st_mode);}
00509
00510 static inline bool issys(struct stat *inode)
00511 {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
00512 };
00513
00519 class __EXPORT charfile : public CharacterProtocol
00520 {
00521 private:
00522 FILE *fp;
00523 bool opened;
00524
00525 int _putch(int code);
00526
00527 int _getch(void);
00528
00529 public:
00534 inline charfile(FILE *file)
00535 {fp = file; opened = false;}
00536
00542 charfile(const char *path, const char *mode);
00543
00547 charfile();
00548
00552 ~charfile();
00553
00558 inline operator bool()
00559 {return fp != NULL;}
00560
00565 inline bool operator !()
00566 {return fp == NULL;}
00567
00573 void open(const char *path, const char *mode);
00574
00578 void close(void);
00579
00585 size_t put(const char *string);
00586
00596 size_t readline(char *string, size_t size);
00597
00606 size_t readline(string& string);
00607
00608 inline size_t put(const void *data, size_t size)
00609 { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
00610
00611 size_t get(void *data, size_t size)
00612 { return fp == NULL ? 0 : fread(data, 1, size, fp);}
00613
00614 inline void get(fpos_t& pos)
00615 { if(fp) fsetpos(fp, &pos);}
00616
00617 inline void set(fpos_t& pos)
00618 { if(fp) fgetpos(fp, &pos);}
00619
00620 int err(void);
00621
00622 bool eof(void);
00623
00624 inline void seek(long offset)
00625 {if(fp) fseek(fp, offset, SEEK_SET);}
00626
00627 inline void move(long offset)
00628 {if(fp) fseek(fp, offset, SEEK_CUR);}
00629
00630 inline void append(void)
00631 {if (fp) fseek(fp, 0l, SEEK_END);}
00632
00633 inline void rewind(void)
00634 {if(fp) ::rewind(fp);}
00635
00636 size_t printf(const char *format, ...) __PRINTF(2, 3);
00637 };
00638
00639 String str(charfile& fp, strsize_t size);
00640
00644 typedef fsys fsys_t;
00645
00646 END_NAMESPACE
00647
00648 #endif
00649