Skip to content

Commit

Permalink
Implement isadirectory with std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
LegalizeAdulthood committed Feb 29, 2024
1 parent 402baf3 commit db1e4b8
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ add_library(libid
headers/get_ifs_token.h
common/help.cpp
common/intro.cpp headers/intro.h
headers/is_directory.h
common/jiim.cpp headers/jiim.h
headers/make_path.h
common/miscovl.cpp headers/miscovl.h
Expand Down Expand Up @@ -292,6 +293,7 @@ source_group("Header Files/common/ui" FILES
headers/help.h
headers/helpdefs.h
headers/intro.h
headers/is_directory.h
headers/jiim.h
headers/make_path.h
headers/miscovl.h
Expand Down
1 change: 1 addition & 0 deletions common/cmdfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "framain2.h"
#include "helpcom.h"
#include "id_data.h"
#include "is_directory.h"
#include "jb.h"
#include "line3d.h"
#include "loadfile.h"
Expand Down
1 change: 1 addition & 0 deletions common/prompts2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "helpdefs.h"
#include "id_data.h"
#include "id_io.h"
#include "is_directory.h"
#include "loadfile.h"
#include "loadmap.h"
#include "lorenz.h"
Expand Down
8 changes: 8 additions & 0 deletions headers/is_directory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <filesystem>

inline bool isadirectory(const char *s)
{
return std::filesystem::is_directory(s);
}
1 change: 0 additions & 1 deletion headers/prompts2.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ extern int get_view_params();
extern int get_starfield_params();
extern int get_commands();
extern void goodbye();
extern bool isadirectory(char const *s);
extern bool getafilename(char const *hdg, char const *file_template, char *flname);
extern bool getafilename(char const *hdg, char const *file_template, std::string &flname);
extern int splitpath(char const *file_template, char *drive, char *dir, char *fname, char *ext);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_executable(test-id
test_find_file.cpp
test_find_path.cpp
test_get_ifs_token.cpp
test_is_directory.cpp
test_make_path.cpp
test_path_match.cpp
test_search_path.cpp
Expand Down
21 changes: 21 additions & 0 deletions tests/test_is_directory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <is_directory.h>

#include "test_data.h"

#include <gtest/gtest.h>

#include <filesystem>

namespace fs = std::filesystem;

TEST(TestIsDirectory, affirmative)
{
EXPECT_TRUE(isadirectory(ID_TEST_DATA_DIR));
EXPECT_TRUE(isadirectory(ID_TEST_DATA_SUBDIR));
}

TEST(TestIsDirectory, negative)
{
EXPECT_FALSE(isadirectory((fs::path{ID_TEST_DATA_DIR} / ID_TEST_IFS_FILE).string().c_str()));
EXPECT_FALSE(isadirectory((fs::path{ID_TEST_DATA_SUBDIR} / ID_TEST_IFS_FILE2).string().c_str()));
}
40 changes: 0 additions & 40 deletions unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,6 @@ VIDEOINFO g_video_table[MAX_VIDEO_MODES]{};
// Global variables that should be phased out (old video mode stuff)
int g_video_vram = 0;

bool isadirectory(char const *s)
{
int len;
char sv;
if (std::strchr(s, '*') || std::strchr(s, '?'))
return false; // for my purposes, not a directory

len = (int) std::strlen(s);
if (len > 0)
sv = s[len-1]; // last char
else
sv = 0;

if (fr_findfirst(s) != 0) // couldn't find it
{
// any better ideas??
if (sv == SLASHC) // we'll guess it is a directory
return true;
else
return false; // no slashes - we'll guess it's a file
}
else if ((DTA.attribute & SUBDIR) != 0)
{
if (sv == SLASHC)
{
// strip trailing slash and try again
std::string path{s, &s[len-1]};
if (fr_findfirst(path.c_str()) != 0) // couldn't find it
return false;
else if ((DTA.attribute & SUBDIR) != 0)
return true; // we're SURE it's a directory
else
return false;
}
else
return true; // we're SURE it's a directory
}
return false;
}

// converts relative path to absolute path
int expand_dirname(char *dirname, char *drive)
{
Expand Down
5 changes: 0 additions & 5 deletions win32/os_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,6 @@ void home()
g_text_col = 0;
}

bool isadirectory(char const *s)
{
return PathIsDirectory(s) != 0;
}

// tenths of millisecond timewr routine
// static struct timeval tv_start;

Expand Down

0 comments on commit db1e4b8

Please sign in to comment.