| 1 |
#include <sys\stat.h> |
|---|
| 2 |
#include "dirent.h" |
|---|
| 3 |
|
|---|
| 4 |
#include <windows.h> |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
#include "file.h" |
|---|
| 8 |
#include <direct.h> |
|---|
| 9 |
|
|---|
| 10 |
void File::_getDetails() |
|---|
| 11 |
{ struct stat statData; |
|---|
| 12 |
|
|---|
| 13 |
if (this->path.length() < 5) |
|---|
| 14 |
{ this->does_exist = true; |
|---|
| 15 |
this->size = 0; |
|---|
| 16 |
this->is_dir = true; |
|---|
| 17 |
this->is_writable = true; |
|---|
| 18 |
} |
|---|
| 19 |
else |
|---|
| 20 |
{ if (stat (this->path.c_str(), &statData) == -1) |
|---|
| 21 |
{ this->does_exist = false; |
|---|
| 22 |
this->size = 0; |
|---|
| 23 |
this->is_dir = false; |
|---|
| 24 |
this->is_writable = false; |
|---|
| 25 |
} |
|---|
| 26 |
else |
|---|
| 27 |
{ this->does_exist = (statData.st_mode & (S_IFDIR | S_IFREG)) != 0; |
|---|
| 28 |
this->is_dir = ((statData.st_mode & S_IFDIR) != 0); |
|---|
| 29 |
this->size = statData.st_size; |
|---|
| 30 |
this->is_writable = ((statData.st_mode & S_IWRITE) != 0); |
|---|
| 31 |
} |
|---|
| 32 |
} |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
void File::getChildren() |
|---|
| 36 |
{ DIR * directory; |
|---|
| 37 |
struct dirent * entry; |
|---|
| 38 |
|
|---|
| 39 |
directory = opendir(this->path.c_str()); |
|---|
| 40 |
if (directory == NULL) |
|---|
| 41 |
{ return; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
while ((entry = readdir(directory)) != NULL) |
|---|
| 45 |
{ string childName; |
|---|
| 46 |
|
|---|
| 47 |
if (strcmp(entry->d_name, "..") == 0 || |
|---|
| 48 |
strcmp(entry->d_name, ".") == 0) |
|---|
| 49 |
{ continue; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
childName = this->path; |
|---|
| 53 |
childName.append("\\"); |
|---|
| 54 |
childName.append(entry->d_name); |
|---|
| 55 |
|
|---|
| 56 |
this->children.push_back(*(new File(childName))); |
|---|
| 57 |
} |
|---|
| 58 |
closedir(directory); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
File::File() |
|---|
| 62 |
{ this->size = 0; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
File::File(string filename) : FilePath(filename) |
|---|
| 66 |
{ |
|---|
| 67 |
this->_getDetails(); |
|---|
| 68 |
if (this->is_dir) |
|---|
| 69 |
{ this->getChildren(); |
|---|
| 70 |
} |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
unsigned long File::getDiskSpace(DiskSpace &diskSpace) |
|---|
| 84 |
{ unsigned long totalSize = 0; |
|---|
| 85 |
|
|---|
| 86 |
if (this->is_dir) |
|---|
| 87 |
{ vector<File>::iterator here = this->children.begin(); |
|---|
| 88 |
vector<File>::iterator end = this->children.end(); |
|---|
| 89 |
|
|---|
| 90 |
while (here != end) |
|---|
| 91 |
{ totalSize += here->getDiskSpace(diskSpace); |
|---|
| 92 |
here ++; |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
totalSize += diskSpace.fileSpace(this->size); |
|---|
| 96 |
return totalSize; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
unsigned long File::getRawFileSize() |
|---|
| 106 |
{ return this->size; |
|---|
| 107 |
} |
|---|
| 108 |
unsigned long File::getFileSize(){ unsigned long size, files; this->getFileSize(size, files); return size;} |
|---|
| 109 |
void File::getFileSize(unsigned long &totalSize, unsigned long &totalFiles) |
|---|
| 110 |
{ totalSize = 0; totalFiles = 1; |
|---|
| 111 |
|
|---|
| 112 |
if (this->is_dir) |
|---|
| 113 |
{ vector<File>::iterator here = this->children.begin(); |
|---|
| 114 |
vector<File>::iterator end = this->children.end(); |
|---|
| 115 |
|
|---|
| 116 |
while (here != end) |
|---|
| 117 |
{ unsigned long ldata, lcount; here->getFileSize(ldata, lcount); totalSize += ldata; totalFiles += lcount; |
|---|
| 118 |
here ++; |
|---|
| 119 |
} } totalSize += this->size;} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
vector<File>::iterator File::childBegin() |
|---|
| 123 |
{ return this->children.begin(); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
vector<File>::iterator File::childEnd() |
|---|
| 127 |
{ return this->children.end(); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
bool File::isDirectory() |
|---|
| 131 |
{ return this->is_dir; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
bool File::isWriteable() |
|---|
| 135 |
{ return this->is_writable; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
bool File::exists() |
|---|
| 139 |
{ return this->does_exist; |
|---|
| 140 |
} |
|---|
| 141 |
bool File::remove(){ bool ok = true; if (this->is_dir) { vector<File>::iterator here = this->children.begin(); vector<File>::iterator end = this->children.end(); while (here != end) { ok = here->remove() && ok; here ++; } ok = RemoveDirectory(this->path.c_str()) && ok; } else { ok = DeleteFile(this->path.c_str()) != 0 ? true : false; } return ok;} |
|---|
| 142 |
void File::copy(char *to) |
|---|
| 143 |
{ |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|