Skip to content

Commit

Permalink
parsercsv: prefer standard algorithms and datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoyvwac committed Sep 6, 2022
1 parent a844094 commit 09ccbe6
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/library/parsercsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,28 @@ QList<QString> ParserCsv::parseAllLocations(const QString& playlistFile) {

QList<QList<QString>> tokens = tokenize(bytes, ',');

// detect Location column
int locationColumnIndex = -1;
const auto detect_location_column = [&](const auto& tokens_list, auto predicate) -> std::optional<std::size_t> {
const auto it = std::find_if(std::begin(tokens_list), std::end(tokens_list), predicate);
return (it != std::end(tokens_list))
? std::distance(std::begin(tokens_list), it)
: std::optional<std::size_t>{};
};
if (tokens.size()) {
for (int i = 0; i < tokens[0].size(); ++i) {
if (tokens[0][i] == QObject::tr("Location")) {
locationColumnIndex = i;
break;
}
}
if (locationColumnIndex < 0 && tokens.size() > 1) {
std::optional<std::size_t> locationColumnIndex = detect_location_column(
tokens[0],
[&](auto i) { return i == QObject::tr("Location"); });
if ((!locationColumnIndex.has_value()) && tokens.size() > 1) {
// Last resort, find column with path separators
// This happens in case of csv files in a different language
for (int i = 0; i < tokens[1].size(); ++i) {
if (tokens[1][i].contains(QDir::separator())) {
locationColumnIndex = i;
break;
}
}
locationColumnIndex = detect_location_column(tokens[1],
[&](auto i) { return i.contains(QDir::separator()); });
}
if (locationColumnIndex >= 0) {
for (int row = 1; row < tokens.size(); ++row) {
if (locationColumnIndex < tokens[row].size()) {
locations.append(tokens[row][locationColumnIndex]);
if (locationColumnIndex.has_value()) {
std::for_each(std::next(std::begin(tokens)), std::end(tokens), [&](const auto& token) {
if (locationColumnIndex < token.size()) {
locations.append(token[static_cast<int>(*locationColumnIndex)]);
}
}
});
} else {
qInfo() << "No location column found in"
<< playlistFile;
Expand Down

0 comments on commit 09ccbe6

Please sign in to comment.