Refactor Qt GUI function fceuExecutablePath to be more efficient. Since the path to the executable never changes during the lifetime of the process, save it off the first time this function is called for later use. Function interface changed to just pass back a pointer to the saved string. Saves on stack memory and is more efficient since it does not have to continually copy data onto the stack.
This commit is contained in:
parent
b3717c008b
commit
4e54ea54ac
|
@ -21,6 +21,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <QWindow>
|
#include <QWindow>
|
||||||
#include <QScreen>
|
#include <QScreen>
|
||||||
|
@ -47,6 +48,7 @@
|
||||||
#include "Qt/fceuWrapper.h"
|
#include "Qt/fceuWrapper.h"
|
||||||
#include "Qt/ConsoleUtilities.h"
|
#include "Qt/ConsoleUtilities.h"
|
||||||
|
|
||||||
|
static std::string fceuExecPath;
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
int getDirFromFile( const char *path, char *dir, size_t bufSize )
|
int getDirFromFile( const char *path, char *dir, size_t bufSize )
|
||||||
{
|
{
|
||||||
|
@ -229,13 +231,9 @@ int parseFilepath( const char *filepath, char *dir, char *base, char *suffix )
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// Returns the path of fceux.exe as a string.
|
// Returns the path of fceux.exe as a string.
|
||||||
int fceuExecutablePath( char *outputPath, size_t outputSize )
|
static int _fceuExecutablePath( std::string &outputPath )
|
||||||
{
|
{
|
||||||
if ( (outputPath == NULL) || (outputSize <= 0) )
|
outputPath.clear();
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
outputPath[0] = 0;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
char fullPath[2048];
|
char fullPath[2048];
|
||||||
|
@ -246,8 +244,7 @@ int fceuExecutablePath( char *outputPath, size_t outputSize )
|
||||||
GetModuleFileNameA(NULL, fullPath, 2048);
|
GetModuleFileNameA(NULL, fullPath, 2048);
|
||||||
_splitpath(fullPath, driveLetter, directory, NULL, NULL);
|
_splitpath(fullPath, driveLetter, directory, NULL, NULL);
|
||||||
snprintf(finalPath, sizeof(finalPath), "%s%s", driveLetter, directory);
|
snprintf(finalPath, sizeof(finalPath), "%s%s", driveLetter, directory);
|
||||||
strncpy( outputPath, finalPath, outputSize );
|
outputPath.assign( finalPath );
|
||||||
outputPath[outputSize-1] = 0;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#elif __linux__ || __unix__
|
#elif __linux__ || __unix__
|
||||||
|
@ -265,8 +262,7 @@ int fceuExecutablePath( char *outputPath, size_t outputSize )
|
||||||
if ( dir )
|
if ( dir )
|
||||||
{
|
{
|
||||||
//printf("DIR Path: '%s' \n", dir );
|
//printf("DIR Path: '%s' \n", dir );
|
||||||
strncpy( outputPath, dir, outputSize );
|
outputPath.assign( dir );
|
||||||
outputPath[outputSize-1] = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,8 +282,7 @@ int fceuExecutablePath( char *outputPath, size_t outputSize )
|
||||||
if ( dir )
|
if ( dir )
|
||||||
{
|
{
|
||||||
//printf("DIR Path: '%s' \n", dir );
|
//printf("DIR Path: '%s' \n", dir );
|
||||||
strncpy( outputPath, dir, outputSize );
|
outputPath.assign( dir );
|
||||||
outputPath[outputSize-1] = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -295,6 +290,15 @@ int fceuExecutablePath( char *outputPath, size_t outputSize )
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
const char *fceuExecutablePath(void)
|
||||||
|
{
|
||||||
|
if (fceuExecPath.size() == 0)
|
||||||
|
{
|
||||||
|
_fceuExecutablePath( fceuExecPath );
|
||||||
|
}
|
||||||
|
return fceuExecPath.c_str();
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
int fceuLoadConfigColor( const char *confName, QColor *color )
|
int fceuLoadConfigColor( const char *confName, QColor *color )
|
||||||
{
|
{
|
||||||
std::string colorString;
|
std::string colorString;
|
||||||
|
|
|
@ -17,7 +17,7 @@ int getFileBaseName( const char *filepath, char *base, char *suffix = NULL );
|
||||||
|
|
||||||
int parseFilepath( const char *filepath, char *dir, char *base, char *suffix = NULL );
|
int parseFilepath( const char *filepath, char *dir, char *base, char *suffix = NULL );
|
||||||
|
|
||||||
int fceuExecutablePath( char *outputPath, size_t outputSize );
|
const char *fceuExecutablePath(void);
|
||||||
|
|
||||||
int fceuLoadConfigColor( const char *confName, QColor *color );
|
int fceuLoadConfigColor( const char *confName, QColor *color );
|
||||||
|
|
||||||
|
|
|
@ -370,12 +370,12 @@ void GuiConfDialog_t::openQss(void)
|
||||||
QString filename;
|
QString filename;
|
||||||
std::string last, iniPath;
|
std::string last, iniPath;
|
||||||
char dir[512];
|
char dir[512];
|
||||||
char exePath[512];
|
const char *exePath = nullptr;
|
||||||
QFileDialog dialog(this, tr("Open Qt Stylesheet (QSS)"));
|
QFileDialog dialog(this, tr("Open Qt Stylesheet (QSS)"));
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
|
|
||||||
fceuExecutablePath(exePath, sizeof(exePath));
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile(QDir::rootPath());
|
urls << QUrl::fromLocalFile(QDir::rootPath());
|
||||||
|
@ -474,12 +474,12 @@ void GuiConfDialog_t::openQPal(void)
|
||||||
QString filename;
|
QString filename;
|
||||||
std::string last, iniPath;
|
std::string last, iniPath;
|
||||||
char dir[512];
|
char dir[512];
|
||||||
char exePath[512];
|
const char *exePath = nullptr;
|
||||||
QFileDialog dialog(this, tr("Open Qt QPalette File (QPAL)"));
|
QFileDialog dialog(this, tr("Open Qt QPalette File (QPAL)"));
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
|
|
||||||
fceuExecutablePath(exePath, sizeof(exePath));
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile(QDir::rootPath());
|
urls << QUrl::fromLocalFile(QDir::rootPath());
|
||||||
|
@ -1177,13 +1177,13 @@ void GuiPaletteEditDialog_t::paletteSaveAs(void)
|
||||||
QString filename;
|
QString filename;
|
||||||
std::string last, iniPath;
|
std::string last, iniPath;
|
||||||
char dir[512];
|
char dir[512];
|
||||||
char exePath[512];
|
const char *exePath = nullptr;
|
||||||
QFileDialog dialog(this, tr("Save QPalette (qpal)"));
|
QFileDialog dialog(this, tr("Save QPalette (qpal)"));
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
QPalette pal = this->palette();
|
QPalette pal = this->palette();
|
||||||
|
|
||||||
fceuExecutablePath(exePath, sizeof(exePath));
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile(QDir::rootPath());
|
urls << QUrl::fromLocalFile(QDir::rootPath());
|
||||||
|
|
|
@ -280,13 +280,13 @@ void LuaControlDialog_t::openLuaScriptFile(void)
|
||||||
QString filename;
|
QString filename;
|
||||||
std::string last;
|
std::string last;
|
||||||
char dir[2048];
|
char dir[2048];
|
||||||
char exePath[2048];
|
const char *exePath = nullptr;
|
||||||
const char *luaPath;
|
const char *luaPath = nullptr;
|
||||||
QFileDialog dialog(this, tr("Open LUA Script"));
|
QFileDialog dialog(this, tr("Open LUA Script"));
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
|
|
||||||
fceuExecutablePath(exePath, sizeof(exePath));
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile(QDir::rootPath());
|
urls << QUrl::fromLocalFile(QDir::rootPath());
|
||||||
|
|
|
@ -495,12 +495,12 @@ void PaletteConfDialog_t::openPaletteFile(void)
|
||||||
QString filename;
|
QString filename;
|
||||||
std::string last, iniPath;
|
std::string last, iniPath;
|
||||||
char dir[512];
|
char dir[512];
|
||||||
char exePath[512];
|
const char *exePath = nullptr;
|
||||||
QFileDialog dialog(this, tr("Open NES Palette"));
|
QFileDialog dialog(this, tr("Open NES Palette"));
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
|
|
||||||
fceuExecutablePath(exePath, sizeof(exePath));
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile(QDir::rootPath());
|
urls << QUrl::fromLocalFile(QDir::rootPath());
|
||||||
|
|
|
@ -315,13 +315,13 @@ void PaletteEditorDialog_t::openPaletteFileDialog(void)
|
||||||
int ret, useNativeFileDialogVal;
|
int ret, useNativeFileDialogVal;
|
||||||
QString filename;
|
QString filename;
|
||||||
char dir[512];
|
char dir[512];
|
||||||
char exePath[512];
|
const char *exePath = nullptr;
|
||||||
std::string last, iniPath;
|
std::string last, iniPath;
|
||||||
QFileDialog dialog(this, tr("Open Palette From File") );
|
QFileDialog dialog(this, tr("Open Palette From File") );
|
||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QDir d;
|
QDir d;
|
||||||
|
|
||||||
fceuExecutablePath( exePath, sizeof(exePath) );
|
exePath = fceuExecutablePath();
|
||||||
|
|
||||||
//urls = dialog.sidebarUrls();
|
//urls = dialog.sidebarUrls();
|
||||||
urls << QUrl::fromLocalFile( QDir::rootPath() );
|
urls << QUrl::fromLocalFile( QDir::rootPath() );
|
||||||
|
|
Loading…
Reference in New Issue