00001 /* This file is part of the KDE project 00002 Copyright (C) 2002 Simon Hausmann <hausmann@kde.org> 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; see the file COPYING. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "memfile.h" 00021 00022 /* OPIE */ 00023 #include <opie2/odebug.h> 00024 00025 /* STD */ 00026 #include <unistd.h> 00027 #include <sys/mman.h> 00028 00029 MemFile::MemFile() 00030 { 00031 } 00032 00033 MemFile::MemFile( const QString &name ) 00034 : QFile( name ) 00035 { 00036 } 00037 00038 MemFile::~MemFile() 00039 { 00040 close(); 00041 } 00042 00043 void MemFile::close() 00044 { 00045 unmap(); 00046 QFile::close(); 00047 } 00048 00049 void MemFile::unmap() 00050 { 00051 #if defined(Q_WS_X11) || defined(Q_WS_QWS) 00052 if ( m_data.data() ) 00053 { 00054 munmap( m_data.data(), m_data.size() ); 00055 m_data.resetRawData( m_data.data(), m_data.size() ); 00056 } 00057 #endif 00058 } 00059 00060 QByteArray &MemFile::data() 00061 { 00062 if ( !m_data.data() ) 00063 { 00064 #if defined(Q_WS_X11) || defined(Q_WS_QWS) 00065 const char *rawData = (const char *)mmap( 0, size(), PROT_READ, 00066 MAP_SHARED, handle(), 0 ); 00067 if ( rawData ) 00068 { 00069 m_data.setRawData( rawData, size() ); 00070 return m_data; 00071 } 00072 else 00073 odebug << "MemFile: mmap() failed!" << oendl; 00074 // fallback 00075 #endif 00076 m_data = readAll(); 00077 } 00078 return m_data; 00079 } 00080
1.4.2