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