Use std::vector<string> instead of QStringList in main()

In an effort to convert core to C++ structures.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-03-25 10:58:27 +01:00 committed by bstoeger
parent c6cd10a43f
commit 4af2ec88bd
8 changed files with 80 additions and 58 deletions

View file

@ -44,6 +44,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/file.cpp \ core/file.cpp \
core/fulltext.cpp \ core/fulltext.cpp \
core/subsurfacestartup.cpp \ core/subsurfacestartup.cpp \
core/subsurface-string.cpp \
core/pref.c \ core/pref.c \
core/profile.cpp \ core/profile.cpp \
core/device.cpp \ core/device.cpp \

View file

@ -172,6 +172,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
string-format.cpp string-format.cpp
strtod.c strtod.c
subsurface-float.h subsurface-float.h
subsurface-string.cpp
subsurface-string.h subsurface-string.h
subsurfacestartup.cpp subsurfacestartup.cpp
subsurfacestartup.h subsurfacestartup.h

View file

@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0
#include "subsurface-string.h"
std::string join(const std::vector<std::string> &l, const std::string &separator, bool skip_empty)
{
std::string res;
bool first = true;
for (const std::string &s: l) {
if (skip_empty && l.empty())
continue;
if (!first)
res += separator;
res += s;
first = false;
}
return res;
}

View file

@ -66,6 +66,7 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags
} }
#include <string> #include <string>
#include <vector>
// Sadly, starts_with only with C++20! // Sadly, starts_with only with C++20!
inline bool starts_with(const std::string &s, const char *s2) inline bool starts_with(const std::string &s, const char *s2)
@ -73,6 +74,8 @@ inline bool starts_with(const std::string &s, const char *s2)
return s.rfind(s2, 0) == 0; return s.rfind(s2, 0) == 0;
} }
std::string join(const std::vector<std::string> &l, const std::string &separator, bool skip_empty = false);
#endif #endif
#endif // SUBSURFACE_STRING_H #endif // SUBSURFACE_STRING_H

View file

