GNU CommonC++
file.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_FILE_H_
44 #define CCXX_FILE_H_
45 
46 #ifndef CCXX_CONFIG_H_
47 #include <cc++/config.h>
48 #endif
49 
50 #ifndef CCXX_MISSING_H_
51 #include <cc++/missing.h>
52 #endif
53 
54 #ifndef CCXX_THREAD_H_
55 #include <cc++/thread.h>
56 #endif
57 
58 #ifndef CCXX_EXCEPTION_H_
59 #include <cc++/exception.h>
60 #endif
61 
62 #ifndef WIN32
63 # ifdef __BORLANDC__
64 # include <stdio.h>
65 # include <sys/types.h>
66 # else
67 # include <cstdio>
68 # endif
69 # include <dirent.h>
70 # include <sys/stat.h>
71 # include <sys/mman.h>
72 #else
73 # if __BORLANDC__ >= 0x0560
74 # include <dirent.h>
75 # include <sys/stat.h>
76 # else
77 # include <direct.h>
78 # endif
79 #endif
80 
81 #ifdef HAVE_SHL_LOAD
82 #include <dl.h>
83 #endif
84 
85 #ifdef HAVE_MACH_DYLD
86 #include <mach-o/dyld.h>
87 #endif
88 
89 #ifdef CCXX_NAMESPACES
90 namespace ost {
91 #endif
92 
93 typedef unsigned long pos_t;
94 #ifndef WIN32
95 // use a define so that if the sys/types.h header already defines caddr_t
96 // as it may on BSD systems, we do not break it by redefining again.
97 #undef caddr_t
98 #define caddr_t char *
99 typedef size_t ccxx_size_t;
100 #else
101 #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x0560
102 typedef LONG off_t;
103 #endif
104 typedef void* caddr_t;
105 typedef DWORD ccxx_size_t;
106 #endif
107 
108 #ifndef PATH_MAX
109 #define PATH_MAX 256
110 #endif
111 
112 #ifndef NAME_MAX
113 #define NAME_MAX 64
114 #endif
115 
117 {
118 public:
119  enum Error {
120  errSuccess = 0,
134  errExtended
135  };
136  typedef enum Error Error;
137 
138  enum Access {
139 #ifndef WIN32
140  accessReadOnly = O_RDONLY,
141  accessWriteOnly= O_WRONLY,
142  accessReadWrite = O_RDWR
143 #else
144  accessReadOnly = GENERIC_READ,
145  accessWriteOnly = GENERIC_WRITE,
146  accessReadWrite = GENERIC_READ | GENERIC_WRITE
147 #endif
148  };
149  typedef enum Access Access;
150 
151 protected:
152  typedef struct _fcb {
153  struct _fcb *next;
156  off_t pos;
157  bool locked;
158  } fcb_t;
159 
160 public:
161 #ifdef WIN32
162  enum Open {
163  openReadOnly, // = FILE_OPEN_READONLY,
164  openWriteOnly, // = FILE_OPEN_WRITEONLY,
165  openReadWrite, // = FILE_OPEN_READWRITE,
166  openAppend, // = FILE_OPEN_APPEND,
167  openTruncate // = FILE_OPEN_TRUNCATE
168  };
169  #else
170  enum Open {
171  openReadOnly = O_RDONLY,
172  openWriteOnly = O_WRONLY,
173  openReadWrite = O_RDWR,
174  openAppend = O_WRONLY | O_APPEND,
175 #ifdef O_SYNC
176  openSync = O_RDWR | O_SYNC,
177 #else
178  openSync = O_RDWR,
179 #endif
180  openTruncate = O_RDWR | O_TRUNC
181  };
182  typedef enum Open Open;
183 
184 /* to be used in future */
185 
186 #ifndef S_IRUSR
187 #define S_IRUSR 0400
188 #define S_IWUSR 0200
189 #define S_IRGRP 0040
190 #define S_IWGRP 0020
191 #define S_IROTH 0004
192 #define S_IWOTH 0002
193 #endif
194 
195 #endif // !WIN32
196 
197 #ifndef WIN32
198  enum Attr {
199  attrInvalid = 0,
200  attrPrivate = S_IRUSR | S_IWUSR,
201  attrGroup = attrPrivate | S_IRGRP | S_IWGRP,
202  attrPublic = attrGroup | S_IROTH | S_IWOTH
203  };
204  #else // defined WIN32
205  enum Attr {
206  attrInvalid=0,
207  attrPrivate,
208  attrGroup,
209  attrPublic
210  };
211 #endif // !WIN32
212  typedef enum Attr Attr;
213 
214 #ifdef WIN32
215  enum Complete {
216  completionImmediate, // = FILE_COMPLETION_IMMEDIATE,
217  completionDelayed, // = FILE_COMPLETION_DELAYED,
218  completionDeferred // = FILE_COMPLETION_DEFERRED
219  };
220 
221  enum Mapping {
222  mappedRead,
223  mappedWrite,
224  mappedReadWrite
225  };
226 #else
227  enum Mapping {
228  mappedRead = accessReadOnly,
229  mappedWrite = accessWriteOnly,
230  mappedReadWrite = accessReadWrite
231  };
232  enum Complete {
235  completionDeferred
236  };
237 #endif
238  typedef enum Complete Complete;
239  typedef enum Mapping Mapping;
240 
241 public:
242  static const char *getExtension(const char *path);
243  static const char *getFilename(const char *path);
244  static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX);
245  static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX);
246  static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);
247 };
248 
257 class __EXPORT Dir : public File
258 {
259 private:
260 #ifndef WIN32
261  DIR *dir;
262 #ifdef HAVE_READDIR_R
263  struct dirent *save;
264  char save_space[sizeof(struct dirent) + PATH_MAX + 1];
265 #endif
266  struct dirent *entry;
267 #else
268  HANDLE hDir;
269  WIN32_FIND_DATA data, fdata;
270  char *name;
271 #endif
272 
273 public:
274  Dir(const char *name = NULL);
275 
276  static bool create(const char *path, Attr attr = attrGroup);
277  static bool remove(const char *path);
278  static bool setPrefix(const char *path);
279  static bool getPrefix(char *path, size_t size = PATH_MAX);
280 
281  void open(const char *name);
282  void close(void);
283 
284  virtual ~Dir();
285 
286  const char *getName(void);
287 
288  const char *operator++()
289  {return getName();};
290 
291  const char *operator++(int)
292  {return getName();};
293 
294  const char *operator*();
295 
296  bool rewind(void);
297 
298  bool operator!()
299 #ifndef WIN32
300  {return !dir;};
301 #else
302  {return hDir != INVALID_HANDLE_VALUE;};
303 #endif
304 
305  bool isValid(void);
306 };
307 
315 {
316 private:
317  char path[PATH_MAX + 1];
318  Dir *dir;
319  unsigned max, current, prefixpos;
320 
321 protected:
331  virtual bool filter(const char *file, struct stat *ino);
332 
333 public:
341  DirTree(const char *prefix, unsigned maxdepth);
342 
348  DirTree(unsigned maxdepth);
349 
350  virtual ~DirTree();
351 
357  void open(const char *prefix);
358 
362  void close(void);
363 
371  char *getPath(void);
372 
382  unsigned perform(const char *prefix);
383 };
384 
395 class __EXPORT RandomFile : protected Mutex, public File
396 {
397 private:
398  Error errid;
399  char *errstr;
400 
401 protected:
402 #ifndef WIN32
403  int fd;
404  // FIXME: WIN32 as no access member
406 #else
407  HANDLE fd;
408 #endif
409  char *pathname;
410 
411  struct {
412  unsigned count : 16;
413  bool thrown : 1;
414  bool initial : 1;
415 #ifndef WIN32
416  bool immediate : 1;
417 #endif
418  bool temp : 1;
419  } flags;
420 
424  RandomFile(const char *name = NULL);
425 
429  RandomFile(const RandomFile &rf);
430 
438  Error error(Error errid, char *errstr = NULL);
439 
446  inline Error error(char *err)
447  {return error(errExtended, err);};
448 
455  inline void setError(bool enable)
456  {flags.thrown = !enable;};
457 
458 #ifndef WIN32
459 
466  Error setCompletion(Complete mode);
467 #endif
468 
475  inline void setTemporary(bool enable)
476  {flags.temp = enable;};
477 
489  virtual Attr initialize(void);
490 
494  void final(void);
495 
496 public:
500  virtual ~RandomFile();
501 
510  bool initial(void);
511 
517  off_t getCapacity(void);
518 
524  virtual Error restart(void);
525 
531  inline Error getErrorNumber(void)
532  {return errid;};
533 
539  inline char *getErrorString(void)
540  {return errstr;};
541 
542  bool operator!(void);
543 };
544 
565 {
566 private:
567  ThreadKey state;
568  fcb_t *first;
569  fcb_t *getFCB(void);
570  Error open(const char *path);
571 
572 public:
579  ThreadFile(const char *path);
580 
584  virtual ~ThreadFile();
585 
591  Error restart(void);
592 
602  Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
603 
613  Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
614 
620  Error append(caddr_t address = NULL, ccxx_size_t length = 0);
621 
627  off_t getPosition(void);
628 
629  bool operator++(void);
630  bool operator--(void);
631 };
632 
648 {
649 private:
650  fcb_t fcb;
651  Error open(const char *path);
652 
653 public:
660  SharedFile(const char *path);
661 
668  SharedFile(const SharedFile &file);
669 
673  virtual ~SharedFile();
674 
680  Error restart(void)
681  {return open(pathname);};
682 
693  Error fetch(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
694 
705  Error update(caddr_t address = NULL, ccxx_size_t length = 0, off_t position = -1);
706 
715  Error clear(ccxx_size_t length = 0, off_t pos = -1);
716 
723  Error append(caddr_t address = NULL, ccxx_size_t length = 0);
724 
730  off_t getPosition(void);
731 
732  bool operator++(void);
733  bool operator--(void);
734 };
735 
747 {
748 private:
749  fcb_t fcb;
750  int prot;
751 #ifdef WIN32
752  HANDLE map;
753  char mapname[64];
754 #endif
755 
756 public:
764  MappedFile(const char *fname, Access mode);
765 
774  MappedFile(const char *fname, Access mode, size_t size);
775 
786  MappedFile(const char *fname, pos_t offset, size_t size, Access mode);
787 
792  virtual ~MappedFile();
793 
794  // FIXME: not use library function in header ??
800  void sync(void);
801 
808  void sync(caddr_t address, size_t len);
809 
818  void update(size_t offset = 0, size_t len = 0);
819 
827  void update(caddr_t address, size_t len);
828 
835  void release(caddr_t address, size_t len);
836 
845  inline caddr_t fetch(size_t offset = 0)
846  {return ((char *)(fcb.address)) + offset;};
847 
856  caddr_t fetch(off_t pos, size_t len);
857 
863  bool lock(void);
864 
868  void unlock(void);
869 
876  size_t pageAligned(size_t size);
877 };
878 
879 
889 {
890 private:
891  const char *err;
892 #ifdef HAVE_MODULES
893  static Mutex mutex;
894  static DSO *first;
895  static DSO *last;
896  DSO *next, *prev;
897  const char *id;
898 #if defined(HAVE_MACH_DYLD)
899  NSModule oModule;
900 #elif defined(HAVE_SHL_LOAD)
901  shl_t image;
902 #elif defined(WIN32)
903  HINSTANCE hImage;
904 #else
905  void *image;
906 #endif
907  void loader(const char *filename, bool resolve);
908 #endif
909 
910 public:
916 #ifdef HAVE_MODULES
917  DSO(const char *filename)
918  {loader(filename, true);};
919 
920  DSO(const char *filename, bool resolve)
921  {loader(filename, resolve);};
922 #else
923  DSO(const char *filename)
924  {throw this;};
925  DSO(const char *filename, bool resolve)
926  {throw this;};
927 #endif
928 
933  inline const char *getError(void)
934  {return err;};
935 
939 #ifdef HAVE_MODULES
940  virtual ~DSO();
941 #endif
942 
946 #ifdef HAVE_MODULES
947  void* operator[](const char *sym);
948 #else
949  void *operator[](const char *)
950  {return NULL;};
951 #endif
952 
953 #ifdef HAVE_MODULES
954  static void dynunload(void);
955 #else
956  static void dynunload(void)
957  {return;};
958 #endif
959 
965  static DSO *getObject(const char *name);
966 
972  bool isValid(void);
973 
977  static void setDebug(void);
978 };
979 
981 bool __EXPORT isDir(const char *path);
983 bool __EXPORT isFile(const char *path);
984 #ifndef WIN32
985 
986 bool __EXPORT isDevice(const char *path);
987 #else
988 
989 inline bool isDevice(const char *path)
990 { return false; }
991 #endif
992 
993 bool __EXPORT canAccess(const char *path);
995 bool __EXPORT canModify(const char *path);
997 time_t __EXPORT lastModified(const char *path);
999 time_t __EXPORT lastAccessed(const char *path);
1000 
1001 #ifdef COMMON_STD_EXCEPTION
1002 
1003 class DirException : public IOException
1004 {
1005 public:
1006  DirException(const String &str) : IOException(str) {};
1007 };
1008 
1009 class __EXPORT DSOException : public IOException
1010 {
1011 public:
1012  DSOException(const String &str) : IOException(str) {};
1013 };
1014 
1015 class __EXPORT FileException : public IOException
1016 {
1017 public:
1018  FileException(const String &str) : IOException(str) {};
1019 };
1020 
1021 #endif
1022 
1023 #ifdef CCXX_NAMESPACES
1024 }
1025 #endif
1026 
1027 #endif
1028