Commit
Implement checking of directory size
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,28 @@ | |
|
||
#include "PlatformDetermination.h" | ||
#include "Exceptions.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <cmath> | ||
#include "float.h" | ||
#include <iterator> | ||
#include <algorithm> | ||
#include <functional> | ||
#include <cctype> | ||
#include <map> | ||
#include <locale> | ||
#include <fstream> | ||
#include <cerrno> | ||
#include <numeric> | ||
#include <set> | ||
|
||
#if defined(__ISWINDOWS__) | ||
#include "Windows.h" | ||
#endif | ||
|
||
// Always undef these stupid macros | ||
#undef min | ||
#undef max | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ibell
via email
Author
Contributor
|
||
|
||
#ifndef M_PI | ||
# define M_PI 3.14159265358979323846 | ||
|
@@ -66,8 +83,6 @@ | |
#endif | ||
|
||
#if defined(__powerpc__) | ||
#undef min | ||
#undef max | ||
#undef EOS | ||
#endif | ||
|
||
|
@@ -86,17 +101,58 @@ | |
#pragma message("WARNING: You need to implement DEPRECATED for this compiler") | ||
#define DEPRECATED(func) func | ||
#endif | ||
|
||
/// Copy string to wstring | ||
/// Dangerous if the string has non-ASCII characters; from http://stackoverflow.com/a/8969776/1360263 | ||
inline void StringToWString(const std::string &s, std::wstring &ws) | ||
This comment has been minimized.
Sorry, something went wrong.
mikekaganski
Contributor
|
||
{ | ||
ws = std::wstring(s.begin(), s.end()); | ||
} | ||
|
||
#include <iterator> | ||
#include <algorithm> | ||
#include <functional> | ||
#include <cctype> | ||
#include <map> | ||
#include <locale> | ||
#include <fstream> | ||
#include <cerrno> | ||
#include <numeric> | ||
#include <set> | ||
#if defined(__ISWINDOWS__) | ||
/// From http://stackoverflow.com/a/17827724/1360263 | ||
inline bool IsBrowsePath(const std::wstring& path) | ||
{ | ||
return (path == L"." || path == L".."); | ||
} | ||
inline unsigned long long CalculateDirSize(const std::wstring &path, std::vector<std::wstring> *errVect = NULL, unsigned long long size = 0) | ||
{ | ||
WIN32_FIND_DATA data; | ||
HANDLE sh = NULL; | ||
sh = FindFirstFile((path + L"\\*").c_str(), &data); | ||
This comment has been minimized.
Sorry, something went wrong.
mikekaganski
Contributor
|
||
|
||
if (sh == INVALID_HANDLE_VALUE ) | ||
{ | ||
//if we want, store all happened error | ||
if (errVect != NULL) | ||
errVect ->push_back(path); | ||
return size; | ||
} | ||
|
||
do | ||
{ | ||
// skip current and parent | ||
if (!IsBrowsePath(data.cFileName)) | ||
{ | ||
// if found object is ... | ||
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) | ||
// directory, then search it recursievly | ||
size = CalculateDirSize(path + L"\\" + data.cFileName, NULL, size); | ||
This comment has been minimized.
Sorry, something went wrong.
mikekaganski
Contributor
|
||
else | ||
// otherwise get object size and add it to directory size | ||
size += (unsigned long long) (data.nFileSizeHigh * (MAXDWORD ) + data.nFileSizeLow); | ||
This comment has been minimized.
Sorry, something went wrong.
mikekaganski
Contributor
|
||
} | ||
|
||
} while (FindNextFile(sh, &data)); // do | ||
|
||
FindClose(sh); | ||
|
||
return size; | ||
} | ||
#else | ||
/// Get the size of a directory in bytes | ||
unsigned long long CalculateDirSize(const std::string &path); | ||
#endif | ||
|
||
/// The following code for the trim functions was taken from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring | ||
// trim from start | ||
|
Why don't you define NOMINMAX? See minwindef.h. See http://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c
I would suggest defining it as compiler option, to cover all compilation units, no matter what: -DNOMINMAX
Also, using syntax like (std::min)(a,b) will prevent using macros. But this is the local solution...