-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_command_os.hpp
79 lines (61 loc) · 1.8 KB
/
tf_command_os.hpp
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
#ifndef TFUTILS_TF_COMMAND_OS_HPP
#define TFUTILS_TF_COMMAND_OS_HPP
namespace tfutils {
class OSRemove : public AbstractCommand {
public:
using AbstractCommand::AbstractCommand;
Jint execute(const UP<ICommandArgs> &v) override {
if (v->isEmpty())
return -1;
return File::remove((*v)[0]);
}
};
class OSCopy : public AbstractCommand {
private:
constexpr static Jchar MODEL_READ[] = "rb";
constexpr static Jchar MODEL_WRITE[] = "wb";
constexpr static Jint SIZE_BUFFER = 4096 * 1024;
Jchar mBuffer[SIZE_BUFFER];
public:
OSCopy() : AbstractCommand(), mBuffer() {}
explicit OSCopy(const Jchar *v) : AbstractCommand(v), mBuffer() {}
Jint execute(const UP<ICommandArgs> &v) override {
Jint retLen = 0;
if (v->getLength() < 2)
return -1;
auto &&in = fopen((*v)[0], MODEL_READ);
auto &&ou = fopen((*v)[1], MODEL_WRITE);
auto &&inLen = File::getSize(in);
auto &&prog = Program(inLen);
do {
if ((in == nullptr) || (ou == nullptr))
break;
do {
retLen = fread(this->mBuffer, 1, sizeof(this->mBuffer), in);
if (retLen > 0) {
fwrite(this->mBuffer, retLen, 1, ou);
prog.update(retLen);
}
} while (retLen == sizeof(this->mBuffer));
} while (false);
if (in != nullptr)
fclose(in);
if (ou != nullptr)
fclose(ou);
return 0;
}
};
class OSMove : public AbstractCommand {
public:
using AbstractCommand::AbstractCommand;
Jint execute(const UP<ICommandArgs> &v) override {
SP<AbstractCommand> cmd;
cmd = make<OSCopy>();
if (cmd->execute(v) != 0)
return -1;
cmd = make<OSRemove>();
return cmd->execute(v);
}
};
} // namespace tfutils
#endif // TFUTILS_TF_COMMAND_OS_HPP