Skip to content

Commit

Permalink
add file utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wootguy committed Nov 3, 2024
1 parent 358d860 commit 67d6cfe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dlls/enginecallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ EXPORT void DEBUG_MSG(ALERT_TYPE target, const char* format, ...);

#if defined(PLUGIN_BUILD) && defined(PLUGIN_NAME)
#define ALERT(target, fmt, ...) { \
DEBUG_MSG(target, "[" PLUGIN_NAME "] ", NULL); \
DEBUG_MSG(target, fmt, ##__VA_ARGS__, NULL); \
DEBUG_MSG(target, "[" PLUGIN_NAME "] "); \
DEBUG_MSG(target, fmt, ##__VA_ARGS__); \
}
#else
#define ALERT DEBUG_MSG
Expand Down
51 changes: 47 additions & 4 deletions dlls/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@

using namespace std::chrono;

#ifndef WIN32
#ifdef WIN32
#include <windows.h>
#include <direct.h>
#define mkdir(path, perms) _mkdir(path)
#define stat _stat
#else
#include <unistd.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#else
#define stat _stat
#include <sys/statvfs.h>
#endif

float UTIL_WeaponTimeBase( void )
Expand Down Expand Up @@ -2841,4 +2845,43 @@ float normalizeRangef(const float value, const float start, const float end)

return (offsetValue - (floor(offsetValue / width) * width)) + start;
// + start to reset back to start of original range
}
}

bool createFolder(const std::string& path) {
if (mkdir(path.c_str(), 0777) == 0) {
return true;
}

return false;
}

bool folderExists(const std::string& path) {
struct stat info;

if (stat(path.c_str(), &info) != 0) {
return false;
}

return (info.st_mode & S_IFDIR) != 0;
}


uint64_t getFreeSpace(const std::string& path) {
#if defined(_WIN32)
ULARGE_INTEGER freeBytesAvailable;
if (GetDiskFreeSpaceEx(path.c_str(), &freeBytesAvailable, NULL, NULL)) {
return freeBytesAvailable.QuadPart;
}
else {
ALERT(at_console, "Error getting free space.\n");
return 0;
}
#else
struct statvfs stat;
if (statvfs(path.c_str(), &stat) != 0) {
ALERT(at_console, "Error getting free space.\n");
return 0;
}
return stat.f_bavail * stat.f_frsize;
#endif
}
8 changes: 7 additions & 1 deletion dlls/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,10 @@ EXPORT void KickPlayer(edict_t* ent, const char* reason="");
// by assuming the range wraps around when going below min or above max
EXPORT float normalizeRangef(const float value, const float start, const float end);

EXPORT void handleThreadPrints();
EXPORT void handleThreadPrints();

EXPORT bool createFolder(const std::string& path);

EXPORT bool folderExists(const std::string& path);

EXPORT uint64_t getFreeSpace(const std::string& path);

0 comments on commit 67d6cfe

Please sign in to comment.