-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmit.cpp
More file actions
162 lines (152 loc) · 4.84 KB
/
Submit.cpp
File metadata and controls
162 lines (152 loc) · 4.84 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
#include "Submit.h"
Submit::Submit(std::string id, std::string smTime, std::string folder)
:userID(id), folderName(folder)
{
submitTime = boost::posix_time::time_from_string(smTime);
setSubmitID();
}
Submit::Submit()
{}
std::string Submit::getUserFolderName() const
{
return "_" + userID;
}
std::string Submit::getSubmitFolderName() const
{
return "Sub" + std::to_string(submitID);
}
const std::string& Submit::getUserID() const
{
return userID;
}
int Submit::getSubmitID() const
{
return submitID;
}
const boost::posix_time::ptime& Submit::getSubmitTime() const
{
return submitTime;
}
const std::string& Submit::getFolderName() const
{
return folderName;
}
void Submit::setUserID(const std::string& uid)
{
userID = uid;
}
void Submit::setSubmitID(int id)
{
submitID = id;
}
void Submit::setSubmitTime(const boost::posix_time::ptime& time)
{
submitTime = time;
}
void Submit::setFolderName(const std::string& folder)
{
folderName = folder;
}
bool Submit::operator<(Submit other)
{
return (this->submitTime < other.submitTime);
}
bool Submit::operator>(Submit other)
{
return (this->submitTime > other.submitTime);
}
bool Submit::operator==(Submit other)
{
return (this->submitTime == other.submitTime);
}
bool Submit::operator<=(Submit other)
{
return (this->submitTime <= other.submitTime);
}
bool Submit::operator>=(Submit other)
{
return (this->submitTime >= other.submitTime);
}
Submit Submit::fromXMLFile(std::string path)
{
if (!boost::filesystem::exists(boost::filesystem::path(path)))
throw std::exception("File not found!");
tinyxml2::XMLDocument doc;
doc.LoadFile(path.c_str());
tinyxml2::XMLElement* rootEle = doc.FirstChildElement("Upload");
std::string folder = rootEle->FirstChildElement("FolderName")->GetText();
std::string id = rootEle->FirstChildElement("ID")->GetText();
std::string time = rootEle->FirstChildElement("Time")->GetText();
return Submit(id, time, folder);
}
std::string Submit::getExecutionFilePath()
{
std::string submitPath(serverPath + "\\" + this->getUserFolderName() + "\\" + this->getSubmitFolderName());
submitPath += "\\" + getExecutionFileName();
return submitPath;
}
std::string Submit::getExecutionFileName()
{
return this->getUserFolderName() + "_" + std::to_string(this->getSubmitID()) + ".exe";
}
void Submit::copyFile()
{
boost::filesystem::path srcPath(serverPath + "\\Unprocess\\" + this->folderName);
boost::filesystem::path desPath(serverPath + "\\_" + this->userID);
boost::filesystem::directory_iterator it(srcPath), eod;
std::string xmlPath = getXMLFilePath();
tinyxml2::XMLDocument doc;
doc.InsertEndChild(doc.NewDeclaration());
tinyxml2::XMLElement* rootEle = doc.NewElement("Files");
if (!boost::filesystem::exists(desPath))
boost::filesystem::create_directory(desPath);
if (!boost::filesystem::exists(boost::filesystem::path(desPath.string() + "\\Sub" + std::to_string(this->submitID))))
boost::filesystem::create_directory(boost::filesystem::path(desPath.string() + "\\Sub" + std::to_string(this->submitID)));
BOOST_FOREACH(boost::filesystem::path const &p, std::make_pair(it, eod))
{
if (boost::filesystem::is_regular_file(p) && (p.extension().string() == ".cpp" || p.extension().string() == ".h"))
{
std::string newFileName = serverPath + "\\_" + this->userID + "\\Sub" + std::to_string(this->submitID) + "\\" + p.filename().string();
boost::filesystem::path des(newFileName.c_str());
boost::filesystem::copy(p, des);
tinyxml2::XMLElement* ele = doc.NewElement("File");
ele->SetText(p.filename().string().c_str());
rootEle->InsertEndChild(ele);
}
}
doc.InsertEndChild(rootEle);
doc.SaveFile(xmlPath.c_str());
}
std::string Submit::getXMLFilePath()
{
return serverPath + "\\_" + this->userID + "\\Sub" + std::to_string(this->submitID) + "\\Files.xml";
}
void Submit::setSubmitID()
{
boost::filesystem::path submitInfoPath(serverPath + "\\" + this->getUserFolderName() + "\\UnloadInfo.xml");
if (!boost::filesystem::exists(submitInfoPath))
{
tinyxml2::XMLDocument doc;
doc.InsertFirstChild(doc.NewDeclaration());
tinyxml2::XMLElement* ele = doc.NewElement("Count");
ele->SetText(1);
doc.InsertEndChild(ele);
boost::filesystem::path xmlInfoPath(serverPath + "\\_" + userID + "\\Info.xml");
if (!boost::filesystem::exists(boost::filesystem::path(serverPath + "\\_" + userID)))
boost::filesystem::create_directory(boost::filesystem::path(serverPath + "\\_" + userID));
doc.SaveFile(submitInfoPath.string().c_str());
submitID = 1;
}
try
{
tinyxml2::XMLDocument doc;
doc.LoadFile(submitInfoPath.string().c_str());
submitID = std::stoi(doc.FirstChildElement("Count")->GetText()) + 1;
doc.FirstChildElement("Count")->SetText(submitID);
doc.SaveFile(submitInfoPath.string().c_str());
}
catch (...)
{
submitID = 1;
}
}