xml-copy-editor-code/src/binaryfile.cpp

74 lines
1.7 KiB
C++
Raw Normal View History

2009-11-06 18:06:12 +01:00
/*
* Copyright 2005-2007 Gerald Schmidt.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* Xml Copy Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "binaryfile.h"
#include <wx/wfstream.h>
2009-11-06 18:06:12 +01:00
BinaryFile::BinaryFile ( const wxString &fname ) : m_data ( 0 ), m_dataLen ( 0 )
2009-11-06 18:06:12 +01:00
{
wxFileInputStream stream ( fname );
2009-11-06 18:06:12 +01:00
size_t lSize;
char *buffer;
if ( !stream.IsOk() )
2009-11-06 18:06:12 +01:00
{
2009-12-01 14:33:22 +01:00
return;
2009-11-06 18:06:12 +01:00
}
lSize = stream.GetSize();
2009-11-06 18:06:12 +01:00
// allocate memory to contain the whole file:
//buffer = new char[lSize]; // for some reason this is much slower than malloc
buffer = ( char* ) malloc ( sizeof ( char ) *lSize );
if ( buffer == NULL )
{
2009-12-01 14:33:22 +01:00
return;
2009-11-06 18:06:12 +01:00
}
// copy the file into the buffer:
stream.Read ( buffer, lSize );
if ( stream.LastRead() != lSize )
2009-11-06 18:06:12 +01:00
{
if ( !stream.Eof() )
2009-12-01 14:33:22 +01:00
return;
2009-11-06 18:06:12 +01:00
}
/* the whole file is now loaded in the memory buffer. */
m_data = buffer;
m_dataLen = lSize;
}
BinaryFile::~BinaryFile()
{
//delete[] m_data;
free ( m_data );
}
const char *BinaryFile::getData()
{
return ( const char * ) m_data;
}
size_t BinaryFile::getDataLen()
{
return m_dataLen;
}