2008-11-22 22:07:07 +00:00
|
|
|
// This code is in the public domain -- castano@gmail.com
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
#if NV_OS_WIN32
|
2009-03-16 21:08:09 +00:00
|
|
|
#define _CRT_NONSTDC_NO_WARNINGS // _chdir is defined deprecated, but that's a bug, chdir is deprecated, _chdir is *not*.
|
2009-01-19 10:41:51 +00:00
|
|
|
//#include <shlwapi.h> // PathFileExists
|
|
|
|
#include <windows.h> // GetFileAttributes
|
2020-03-23 16:54:09 +00:00
|
|
|
#include <direct.h> // _mkdir, _chdir
|
2010-10-21 18:44:10 +00:00
|
|
|
#elif NV_OS_XBOX
|
|
|
|
#include <Xtl.h>
|
2014-11-04 17:49:29 +00:00
|
|
|
#elif NV_OS_ORBIS
|
|
|
|
#include <fios2.h>
|
2008-11-22 22:07:07 +00:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2009-01-09 02:24:32 +00:00
|
|
|
#include <unistd.h>
|
2008-11-22 22:07:07 +00:00
|
|
|
#endif
|
2010-05-28 07:45:11 +00:00
|
|
|
#include <stdio.h> // remove, unlink
|
2008-11-22 22:07:07 +00:00
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
|
|
|
|
|
|
|
|
bool FileSystem::exists(const char * path)
|
|
|
|
{
|
2009-01-09 02:24:32 +00:00
|
|
|
#if NV_OS_UNIX
|
2009-01-09 05:45:02 +00:00
|
|
|
return access(path, F_OK|R_OK) == 0;
|
|
|
|
//struct stat buf;
|
|
|
|
//return stat(path, &buf) == 0;
|
2010-10-21 18:44:10 +00:00
|
|
|
#elif NV_OS_WIN32 || NV_OS_XBOX
|
2009-01-19 10:41:51 +00:00
|
|
|
// PathFileExists requires linking to shlwapi.lib
|
|
|
|
//return PathFileExists(path) != 0;
|
2012-07-20 16:19:03 +00:00
|
|
|
return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
|
2009-01-09 02:24:32 +00:00
|
|
|
#else
|
|
|
|
if (FILE * fp = fopen(path, "r"))
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#endif
|
2008-11-22 22:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::createDirectory(const char * path)
|
|
|
|
{
|
2010-10-21 18:44:10 +00:00
|
|
|
#if NV_OS_WIN32 || NV_OS_XBOX
|
2012-02-03 16:23:52 +00:00
|
|
|
return CreateDirectoryA(path, NULL) != 0;
|
2014-11-04 17:49:29 +00:00
|
|
|
#elif NV_OS_ORBIS
|
|
|
|
// not implemented
|
|
|
|
return false;
|
2008-11-22 22:07:07 +00:00
|
|
|
#else
|
|
|
|
return mkdir(path, 0777) != -1;
|
|
|
|
#endif
|
|
|
|
}
|
2009-01-09 02:24:32 +00:00
|
|
|
|
2009-03-14 07:27:25 +00:00
|
|
|
bool FileSystem::changeDirectory(const char * path)
|
|
|
|
{
|
|
|
|
#if NV_OS_WIN32
|
|
|
|
return _chdir(path) != -1;
|
2010-10-21 18:44:10 +00:00
|
|
|
#elif NV_OS_XBOX
|
|
|
|
// Xbox doesn't support Current Working Directory!
|
|
|
|
return false;
|
2014-11-04 17:49:29 +00:00
|
|
|
#elif NV_OS_ORBIS
|
|
|
|
// Orbis doesn't support Current Working Directory!
|
|
|
|
return false;
|
2009-03-14 07:27:25 +00:00
|
|
|
#else
|
|
|
|
return chdir(path) != -1;
|
|
|
|
#endif
|
2010-05-27 23:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::removeFile(const char * path)
|
|
|
|
{
|
|
|
|
// @@ Use unlink or remove?
|
|
|
|
return remove(path) == 0;
|
2010-05-28 07:45:11 +00:00
|
|
|
}
|
2018-02-06 02:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "StdStream.h" // for fileOpen
|
|
|
|
|
|
|
|
bool FileSystem::copyFile(const char * src, const char * dst) {
|
|
|
|
|
|
|
|
FILE * fsrc = fileOpen(src, "rb");
|
|
|
|
if (fsrc == NULL) return false;
|
2020-03-30 17:06:10 +00:00
|
|
|
defer{ fclose(fsrc); };
|
2018-02-06 02:55:07 +00:00
|
|
|
|
|
|
|
FILE * fdst = fileOpen(dst, "wb");
|
|
|
|
if (fdst == NULL) return false;
|
2020-03-30 17:06:10 +00:00
|
|
|
defer{ fclose(fdst); };
|
2018-02-06 02:55:07 +00:00
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
while ((n = fread(buffer, sizeof(char), sizeof(buffer), fsrc)) > 0) {
|
|
|
|
if (fwrite(buffer, sizeof(char), n, fdst) != n) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|