Skip to content

Commit

Permalink
ADDED: MakeDirectory()
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed Sep 22, 2024
1 parent 1eb8ff5 commit 55a25ac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ RLAPI const char *GetDirectoryPath(const char *filePath); // Get full pa
RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
RLAPI const char *GetApplicationDirectory(void); // Get the directory of the running application (uses static string)
RLAPI int MakeDirectory(const char *dirPath); // Create directories (including full path requested), returns 0 on success
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
RLAPI bool IsPathFile(const char *path); // Check if a given path is a file or a directory
RLAPI bool IsFileNameValid(const char *fileName); // Check if fileName is valid for the platform/OS
Expand Down
47 changes: 45 additions & 2 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,25 @@ unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#endif

#if defined(_WIN32)
#include <direct.h> // Required for: _getch(), _chdir()
#include <io.h> // Required for: _access() [Used in FileExists()]
#include <direct.h> // Required for: _getch(), _chdir(), _mkdir()
#define GETCWD _getcwd // NOTE: MSDN recommends not to use getcwd(), chdir()
#define CHDIR _chdir
#include <io.h> // Required for: _access() [Used in FileExists()]
#else
#include <unistd.h> // Required for: getch(), chdir() (POSIX), access()
#define GETCWD getcwd
#define CHDIR chdir
#endif

#if defined(_MSC_VER) && ((defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__))
#include <direct.h> // Required for: _mkdir()
#define MKDIR(dir) _mkdir(dir)
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h> // Required for: mkdir()
#define MKDIR(dir) mkdir(dir) // OLD: mkdir(dir, 0777) -> w64devkit complaints!
#endif

//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -2267,6 +2276,40 @@ void UnloadDirectoryFiles(FilePathList files)
RL_FREE(files.paths);
}

// Create directories (including full path requested), returns 0 on success
int MakeDirectory(const char *dirPath)
{
if ((dirPath == NULL) || (dirPath[0] == '\0')) return 1; // Path is not valid
if (DirectoryExists(dirPath)) return 0; // Path already exists (is valid)

// Copy path string to avoid modifying original
int len = (int)strlen(dirPath) + 1;
char *pathcpy = (char *)RL_CALLOC(len, 1);
memcpy(pathcpy, dirPath, len);

// Iterate over pathcpy, create each subdirectory as needed
for (int i = 0; (i < len) && (pathcpy[i] != '\0'); i++)
{
if (pathcpy[i] == ':') i++;
else
{
if ((pathcpy[i] == '\\') || (pathcpy[i] == '/'))
{
pathcpy[i] = '\0';
if (!DirectoryExists(pathcpy)) MKDIR(pathcpy);
pathcpy[i] = '/';
}
}
}

// Create final directory
if (!DirectoryExists(pathcpy)) MKDIR(pathcpy);

RL_FREE(pathcpy);

return 0;
}

// Change working directory, returns true on success
bool ChangeDirectory(const char *dir)
{
Expand Down

0 comments on commit 55a25ac

Please sign in to comment.