Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handled getting file extensions from filenames without a full path #34

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 88 additions & 58 deletions UnitTests/UnitTest_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,119 @@

namespace novac
{
TEST_CASE("GetFileName Returns expected filename", "[GetFileName][File]")
TEST_CASE("GetFileName Returns expected filename", "[GetFileName][File]")
{
SECTION("Windows path with regular file extension")
{
SECTION("Windows path with regular file extension")
{
std::string input = "C:\\Windows\\System32\\wordfile.docx";
std::string expected = "wordfile.docx";
const std::string input = "C:\\Windows\\System32\\wordfile.docx";
const std::string expected = "wordfile.docx";

std::string result = GetFileName(input);
const std::string result = GetFileName(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Windows path without file extension")
{
std::string input = "C:\\Windows\\System32\\hosts";
std::string expected = "hosts";
SECTION("Windows path without file extension")
{
const std::string input = "C:\\Windows\\System32\\hosts";
const std::string expected = "hosts";

std::string result = GetFileName(input);
const std::string result = GetFileName(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Windows path without file, returns exmpty string")
{
std::string input = "C:\\Windows\\System32\\";
std::string expected = "";
SECTION("Windows path without file, returns exmpty string")
{
const std::string input = "C:\\Windows\\System32\\";
const std::string expected = "";

std::string result = GetFileName(input);
const std::string result = GetFileName(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Linux path without file extension")
{
std::string input = "/home/user/myfile.sh";
std::string expected = "myfile.sh";
SECTION("Linux path without file extension")
{
const std::string input = "/home/user/myfile.sh";
const std::string expected = "myfile.sh";

std::string result = GetFileName(input);
const std::string result = GetFileName(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}
}

TEST_CASE("GetFileExtension Returns expected extension", "[GetFileName][File]")
TEST_CASE("GetFileExtension Returns expected extension", "[GetFileName][File]")
{
SECTION("Windows path with regular file extension")
{
SECTION("Windows path with regular file extension")
{
std::string input = "C:\\Windows\\System32\\wordfile.docx";
std::string expected = ".docx";
const std::string input = "C:\\Windows\\System32\\wordfile.docx";
const std::string expected = ".docx";

std::string result = GetFileExtension(input);
const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Windows path without file extension")
{
std::string input = "C:\\Windows\\System32\\hosts";
std::string expected = "";
SECTION("Filename only, with lowercase file extension")
{
const std::string input = "wordfile.docx";
const std::string expected = ".docx";

std::string result = GetFileExtension(input);
const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Windows path without file, returns exmpty string")
{
std::string input = "C:\\Windows\\System32\\";
std::string expected = "";
SECTION("Filename only, with uppercase file extension")
{
const std::string input = "WORDFILE.DOCX";
const std::string expected = ".DOCX";

std::string result = GetFileExtension(input);
const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Linux path without file extension")
{
std::string input = "/home/user/myfile.sh";
std::string expected = ".sh";
SECTION("Windows path without file extension")
{
const std::string input = "C:\\Windows\\System32\\hosts";
const std::string expected = "";

std::string result = GetFileExtension(input);
const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}
REQUIRE(expected == result);
}

SECTION("Windows path without file, returns empty string")
{
const std::string input = "C:\\Windows\\System32\\";
const std::string expected = "";

const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}

SECTION("Linux path with file extension")
{
const std::string input = "/home/user/myfile.sh";
const std::string expected = ".sh";

const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}

SECTION("Linux path without file extension")
{
const std::string input = "/home/user/myfile";
const std::string expected = "";

const std::string result = GetFileExtension(input);

REQUIRE(expected == result);
}
}
}
2 changes: 1 addition & 1 deletion include/SpectralEvaluation/File/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::string EnsureFilenameHasSuffix(const std::string& fullFilePath, const std::
/** Returns the file name component of the provide full filename and path */
std::string GetFileName(const std::string& fullFileNameAndPath);

/** Returns the file extension (suffix) from the provided full file path */
/* Returns the file extension (suffix) from the provided full file path or filename */
std::string GetFileExtension(const std::string& fullFilePath);

/** Saves the full instrument calibration to a single file using the extended STD-format.
Expand Down
20 changes: 4 additions & 16 deletions src/File/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,10 @@ std::string GetFileExtension(const std::string& fullFilePath)
return std::string();
}

const size_t lastForwardSlash = fullFilePath.rfind('\\');
const size_t lastBackwardSlash = fullFilePath.rfind('/');

if ((lastForwardSlash != fullFilePath.npos && lastPeriod > lastForwardSlash) ||
(lastBackwardSlash != fullFilePath.npos && lastPeriod > lastBackwardSlash))
{
return fullFilePath.substr(lastPeriod, fullFilePath.size() - lastPeriod);
}
else
{
// no period found _after_ the last path-separator character, hence no suffix
return std::string();
}
return fullFilePath.substr(lastPeriod, fullFilePath.size() - lastPeriod);
}

std::string GetFileName(const std::string& fullFileNameAndPath, char pathSeparator)
static std::string GetFileName(const std::string& fullFileNameAndPath, char pathSeparator)
{
auto position = fullFileNameAndPath.rfind(pathSeparator);
if (position == std::string::npos)
Expand All @@ -375,7 +363,7 @@ std::string GetFileName(const std::string& fullFileNameAndPath, char pathSeparat
std::string GetFileName(const std::string& fullFileNameAndPath)
{
// first attempt with using windows path-separator.
std::string result = GetFileName(fullFileNameAndPath, '\\');
const std::string result = GetFileName(fullFileNameAndPath, '\\');
if (!result.empty())
{
return result;
Expand All @@ -385,7 +373,7 @@ std::string GetFileName(const std::string& fullFileNameAndPath)
return GetFileName(fullFileNameAndPath, '/');
}

std::pair<std::string, std::string> FormatProperty(const char* name, double value)
static std::pair<std::string, std::string> FormatProperty(const char* name, double value)
{
char formattedValue[128];
sprintf(formattedValue, "%.9g", value);
Expand Down
Loading