<commit_before>// Copyright (C) 2005  <NAME>, <NAME>
// Copyright (C) 2005  <NAME>
// Copyright (C) 1999  <NAME>
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//

// TODO:
// - NA
//

// wxwidgets headers
#include <wx/wx.h>
#include <wx/wfstream.h>
#include <wx/except.h>

#include "plDevs.h"

// plplot headers
#include "plplotP.h"
#include "drivers.h"

// C/C++ headers
#include <cstdio>

#include "wxwidgets.h"


// private functions needed by the wxwidgets Driver
//static void fill_polygon( PLStream *pls );

#ifdef __WXMAC__
        #include <Carbon/Carbon.h>
extern "C" { void CPSEnableForegroundOperation( ProcessSerialNumber* psn ); }
#endif


//--------------------------------------------------------------------------
//  void Log_Verbose( const char *fmt, ... )
//
//  Print verbose debug message to stderr (printf style).
//--------------------------------------------------------------------------
void Log_Verbose( const char *fmt, ... )
{
#ifdef _DEBUG_VERBOSE
    va_list args;
    va_start( args, fmt );
    fprintf( stderr, "Verbose: " );
    vfprintf( stderr, fmt, args );
    fprintf( stderr, "\n" );
    va_end( args );
    fflush( stderr );
#else
    (void) fmt;        // Cast to void to silence compiler warnings about unused paraemeter
#endif
}


//--------------------------------------------------------------------------
//  void Log_Debug( const char *fmt, ... )
//
//  Print debug message to stderr (printf style).
//--------------------------------------------------------------------------
void Log_Debug( const char *fmt, ... )
{
#ifdef _DEBUG
    va_list args;
    va_start( args, fmt );
    fprintf( stderr, "Debug: " );
    vfprintf( stderr, fmt, args );
    fprintf( stderr, "\n" );
    va_end( args );
    fflush( stderr );
#else
    (void) fmt;        // Cast to void to silence compiler warnings about unused paraemeter
#endif
}


//--------------------------------------------------------------------------
//  In the following you'll find the driver functions which are
//  are needed by the plplot core.
//--------------------------------------------------------------------------

// Device info
#ifdef __cplusplus
extern "C" {
#endif

PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_wxwidgets =
#ifdef PLD_wxwidgets
    "wxwidgets:wxWidgets Driver:1:wxwidgets:51:wxwidgets\n"
#endif
#ifdef PLD_wxpng
    "wxpng:wxWidgets PNG Driver:0:wxwidgets:52:wxpng\n"
#endif
;

#ifdef __cplusplus
}
#endif

PLMemoryMap::PLMemoryMap()
{
#ifdef WIN32
	m_mapFile=NULL;
#else
	m_mapFile = -1;
	m_name = NULL;
#endif
	m_buffer=NULL;
}
PLMemoryMap::PLMemoryMap( const char *name, PLINT size, bool onlyIfExists )
{
#ifdef WIN32
	m_mapFile = NULL;
#else
	m_mapFile = -1;
	m_name = NULL;
#endif
	m_buffer=NULL;
	create( name, size, onlyIfExists );
}
void PLMemoryMap::create( const char *name, PLINT size, bool onlyIfExists )
{
	close();

#ifdef WIN32
	if( onlyIfExists )
		m_mapFile = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, name);
	else
		m_mapFile = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL,
			PAGE_READWRITE, 0, size, name );

	if( m_mapFile )
		m_buffer = MapViewOfFile( m_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, size );
#else
	if( onlyIfExists )
		m_mapFile = shm_open( name, O_RDWR, 0 );
	else
		m_mapFile = shm_open( name, O_RDWR|O_CREAT, S_IRWXU ); //S_IRWXU gives user wrx permissions
	if( m_mapFile != -1 )
	{
		if ( ftruncate( m_mapFile, size ) != -1 )
		{
			m_buffer = mmap( NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, m_mapFile, 0 );
			m_size = size;
		}
		m_name = new char[strlen( name ) + 1];
		strcpy( m_name, name );
	}
