Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

mmfile.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: mmfile.h,v 1.12 2008-10-07 11:06:26 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_OS_MMFILE_H
00008 #define _MIMETIC_OS_MMFILE_H
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include <fcntl.h>
00012 #include <sys/mman.h>
00013 #include <string>
00014 #include <cstring>
00015 #include <mimetic/os/fileop.h>
00016 
00017 namespace mimetic
00018 {
00019 
00020 /// Memory mapped file
00021 struct MMFile: public FileOp
00022 {
00023     typedef char* iterator;
00024     typedef const char* const_iterator;
00025     MMFile();
00026     MMFile(const std::string&, int mode = O_RDONLY);
00027     ~MMFile();
00028     operator bool() const;
00029     bool open(const std::string&, int mode = O_RDONLY);
00030     void close();
00031     uint read(char*, int);
00032 
00033     iterator begin();
00034     const_iterator begin() const;
00035     iterator end();
00036     const_iterator end() const;
00037 
00038 protected:
00039     bool map();
00040     bool open(int flags);
00041     bool stat();
00042 
00043     std::string m_fqn;
00044     bool m_stated;
00045     struct stat m_st;
00046     int m_fd;
00047 
00048     char *m_beg, *m_end;
00049 };
00050 
00051 }
00052 
00053 
00054 #endif
00055