More simplifications

This commit is contained in:
Sergio Martin 2024-01-22 19:30:14 +01:00
parent 9802883fee
commit 7164094e95
12 changed files with 7 additions and 404 deletions

1
extern/hqn/hqn.cpp vendored
View File

@ -1,7 +1,6 @@
#include <cmath>
#include <SDL_timer.h>
#include "hqn.h"
#include "hqn_util.h"
namespace hqn
{

10
extern/hqn/hqn.h vendored
View File

@ -3,19 +3,11 @@
#include <cstdint>
#include <stdio.h>
#include <emuInstance.hpp>
#define BLIT_SIZE 65536
// Creating emulator instance
#ifdef _USE_QUICKNES
#include <Nes_Emu.hpp>
typedef Nes_Emu emulator_t;
#endif
#ifdef _USE_QUICKERNES
#include <emu.hpp>
typedef quickerNES::Emu emulator_t;
#endif
namespace hqn
{

View File

@ -1,5 +1,4 @@
#include "hqn_gui_controller.h"
#include "hqn_util.h"
#include <algorithm>
#include <cstring>
#include <SDL.h>

View File

@ -1,95 +0,0 @@
#include "hqn_util.h"
#include <cctype>
#include <cstdio>
#include <sys/stat.h>
namespace hqn
{
bool fileSize(const char *filename, size_t *size)
{
struct stat s;
if (stat(filename, &s) == 0)
{
*size = s.st_size;
return true;
}
else
{
*size = 0;
return false;
}
}
bool load_file(const char *filename, char **data, size_t *size)
{
char *dataRef = nullptr;
char *dataInsert;
size_t dataSize;
size_t readAmount;
FILE *fd;
if (!fileSize(filename, &dataSize))
{ goto read_failed; }
dataRef = new char[dataSize];
dataInsert = dataRef;
readAmount = 0; // how many bytes we read
fd = fopen(filename, "rb");
if (!fd)
{ goto read_failed; }
do
{
readAmount = fread(dataInsert, 1, dataSize - (dataInsert - dataRef), fd);
dataInsert += readAmount;
} while (readAmount != 0);
fclose(fd);
*data = dataRef;
*size = dataSize;
return true;
// Jump here if we failed to open the file.
read_failed:
if (dataRef)
{ delete[] dataRef; }
*data = nullptr;
*size = 0;
return false;
}
bool save_file(const char *filename, const char *data, size_t size)
{
FILE *fd = fopen(filename, "wb");
if (!fd)
{ return false; }
if (fwrite(data, 1, size, fd) != size)
{
fclose(fd);
return false;
}
fclose(fd);
return true;
}
bool file_exists(const char *filename)
{
struct stat s;
return stat(filename, &s) == 0;
}
int stricmp(char const *a, char const *b)
{
for (;; a++, b++) {
int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a)
return d;
}
}
}

38
extern/hqn/hqn_util.h vendored
View File

@ -1,38 +0,0 @@
#ifndef __HQN_UTIL_H__
#define __HQN_UTIL_H__
#include <cstdint>
#include <cstdlib>
namespace hqn
{
/*
Get the size of the given file in bytes and store it in size.
Returns false if the file could not be read.
*/
bool fileSize(const char *filename, size_t *size);
/*
Load the named file into ram. Allocates a buffer using new[].
The caller is responsible for calling delete[] on the buffer.
*/
bool load_file(const char *filename, char **data, size_t *size);
/*
Save the data into the named file.
*/
bool save_file(const char *filename, const char *data, size_t size);
/*
Return true if the file exists and false otherwise.
*/
bool file_exists(const char *filename);
/* Compare ascii strings in a case-insensitive manner. */
int stricmp(char const *a, char const *b);
}
#endif /* __HQN_UTIL_H__ */

View File

@ -1,17 +0,0 @@
# Source files and include dirs for HQN
srcDir = meson.current_source_dir()
hqnSrc = [
srcDir + '/src/hqn.cpp',
srcDir + '/src/hqn_gui_controller.cpp',
srcDir + '/src/hqn_surface.cpp',
srcDir + '/src/hqn_util.cpp',
srcDir + '/src/options.cpp'
]
hqnIncludes = include_directories([
'src'
])
hqnCFlags = [ ]

158
extern/hqn/options.cpp vendored
View File

@ -1,158 +0,0 @@
#include "options.h"
#include <cstdio>
#include <cctype>
#define MAX_LINE 2048
// Remove whitespace around a string
std::string trim(const std::string &in)
{
size_t first = in.find_first_not_of(" ");
size_t last = in.find_last_not_of(" ");
if (first == std::string::npos || last == std::string::npos)
{
return in;
}
else
{
return in.substr(first, last - first + 1);
}
}
Options::Options()
{}
Options::~Options()
{}
int Options::load(const std::string &filename)
{
FILE *fd;
char line[MAX_LINE];
int lineno = 1;
fd = fopen(filename.c_str(), "r");
if (!fd)
{
return -1;
}
while (fgets(line, MAX_LINE, fd) != nullptr)
{
// split line at the = sign
std::string ln = trim(line);
size_t eqPos = ln.find("=");
if (eqPos == std::string::npos)
{
// clean up and exit
fclose(fd);
return lineno;
}
else
{
std::string key = trim(ln.substr(0, eqPos - 1));
std::string value = trim(ln.substr(eqPos + 1));
m_data[key] = value;
}
lineno++;
}
fclose(fd);
return 0;
}
int Options::save(const std::string &filename)
{
FILE *fd;
fd = fopen(filename.c_str(), "w");
if (!fd)
{
return -1;
}
for (auto it : m_data)
{
fprintf(fd, "%s = %s\n", it.first.c_str(), it.second.c_str());
}
fclose(fd);
return 0;
}
int Options::getInt(const std::string &name, int def)
{
int result;
if (!has(name))
return def;
if (sscanf(m_data[name].c_str(), "%d", &result) != 1)
result = def;
return result;
}
double Options::getNum(const std::string &name, double def)
{
double result;
if (!has(name))
return def;
if (sscanf(m_data[name].c_str(), "%lf", &result) != 1)
result = def;
return result;
}
bool Options::getBool(const std::string &name, bool def)
{
if (!has(name))
return def;
// test for true/false strings
const std::string &value = m_data[name];
if (string_icompare(value, "true") == 0)
return true;
if (string_icompare(value, "false") == 0)
return false;
// try to convert to an int and use that as a bool
size_t pos;
int asInt = std::stoi(value, &pos);
if (pos)
return (bool)asInt;
// fall back to default
return def;
}
const std::string &Options::getString(const std::string &name, const std::string &def)
{
if (!has(name))
return def;
return m_data[name];
}
void Options::set(const std::string &name, const std::string &value)
{
m_data[name] = value;
}
void Options::set(const std::string &name, double value)
{
m_data[name] = std::to_string(value);
}
void Options::set(const std::string &name, bool value)
{
m_data[name] = value ? "true" : "false";
}
bool Options::has(const std::string &name)
{
return m_data.find(name) != m_data.end();
}
int string_icompare(const std::string &a, const std::string &b)
{
auto itA = a.begin();
auto itB = b.begin();
while (itA != a.end() && itB != b.end())
{
int diff = toupper(*itA) - toupper(*itB);
if (diff)
return diff;
itA++;
itB++;
}
return 0;
}

77
extern/hqn/options.h vendored
View File

@ -1,77 +0,0 @@
#ifndef __OPTIONS_H__
#define __OPTIONS_H__
#include <string>
#include <map>
/**
* Compare two strings case-insensitively.
*/
int string_icompare(const std::string& a, const std::string &b);
/**
* Holds options provides an interface to load them
* and save them.
*/
class Options
{
public:
Options();
~Options();
/**
* Tries to load options from the filename.
* If the file cannot be opened returns -1,
* if there is an error returns the line the error was on (starting at 1).
* Otherwise returns 0.
*/
int load(const std::string &filename);
/**
* Save the options to the named file.
* If all goes well returns 0 otherwise returns an error code.
*
* Errors:
* -1 = failed to open file
*/
int save(const std::string &filename);
/**
* Get a named integer or return the default value.
*/
int getInt(const std::string &name, int def=0);
/**
* Get the option as a double.
*/
double getNum(const std::string &name, double def=0.0);
/**
* Get the named option as a boolean or use the default value.
*/
bool getBool(const std::string &name, bool def=false);
/**
* Get the given option as a string, or return the default value.
* @param name the name of the option to get
* @param def default value if name is not found
*/
const std::string &getString(const std::string &name, const std::string &def="");
/**
* Check if the option set has the given option.
*/
bool has(const std::string &name);
/**
* Set a value in the options.
*/
void set(const std::string &name, const std::string &value);
void set(const std::string &name, double value);
void set(const std::string &name, bool value);
private:
std::map<std::string, std::string> m_data;
};
#endif // __OPTIONS_H__

View File

@ -19,8 +19,6 @@ quickerNESPlayerSrc = [
'extern/hqn/hqn.cpp',
'extern/hqn/hqn_gui_controller.cpp',
'extern/hqn/hqn_surface.cpp',
'extern/hqn/hqn_util.cpp',
'extern/hqn/options.cpp',
]
if get_option('buildPlayer') == true

View File

@ -71,7 +71,7 @@ quickNESSrc = [
# quickNES Core Configuration
quickNESDependency = declare_dependency(
compile_args : [ '-D_USE_QUICKNES' ],
compile_args : [ ],
include_directories : include_directories(['.', 'core/nes_emu']),
sources : [ quickNESSrc ]
)

View File

@ -31,7 +31,7 @@ quickerNESSrc = quickerNESAPUSrc + quickerNESPPUSrc + [
# quickerNES Core Configuration
quickerNESDependency = declare_dependency(
compile_args : [ '-D_USE_QUICKERNES', '-D_PAGE_SIZE="' + pageSize.to_string() + '"'],
compile_args : [ '-D_PAGE_SIZE="' + pageSize.to_string() + '"'],
include_directories : include_directories(['.', 'core']),
sources : [ quickerNESSrc ]
)

View File

@ -57,7 +57,7 @@ inline int getKeyPress()
return getch();
}
void initializeTerminal()
inline void initializeTerminal()
{
// Initializing ncurses screen
initscr();
@ -67,17 +67,17 @@ void initializeTerminal()
scrollok(stdscr, TRUE);
}
void clearTerminal()
inline void clearTerminal()
{
clear();
}
void finalizeTerminal()
inline void finalizeTerminal()
{
endwin();
}
void refreshTerminal()
inline void refreshTerminal()
{
refresh();
}