#endif
}
void PLMemoryMap::close()
{
#ifdef WIN32
	if( m_buffer )
		UnmapViewOfFile( m_buffer );
	if( m_mapFile )<commit_msg>removed ftruncate call when creating memory map in wxwidgets.cpp with onlyIfExists set to true.<commit_after>// Copyright (C) 2005  <NAME>, <NAME>
// Copyright (C) 2005  <NAME>
// Copyright (C) 1999  <NAME>
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//

// TODO:
// - NA
//

// wxwidgets headers
#include <wx/wx.h>
#include <wx/wfstream.h>
#include <wx/except.h>

#include "plDevs.h"

// plplot headers
#include "plplotP.h"
#include "drivers.h"

// C/C++ headers
#include <cstdio>

#include "wxwidgets.h"


// private functions needed by the wxwidgets Driver
//static void fill_polygon( PLStream *pls );

#ifdef __WXMAC__
        #include <Carbon/Carbon.h>
extern "C" { void CPSEnableForegroundOperation( ProcessSerialNumber* psn ); }
#endif


//--------------------------------------------------------------------------
//  void Log_Verbose( const char *fmt, ... )
//
//  Print verbose debug message to stderr (printf style).
//--------------------------------------------------------------------------
void Log_Verbose( const char *fmt, ... )
{
#ifdef _DEBUG_VERBOSE
    va_list args;
    va_start( args, fmt );
    fprintf( stderr, "Verbose: " );
    vfprintf( stderr, fmt, args );
    fprintf( stderr, "\n" );
    va_end( args );
    fflush( stderr );
#else
    (void) fmt;        // Cast to void to silence compiler warnings about unused paraemeter
#endif
}


//--------------------------------------------------------------------------
//  void Log_Debug( const char *fmt, ... )
//
//  Print debug message to stderr (printf style).
//--------------------------------------------------------------------------
void Log_Debug( const char *fmt, ... )
{
#ifdef _DEBUG
    va_list args;
    va_start( args, fmt );
    fprintf( stderr, "Debug: " );
    vfprintf( stderr, fmt, args );
    fprintf( stderr, "\n" );
    va_end( args );
    fflush( stderr );
#else
    (void) fmt;        // Cast to void to silence compiler warnings about unused paraemeter
#endif
}


//--------------------------------------------------------------------------
//  In the following you'll find the driver functions which are
//  are needed by the plplot core.
//--------------------------------------------------------------------------

// Device info
#ifdef __cplusplus
extern "C" {
#endif

PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_wxwidgets =
#ifdef PLD_wxwidgets
    "wxwidgets:wxWidgets Driver:1:wxwidgets:51:wxwidgets\n"
#endif
#ifdef PLD_wxpng
    "wxpng:wxWidgets PNG Driver:0:wxwidgets:52:wxpng\n"
#endif
;

#ifdef __cplusplus
}
#endif

PLMemoryMap::PLMemoryMap()
{
#ifdef WIN32
	m_mapFile=NULL;
#else
	m_mapFile = -1;
	m_name = NULL;
#endif
	m_buffer=NULL;
}
PLMemoryMap::PLMemoryMap( const char *name, PLINT size, bool onlyIfExists )
{
#ifdef WIN32
	m_mapFile = NULL;
#else
	m_mapFile = -1;
	m_name = NULL;
#endif
	m_buffer=NULL;
	create( name, size, onlyIfExists );
}
void PLMemoryMap::create( const char *name, PLINT size, bool onlyIfExists )
{
	close();

#ifdef WIN32
	if( onlyIfExists )
		m_mapFile = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, name);
	else
		m_mapFile = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL,
			PAGE_READWRITE, 0, size, name );

	if( m_mapFile )
		m_buffer = MapViewOfFile( m_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, size );
#else
	if( onlyIfExists )
		m_mapFile = shm_open( name, O_RDWR, 0 );
	else
	{
		m_mapFile = shm_open( name, O_RDWR|O_CREAT, S_IRWXU ); //S_IRWXU gives user wrx permissions
		if( ftruncate( m_mapFile, size ) == -1 )
			close( );
	}
	if( m_mapFile != -1 )
	{
		m_buffer = mmap( NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, m_mapFile, 0 );
		m_size = size;
		m_name = new char[strlen( name ) + 1];
		strcpy( m_name, name );
	}
#endif
}
void PLMemoryMap::close()
{
#ifdef WIN32
	if( m_buffer )
		UnmapViewOfFile( m_buffer );
	if( m_mapFile )
{'fraction_non_alphanumeric': 0.20903384891279983, 'fraction_numerical': 0.010759919300605245, 'mean_word_length': 2.3269947800149144, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '75897', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2862, 'n_words': 920}