@ -376,13 +376,13 @@ void MainWindow::on_actionOpen_triggered()
// some file dialogs decide to add the default extension to a filename without extension // some file dialogs decide to add the default extension to a filename without extension
// so we would get dir[branch].ssrf when trying to select dir[branch]. // so we would get dir[branch].ssrf when trying to select dir[branch].
// let's detect that and remove the incorrect extension // let's detect that and remove the incorrect extension
QStringList cleanFilenames; std::vector<std::string> cleanFilenames;
QRegularExpression reg(".*\\[[^]]+]\\.ssrf", QRegularExpression::CaseInsensitiveOption); QRegularExpression reg(".*\\[[^]]+]\\.ssrf", QRegularExpression::CaseInsensitiveOption);
for (QString filename: filenames) { for (QString filename: filenames) {
if (reg.match(filename).hasMatch()) if (reg.match(filename).hasMatch())
filename.remove(QRegularExpression("\\.ssrf$", QRegularExpression::CaseInsensitiveOption)); filename.remove(QRegularExpression("\\.ssrf$", QRegularExpression::CaseInsensitiveOption));
cleanFilenames << filename; cleanFilenames.push_back(filename.toStdString());
} }
loadFiles(cleanFilenames); loadFiles(cleanFilenames);
} }
@ -399,6 +399,11 @@ void MainWindow::on_actionSaveAs_triggered()
file_save_as(); file_save_as();
} }
static std::string encodeFileName(const std::string &fn)
{
return QFile::encodeName(QString::fromStdString(fn)).toStdString();
}
void MainWindow::on_actionCloudstorageopen_triggered() void MainWindow::on_actionCloudstorageopen_triggered()
{ {
if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file."))) if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file.")))
@ -414,9 +419,9 @@ void MainWindow::on_actionCloudstorageopen_triggered()
closeCurrentFile(); closeCurrentFile();
showProgressBar(); showProgressBar();
QByteArray fileNamePtr = QFile::encodeName(QString::fromStdString(*filename)); std::string encoded = encodeFileName(*filename);
if (!parse_file(fileNamePtr.data(), &divelog)) if (!parse_file(encoded.c_str(), &divelog))
setCurrentFile(fileNamePtr.toStdString()); setCurrentFile(encoded);
process_loaded_dives(); process_loaded_dives();
hideProgressBar(); hideProgressBar();
refreshDisplay(); refreshDisplay();
@ -1156,7 +1161,7 @@ void MainWindow::recentFileTriggered(bool)
updateLastUsedDir(QFileInfo(filename).dir().path()); updateLastUsedDir(QFileInfo(filename).dir().path());
closeCurrentFile(); closeCurrentFile();
loadFiles(QStringList() << filename); loadFiles(std::vector<std::string> { filename.toStdString() });
} }
int MainWindow::file_save_as(void) int MainWindow::file_save_as(void)
@ -1280,33 +1285,32 @@ void MainWindow::setTitle()
setWindowTitle("Subsurface: " + displayedFilename(existing_filename) + unsaved + shown); setWindowTitle("Subsurface: " + displayedFilename(existing_filename) + unsaved + shown);
} }
void MainWindow::importFiles(const QStringList &fileNames) void MainWindow::importFiles(const std::vector<std::string> &fileNames)
{ {
if (fileNames.isEmpty()) if (fileNames.empty())
return; return;
QByteArray fileNamePtr;
struct divelog log; struct divelog log;
for (int i = 0; i < fileNames.size(); ++i) { for (const std::string &fn: fileNames) {
fileNamePtr = QFile::encodeName(fileNames.at(i)); std::string encoded = encodeFileName(fn);
parse_file(fileNamePtr.data(), &log); parse_file(encoded.c_str(), &log);
} }
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files"); QString source = fileNames.size() == 1 ? QString::fromStdString(fileNames[0]) : tr("multiple files");
Command::importDives(&log, IMPORT_MERGE_ALL_TRIPS, source); Command::importDives(&log, IMPORT_MERGE_ALL_TRIPS, source);
} }
void MainWindow::loadFiles(const QStringList &fileNames) void MainWindow::loadFiles(const std::vector<std::string> &fileNames)
{ {
if (fileNames.isEmpty()) { if (fileNames.empty()) {
refreshDisplay(); refreshDisplay();
return; return;
} }
QByteArray fileNamePtr; QByteArray fileNamePtr;
showProgressBar(); showProgressBar();
for (int i = 0; i < fileNames.size(); ++i) { for (const std::string &fn: fileNames) {
fileNamePtr = QFile::encodeName(fileNames.at(i)); fileNamePtr = QFile::encodeName(QString::fromStdString(fn));
if (!parse_file(fileNamePtr.data(), &divelog)) { if (!parse_file(fileNamePtr.data(), &divelog)) {
setCurrentFile(fileNamePtr.toStdString()); setCurrentFile(fileNamePtr.toStdString());
addRecentFile(fileNamePtr, false); addRecentFile(fileNamePtr, false);
@ -1352,13 +1356,13 @@ void MainWindow::on_actionImportDiveLog_triggered()
return; return;
updateLastUsedDir(QFileInfo(fileNames[0]).dir().path()); updateLastUsedDir(QFileInfo(fileNames[0]).dir().path());
QStringList logFiles; std::vector<std::string> logFiles;
QStringList csvFiles; QStringList csvFiles;
for (const QString &fn: fileNames) { for (const QString &fn: fileNames) {
if (isCsvFile(fn)) if (isCsvFile(fn))
csvFiles.append(fn); csvFiles.append(fn);
else else
logFiles.append(fn); logFiles.push_back(fn.toStdString());
} }
if (logFiles.size()) if (logFiles.size())

View file

@ -67,8 +67,8 @@ public:
Count Count
}; };
void loadFiles(const QStringList &files); void loadFiles(const std::vector<std::string> &files);
void importFiles(const QStringList &importFiles); void importFiles(const std::vector<std::string> &importFiles);
void setToolButtonsEnabled(bool enabled); void setToolButtonsEnabled(bool enabled);
void setApplicationState(ApplicationState state); void setApplicationState(ApplicationState state);
void enterPreviousState(); void enterPreviousState();

View file

@ -13,6 +13,7 @@
#include "core/qt-gui.h" #include "core/qt-gui.h"
#include "core/qthelper.h" #include "core/qthelper.h"
#include "core/subsurfacestartup.h" #include "core/subsurfacestartup.h"
#include "core/subsurface-string.h"
#include "core/settings/qPref.h" #include "core/settings/qPref.h"
#include "core/tag.h" #include "core/tag.h"
#include "desktop-widgets/mainwindow.h" #include "desktop-widgets/mainwindow.h"
@ -37,23 +38,22 @@ int main(int argc, char **argv)
if (verbose) /* print the version if the Win32 console_init() code enabled verbose. */ if (verbose) /* print the version if the Win32 console_init() code enabled verbose. */
print_version(); print_version();
int i;
bool no_filenames = true; bool no_filenames = true;
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
std::unique_ptr<QApplication> app(new QApplication(argc, argv)); std::unique_ptr<QApplication> app(new QApplication(argc, argv));
QStringList files; std::vector<std::string> files;
QStringList importedFiles; std::vector<std::string> importedFiles;
QStringList arguments = QCoreApplication::arguments(); QStringList arguments = QCoreApplication::arguments();
const char *default_directory = system_default_directory(); const char *default_directory = system_default_directory();
subsurface_mkdir(default_directory); subsurface_mkdir(default_directory);
for (i = 1; i < arguments.length(); i++) { for (int i = 1; i < arguments.length(); i++) {
QString a = arguments.at(i); std::string a = arguments[i].toStdString();
if (a.isEmpty()) if (a.empty())
continue; continue;
if (a.at(0) == '-') { if (a[0] == '-') {
parse_argument(qPrintable(a)); parse_argument(a.c_str());
continue; continue;
} }
if (imported) { if (imported) {
@ -85,21 +85,20 @@ int main(int argc, char **argv)
init_ui(); init_ui();
if (no_filenames) { if (no_filenames) {
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) { if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
QString defaultFile(prefs.default_filename); if (!empty_string(prefs.default_filename))
if (!defaultFile.isEmpty()) files.emplace_back(prefs.default_filename ? prefs.default_filename : "");
files.push_back(QString(prefs.default_filename));
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) { } else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
auto cloudURL = getCloudURL(); auto cloudURL = getCloudURL();
if (cloudURL) if (cloudURL)
files.push_back(QString::fromStdString(*cloudURL)); files.push_back(*cloudURL);
} }
} }
MainWindow *m = MainWindow::instance(); MainWindow *m = MainWindow::instance();
if (verbose && !files.isEmpty()) if (verbose && !files.empty())
qDebug() << "loading dive data from" << files; report_info("loading dive data from: %s", join(files, std::string(", ")).c_str());
m->loadFiles(files); m->loadFiles(files);
if (verbose && !importedFiles.isEmpty()) if (verbose && !importedFiles.empty())
qDebug() << "importing dive data from" << importedFiles; report_info("importing dive data from %s", join(importedFiles, std::string(", ")).c_str());
m->importFiles(importedFiles); m->importFiles(importedFiles);
if (verbose > 0) if (verbose > 0)

View file

@ -36,11 +36,10 @@ int main(int argc, char **argv)
// supporting BT makes sense when used with an iPhone and an rfcomm BT device? // supporting BT makes sense when used with an iPhone and an rfcomm BT device?
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
int i;
bool no_filenames = true; bool no_filenames = true;
std::unique_ptr<QCoreApplication> app(new QCoreApplication(argc, argv)); std::unique_ptr<QCoreApplication> app(new QCoreApplication(argc, argv));
QStringList files; std::vector<std::string> files;
QStringList importedFiles; std::vector<std::string> importedFiles;
QStringList arguments = QCoreApplication::arguments(); QStringList arguments = QCoreApplication::arguments();
// set a default logfile name for libdivecomputer so we always get a logfile // set a default logfile name for libdivecomputer so we always get a logfile
@ -60,12 +59,12 @@ int main(int argc, char **argv)
// now handle the arguments // now handle the arguments
fill_computer_list(); fill_computer_list();
for (i = 1; i < arguments.length(); i++) { for (int i = 1; i < arguments.length(); i++) {
QString a = arguments.at(i); std::string a = arguments[i].toStdString();
if (a.isEmpty()) if (a.empty())
continue; continue;
if (a.at(0) == '-') { if (a[0] == '-') {
parse_argument(qPrintable(a)); parse_argument(a.c_str());
continue; continue;
} }
if (imported) { if (imported) {
@ -80,19 +79,18 @@ int main(int argc, char **argv)
if (no_filenames) { if (no_filenames) {
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) { if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
QString defaultFile(prefs.default_filename); if (!empty_string(prefs.default_filename))
if (!defaultFile.isEmpty()) files.emplace_back(prefs.default_filename ? prefs.default_filename : "");
files.push_back(QString(prefs.default_filename));
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) { } else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
auto cloudURL = getCloudURL(); auto cloudURL = getCloudURL();
if (cloudURL) if (cloudURL)
files.push_back(QString::fromStdString(*cloudURL)); files.emplace_back(*cloudURL);
} }
} }
if (!files.isEmpty()) { if (!files.empty()) {
qDebug() << "loading dive data from" << files; report_info("loading dive data from %s", join(files, ", ").c_str());
if (parse_file(qPrintable(files.first()), &divelog) < 0) { if (parse_file(files.front().c_str(), &divelog) < 0) {
printf("Failed to load dives from file '%s', aborting.\n", qPrintable(files.first())); printf("Failed to load dives from file '%s', aborting.\n", files.front().c_str());
exit(1); exit(1);
} }
} }
@ -104,11 +102,10 @@ int main(int argc, char **argv)
cliDownloader(prefs.dive_computer.vendor, prefs.dive_computer.product, prefs.dive_computer.device); cliDownloader(prefs.dive_computer.vendor, prefs.dive_computer.product, prefs.dive_computer.device);
} }
} }
if (!files.isEmpty()) { if (!files.empty()) {
qDebug() << "saving dive data to" << files; report_info("saving dive data to %s", join(files, ", ").c_str());
save_dives(qPrintable(files.first())); save_dives(files.front().c_str());
} } else {
else {
printf("No log files given, not saving dive data.\n"); printf("No log files given, not saving dive data.\n");
printf("Give a log file name as argument, or configure a cloud URL.\n"); printf("Give a log file name as argument, or configure a cloud URL.\n");
} }