Scopira 20080306

fileflow.h

00001  
00002 /*
00003  *  Copyright (c) 2002    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013  
00014 #ifndef __INCLUDED_SCOPIRA_TOOL_FILEFLOW_H__
00015 #define __INCLUDED_SCOPIRA_TOOL_FILEFLOW_H__
00016 
00017 #include <fcntl.h>
00018 
00019 #include <scopira/tool/platform.h>
00020 
00021 #include <scopira/tool/flow.h>
00022 #include <scopira/tool/export.h>
00023 
00024 namespace scopira
00025 {
00026   namespace tool
00027   {
00028     class fileflow;
00029 
00030     class read_flocker;
00031     class write_flocker;
00032 
00033     class filememory;
00034 
00035     enum {
00036       copy_c = 0,
00037       copysoftlink_c = 1,
00038       copyhardlink_c = 2,
00039     };
00040 
00047     bool copy_file(const std::string &srcfile, const std::string &destfile, short copytype = copy_c);
00048   }
00049 }
00050 
00060 class scopira::tool::fileflow : public scopira::tool::iflow_i, public scopira::tool::oflow_i
00061 {
00062   protected:
00064     mode_t dm_mode;
00066     int dm_hand;
00068     bool dm_fail;
00069 
00070   public:
00071 
00073     enum {
00075       append_c = 1024,
00077       trunc_c = 2048,
00079       linked_c = 4096,
00081       existing_c = 8192,
00082     };
00084     enum {
00086       stdin_c = -1,
00088       stdout_c = -2,
00090       stderr_c = -3
00091     };
00092     enum seek_dir_t {
00093       seek_start_c = 0,
00094       seek_end_c = 1,
00095       seek_cur_c
00096     };
00097     typedef ::off_t offset_t;
00098 
00106     SCOPIRA_EXPORT fileflow(void);
00113     SCOPIRA_EXPORT fileflow(const std::string& filename, mode_t mode);
00122     SCOPIRA_EXPORT fileflow(int fd, mode_t mode);
00123 
00124     SCOPIRA_EXPORT virtual ~fileflow();
00125 
00132     SCOPIRA_EXPORT virtual bool failed(void) const;
00133 
00142     SCOPIRA_EXPORT virtual size_t read(byte_t* _buf, size_t _maxsize);
00151     SCOPIRA_EXPORT virtual size_t write(const byte_t* _buf, size_t _size);
00152 
00173     SCOPIRA_EXPORT void open(const std::string& filename, mode_t mode);
00189     SCOPIRA_EXPORT void open(int fd,  mode_t mode);
00200     SCOPIRA_EXPORT void close(void);
00201 
00202     // gets the raw OS object. use with care
00203     int get_os_handle(void) const { return dm_hand; }
00204 
00205     // additional fileflow specific stuff: seeking
00206 
00208     SCOPIRA_EXPORT offset_t seek(offset_t howmuch, seek_dir_t dir = seek_start_c);
00210     SCOPIRA_EXPORT void seek_reset(void) { seek(0); };
00211 
00213     SCOPIRA_EXPORT offset_t tell(void) const;
00214 
00222     SCOPIRA_EXPORT bool resize(offset_t newsz);
00223 
00224     // additional fileflow specific stuff: locking
00225 
00233     SCOPIRA_EXPORT void read_lock(void);
00241     SCOPIRA_EXPORT bool try_read_lock(void);
00249     SCOPIRA_EXPORT void write_lock(void);
00257     SCOPIRA_EXPORT bool try_write_lock(void);
00263     SCOPIRA_EXPORT void unlock(void);
00264 };
00265 
00270 class scopira::tool::read_flocker
00271 {
00272   protected:
00273     scopira::tool::fileflow &dm_file;
00274   public:
00276     read_flocker(fileflow &f) : dm_file(f) { dm_file.read_lock(); }
00278     ~read_flocker() { dm_file.unlock(); }
00279 };
00280 
00285 class scopira::tool::write_flocker
00286 {
00287   protected:
00288     scopira::tool::fileflow &dm_file;
00289   public:
00291     write_flocker(fileflow &f) : dm_file(f) { dm_file.write_lock(); }
00293     ~write_flocker() { dm_file.unlock(); }
00294 };
00295 
00301 class scopira::tool::filememory
00302 {
00303   public:
00304     typedef ::off_t offset_t;
00305 
00306     enum {
00307       read_c = 1,
00308       write_c = 2,
00309       private_c = 4,      // does MAP_PRIVATE instead of MAP_SHARED
00310     };
00311   private:
00312     void *dm_ptr;
00313     size_t dm_len;
00314 #ifdef PLATFORM_win32
00315     HANDLE dm_mappingobj;
00316 #endif
00317   public:
00319     SCOPIRA_EXPORT filememory(void);
00321     SCOPIRA_EXPORT ~filememory();
00322 
00324     void *c_array(void) const { return dm_ptr; }
00325 
00331     SCOPIRA_EXPORT bool open(fileflow &f, size_t len, int flags = read_c|write_c);
00333     SCOPIRA_EXPORT void close(void);
00334 
00336     SCOPIRA_EXPORT void sync(void);
00337 };
00338 
00339 #endif
00340