Skip to content

Commit

Permalink
more information like check if file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharkbyteprojects committed May 4, 2021
1 parent fca25c4 commit 6d2a31f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- <strong>Windows:</strong> you need Visual Studio and CMake
- <strong>Linux:</strong> you need make, cmake and a c++ compiler installed

- Use `cmake --build <path to store the build> --config Release` to build the app (the main app is `sharkFsStats` (`sharkFsStats.exe` on windows)).
- Use `cmake -S . -B <path to store the build>` then `cmake --build <path to store the build> --config Release` to build the app (the main app is `sharkFsStats` (`sharkFsStats.exe` on windows)).

## Command Line Arguments:

Expand Down
17 changes: 15 additions & 2 deletions firstuniversalWINuLINUXcpp/firstuniversalWINuLINUXcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@ void sharkName() {
#define cxExtendet (shark::uminiint)0b100
#define cxNone (shark::uminiint)0b000

template<typename booltype = shark::BOOL>
char* booltostring(booltype x) {
char* bExists = "false";
if (x) {
bExists = "true";
}
return bExists;
}

void content(shark::uminiint cx, char* path = ".") {
shark::BOOL cExtendet = cx & cxExtendet, cJson = cx & cxJson;
shark::int64 freediskspace = disks::freeDiskSpace(path);
shark::BOOL isFolder = disks::isPathFolder(path) & dfile, exists = disks::fileExists(path);
if (cJson) {
cout << "{" << "\"currentFreeSpace\": " << freediskspace;
if (cExtendet) {
cout << ", \"targetOS\": \"" << ostype << "\"";
cout << ", \"targetOS\": \"" << ostype << "\", \"pathexists\": " << booltostring(exists) <<
", \"isPathFile\": " << booltostring(isFolder);
}
}
else {
sharkName();
cout << "Current Free Space: " << freediskspace << " bytes\n";
if (cExtendet) {
cout << "Compiled for OS: " << ostype << "\n" << "Selected Path: " << path << "\n";
cout << "Compiled for OS: " << ostype << "\n" << "Selected Path: " << path << "\n" <<
"Selected Path Exists: " << booltostring(exists) << "\n" <<
"Is Path File: " << booltostring(isFolder) << "\n";
}
cout << flush;
}
Expand Down
41 changes: 41 additions & 0 deletions firstuniversalWINuLINUXcpp/getDiskStats/diskstats.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,45 @@ shark::int64 disks::freeDiskSpace(char* drivename) {
val = (shark::int64)(sfs.f_bsize * sfs.f_bfree);
#endif
return val;
}

shark::BOOL disks::fileExists(char* filename) {
#if !defined(linux)&&defined(windows)
return PathFileExistsA(filename);
#else
struct stat buffer;
return (stat(filename, &buffer) == 0);
#endif
}

shark::uminiint disks::isPathFolder(char* filename) {
shark::uminiint ret = dnothing;
#if !defined(linux)&&defined(windows)
DWORD x = GetFileAttributesA(filename);
if (x && FILE_ATTRIBUTE_DIRECTORY) {
ret |= dfolder;
}
else {
ret |= dfile;
}
#else
struct stat st;
if (stat(filename, &st) == 0) {
if (st.st_mode & S_IFDIR)
{
ret |= dfolder;
}
else if (st.st_mode & S_IFREG)
{
ret |= dfile;
}
else {
ret |= derr;
}
}
else {
ret |= derr;
}
#endif
return ret;
}
12 changes: 12 additions & 0 deletions firstuniversalWINuLINUXcpp/getDiskStats/diskstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@

#ifdef linux
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#else
#ifdef windows
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#else
#error Diskstats: OS not Supported!
#endif
#endif

#define dfolder 0b0001
#define dfile 0b0010
#define dnothing 0b0000
#define derr 0b0000

namespace disks {
shark::int64 freeDiskSpace(char* drivename);
shark::BOOL fileExists(char* filename);
shark::uminiint isPathFolder(char* filename);
}
2 changes: 1 addition & 1 deletion firstuniversalWINuLINUXcpp/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#endif

namespace shark {
typedef unsigned char BOOL;
typedef unsigned int BOOL;
typedef long long int64;
typedef unsigned char uminiint;
}
Expand Down

0 comments on commit 6d2a31f

Please sign in to comment.