Skip to content

Commit

Permalink
Begin using std::filesystem in DDIO
Browse files Browse the repository at this point in the history
Use std::filesystem for trivially-translateable use cases within DDIO.
  • Loading branch information
tophyr committed Apr 28, 2024
1 parent d1a6759 commit a74484e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 72 deletions.
31 changes: 0 additions & 31 deletions ddio_lnx/lnxfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@
// ---------------------------------------------------------------------------
// File operations

// creates a directory or folder on disk
bool ddio_CreateDir(const char *path) { return (mkdir(path, S_IRWXU)) ? false : true; }

// destroys a directory
bool ddio_RemoveDir(const char *path) { return (rmdir(path)) ? false : true; }

// retrieve the current working folder where file operation will occur.
void ddio_GetWorkingDir(char *path, int len) { getcwd(path, len); }

Expand Down Expand Up @@ -214,9 +208,6 @@ void ddio_CopyFileTime(char *destname, const char *srcname) {
Int3();
}

// deletes a file. Returns 1 if successful, 0 on failure
int ddio_DeleteFile(char *name) { return (!unlink(name)); }

// Save/Restore the current working directory

static char SavedWorkingDir[_MAX_DIR];
Expand All @@ -225,18 +216,6 @@ void ddio_SaveWorkingDir(void) { ddio_GetWorkingDir(SavedWorkingDir, _MAX_DIR);

void ddio_RestoreWorkingDir(void) { ddio_SetWorkingDir(SavedWorkingDir); }

// Checks if a directory exists (returns 1 if it does, 0 if not)
// This pathname is *RELATIVE* not fully qualified
bool ddio_DirExists(const char *path) {
bool res;

ddio_SaveWorkingDir();
res = ddio_SetWorkingDir(path);
ddio_RestoreWorkingDir();

return (res) ? true : false;
}

// rcg06192000 extern "C" is my add, so nettest would link.
// extern "C"
//{
Expand Down Expand Up @@ -783,16 +762,6 @@ bool ddio_GetFullPath(char *full_path, const char *rel_path) {
return 1;
}

// Renames file
// Returns true on success or false on an error
bool ddio_RenameFile(char *oldfile, char *newfile) {
int rcode = rename(oldfile, newfile);
if (!rcode)
return true;
else
return false;
}

// Generates a temporary filename based on the prefix, and basedir
// Parameters:
// basedir - directory to put the files
Expand Down
31 changes: 0 additions & 31 deletions ddio_win/winfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@
// ---------------------------------------------------------------------------
// File operations

// creates a directory or folder on disk
bool ddio_CreateDir(const char *path) { return (CreateDirectory(path, NULL)) ? true : false; }

// destroys a directory
bool ddio_RemoveDir(const char *path) { return (RemoveDirectory(path)) ? true : false; }

// retrieve the current working folder where file operation will occur.
void ddio_GetWorkingDir(char *path, int len) { GetCurrentDirectory(len, path); }

Expand Down Expand Up @@ -235,9 +229,6 @@ try_again:;
CloseHandle(srchandle);
}

// deletes a file. Returns 1 if successful, 0 on failure
int ddio_DeleteFile(char *name) { return (DeleteFile(name)); }

// Save/Restore the current working directory

static char SavedWorkingDir[_MAX_DIR];
Expand All @@ -246,18 +237,6 @@ void ddio_SaveWorkingDir(void) { GetCurrentDirectory(_MAX_DIR, SavedWorkingDir);

void ddio_RestoreWorkingDir(void) { SetCurrentDirectory(SavedWorkingDir); }

// Checks if a directory exists (returns 1 if it does, 0 if not)
// This pathname is *RELATIVE* not fully qualified
bool ddio_DirExists(const char *path) {
BOOL res;

ddio_SaveWorkingDir();
res = SetCurrentDirectory(path);
ddio_RestoreWorkingDir();

return (res) ? true : false;
}

// Constructs a path in the local file system's syntax
// newPath: stores the constructed path
// absolutePathHeader: absolute path on which the sub directories will be appended
Expand Down Expand Up @@ -610,16 +589,6 @@ bool ddio_GetFullPath(char *full_path, const char *rel_path) {
return 1;
}

// Renames file
// Returns true on success or false on an error
bool ddio_RenameFile(char *oldfile, char *newfile) {
int rcode = rename(oldfile, newfile);
if (!rcode)
return true;
else
return false;
}

// Give a volume label to look for, and if it's found returns a path
// If it isn't found, return NULL (used to return "")
char *ddio_GetCDDrive(char *vol) {
Expand Down
37 changes: 27 additions & 10 deletions lib/ddio.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@

class oeApplication;

#include <filesystem>
#include <stdio.h>

#include "pstypes.h"
Expand Down Expand Up @@ -320,14 +321,25 @@ void ddio_MouseSetVCoords(int width, int height);
// File Operations
// ---------------------------------------------------------------------------

// creates or destroys a directory or folder on disk
// This pathname is *RELATIVE* not fully qualified
bool ddio_CreateDir(const char *path);
bool ddio_RemoveDir(const char *path);
// creates a directory on disk
inline bool ddio_CreateDir(std::filesystem::path const& path) {
std::error_code ec{};
std::filesystem::create_directory(path, ec);
return !ec;
}

inline bool ddio_RemoveDir(std::filesystem::path const &path) {
std::error_code ec{};
std::filesystem::remove(path, ec);
return !ec;
}

// deletes a file. Returns 1 if successful, 0 on failure
// This pathname is *RELATIVE* not fully qualified
int ddio_DeleteFile(char *name);
inline int ddio_DeleteFile(std::filesystem::path const &path) {
std::error_code ec{};
std::filesystem::remove(path, ec);
return ec ? 0 : 1;
}

// Save/Restore the current working directory
void ddio_SaveWorkingDir(void);
Expand All @@ -339,9 +351,10 @@ void ddio_RestoreWorkingDir(void);
void ddio_GetWorkingDir(char *path, int len);
bool ddio_SetWorkingDir(const char *path);

// Checks if a directory exists (returns 1 if it does, 0 if not)
// This pathname is *RELATIVE* not fully qualified
bool ddio_DirExists(const char *path);
// Checks if a directory exists
inline bool ddio_DirExists(std::filesystem::path const &path) {
return std::filesystem::is_directory(path);
}

// get a file length of a FILE
int ddio_GetFileLength(FILE *filePtr);
Expand Down Expand Up @@ -415,7 +428,11 @@ bool ddio_GetTempFileName(char *basedir, char *prefix, char *filename);

// Renames file
// Returns true on success or false on an error
bool ddio_RenameFile(char *oldfile, char *newfile);
inline bool ddio_RenameFile(std::filesystem::path const& oldpath, std::filesystem::path const& newpath) {
std::error_code ec{};
std::filesystem::rename(oldpath, newpath, ec);
return !ec;
}

// Give a volume label to look for, and if it's found returns a path
// If it isn't found, return ""
Expand Down

0 comments on commit a74484e

Please sign in to comment.