1 /******************************************************************************
3 * Copyright (C) 1997-2012 by Dimitri van Heesch.
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
24 /** @brief Store implementation based on a file.
25 Writing is linear, after that the file is re-opened for reading.
26 Reading is random (seek+read).
28 class FileStorage : public StorageIntf
31 FileStorage() : m_readOnly(FALSE) {}
32 FileStorage( const QString &name) :
33 m_readOnly(FALSE) { m_file.setName(name); }
34 int read(char *buf,uint size) { return m_file.readBlock(buf,size); }
35 int write(const char *buf,uint size) { assert(m_readOnly==FALSE); return m_file.writeBlock(buf,size); }
36 bool open( int m ) { m_readOnly = m==IO_ReadOnly; return m_file.open(m); }
37 bool seek(int64 pos) { return m_file.seek(pos); }
38 int64 pos() const { return m_file.pos(); }
39 void close() { m_file.close(); }
40 void setName( const char *name ) { m_file.setName(name); }
46 #if 0 // experimental version using mmap after opening the file as read only.
54 class FileStorage : public StorageIntf
57 FileStorage() : m_readOnly(FALSE), m_map(0), m_off(0), m_size(0) {}
58 FileStorage( const QString &name) :
59 m_readOnly(FALSE) { m_file.setName(name); }
60 void setName( const char *name ) { m_file.setName(name); }
66 QString name = m_file.name();
68 m_fd = ::open(name.data(),O_RDONLY);
71 m_size = stat.st_size;
72 m_map = mmap(NULL,m_size,PROT_READ,MAP_SHARED,m_fd,0);
73 if (m_map==MAP_FAILED) perror("mmap failed");
74 assert(m_map!=MAP_FAILED);
81 return m_file.open(m);
84 int write(const char *buf,uint size)
87 return m_file.writeBlock(buf,size);
89 int read(char *buf,uint size)
92 memcpy(buf,((char *)m_map)+m_off,size);
116 munmap(m_map,m_size);