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/fulltext.cpp \
core/subsurfacestartup.cpp \
core/subsurface-string.cpp \
core/pref.c \
core/profile.cpp \
core/device.cpp \

View file

@ -172,6 +172,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
string-format.cpp
strtod.c
subsurface-float.h
subsurface-string.cpp
subsurface-string.h
subsurfacestartup.cpp
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 <vector>
// Sadly, starts_with only with C++20!
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;
}
std::string join(const std::vector<std::string> &l, const std::string &separator, bool skip_empty = false);
#endif
#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
// so we would get dir[branch].ssrf when trying to select dir[branch].
// let's detect that and remove the incorrect extension
QStringList cleanFilenames;
std::vector<std::string> cleanFilenames;
QRegularExpression reg(".*\\[[^]]+]\\.ssrf", QRegularExpression::CaseInsensitiveOption);
for (QString filename: filenames) {
if (reg.match(filename).hasMatch())
filename.remove(QRegularExpression("\\.ssrf$", QRegularExpression::CaseInsensitiveOption));
cleanFilenames << filename;
cleanFilenames.push_back(filename.toStdString());
}
loadFiles(cleanFilenames);
}
@ -399,6 +399,11 @@ void MainWindow::on_actionSaveAs_triggered()
file_save_as();
}
static std::string encodeFileName(const std::string &fn)
{
return QFile::encodeName(QString::fromStdString(fn)).toStdString();
}
void MainWindow::on_actionCloudstorageopen_triggered()
{
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();
showProgressBar();
QByteArray fileNamePtr = QFile::encodeName(QString::fromStdString(*filename));
if (!parse_file(fileNamePtr.data(), &divelog))
setCurrentFile(fileNamePtr.toStdString());
std::string encoded = encodeFileName(*filename);
if (!parse_file(encoded.c_str(), &divelog))
setCurrentFile(encoded);
process_loaded_dives();
hideProgressBar();
refreshDisplay();
@ -1156,7 +1161,7 @@ void MainWindow::recentFileTriggered(bool)
updateLastUsedDir(QFileInfo(filename).dir().path());
closeCurrentFile();
loadFiles(QStringList() << filename);
loadFiles(std::vector<std::string> { filename.toStdString() });
}
int MainWindow::file_save_as(void)
@ -1280,33 +1285,32 @@ void MainWindow::setTitle()
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;
QByteArray fileNamePtr;
struct divelog log;
for (int i = 0; i < fileNames.size(); ++i) {
fileNamePtr = QFile::encodeName(fileNames.at(i));
parse_file(fileNamePtr.data(), &log);
for (const std::string &fn: fileNames) {
std::string encoded = encodeFileName(fn);
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);
}
void MainWindow::loadFiles(const QStringList &fileNames)
void MainWindow::loadFiles(const std::vector<std::string> &fileNames)
{
if (fileNames.isEmpty()) {
if (fileNames.empty()) {
refreshDisplay();
return;
}
QByteArray fileNamePtr;
showProgressBar();
for (int i = 0; i < fileNames.size(); ++i) {
fileNamePtr = QFile::encodeName(fileNames.at(i));
for (const std::string &fn: fileNames) {
fileNamePtr = QFile::encodeName(QString::fromStdString(fn));
if (!parse_file(fileNamePtr.data(), &divelog)) {
setCurrentFile(fileNamePtr.toStdString());
addRecentFile(fileNamePtr, false);
@ -1352,13 +1356,13 @@ void MainWindow::on_actionImportDiveLog_triggered()
return;
updateLastUsedDir(QFileInfo(fileNames[0]).dir().path());
QStringList logFiles;
std::vector<std::string> logFiles;
QStringList csvFiles;
for (const QString &fn: fileNames) {
if (isCsvFile(fn))
csvFiles.append(fn);
else
logFiles.append(fn);
logFiles.push_back(fn.toStdString());
}
if (logFiles.size())

View file

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

View file

@ -13,6 +13,7 @@
#include "core/qt-gui.h"
#include "core/qthelper.h"
#include "core/subsurfacestartup.h"
#include "core/subsurface-string.h"
#include "core/settings/qPref.h"
#include "core/tag.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. */
print_version();
int i;
bool no_filenames = true;
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
std::unique_ptr<QApplication> app(new QApplication(argc, argv));
QStringList files;
QStringList importedFiles;
std::vector<std::string> files;
std::vector<std::string> importedFiles;
QStringList arguments = QCoreApplication::arguments();
const char *default_directory = system_default_directory();
subsurface_mkdir(default_directory);
for (i = 1; i < arguments.length(); i++) {
QString a = arguments.at(i);
if (a.isEmpty())
for (int i = 1; i < arguments.length(); i++) {
std::string a = arguments[i].toStdString();
if (a.empty())
continue;
if (a.at(0) == '-') {
parse_argument(qPrintable(a));
if (a[0] == '-') {
parse_argument(a.c_str());
continue;
}
if (imported) {
@ -85,21 +85,20 @@ int main(int argc, char **argv)
init_ui();
if (no_filenames) {
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
QString defaultFile(prefs.default_filename);
if (!defaultFile.isEmpty())
files.push_back(QString(prefs.default_filename));
if (!empty_string(prefs.default_filename))
files.emplace_back(prefs.default_filename ? prefs.default_filename : "");
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
auto cloudURL = getCloudURL();
if (cloudURL)
files.push_back(QString::fromStdString(*cloudURL));
files.push_back(*cloudURL);
}
}
MainWindow *m = MainWindow::instance();
if (verbose && !files.isEmpty())
qDebug() << "loading dive data from" << files;
if (verbose && !files.empty())
report_info("loading dive data from: %s", join(files, std::string(", ")).c_str());
m->loadFiles(files);
if (verbose && !importedFiles.isEmpty())
qDebug() << "importing dive data from" << importedFiles;
if (verbose && !importedFiles.empty())
report_info("importing dive data from %s", join(importedFiles, std::string(", ")).c_str());
m->importFiles(importedFiles);
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?
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
int i;
bool no_filenames = true;
std::unique_ptr<QCoreApplication> app(new QCoreApplication(argc, argv));
QStringList files;
QStringList importedFiles;
std::vector<std::string> files;
std::vector<std::string> importedFiles;
QStringList arguments = QCoreApplication::arguments();
// 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
fill_computer_list();
for (i = 1; i < arguments.length(); i++) {
QString a = arguments.at(i);
if (a.isEmpty())
for (int i = 1; i < arguments.length(); i++) {
std::string a = arguments[i].toStdString();
if (a.empty())
continue;
if (a.at(0) == '-') {
parse_argument(qPrintable(a));
if (a[0] == '-') {
parse_argument(a.c_str());
continue;
}
if (imported) {
@ -80,19 +79,18 @@ int main(int argc, char **argv)
if (no_filenames) {
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
QString defaultFile(prefs.default_filename);
if (!defaultFile.isEmpty())
files.push_back(QString(prefs.default_filename));
if (!empty_string(prefs.default_filename))
files.emplace_back(prefs.default_filename ? prefs.default_filename : "");
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
auto cloudURL = getCloudURL();
if (cloudURL)
files.push_back(QString::fromStdString(*cloudURL));
files.emplace_back(*cloudURL);
}
}
if (!files.isEmpty()) {
qDebug() << "loading dive data from" << files;
if (parse_file(qPrintable(files.first()), &divelog) < 0) {
printf("Failed to load dives from file '%s', aborting.\n", qPrintable(files.first()));
if (!files.empty()) {
report_info("loading dive data from %s", join(files, ", ").c_str());
if (parse_file(files.front().c_str(), &divelog) < 0) {
printf("Failed to load dives from file '%s', aborting.\n", files.front().c_str());
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);
}
}
if (!files.isEmpty()) {
qDebug() << "saving dive data to" << files;
save_dives(qPrintable(files.first()));
}
else {
if (!files.empty()) {
report_info("saving dive data to %s", join(files, ", ").c_str());
save_dives(files.front().c_str());
} else {
printf("No log files given, not saving dive data.\n");
printf("Give a log file name as argument, or configure a cloud URL.\n");
}