Skip to content

Commit

Permalink
Simply and fix Mac/Linux GetFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
fredemmott committed Jun 20, 2020
1 parent 94b494b commit 391b126
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions StreamDeckSDK/ESDUtilitiesPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ LICENSE file.
#include <fmt/format.h>
#include <unistd.h>

#include "ESDUtilities.h"
#include "ESDLogger.h"
#include "ESDUtilities.h"

namespace {
// Remove trailing delimiters
std::string TrimRight(const std::string& in) {
const auto idx = in.find_last_not_of('/');
if (idx == std::string::npos) {
return "/";
}
return in.substr(0, idx + 1);
}
}// namespace

void ESDUtilities::DoSleep(int inMilliseconds) {
usleep(1000 * inMilliseconds);
Expand Down Expand Up @@ -52,41 +63,25 @@ std::string ESDUtilities::GetParentDirectoryPath(const std::string& inPath) {
return {};
}

auto idx = inPath.find_last_not_of('/');
if (idx == std::string::npos) {
return "/";
}
const auto trimmed(inPath.substr(0, idx + 1));
const auto trimmed(TrimRight(inPath));

idx = trimmed.find_last_of('/');
const auto idx = trimmed.find_last_of('/');
if (idx == 0) {
return "/";
}
return trimmed.substr(0, idx);
}

std::string ESDUtilities::GetFileName(const std::string& inPath) {
//
// Use the platform specific delimiter
//
auto delimiter = '/';

//
// Remove the trailing delimiters
//
const auto lastNonDelim = inPath.find_last_not_of(delimiter);
if (lastNonDelim == std::string::npos) {
return std::string();
}

const auto normalized = inPath.substr(0, lastNonDelim + 1);

const auto delimIdx = inPath.find_last_of(delimiter);
if (delimIdx == std::string::npos) {
return normalized;
// Re-use the same code for special casing
const auto parent = GetParentDirectoryPath(inPath);
if (parent.length() >= inPath.length()) {
return parent;
}

return normalized.substr(delimIdx + 1);
const auto trimmed = TrimRight(inPath);
const auto pos = trimmed.find_last_of('/');
return trimmed.substr(pos + 1);
}

std::string ESDUtilities::GetPluginDirectoryPath() {
Expand Down

0 comments on commit 391b126

Please sign in to comment.