Skip to content

Commit

Permalink
Implement search_path, find_path with std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
LegalizeAdulthood committed Feb 25, 2024
1 parent 33b023b commit 817a255
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 159 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ project("${ID_PROJECT_NAME}"
include(CTest)
include(TestBigEndian)

find_package(Boost REQUIRED)

set(home_dir "${CMAKE_SOURCE_DIR}/home")
set(ID_DIR ${CMAKE_CURRENT_SOURCE_DIR})

Expand Down Expand Up @@ -123,6 +125,7 @@ add_library(libid
common/drivers.cpp
common/memory.cpp headers/memory.h

common/find_path.cpp headers/find_path.h
common/fractint.cpp
common/framain2.cpp headers/framain2.h
headers/get_ifs_token.h
Expand All @@ -135,6 +138,7 @@ add_library(libid
common/prompts1.cpp headers/prompts1.h
common/prompts2.cpp headers/prompts2.h
common/realdos.cpp headers/realdos.h
common/search_path.cpp headers/search_path.h
common/update_save_name.cpp headers/update_save_name.h
common/zoom.cpp headers/zoom.h

Expand Down Expand Up @@ -272,6 +276,7 @@ source_group("Source Files/common/plumbing" FILES
common/memory.cpp
)
source_group("Header Files/common/ui" FILES
headers/find_path.h
headers/fractint.h
headers/framain2.h
headers/get_ifs_token.h
Expand All @@ -285,10 +290,12 @@ source_group("Header Files/common/ui" FILES
headers/prompts1.h
headers/prompts2.h
headers/realdos.h
headers/search_path.h
headers/update_save_name.h
headers/zoom.h
)
source_group("Source Files/common/ui" FILES
common/find_path.cpp
common/fractint.cpp
common/framain2.cpp
common/help.cpp
Expand All @@ -299,6 +306,7 @@ source_group("Source Files/common/ui" FILES
common/prompts1.cpp
common/prompts2.cpp
common/realdos.cpp
common/search_path.cpp
common/update_save_name.cpp
common/zoom.cpp
)
Expand All @@ -309,7 +317,8 @@ set_src_dir(common/fractint.cpp)
target_compile_definitions(libid PUBLIC ${ID_TARGET_DEFINITIONS} $<$<CONFIG:Debug>:${ID_TARGET_DEFINITIONS_DEBUG}>)
target_compile_options(libid PUBLIC ${ID_TARGET_OPTIONS})
target_include_directories(libid PUBLIC headers)
target_link_libraries(libid PRIVATE helpcom os config)
target_link_libraries(libid PRIVATE helpcom os config Boost::boost)
target_link_libraries(libid PUBLIC config)
add_dependencies(libid native_help)

add_executable(id ${ID_EXECUTABLE_TYPE}
Expand Down
1 change: 1 addition & 0 deletions common/cmdfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "calcfrac.h"
#include "cmdfiles.h"
#include "drivers.h"
#include "find_path.h"
#include "fracsuba.h"
#include "fracsubr.h"
#include "fractalb.h"
Expand Down
94 changes: 94 additions & 0 deletions common/find_path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "find_path.h"

#include "port.h"
#include "prototyp.h"

#include "cmdfiles.h"
#include "fractint.h"
#include "id_data.h"
#include "make_path.h"
#include "prompts2.h"
#include "search_path.h"

#ifdef WIN32
#include <corecrt_io.h>
#else
#include <unistd.h>
#endif

#include <filesystem>

namespace fs = std::filesystem;

/*
*----------------------------------------------------------------------
*
* findpath --
*
* Find where a file is.
* We return filename if it is an absolute path.
* Otherwise, we first try FRACTDIR/filename, SRCDIR/filename,
* and then ./filename.
*
* Results:
* Returns full pathname in fullpathname.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
std::string find_path(const char *filename,
const std::function<const char *(const char *)> &get_env) // return full pathnames
{
char fullpathname[FILE_MAX_PATH];
fullpathname[0] = 0; // indicate none found

char fname[FILE_MAX_FNAME];
char ext[FILE_MAX_EXT];
char temp_path[FILE_MAX_PATH];

// check current directory if curdir= parameter set
split_fname_ext(filename, fname, ext);
make_path(temp_path, "", "", fname, ext);
if (g_check_cur_dir && fs::exists(temp_path)) // file exists
{
return (fs::current_path() / filename).make_preferred().string();
}

// check for absolute path
//if (fs::path{filename}.is_absolute() && )
//{
//}
std::strcpy(temp_path, filename); // avoid side effect changes to filename
if (temp_path[0] == SLASHC || (temp_path[0] && temp_path[1] == ':'))
{
if (access(temp_path, 0) == 0) // file exists
{
std::strcpy(fullpathname, temp_path);
return fullpathname;
}

split_fname_ext(temp_path, fname, ext);
make_path(temp_path, "", "", fname, ext);
}

// check FRACTDIR
make_path(temp_path, "", g_fractal_search_dir1.c_str(), fname, ext);
if (access(temp_path, 0) == 0)
{
std::strcpy(fullpathname, temp_path);
return fullpathname;
}

// check SRCDIR
make_path(temp_path, "", g_fractal_search_dir2.c_str(), fname, ext);
if (access(temp_path, 0) == 0)
{
std::strcpy(fullpathname, temp_path);
return fullpathname;
}

// check PATH
return search_path(temp_path, "PATH", get_env);
}
1 change: 1 addition & 0 deletions common/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "cmdfiles.h"
#include "drivers.h"
#include "find_path.h"
#include "helpcom.h"
#include "helpdefs.h"
#include "id_data.h"
Expand Down
1 change: 1 addition & 0 deletions common/loadmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "prototyp.h"

#include "cmdfiles.h"
#include "find_path.h"
#include "miscres.h"
#include "prompts1.h"
#include "prompts2.h"
Expand Down
1 change: 1 addition & 0 deletions common/miscovl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "calcfrac.h"
#include "cmdfiles.h"
#include "drivers.h"
#include "find_path.h"
#include "fractalp.h"
#include "fractype.h"
#include "framain2.h"
Expand Down
10 changes: 6 additions & 4 deletions common/prompts2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "diskvid.h"
#include "drivers.h"
#include "evolve.h"
#include "find_path.h"
#include "fracsubr.h"
#include "fractalp.h"
#include "fractype.h"
Expand Down Expand Up @@ -1939,22 +1940,23 @@ void make_path(char *template_str, char const *drive, char const *dir, char cons
}

fs::path result;
auto not_empty = [](const char *ptr) { return ptr != nullptr && ptr[0] != 0; };
#ifndef XFRACT
if (drive)
if (not_empty(drive))
{
result = drive;
}
#endif
if (dir)
if (not_empty(dir))
{
result /= dir;
result += '/';
}
if (fname)
if (not_empty(fname))
{
result /= fname;
}
if (ext)
if (not_empty(ext))
{
result.replace_extension(ext);
}
Expand Down
1 change: 1 addition & 0 deletions common/realdos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "diskvid.h"
#include "drivers.h"
#include "editpal.h"
#include "find_path.h"
#include "fractalp.h"
#include "fractype.h"
#include "helpcom.h"
Expand Down
36 changes: 36 additions & 0 deletions common/search_path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "search_path.h"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

#include <filesystem>
#include <vector>

namespace fs = std::filesystem;

std::string search_path(const char *filename, const char *path_var, std::function<const char *(const char *)> get_env)
{
if (filename == nullptr || path_var == nullptr)
{
return {};
}

const char *path = get_env(path_var);
if (path == nullptr)
{
return {};
}

std::vector<std::string> parts;
split(parts, path, boost::algorithm::is_any_of(PATH_SEPARATOR));
for (const fs::path dir : parts)
{
fs::path candidate{dir / filename};
if (exists(candidate))
{
return candidate.make_preferred().string();
}
}

return {};
}
14 changes: 14 additions & 0 deletions headers/find_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <cstdlib>
#include <cstring>
#include <functional>
#include <string>

std::string find_path(const char *filename, const std::function<const char *(const char *)> &get_env);

inline void findpath(char const *filename, char *fullpathname)
{
const std::string result = find_path(filename, [](const char *var) -> const char * { return std::getenv(var); });
std::strcpy(fullpathname, result.c_str());
}
1 change: 0 additions & 1 deletion headers/miscres.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extern void (*mtrig2)();
extern void (*mtrig3)();

extern void restore_active_ovly();
extern void findpath(char const *filename, char *fullpathname);
extern void notdiskmsg();
extern void cvtcentermag(double *, double *, LDBL *, double *, double *, double *);
extern void cvtcorners(double, double, LDBL, double, double, double);
Expand Down
18 changes: 18 additions & 0 deletions headers/search_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <cstdlib>
#include <functional>
#include <string>

#ifdef WIN32
constexpr const char *const PATH_SEPARATOR{";"};
#else
constexpr const char *const PATH_SEPARATOR{":"};
#endif

std::string search_path(const char *filename, const char *path_var, std::function<const char *(const char *)> get_env);

inline std::string search_path(const char * filename, const char *path_var)
{
return search_path(filename, path_var, [](const char *var) -> const char * { return std::getenv(var); });
}
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ set(ID_TEST_DATA_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_data")
SET(ID_TEST_FIRST_IFS_NAME "binary")
set(ID_TEST_FIRST_IFS_PARAM1 ".5")
set(ID_TEST_SECOND_IFS_NAME "3dfern")
set(ID_TEST_IFS_FILE "test.ifs")
configure_file(test.ifs.in "${ID_TEST_DATA_DIR}/${ID_TEST_IFS_FILE}")

set(ID_TEST_DATA_SUBDIR "${ID_TEST_DATA_DIR}/subdir")
set(ID_TEST_IFS_FILE2 "test2.ifs")
configure_file(test.ifs.in "${ID_TEST_DATA_SUBDIR}/${ID_TEST_IFS_FILE2}")

configure_file(test_data.h.in include/test_data.h @ONLY)
configure_file(test.ifs.in "${ID_TEST_DATA_DIR}/test.ifs")

add_executable(test-id
test.ifs.in
"${ID_TEST_DATA_DIR}/test.ifs"
"${CMAKE_CURRENT_BINARY_DIR}/include/test_data.h"
test_data.h.in
test_find_path.cpp
test_get_ifs_token.cpp
test_make_path.cpp
test_search_path.cpp
test_update_save_name.cpp
)
source_group("CMake Templates" REGULAR_EXPRESSION ".*\\.in$")
Expand Down
7 changes: 5 additions & 2 deletions tests/test_data.h.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#define ID_TEST_DATA_DIR "@ID_TEST_DATA_DIR@"
#define ID_TEST_DATA_DIR "@ID_TEST_DATA_DIR@"
#define ID_TEST_DATA_SUBDIR "@ID_TEST_DATA_SUBDIR@"

// IFS test data
#define ID_TEST_IFS_FILE "test.ifs"
#define ID_TEST_IFS_FILE "@ID_TEST_IFS_FILE@"
#define ID_TEST_FIRST_IFS_NAME "@ID_TEST_FIRST_IFS_NAME@"
#define ID_TEST_FIRST_IFS_PARAM1 "@ID_TEST_FIRST_IFS_PARAM1@"
#define ID_TEST_SECOND_IFS_NAME "@ID_TEST_SECOND_IFS_NAME@"

#define ID_TEST_IFS_FILE2 "@ID_TEST_IFS_FILE2@"
Loading

0 comments on commit 817a255

Please sign in to comment.