-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileMan.cpp
More file actions
280 lines (241 loc) · 6.43 KB
/
fileMan.cpp
File metadata and controls
280 lines (241 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "fileMan.h"
fileMan::fileMan(string l, string r, size_t x, size_t y)
: activeLeft(true), posX(x), posY(y),
left(new side(l, x, y)), right(new side(r, x+40, y )), active(nullptr)
{
active = left;
ramka();
left->show();
right->show();
right->writePath();
left->writePath();
right->inactive();
}
void fileMan::keyLisener()
{
int key[2] = {0};
do
{
key[0] = _getch();
clearStatus();
if (_kbhit())
key[1] = _getch();
switch (key[0])
{
case ENTER:
active->enter();
break;
case TAB:
active->inactive();
active = activeLeft ? right : left;
activeLeft = activeLeft ? false : true;
active->active();
break;
case SKIP:
switch (key[1])
{
case UP:
active->mooveUp();
break;
case DOWN:
active->mooveDown();
break;
}
break;
case FSCIP:
switch (key[1])
{
case F5:
copy();
break;
case F7:
makeDir();
break;
}
break;
}
} while (key[0]!=ESC);
}
bool fileMan::copyFile(string pathSrc, string pathDst, string fileName)
{
if (!pathSrc.compare(pathDst))
{
sMessage("We can't cpy in the same folder.");
return false;
}
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1 });
for (size_t a = 80; a; --a) cout << ' ';
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1 });
cout << "Copying: " << fileName;
if (findExistedItem(pathDst, fileName)) // òî òàêèé ôàéë âæå º
{
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
SetConsoleTextAttribute(side::hand, 7);
cout << "File " << fileName << "already exists! rwrite? (enter/esc)";
int key = 0;
do
{
key = _getch();
} while (key != ENTER && key != ESC);
if (key == ESC)
return false;
}
#ifdef DEBUG_FILEMAN_COPY_FILE
string s1(pathSrc + fileName);
string s2(pathDst + fileName);
#endif
bool res = CopyFileA((pathSrc+fileName).c_str(), (pathDst+fileName).c_str(), 0);
return res;
}
bool fileMan::findExistedItem(string path, string name)
{
_finddata_t finfo;
_set_errno(0);
intptr_t done = _findfirst((path + name).c_str(), &finfo);
if (done == -1 && (errno & ENOENT)) // ôàéë íå çíàéäåíî
{
_findclose(done);
return false;
}
//ôàéë çíàéäåíî
_findclose(done);
return true;
}
bool fileMan::copyDir(string pathSrc, string pathDst, string dirName)
{
if (!pathSrc.compare(pathDst))
{
sMessage("We can't cpy in the same folder.");
return false;
}
else if (pathDst.find_first_of(pathSrc) != string::npos)
return false;
if (findExistedItem(pathDst, dirName)) // òî òàêà ïàïêà âæå º
{
sMessage(string("Dir ") + dirName + "already exists! rwrite? (enter/esc)");
//SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles });
//SetConsoleTextAttribute(side::hand, 7);
//cout << "Dir " << dirName << "already exists! rwrite? (enter/esc)";
int key = 0;
do
{
key = _getch();
} while (key != ENTER && key != ESC);
if (key == ESC)
return false;
}
#ifdef DEBUG_FILEMAN_COPY_DIR
string newdir(pathDst + dirName);
#endif
_mkdir((pathDst + dirName).c_str());
_finddata_t finfo;
intptr_t done = _findfirst((pathSrc + dirName + '\\' + "*.*").c_str(), &finfo);
if (done == -1)
{
_findclose(done);
return false;
}
#ifdef DEBUG_FILEMAN_COPY_DIR
bool isDir = finfo.attrib & _A_SUBDIR;
string src(pathSrc + dirName + '\\');
string dst(pathDst + dirName + '\\');
string name(finfo.name);
#endif
if (_stricmp(finfo.name, ".") && _stricmp(finfo.name, ".."))
{
if (finfo.attrib & _A_SUBDIR) // ÿêùî ïàïêà - ðåêóðñ³ÿ
copyDir(pathSrc + dirName + '\\', pathDst + dirName + '\\', finfo.name);
else // êîï³þºìî ôàéë
copyFile(pathSrc + dirName + '\\', pathDst + dirName + '\\', finfo.name);
}
while (_findnext(done, &finfo) != -1) // òå æ ñàìå ò³ëüêè â öèêë³
{
#ifdef DEBUG_FILEMAN_COPY_DIR
bool isDir = finfo.attrib & _A_SUBDIR;
string src(pathSrc + dirName + '\\');
string dst(pathDst + dirName + '\\');
string name(finfo.name);
#endif
if (_stricmp(finfo.name, ".") && _stricmp(finfo.name, ".."))
{
if (finfo.attrib & _A_SUBDIR) // ÿêùî ïàïêà - ðåêóðñ³ÿ
copyDir(pathSrc + dirName + '\\', pathDst + dirName + '\\', finfo.name);
else // êîï³þºìî ôàéë
copyFile(pathSrc + dirName + '\\', pathDst + dirName + '\\', finfo.name);
}
}
_findclose(done);
return true;
}
bool fileMan::copy()
{
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
SetConsoleTextAttribute(side::hand, 7);
cout << "Do you want to copy: " << active->getActiveFInfo().name << "(enter/esc)";
int key = 0;
do
{
key = _getch();
} while (key != ENTER && key != ESC);
if (key == ESC)
return false;
bool res = 0;
#ifdef DEBUG_FILEMAN_COPY
string src(active->getPath());
string dst(getInactive()->getPath());
string name(active->getActiveFInfo().name);
bool isDir = active->getActiveFInfo().attrib & _A_SUBDIR;
#endif
if (active->getActiveFInfo().attrib & _A_SUBDIR) // ÿêùî ïàïêà - ðåêóðñ³ÿ
res = copyDir(active->getPath(), getInactive()->getPath(), active->getActiveFInfo().name);
else // êîï³þºìî ôàéë
res = copyFile(active->getPath(), getInactive()->getPath(), active->getActiveFInfo().name);
if (!res)
{
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
for (size_t a = 80; a; --a) cout << ' ';
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
cout << "Copy fail!";
return false;
}
getInactive()->fillFiles();
getInactive()->clearIndex();
getInactive()->show();
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
for (size_t a = 80; a; --a) cout << ' ';
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles +1});
cout << "Copy complete.";
return true;
}
void fileMan::makeDir()
{
sMessage("Enter the name of new dir: ");
string name;
std::cin >> name;
active->makeDir(name);
}
fileMan::~fileMan()
{
delete left;
delete right;
}
void fileMan::ramka() const
{
// head
SetConsoleCursorPosition(side::hand, { 36, 0 });
cout << "fileman :)";
// top
SetConsoleCursorPosition(side::hand, { posX, posY - 2 });
printf_s("%c", 218);
for (size_t a = 1; a <= 38; ++a) printf_s("%c", 196);
printf_s("%c%c", 194,194);
for (size_t a = 1; a <= 38; ++a) printf_s("%c", 196);
printf_s("%c", 191);
// bottom
SetConsoleCursorPosition(side::hand, { posX, posY + side::countFiles});
printf_s("%c", 192);
for (size_t a = 1; a <= 38; ++a) printf_s("%c", 196);
printf_s("%c%c", 193, 193);
for (size_t a = 1; a <= 38; ++a) printf_s("%c", 196);
printf_s("%c", 217);
}