UCommon
fsys.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
27 #ifndef _UCOMMON_FILE_H_
28 #define _UCOMMON_FILE_H_
29 
30 #ifndef _UCOMMON_CONFIG_H_
31 #include <ucommon/platform.h>
32 #endif
33 
34 #ifndef _UCOMMON_PROTOCOLS_H_
35 #include <ucommon/protocols.h>
36 #endif
37 
38 #ifndef _UCOMMON_THREAD_H_
39 #include <ucommon/thread.h>
40 #endif
41 
42 #ifndef _UCOMMON_STRING_H_
43 #include <ucommon/string.h>
44 #endif
45 
46 #ifndef _UCOMMON_MEMORY_H_
47 #include <ucommon/memory.h>
48 #endif
49 
50 #ifndef _MSWINDOWS_
51 #include <sys/stat.h>
52 #else
53 #include <io.h>
54 #ifndef R_OK
55 #define F_OK 0
56 #define X_OK 1
57 #define W_OK 2
58 #define R_OK 4
59 #endif
60 #endif
61 
62 #include <errno.h>
63 #include <stdio.h>
64 
65 #ifndef __S_ISTYPE
66 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
67 #endif
68 
69 #if !defined(S_ISDIR) && defined(S_IFDIR)
70 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
71 #endif
72 
73 #if !defined(S_ISCHR) && defined(S_IFCHR)
74 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
75 #elif !defined(S_ISCHR)
76 #define S_ISCHR(mode) 0
77 #endif
78 
79 #if !defined(S_ISBLK) && defined(S_IFBLK)
80 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
81 #elif !defined(S_ISBLK)
82 #define S_ISBLK(mode) 0
83 #endif
84 
85 #if !defined(S_ISREG) && defined(S_IFREG)
86 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
87 #elif !defined(S_ISREG)
88 #define S_ISREG(mode) 1
89 #endif
90 
91 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
92 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
93 #elif !defined(S_ISSOCK)
94 #define S_ISSOCK(mode) (0)
95 #endif
96 
97 #if !defined(S_ISFIFO) && defined(S_IFIFO)
98 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
99 #elif !defined(S_ISFIFO)
100 #define S_ISFIFO(mode) (0)
101 #endif
102 
103 #if !defined(S_ISLNK) && defined(S_IFLNK)
104 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
105 #elif !defined(S_ISLNK)
106 #define S_ISLNK(mode) (0)
107 #endif
108 
109 NAMESPACE_UCOMMON
110 
114 typedef void *dir_t;
115 
119 typedef void *mem_t;
120 
129 class __EXPORT fsys
130 {
131 protected:
132  fd_t fd;
133 #ifdef _MSWINDOWS_
134  WIN32_FIND_DATA *ptr;
135  HINSTANCE mem;
136 #else
137  void *ptr;
138 #endif
139  int error;
140 
141 public:
142  typedef struct stat fileinfo_t;
143 
144 #ifdef _MSWINDOWS_
145  static int remapError(void);
146 #else
147  inline static int remapError(void)
148  {return errno;};
149 #endif
150 
154  typedef enum {
155  ACCESS_RDONLY,
156  ACCESS_WRONLY,
157  ACCESS_REWRITE,
158  ACCESS_RDWR = ACCESS_REWRITE,
159  ACCESS_APPEND,
160  ACCESS_SHARED,
161  ACCESS_DIRECTORY,
162  ACCESS_STREAM,
163  ACCESS_RANDOM
164  } access_t;
165 
169  typedef long offset_t;
170 
174  static const offset_t end;
175 
179  fsys();
180 
184  fsys(fd_t handle);
185 
190  fsys(const fsys& descriptor);
191 
197  fsys(const char *path, access_t access);
198 
205  fsys(const char *path, access_t access, unsigned permission);
206 
210  ~fsys();
211 
216  inline fd_t operator*() const
217  {return fd;};
218 
223  inline operator fd_t() const
224  {return fd;};
225 
230  inline operator bool() const
231  {return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
232 
237  inline bool operator!() const
238  {return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
239 
244  void operator=(const fsys& descriptor);
245 
251  void operator*=(fd_t& descriptor);
252 
257  void operator=(fd_t descriptor);
258 
263  inline fd_t getHandle(void) const
264  {return fd;};
265 
270  void set(fd_t descriptor);
271 
276  fd_t release(void);
277 
283  int seek(offset_t offset);
284 
290  int drop(offset_t size = 0);
291 
296  bool istty(void);
297 
302  static bool istty(fd_t fd);
303 
310  ssize_t read(void *buffer, size_t count);
311 
318  ssize_t write(const void *buffer, size_t count);
319 
325  int fileinfo(fileinfo_t *buffer);
326 
333  int trunc(offset_t offset);
334 
339  int sync(void);
340 
346  static int changeDir(const char *path);
347 
354  static int getPrefix(char *path, size_t size);
355 
362  static int fileinfo(const char *path, fileinfo_t *buffer);
363 
369  static int remove(const char *path);
370 
378  static int copy(const char *source, const char *target, size_t size = 1024);
379 
386  static int rename(const char *oldpath, const char *newpath);
387 
394  static int change(const char *path, unsigned mode);
395 
402  static int access(const char *path, unsigned mode);
403 
409  static bool isfile(const char *path);
410 
416  static bool isdir(const char *path);
417 
423  static bool islink(const char *path);
424 
430  static bool ishidden(const char *path);
431 
439  inline static ssize_t read(fsys& descriptor, void *buffer, size_t count)
440  {return descriptor.read(buffer, count);};
441 
449  inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count)
450  {return descriptor.write(buffer, count);};
451 
458  inline static int seek(fsys& descriptor, offset_t offset)
459  {return descriptor.seek(offset);};
460 
467  inline static int drop(fsys& descriptor, offset_t size)
468  {return descriptor.drop(size);};
469 
475  void open(const char *path, access_t access);
476 
481  inline void assign(fd_t descriptor)
482  {close(); fd = descriptor;};
483 
489  inline static void assign(fsys& object, fd_t descriptor)
490  {object.close(); object.fd = descriptor;};
491 
498  void create(const char *path, access_t access, unsigned mode);
499 
506  static int createDir(const char *path, unsigned mode);
507 
513  static int removeDir(const char *path);
514 
522  static int unlink(const char *path);
523 
530  static int link(const char *path, const char *target);
531 
538  static int hardlink(const char *path, const char *target);
539 
546  static int linkinfo(const char *path, char *buffer, size_t size);
547 
552  inline static void close(fsys& descriptor)
553  {descriptor.close();};
554 
558  void close(void);
559 
564  inline int err(void) const
565  {return error;}
566 
573  inline static void open(fsys& object, const char *path, access_t access)
574  {object.open(path, access);};
575 
581  static fd_t input(const char *path);
582 
588  static fd_t output(const char *path);
589 
595  static fd_t append(const char *path);
596 
601  static void release(fd_t descriptor);
602 
610  static int pipe(fd_t& input, fd_t& output, size_t size = 0);
611 
620  static int inherit(fd_t& descriptor, bool enable);
621 
626  static fd_t nullfile(void);
627 
635  inline static void create(fsys& object, const char *path, access_t access, unsigned mode)
636  {object.create(path, access, mode);};
637 
643  static int load(const char *path);
644 
651  static int exec(const char *path, char **argv);
652 
653  static int exec(const char *path, char **argv, char **envp);
654 
660  static void load(fsys& module, const char *path);
661 
666  static void unload(fsys& module);
667 
674  static void *find(fsys& module, const char *symbol);
675 
676  static inline bool isfile(struct stat *inode)
677  {return S_ISREG(inode->st_mode);}
678 
679  static inline bool isdir(struct stat *inode)
680  {return S_ISDIR(inode->st_mode);}
681 
682  static inline bool islink(struct stat *inode)
683  {return S_ISLNK(inode->st_mode);}
684 
685  static inline bool isdev(struct stat *inode)
686  {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
687 
688  static inline bool ischar(struct stat *inode)
689  {return S_ISCHR(inode->st_mode);}
690 
691  static inline bool isdisk(struct stat *inode)
692  {return S_ISBLK(inode->st_mode);}
693 
694  static inline bool issys(struct stat *inode)
695  {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
696 };
697 
703 class __EXPORT charfile : public CharacterProtocol
704 {
705 private:
706  FILE *fp;
707  bool opened;
708 
709  int _putch(int code);
710 
711  int _getch(void);
712 
713 public:
714  typedef ::fpos_t bookmark_t;
715 
720  inline charfile(FILE *file)
721  {fp = file; opened = false;}
722 
728  charfile(const char *path, const char *mode);
729 
733  charfile();
734 
738  ~charfile();
739 
744  inline operator bool()
745  {return fp != NULL;}
746 
751  inline bool operator !()
752  {return fp == NULL;}
753 
754  inline operator FILE *()
755  {return fp;}
756 
762  void open(const char *path, const char *mode);
763 
767  void close(void);
768 
774  size_t put(const char *string);
775 
785  size_t readline(char *string, size_t size);
786 
795  size_t readline(string& string);
796 
797  inline size_t put(const void *data, size_t size)
798  { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
799 
800  size_t get(void *data, size_t size)
801  { return fp == NULL ? 0 : fread(data, 1, size, fp);}
802 
803  inline void get(bookmark_t& pos)
804  { if(fp) fsetpos(fp, &pos);}
805 
806  inline void set(bookmark_t& pos)
807  { if(fp) fgetpos(fp, &pos);}
808 
809  int err(void);
810 
811  bool eof(void);
812 
813  inline void seek(long offset)
814  {if(fp) fseek(fp, offset, SEEK_SET);}
815 
816  inline void move(long offset)
817  {if(fp) fseek(fp, offset, SEEK_CUR);}
818 
819  inline void append(void)
820  {if (fp) fseek(fp, 0l, SEEK_END);}
821 
822  inline void rewind(void)
823  {if(fp) ::rewind(fp);}
824 
825  size_t printf(const char *format, ...) __PRINTF(2, 3);
826 
827  bool istty(void);
828 };
829 
830 String str(charfile& fp, strsize_t size);
831 
835 typedef fsys fsys_t;
836 
837 extern charfile cstdin, cstdout, cstderr;
838 
839 END_NAMESPACE
840 
841 #endif
842