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