Skip to content

Commit

Permalink
Use absolute path to check if FMU exists. (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnHeuermann committed Jul 6, 2021
1 parent a0a938f commit a6d1c8a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/OMSimulatorLib/OMSFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ filesystem::path oms_unique_path(const std::string& prefix)
for(int i=0; i<8; i++)
s += std::string(1, lt[rand() % size]);

filesystem::path p(s);
return p;
return filesystem::path(s);
}

void oms_copy_file(const filesystem::path& from, const filesystem::path& to)
Expand All @@ -161,6 +160,15 @@ void oms_copy_file(const filesystem::path& from, const filesystem::path& to)
#endif
}

filesystem::path oms_absolute(const filesystem::path& p)
{
#if OMC_STD_FS == 1
return filesystem::absolute(p);
#else
return p;
#endif
}

/*
The code above is partially based on the boost implementation for
Expand Down
5 changes: 3 additions & 2 deletions src/OMSimulatorLib/OMSFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ namespace filesystem = std::filesystem;
#define OMC_STD_FS 1
#endif
#endif

#if !defined(OMC_STD_FS) && __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace filesystem = std::experimental::filesystem::v1;
#define OMC_STD_FS 1
#endif

#endif

#if OMC_STD_FS == 1
Expand All @@ -23,7 +25,6 @@ namespace filesystem = std::experimental::filesystem::v1;

#else // boost part


#include <string>
#include <boost/version.hpp>
// boost version < 1.57 has issues linking boost::filesystem::copy_file
Expand All @@ -47,7 +48,6 @@ namespace filesystem = std::experimental::filesystem::v1;
#define OMS_RECURSIVE_DIRECTORY_ITERATOR(path) (filesystem::recursive_directory_iterator{path})
#endif


#include <boost/filesystem.hpp>
namespace filesystem = boost::filesystem;
#endif
Expand All @@ -57,5 +57,6 @@ filesystem::path oms_canonical(filesystem::path p);
void oms_copy_file(const filesystem::path& from, const filesystem::path& to);
filesystem::path oms_unique_path(const std::string& prefix);
filesystem::path naive_uncomplete(const filesystem::path& path, const filesystem::path& base);
filesystem::path oms_absolute(const filesystem::path& p);

#endif
18 changes: 10 additions & 8 deletions src/OMSimulatorLib/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,28 @@ oms_status_enu_t oms::System::addSubSystem(const oms::ComRef& cref, oms_system_e

oms_status_enu_t oms::System::addSubModel(const oms::ComRef& cref, const std::string& path)
{
std::string absPath = oms_absolute(path).string();

if (cref.isValidIdent())
{
if (!validCref(cref))
return logError_AlreadyInScope(getFullCref() + cref);

if (!filesystem::exists(path))
return logError("file does not exist: \"" + path + "\"");
if (!filesystem::exists(absPath))
return logError("file does not exist: \"" + absPath + "\"");

Component* component = NULL;

std::string extension = "";
if (path.length() > 4)
extension = path.substr(path.length() - 4);
if (absPath.length() > 4)
extension = absPath.substr(absPath.length() - 4);

if (extension == ".fmu" && oms_system_wc == type)
component = ComponentFMUCS::NewComponent(cref, this, path);
component = ComponentFMUCS::NewComponent(cref, this, absPath);
else if (extension == ".fmu" && oms_system_sc == type)
component = ComponentFMUME::NewComponent(cref, this, path);
component = ComponentFMUME::NewComponent(cref, this, absPath);
else if (extension == ".csv" || extension == ".mat")
component = ComponentTable::NewComponent(cref, this, path);
component = ComponentTable::NewComponent(cref, this, absPath);
else
return logError("supported sub-model formats are \".fmu\", \".csv\", \".mat\"");

Expand All @@ -317,7 +319,7 @@ oms_status_enu_t oms::System::addSubModel(const oms::ComRef& cref, const std::st
if (!system)
return logError("System \"" + std::string(getFullCref()) + "\" does not contain system \"" + std::string(front) + "\"");

return system->addSubModel(tail, path);
return system->addSubModel(tail, absPath);
}

oms_status_enu_t oms::System::addResources(const ComRef& cref, std::string& filename)
Expand Down

0 comments on commit a6d1c8a

Please sign in to comment.