Skip to content

Commit

Permalink
Application: Add HOME support and improve filesystem usage (#1833)
Browse files Browse the repository at this point in the history
 - Add support for using HOME (~) in any path input from F3D user
 - Cleanup of F3D application code in regards to std::filesystem usage
 - Other small cleanups
  • Loading branch information
mwestphal authored Jan 3, 2025
1 parent a0eadcf commit 8b78094
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ _version.py

# wasm process related
node_modules

# testing related
.cache
62 changes: 35 additions & 27 deletions application/F3DColorMapTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,71 @@

#include "image.h"
#include "log.h"
#include "utils.h"

#include <filesystem>

namespace fs = std::filesystem;

namespace F3DColorMapTools
{
std::string Find(const std::string& str)
fs::path Find(const std::string& str)
{
if (fs::exists(str))
try
{
fs::path resolved = fs::canonical(str);
if (fs::is_regular_file(resolved))
fs::path fullPath(f3d::utils::collapsePath(str));
if (fs::exists(fullPath))
{
// already full path
return resolved.string();
if (fs::is_regular_file(fullPath))
{
// already full path
return fullPath;
}
}
}

std::vector<fs::path> dirsToCheck{ F3DSystemTools::GetUserConfigFileDirectory() / "colormaps",
std::vector<fs::path> dirsToCheck{ F3DSystemTools::GetUserConfigFileDirectory() / "colormaps",
#ifdef __APPLE__
"/usr/local/etc/f3d/colormaps",
"/usr/local/etc/f3d/colormaps",
#endif
#ifdef __linux__
"/etc/f3d/colormaps", "/usr/share/f3d/colormaps",
"/etc/f3d/colormaps", "/usr/share/f3d/colormaps",
#endif
F3DSystemTools::GetBinaryResourceDirectory() / "colormaps" };
F3DSystemTools::GetBinaryResourceDirectory() / "colormaps" };

for (const fs::path& dir : dirsToCheck)
{
// If the string is a stem, try adding supported extensions
if (fs::path(str).stem() == str)
for (const fs::path& dir : dirsToCheck)
{
for (const std::string& ext : f3d::image::getSupportedFormats())
// If the string is a stem, try adding supported extensions
if (fs::path(str).stem() == str)
{
fs::path cmPath = dir / (str + ext);
if (fs::exists(cmPath))
for (const std::string& ext : f3d::image::getSupportedFormats())
{
return cmPath.string();
fs::path cmPath = dir / (str + ext);
if (fs::exists(cmPath))
{
return cmPath;
}
}
}
}
else
{
// If not, use directly
fs::path cmPath = dir / str;
if (fs::exists(cmPath))
else
{
return cmPath.string();
// If not, use directly
fs::path cmPath = dir / str;
if (fs::exists(cmPath))
{
return cmPath;
}
}
}
}
catch (const fs::filesystem_error& ex)
{
f3d::log::error("Unable to look for color map ", str, ": ", ex.what());
}

return {};
}

std::vector<double> Read(const std::string& path)
std::vector<double> Read(const fs::path& path)
{
try
{
Expand Down
5 changes: 3 additions & 2 deletions application/F3DColorMapTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#ifndef F3DColorMapReader_h
#define F3DColorMapReader_h

#include <filesystem>
#include <string>
#include <vector>

namespace F3DColorMapTools
{
std::string Find(const std::string& str);
std::vector<double> Read(const std::string& path);
std::filesystem::path Find(const std::string& str);
std::vector<double> Read(const std::filesystem::path& path);
}

#endif
8 changes: 5 additions & 3 deletions application/F3DConfigFileTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nlohmann/json.hpp"

#include "log.h"
#include "utils.h"

#include <filesystem>
#include <fstream>
Expand All @@ -21,7 +22,7 @@ namespace
*/
std::vector<fs::path> GetConfigPaths(const std::string& configSearch)
{
std::vector<std::filesystem::path> paths;
std::vector<fs::path> paths;

fs::path configPath;
std::vector<fs::path> dirsToCheck = {
Expand Down Expand Up @@ -111,7 +112,8 @@ F3DConfigFileTools::ReadConfigFiles(const std::string& userConfig)
}
else
{
configPaths.emplace_back(userConfig);
// Collapse full path into an absolute path
configPaths.emplace_back(f3d::utils::collapsePath(userConfig));
}

// Recover actual individual config file paths
Expand All @@ -133,7 +135,7 @@ F3DConfigFileTools::ReadConfigFiles(const std::string& userConfig)
if (fs::is_directory(configPath))
{
f3d::log::debug("Using config directory ", configPath.string());
for (auto& entry : std::filesystem::directory_iterator(configPath))
for (auto& entry : fs::directory_iterator(configPath))
{
actualConfigFilePaths.emplace(entry);
}
Expand Down
Loading

0 comments on commit 8b78094

Please sign in to comment.