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

@ -12,6 +12,7 @@
#include <errno.h>
#include <unistd.h>
#include <zip.h>
#include <string>
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>
@ -26,9 +27,20 @@
#define LOG(x) qDebug() << x;
#endif
#define USB_SERVICE "usb"
static std::string system_default_path()
{
// Qt appears to find a working path for us - let's just go with that
// AppDataLocation allows potential sharing of the files we put there
return QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first().toStdString();
}
static std::string make_default_filename()
{
return system_default_path() + "/subsurface.xml";
}
extern "C" {
const char android_system_divelist_default_font[] = "Roboto";
@ -46,35 +58,19 @@ bool subsurface_ignore_font(const char *font)
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
// AppDataLocation allows potential sharing of the files we put there
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).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 callback, void *userdata, unsigned int transport)
{
/* FIXME: we need to enumerate in some other way on android */