Skip to content

Commit

Permalink
use non-filesystem based way to get function name (#358)
Browse files Browse the repository at this point in the history
* use non-filesystem based way to get function name

* update test

* better test
  • Loading branch information
philbucher authored Sep 21, 2023
1 parent 411b786 commit 2093dfa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
24 changes: 22 additions & 2 deletions co_sim_io/sources/code_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@

// Project includes
#include "includes/code_location.hpp"
#include "includes/filesystem_inc.hpp"

namespace CoSimIO {
namespace Internals {

std::string CodeLocation::GetCleanFileName() const
{
return fs::canonical(fs::path(GetFileName())).lexically_relative(fs::absolute(".")).string();
std::string clean_file_name(GetFileName());

// Replace all backslashes with forward slashes
std::size_t start_position = 0;
const std::string from_string = "\\";
const std::string to_string = "/";
while ((start_position = clean_file_name.find(from_string, start_position)) != std::string::npos) {
clean_file_name.replace(start_position, from_string.length(), to_string);
start_position += to_string.length();
}

// Remove the path up to the co_sim_io root folder
std::size_t co_sim_io_root_position = clean_file_name.rfind("/co_sim_io/");
if (co_sim_io_root_position != std::string::npos) {
clean_file_name.erase(0, co_sim_io_root_position+1);
return clean_file_name;
}
if (co_sim_io_root_position != std::string::npos) {
clean_file_name.erase(0, co_sim_io_root_position + 1 );
}

return clean_file_name;
}

} // namespace Internals
Expand Down
2 changes: 1 addition & 1 deletion co_sim_io/sources/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int GetMinorVersion() {
}

std::string GetPatchVersion() {
return "0";
return "1";
}

} // namespace CoSimIO
2 changes: 1 addition & 1 deletion tests/co_sim_io/cpp/test_code_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST_SUITE("CodeLocation") {
TEST_CASE("macro_CO_SIM_IO_CODE_LOCATION")
{
auto code_loc = testing_aux_namespace::AuxFunction();
CHECK_MESSAGE(code_loc.GetCleanFileName().find(fs::path("co_sim_io/cpp/test_code_location.cpp").make_preferred().string()) != std::string::npos, code_loc.GetCleanFileName()); // must contain at least this
CHECK_EQ("co_sim_io/cpp/test_code_location.cpp", code_loc.GetCleanFileName());
CHECK_EQ(code_loc.GetLineNumber(), 25);
CHECK_MESSAGE(code_loc.GetCleanFunctionName().find("testing_aux_namespace::AuxFunction") != std::string::npos, code_loc.GetCleanFunctionName()); // must contain at least this
}
Expand Down

0 comments on commit 2093dfa

Please sign in to comment.