core: use C++ std::strings for default directory and filename

The memory management and string concatenation was hard to follow.

Since the mobile files ios.cpp and android.cpp were already
converted to C++, let's do the same for Unix, Windows and MacOS.

Simply store the default directory and filename in a function-level
static string. Thus, it will be initialized on first call and
freed on application exit. Since the std::string data is
guaranteed to be contiguous and zero-terminated, it can be used
from C code.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-04-29 11:43:17 +02:00 committed by bstoeger
parent 7771b444a1
commit 540cba07f3
8 changed files with 167 additions and 225 deletions

View file

@ -17,9 +17,21 @@
#include <fcntl.h>
#include <unistd.h>
#include <zip.h>
#include <string>
#include <QStandardPaths>
static std::string system_default_path()
{
// Qt appears to find a working path for us - let's just go with that
return QStandardPaths::standardLocations(QStandardPaths::DataLocation).first().toStdString();
}
static std::string make_default_filename()
{
return system_default_path() + "/subsurface.xml";
}
extern "C" {
const char mac_system_divelist_default_font[] = "Arial";
@ -37,32 +49,16 @@ bool subsurface_ignore_font(const char*)
return false;
}
static const char *system_default_path_append(const char *append)
{
// Qt appears to find a working path for us - let's just go with that
QString path = QStandardPaths::standardLocations(QStandardPaths::DataLocation).first();
if (append)
path += QString("/%1").arg(append);
return copy_qstring(path);
}
const char *system_default_directory(void)
{
static const char *path = NULL;
if (!path)
path = system_default_path_append(NULL);
return path;
static const std::string path = system_default_path();
return path.c_str();
}
const char *system_default_filename(void)
{
static const char *filename = "subsurface.xml";
static const char *path = NULL;
if (!path)
path = system_default_path_append(filename);
return path;
static const std::string fn = make_default_filename();
return fn.c_str();
}
int enumerate_devices(device_callback_t, void *, unsigned int)