Skip to content

Commit

Permalink
Avoid std::pair and remove file exists check
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Jul 22, 2019
1 parent ab18938 commit c1dcdcc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
19 changes: 7 additions & 12 deletions src/library/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,19 @@ bool Parser::isUtf8(const char* string) {
return true;
}

std::pair<TrackFile, bool> Parser::playlistEntryToTrackFile(
TrackFile Parser::playlistEntryToTrackFile(
const QString& playlistEntry,
const QString& basePath) {
if (playlistEntry.startsWith("file:")) {
auto trackFile = TrackFile::fromUrl(QUrl(playlistEntry));
return std::make_pair(trackFile, trackFile.checkFileExists());
// URLs are always absolute
return TrackFile::fromUrl(QUrl(playlistEntry));
}
auto filePath = QString(playlistEntry).replace('\\', '/');
auto trackFile = TrackFile(filePath);
if (trackFile.checkFileExists()) {
return std::make_pair(trackFile, true);
if (basePath.isEmpty() || trackFile.asFileInfo().isAbsolute()) {
return trackFile;
} else {
if (basePath.isEmpty()) {
// No base path
return std::make_pair(trackFile, false);
}
// Try relative to base path
trackFile = TrackFile(QDir(basePath), filePath);
return std::make_pair(trackFile, trackFile.checkFileExists());
// Fallback: Relative to base path
return TrackFile(QDir(basePath), filePath);
}
}
7 changes: 1 addition & 6 deletions src/library/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ from it and overwrite the parse function and add class specific functions to
it afterwards for proper functioning
**/

// TODO (C++17): Replace std::pair with std::optional
#include <utility>

#include <QObject>
#include <QString>
#include <QList>
Expand Down Expand Up @@ -59,10 +56,8 @@ class Parser : public QObject {
bool isBinary(QString);
// check for Utf8 encoding
static bool isUtf8(const char* string);
// reads URLs an plain Part and returns a local file path
// Resolve an absolute or relative file path
// TODO (C++17): Replace std::pair with std::optional
std::pair<TrackFile, bool> playlistEntryToTrackFile(
TrackFile playlistEntryToTrackFile(
const QString& playlistEntry,
const QString& basePath = QString());
};
Expand Down
8 changes: 4 additions & 4 deletions src/library/parserm3u.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ QString ParserM3u::getFilePath(QTextStream* stream, const QString& basePath) {
// Skip comments
continue;
}
auto optTrackFile = playlistEntryToTrackFile(textline, basePath);
if (optTrackFile.second) {
return optTrackFile.first.location();
auto trackFile = playlistEntryToTrackFile(textline, basePath);
if (trackFile.checkFileExists()) {
return trackFile.location();
}
// We couldn't match this to a real file so ignore it
qWarning() << optTrackFile.first << "not found";
qWarning() << trackFile << "not found";
}
// Signal we reached the end
return QString();
Expand Down
8 changes: 4 additions & 4 deletions src/library/parserpls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ QString ParserPls::getFilePath(QTextStream *stream, const QString& basePath) {
++iPos;

QString filename = textline.right(textline.length() - iPos);
auto optTrackFile = playlistEntryToTrackFile(filename, basePath);
if (optTrackFile.second) {
return optTrackFile.first.location();
auto trackFile = playlistEntryToTrackFile(filename, basePath);
if (trackFile.checkFileExists()) {
return trackFile.location();
}
// We couldn't match this to a real file so ignore it
qWarning() << optTrackFile.first << "not found";
qWarning() << trackFile << "not found";
}
textline = stream->readLine();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/playlisttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DummyParser : public Parser {
QString playlistEntryToFilePath(
const QString& playlistEntry,
const QString& basePath = QString()) {
return playlistEntryToTrackFile(playlistEntry, basePath).first.asFileInfo().filePath();
return playlistEntryToTrackFile(playlistEntry, basePath).asFileInfo().filePath();
}
};

Expand Down

0 comments on commit c1dcdcc

Please sign in to comment.