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 b563ed9
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/library/parsercsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +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) {
if (locationColumnIndex.has_value()) {
for (int row = 1; row < tokens.size(); ++row) {
if (locationColumnIndex < tokens[row].size()) {
locations.append(tokens[row][locationColumnIndex]);
locations.append(tokens[row][*locationColumnIndex]);
}
}
} else {
Expand Down

0 comments on commit b563ed9

Please sign in to comment.