/[debian]/mimetic/trunk/mimetic/os/mmfile.cxx
ViewVC logotype

Contents of /mimetic/trunk/mimetic/os/mmfile.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 197 - (show annotations)
Sun Apr 16 12:21:05 2006 UTC (15 years, 3 months ago) by gregoa
File size: 2916 byte(s)
New upstream release.

1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: mmfile.cxx,v 1.4 2006/04/11 10:04:58 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/mman.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <time.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <mimetic/libconfig.h>
25 #include <mimetic/os/mmfile.h>
26
27 using namespace std;
28
29 namespace mimetic
30 {
31
32 MMFile::MMFile()
33 : m_stated(false), m_fd(-1), m_beg(0), m_end(0)
34 {
35 }
36
37 MMFile::MMFile(const string& fqn, int mode)
38 : m_fqn(fqn), m_stated(false), m_fd(-1), m_beg(0), m_end(0)
39 {
40 memset(&m_st, 0, sizeof(m_st));
41 if(!stat())
42 return;
43 open(mode);
44 }
45
46 bool MMFile::open(const std::string& fqn, int mode /*= O_RDONLY*/)
47 {
48 if(!stat() || !S_ISREG(m_st.st_mode))
49 return false;
50 m_fqn = fqn;
51 return open(mode);
52 }
53
54 bool MMFile::open(int mode)
55 {
56 if(!stat() || !S_ISREG(m_st.st_mode))
57 return false;
58 m_fd = ::open(m_fqn.c_str(), mode);
59 if(m_fd > 0)
60 return map();
61 else
62 return false;
63 }
64
65 bool MMFile::map()
66 {
67 m_beg = (char*) mmap(0, m_st.st_size, PROT_READ, MAP_SHARED,m_fd,0);
68 if(m_beg > 0)
69 {
70 m_end = m_beg + m_st.st_size;
71 #if HAVE_MADVISE
72 madvise(m_beg, m_st.st_size, MADV_SEQUENTIAL);
73 #endif
74 return true;
75 }
76 return false;
77 }
78
79 MMFile::~MMFile()
80 {
81 if(m_beg)
82 munmap(m_beg, m_st.st_size);
83 if(m_fd)
84 close();
85 }
86
87 MMFile::iterator MMFile::begin()
88 {
89 return m_beg;
90 }
91
92 MMFile::iterator MMFile::end()
93 {
94 return m_end;
95 }
96
97 MMFile::const_iterator MMFile::begin() const
98 {
99 return m_beg;
100 }
101
102
103 MMFile::const_iterator MMFile::end() const
104 {
105 return m_end;
106 }
107
108 uint MMFile::read(char* buf, int bufsz)
109 {
110 int r;
111 do
112 {
113 r = ::read(m_fd, buf, bufsz);
114 } while(r < 0 && errno == EINTR);
115 return r;
116 }
117
118 MMFile::operator bool() const
119 {
120 return m_fd > 0;
121 }
122
123 bool MMFile::stat()
124 {
125 return m_stated || (m_stated = (::stat(m_fqn.c_str(), &m_st) == 0));
126 }
127
128 void MMFile::close()
129 {
130 while(::close(m_fd) < 0 && errno == EINTR)
131 ;
132 m_fd = -1;
133 }
134
135 }
136

  ViewVC Help
Powered by ViewVC 1.1.26