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 _MSWINDOWS_
47 #include <sys/stat.h>
48 #else
49 #include <io.h>
50 #ifndef R_OK
51 #define F_OK 0
52 #define X_OK 1
53 #define W_OK 2
54 #define R_OK 4
55 #endif
56 #endif
57 
58 #include <errno.h>
59 #include <stdio.h>
60 
61 #ifndef __S_ISTYPE
62 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
63 #endif
64 
65 #if !defined(S_ISDIR) && defined(S_IFDIR)
66 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
67 #endif
68 
69 #if !defined(S_ISCHR) && defined(S_IFCHR)
70 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
71 #elif !defined(S_ISCHR)
72 #define S_ISCHR(mode) 0
73 #endif
74 
75 #if !defined(S_ISBLK) && defined(S_IFBLK)
76 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
77 #elif !defined(S_ISBLK)
78 #define S_ISBLK(mode) 0
79 #endif
80 
81 #if !defined(S_ISREG) && defined(S_IFREG)
82 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
83 #elif !defined(S_ISREG)
84 #define S_ISREG(mode) 1
85 #endif
86 
87 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
88 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
89 #elif !defined(S_ISSOCK)
90 #define S_ISSOCK(mode) (0)
91 #endif
92 
93 #if !defined(S_ISFIFO) && defined(S_IFIFO)
94 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
95 #elif !defined(S_ISFIFO)
96 #define S_ISFIFO(mode) (0)
97 #endif
98 
99 #if !defined(S_ISLNK) && defined(S_IFLNK)
100 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
101 #elif !defined(S_ISLNK)
102 #define S_ISLNK(mode) (0)
103 #endif
104 
105 NAMESPACE_UCOMMON
106 
110 typedef void *dir_t;
111 
115 typedef void *mem_t;
116 
125 class __EXPORT fsys
126 {
127 protected:
128  fd_t fd;
129 #ifdef _MSWINDOWS_
130  WIN32_FIND_DATA *ptr;
131  HINSTANCE mem;
132 #else
133  void *ptr;
134 #endif
135  int error;
136 
137 public:
138  typedef struct stat fileinfo_t;
139 
140 #ifdef _MSWINDOWS_
141  static int remapError(void);
142 #else
143  inline static int remapError(void)
144  {return errno;};
145 #endif
146 
150  typedef enum {
151  ACCESS_RDONLY,
152  ACCESS_WRONLY,
153  ACCESS_REWRITE,
154  ACCESS_RDWR = ACCESS_REWRITE,
155  ACCESS_APPEND,
156  ACCESS_SHARED,
157  ACCESS_DIRECTORY,
158  ACCESS_STREAM,
159  ACCESS_RANDOM
160  } access_t;
161 
165  typedef long offset_t;
166 
170  static const offset_t end;
171 
175  fsys();
176 
180  fsys(fd_t handle);
181 
186  fsys(const fsys& descriptor);
187 
193  fsys(const char *path, access_t access);
194 
201  fsys(const char *path, access_t access, unsigned permission);
202 
206  ~fsys();
207 
212  inline fd_t operator*() const
213  {return fd;};
214 
219  inline operator fd_t() const
220  {return fd;};
221 
226  inline operator bool() const
227  {return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
228 
233  inline bool operator!() const
234  {return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
235 
240  void operator=(const fsys& descriptor);
241 
246  void operator=(fd_t descriptor);
247 
252  inline fd_t getHandle(void) const
253  {return fd;};
254 
259  void set(fd_t descriptor);
260 
265  fd_t release(void);
266 
272  int seek(offset_t offset);
273 
279  int drop(offset_t size = 0);
280 
285  bool istty(void);
286 
291  static bool istty(fd_t fd);
292 
299  ssize_t read(void *buffer, size_t count);
300 
307  ssize_t write(const void *buffer, size_t count);
308 
314  int fileinfo(fileinfo_t *buffer);
315 
322  int trunc(offset_t offset);
323 
328  int sync(void);
329 
335  static int changeDir(const char *path);
336 
343  static int getPrefix(char *path, size_t size);
344 
351  static int fileinfo(const char *path, fileinfo_t *buffer);
352 
358  static int remove(const char *path);
359 
367  static int copy(const char *source, const char *target, size_t size = 1024);
368 
375  static int rename(const char *oldpath, const char *newpath);
376 
383  static int change(const char *path, unsigned mode);
384 
391  static int access(const char *path, unsigned mode);
392 
398  static bool isfile(const char *path);
399 
405  static bool isdir(const char *path);
406 
412  static bool islink(const char *path);
413 
419  static bool ishidden(const char *path);
420 
428  inline static ssize_t read(fsys& descriptor, void *buffer, size_t count)
429  {return descriptor.read(buffer, count);};
430 
438  inline static ssize_t write(fsys& descriptor, const void *buffer, size_t count)
439  {return descriptor.write(buffer, count);};
440 
447  inline static int seek(fsys& descriptor, offset_t offset)
448  {return descriptor.seek(offset);};
449 
456  inline static int drop(fsys& descriptor, offset_t size)
457  {return descriptor.drop(size);};
458 
464  void open(const char *path, access_t access);
465 
470  inline void assign(fd_t descriptor)
471  {close(); fd = descriptor;};
472 
478  inline static void assign(fsys& object, fd_t descriptor)
479  {object.close(); object.fd = descriptor;};
480 
487  void create(const char *path, access_t access, unsigned mode);
488 
495  static int createDir(const char *path, unsigned mode);
496 
502  static int removeDir(const char *path);
503 
511  static int unlink(const char *path);
512 
519  static int link(const char *path, const char *target);
520 
527  static int hardlink(const char *path, const char *target);
528 
535  static int linkinfo(const char *path, char *buffer, size_t size);
536 
541  inline static void close(fsys& descriptor)
542  {descriptor.close();};
543 
547  void close(void);
548 
553  inline int err(void) const
554  {return error;}
555 
562  inline static void open(fsys& object, const char *path, access_t access)
563  {object.open(path, access);};
564 
570  static fd_t input(const char *path);
571 
577  static fd_t output(const char *path);
578 
584  static fd_t append(const char *path);
585 
590  static void release(fd_t descriptor);
591 
599  inline static void create(fsys& object, const char *path, access_t access, unsigned mode)
600  {object.create(path, access, mode);};
601 
607  static int load(const char *path);
608 
614  static void load(fsys& module, const char *path);
615 
620  static void unload(fsys& module);
621 
628  static void *find(fsys& module, const char *symbol);
629 
630  static inline bool isfile(struct stat *inode)
631  {return S_ISREG(inode->st_mode);}
632 
633  static inline bool isdir(struct stat *inode)
634  {return S_ISDIR(inode->st_mode);}
635 
636  static inline bool islink(struct stat *inode)
637  {return S_ISLNK(inode->st_mode);}
638 
639  static inline bool isdev(struct stat *inode)
640  {return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
641 
642  static inline bool ischar(struct stat *inode)
643  {return S_ISCHR(inode->st_mode);}
644 
645  static inline bool isdisk(struct stat *inode)
646  {return S_ISBLK(inode->st_mode);}
647 
648  static inline bool issys(struct stat *inode)
649  {return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
650 };
651 
657 class __EXPORT charfile : public CharacterProtocol
658 {
659 private:
660  FILE *fp;
661  bool opened;
662 
663  int _putch(int code);
664 
665  int _getch(void);
666 
667 public:
668  typedef ::fpos_t bookmark_t;
669 
674  inline charfile(FILE *file)
675  {fp = file; opened = false;}
676 
682  charfile(const char *path, const char *mode);
683 
687  charfile();
688 
692  ~charfile();
693 
698  inline operator bool()
699  {return fp != NULL;}
700 
705  inline bool operator !()
706  {return fp == NULL;}
707 
713  void open(const char *path, const char *mode);
714 
718  void close(void);
719 
725  size_t put(const char *string);
726 
736  size_t readline(char *string, size_t size);
737 
746  size_t readline(string& string);
747 
748  inline size_t put(const void *data, size_t size)
749  { return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
750 
751  size_t get(void *data, size_t size)
752  { return fp == NULL ? 0 : fread(data, 1, size, fp);}
753 
754  inline void get(bookmark_t& pos)
755  { if(fp) fsetpos(fp, &pos);}
756 
757  inline void set(bookmark_t& pos)
758  { if(fp) fgetpos(fp, &pos);}
759 
760  int err(void);
761 
762  bool eof(void);
763 
764  inline void seek(long offset)
765  {if(fp) fseek(fp, offset, SEEK_SET);}
766 
767  inline void move(long offset)
768  {if(fp) fseek(fp, offset, SEEK_CUR);}
769 
770  inline void append(void)
771  {if (fp) fseek(fp, 0l, SEEK_END);}
772 
773  inline void rewind(void)
774  {if(fp) ::rewind(fp);}
775 
776  size_t printf(const char *format, ...) __PRINTF(2, 3);
777 
778  bool istty(void);
779 };
780 
781 String str(charfile& fp, strsize_t size);
782 
786 typedef fsys fsys_t;
787 
788 extern charfile cstdin, cstdout, cstderr;
789 
790 END_NAMESPACE
791 
792 #endif
793