Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

mmfile.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: mmfile.h,v 1.10 2005/02/23 10:26:15 tat Exp $
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_OS_MMFILE_H
00017 #define _MIMETIC_OS_MMFILE_H
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <fcntl.h>
00021 #include <sys/mman.h>
00022 #include <string>
00023 #include <cstring>
00024 #include <mimetic/os/fileop.h>
00025 
00026 namespace mimetic
00027 {
00028 
00029 /// Memory mapped file
00030 struct MMFile: public FileOp
00031 {
00032     typedef char* iterator;
00033     typedef const char* const_iterator;
00034     MMFile();
00035     MMFile(const std::string&, int mode = O_RDONLY);
00036     ~MMFile();
00037     operator bool() const;
00038     bool open(const std::string&, int mode = O_RDONLY);
00039     void close();
00040     uint read(char*, int);
00041 
00042     iterator begin();
00043     const_iterator begin() const;
00044     iterator end();
00045     const_iterator end() const;
00046 
00047 protected:
00048     bool map();
00049     bool open(int flags);
00050     bool stat();
00051 
00052     std::string m_fqn;
00053     bool m_stated;
00054     struct stat m_st;
00055     int m_fd;
00056 
00057     char *m_beg, *m_end;
00058 };
00059 
00060 }
00061 
00062 
00063 #endif
00064