Scopira  20080306
fileflow.h
1 
2 /*
3  * Copyright (c) 2002 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_TOOL_FILEFLOW_H__
15 #define __INCLUDED_SCOPIRA_TOOL_FILEFLOW_H__
16 
17 #include <fcntl.h>
18 
19 #include <scopira/tool/platform.h>
20 
21 #include <scopira/tool/flow.h>
22 #include <scopira/tool/export.h>
23 
24 namespace scopira
25 {
26  namespace tool
27  {
28  class fileflow;
29 
30  class read_flocker;
31  class write_flocker;
32 
33  class filememory;
34 
35  enum {
36  copy_c = 0,
37  copysoftlink_c = 1,
38  copyhardlink_c = 2,
39  };
40 
47  bool copy_file(const std::string &srcfile, const std::string &destfile, short copytype = copy_c);
48  }
49 }
50 
61 {
62  protected:
66  int dm_hand;
68  bool dm_fail;
69 
70  public:
71 
73  enum {
75  append_c = 1024,
77  trunc_c = 2048,
79  linked_c = 4096,
81  existing_c = 8192,
82  };
84  enum {
86  stdin_c = -1,
88  stdout_c = -2,
90  stderr_c = -3
91  };
92  enum seek_dir_t {
93  seek_start_c = 0,
94  seek_end_c = 1,
95  seek_cur_c
96  };
97  typedef ::off_t offset_t;
98 
106  SCOPIRA_EXPORT fileflow(void);
113  SCOPIRA_EXPORT fileflow(const std::string& filename, mode_t mode);
122  SCOPIRA_EXPORT fileflow(int fd, mode_t mode);
123 
124  SCOPIRA_EXPORT virtual ~fileflow();
125 
132  SCOPIRA_EXPORT virtual bool failed(void) const;
133 
142  SCOPIRA_EXPORT virtual size_t read(byte_t* _buf, size_t _maxsize);
151  SCOPIRA_EXPORT virtual size_t write(const byte_t* _buf, size_t _size);
152 
173  SCOPIRA_EXPORT void open(const std::string& filename, mode_t mode);
189  SCOPIRA_EXPORT void open(int fd, mode_t mode);
200  SCOPIRA_EXPORT void close(void);
201 
202  // gets the raw OS object. use with care
203  int get_os_handle(void) const { return dm_hand; }
204 
205  // additional fileflow specific stuff: seeking
206 
208  SCOPIRA_EXPORT offset_t seek(offset_t howmuch, seek_dir_t dir = seek_start_c);
210  SCOPIRA_EXPORT void seek_reset(void) { seek(0); };
211 
213  SCOPIRA_EXPORT offset_t tell(void) const;
214 
222  SCOPIRA_EXPORT bool resize(offset_t newsz);
223 
224  // additional fileflow specific stuff: locking
225 
233  SCOPIRA_EXPORT void read_lock(void);
241  SCOPIRA_EXPORT bool try_read_lock(void);
249  SCOPIRA_EXPORT void write_lock(void);
257  SCOPIRA_EXPORT bool try_write_lock(void);
263  SCOPIRA_EXPORT void unlock(void);
264 };
265 
271 {
272  protected:
273  scopira::tool::fileflow &dm_file;
274  public:
276  read_flocker(fileflow &f) : dm_file(f) { dm_file.read_lock(); }
278  ~read_flocker() { dm_file.unlock(); }
279 };
280 
286 {
287  protected:
288  scopira::tool::fileflow &dm_file;
289  public:
291  write_flocker(fileflow &f) : dm_file(f) { dm_file.write_lock(); }
293  ~write_flocker() { dm_file.unlock(); }
294 };
295 
302 {
303  public:
304  typedef ::off_t offset_t;
305 
306  enum {
307  read_c = 1,
308  write_c = 2,
309  private_c = 4, // does MAP_PRIVATE instead of MAP_SHARED
310  };
311  private:
312  void *dm_ptr;
313  size_t dm_len;
314 #ifdef PLATFORM_win32
315  HANDLE dm_mappingobj;
316 #endif
317  public:
319  SCOPIRA_EXPORT filememory(void);
321  SCOPIRA_EXPORT ~filememory();
322 
324  void *c_array(void) const { return dm_ptr; }
325 
331  SCOPIRA_EXPORT bool open(fileflow &f, size_t len, int flags = read_c|write_c);
333  SCOPIRA_EXPORT void close(void);
334 
336  SCOPIRA_EXPORT void sync(void);
337 };
338 
339 #endif
340 
void * c_array(void) const
gets the memory pointer, null for none right now
Definition: fileflow.h:324
Definition: flow.h:108
~read_flocker()
unlock dot
Definition: fileflow.h:278
virtual size_t write(const byte_t *_buf, size_t _size)
scopira::tool::byte_t byte_t
Definition: flow.h:73
Definition: archiveflow.h:20
open an existinf file. do not trunc. do not append.
Definition: fileflow.h:81
Definition: fileflow.h:285
internal, you dont need to specifiy this
Definition: fileflow.h:79
bool resize(offset_t newsz)
this file descriptor represents the standard output stream, stdout
Definition: fileflow.h:88
Definition: fileflow.h:301
Definition: fileflow.h:60
bool dm_fail
currently failed?
Definition: fileflow.h:68
read_flocker(fileflow &f)
locking ctor
Definition: fileflow.h:276
this is the default, for output files
Definition: fileflow.h:77
int dm_hand
the file handle
Definition: fileflow.h:66
write_flocker(fileflow &f)
locking ctor
Definition: fileflow.h:291
append to any existing file
Definition: fileflow.h:75
int mode_t
Definition: flow.h:78
offset_t seek(offset_t howmuch, seek_dir_t dir=seek_start_c)
seekers
virtual bool failed(void) const
virtual size_t read(byte_t *_buf, size_t _maxsize)
mode_t dm_mode
current modes, 0 for no-file
Definition: fileflow.h:64
this file descriptor represents the standard input stream, stdin
Definition: fileflow.h:86
bool copy_file(const std::string &srcfile, const std::string &destfile, short copytype=copy_c)
offset_t tell(void) const
tells the current location of the file pointer
void seek_reset(void)
seeks to the start
Definition: fileflow.h:210
this file descriptor represents the standard error stream, stderr
Definition: fileflow.h:90
~write_flocker()
unlock dot
Definition: fileflow.h:293
void open(const std::string &filename, mode_t mode)
Definition: fileflow.h:270
Definition: flow.h:159