Skip to content

Commit ae60947

Browse files
ejaquayejaquay
authored andcommitted
SDC use std::filesystem for host file operations
Take advantage of c++17 to clean things up a little bit
1 parent df7dc18 commit ae60947

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

sdc/sdc_cartridge.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@
141141
//----------------------------------------------------------------------
142142
//#define USE_LOGGING
143143

144-
#include <Shlwapi.h> // for PathIsDirectoryEmpty
145-
146144
#include <Windows.h>
145+
#include <filesystem>
147146
#include <vcc/devices/rtc/ds1315.h>
148147
#include <vcc/common/logger.h>
149148
#include <vcc/core/cartridge_capi.h>
@@ -450,6 +449,15 @@ void LoadRom(unsigned char bank)
450449
return;
451450
}
452451

452+
//----------------------------------------------------------------------
453+
// Determine if path is a directory
454+
//----------------------------------------------------------------------
455+
bool IsDirectory(const char * path)
456+
{
457+
std::filesystem::file_status stat = std::filesystem::status(path);
458+
return std::filesystem::is_directory(stat);
459+
}
460+
//
453461
//----------------------------------------------------------------------
454462
// Parse the startup.cfg file
455463
//----------------------------------------------------------------------
@@ -1816,45 +1824,52 @@ void RenameFile(const char *names)
18161824

18171825
GetFullPath(from,names);
18181826
GetFullPath(target,1+strchr(names,'\0'));
1819-
18201827
DLOG_C("UpdateSD rename %s %s\n",from,target);
18211828

1822-
if (std::rename(from,target)) {
1823-
DLOG_C("RenameFile %s\n", strerror(errno));
1824-
IF.status = STA_FAIL | STA_WIN_ERROR;
1825-
} else {
1829+
if (!std::filesystem::exists(from)) {
1830+
IF.status = STA_FAIL | STA_NOTFOUND;
1831+
return;
1832+
}
1833+
1834+
if (std::filesystem::exists(target)) {
1835+
IF.status = STA_FAIL | STA_INVALID;
1836+
return;
1837+
}
1838+
1839+
try {
1840+
std::filesystem::rename(from, target);
18261841
IF.status = STA_NORMAL;
1842+
} catch (const std::filesystem::filesystem_error&) {
1843+
IF.status = STA_FAIL | STA_WIN_ERROR;
18271844
}
18281845
return;
18291846
}
18301847

18311848
//----------------------------------------------------------------------
1832-
// Delete disk or directory
1849+
// Delete disk image or directory
18331850
//----------------------------------------------------------------------
18341851
void KillFile(const char *file)
18351852
{
18361853
char path[MAX_PATH];
18371854
GetFullPath(path,file);
18381855
DLOG_C("KillFile delete %s\n",path);
18391856

1857+
if (!std::filesystem::exists(path)) {
1858+
IF.status = STA_FAIL | STA_NOTFOUND;
1859+
return;
1860+
}
1861+
18401862
if (IsDirectory(path)) {
1841-
if (PathIsDirectoryEmpty(path)) {
1842-
if (RemoveDirectory(path)) {
1843-
IF.status = STA_NORMAL;
1844-
} else {
1845-
DLOG_C("Deletefile %s\n", strerror(errno));
1846-
IF.status = STA_FAIL | STA_NOTFOUND;
1847-
}
1848-
} else {
1863+
if (!std::filesystem::is_empty(path)) {
18491864
IF.status = STA_FAIL | STA_NOTEMPTY;
1865+
return;
18501866
}
1867+
}
1868+
1869+
if (std::filesystem::remove(path)) {
1870+
IF.status = STA_NORMAL;
18511871
} else {
1852-
if (DeleteFile(path)) {
1853-
IF.status = STA_NORMAL;
1854-
} else {
1855-
DLOG_C("Deletefile %s\n", strerror(errno));
1856-
IF.status = STA_FAIL | STA_NOTFOUND;
1857-
}
1872+
IF.status = STA_FAIL | STA_WIN_ERROR;
18581873
}
18591874
return;
18601875
}
@@ -1868,15 +1883,12 @@ void MakeDirectory(const char *name)
18681883
GetFullPath(path,name);
18691884
DLOG_C("MakeDirectory %s\n",path);
18701885

1871-
// Make sure directory is not in use
1872-
struct _stat file_stat;
1873-
int result = _stat(path,&file_stat);
1874-
if (result == 0) {
1886+
if (std::filesystem::exists(path)) {
18751887
IF.status = STA_FAIL | STA_INVALID;
18761888
return;
18771889
}
18781890

1879-
if (CreateDirectory(path,nullptr)) {
1891+
if (std::filesystem::create_directory(path)) {
18801892
IF.status = STA_NORMAL;
18811893
} else {
18821894
DLOG_C("MakeDirectory %s\n", strerror(errno));
@@ -1909,17 +1921,6 @@ void AppendPathChar(char * path, char c)
19091921
path[l+1] = '\0';
19101922
}
19111923

1912-
//----------------------------------------------------------------------
1913-
// Determine if path is a direcory
1914-
//----------------------------------------------------------------------
1915-
bool IsDirectory(const char * path)
1916-
{
1917-
struct _stat file_stat;
1918-
int result = _stat(path,&file_stat);
1919-
if (result != 0) return false;
1920-
return ((file_stat.st_mode & _S_IFDIR) != 0);
1921-
}
1922-
19231924
//----------------------------------------------------------------------
19241925
// Set the Current Directory relative to previous current or to SDRoot
19251926
// This is complicated by the many ways a user can change the directory

sdc/sdc_cartridge.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ unsigned char MemRead(unsigned short);
3939
LRESULT CALLBACK SDC_Control(HWND, UINT, WPARAM, LPARAM);
4040
LRESULT CALLBACK SDC_Configure(HWND, UINT, WPARAM, LPARAM);
4141

42-
// frendly functions
4342
void CloseDrive(int);
4443
bool IsDirectory(const char *);
4544
unsigned char WriteFlashBank(unsigned short);

sdc/sdc_configuration.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// SDC simulator E J Jaquay 2025
2222
//----------------------------------------------------------------------
2323
//
24-
// frendly functions
2524
void update_disk0_box();
2625
void LoadConfig();
2726
void BuildCartridgeMenu();

0 commit comments

Comments
 (0)