-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
203 lines (172 loc) · 5.93 KB
/
Main.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <filesystem>
using namespace std;
// Read a file and return the contents as a string.
template <typename F>
string readFromFile(const F& file)
{
ifstream filein { file };
string nextLine;
ostringstream stringout {};
while (filein && getline(filein, nextLine))
{
stringout << nextLine << endl;
}
return stringout.str();
}
struct Team
{
public:
int id;
string name;
string membersJSON;
};
struct Question
{
public:
int baseID;
string name;
string templateJSON;
};
void replaceAll(string& source, const string& original, const string& replacement)
{
// Find each position in the source where the original occurs and replace it.
// Brute force implementation since there won't be that many replacements to make.
for (auto pos { source.find(original) }; pos != string::npos; pos = source.find(original, pos))
{
source.replace(pos, original.length(), replacement);
}
}
// Populates the template with team information
string populateTemplate(const Question& question, const Team& team)
{
string templateString { question.templateJSON };
replaceAll(templateString, "@QID@", "QID" + to_string(question.baseID + team.id));
replaceAll(templateString, "@QTag@", question.name + "/" + team.name);
replaceAll(templateString, "@TeamID@", to_string(team.id));
replaceAll(templateString, "@TeamName@", team.name);
replaceAll(templateString, "@TeamMembers@", team.membersJSON);
return templateString;
}
// Returns a string containing a copy of the template question populated for each team.
string populateForAllTeams(const Question& question, const vector<Team>& teams)
{
ostringstream stringout;
for (const Team& team : teams)
{
stringout << populateTemplate(question, team);
if (&team != &teams.back())
{
stringout << ',' << endl; // Only print comma if not the last team
}
}
return stringout.str();
}
string buildTeamChoices(const vector<Team>& teams)
{
ostringstream stringout;
// Print team choices
stringout << "\"Choices\" : { " << endl;
for (const Team& team : teams)
{
stringout << '\"' << team.id << "\": {" << endl;
stringout << "\"Display\": \"" << team.name << "\"" << endl;
stringout << "}";
if (&team != &teams.back())
{
stringout << ','; // Only print comma if not the last team
}
stringout << endl;
}
stringout << "}," << endl;
// Specify the choice order
stringout << "\"ChoiceOrder\" : [ " << endl;
for (size_t i { 1 }; i <= teams.size(); i++)
{
stringout << i;
if (i != teams.size())
{
stringout << ','; // Only print comma if not the last team
}
stringout << endl;
}
stringout << "]," << endl;
// Specify which choice IDs would be used if more teams were to be added
stringout << "\"NextChoiceId\": " << teams.size() + 1;
return stringout.str();
}
// Wraps C-style args as a vector of strings.
// Skips argument 0 (the executable name).
vector<string> wrapArgs(int argc, char** argv)
{
vector<string> args {};
for (int i { 1 }; i < argc; i++)
{
args.emplace_back(argv[i]);
}
return args;
}
// Gets the base filename, without directory path or file extension.
string getBaseFileName(string filePath)
{
auto startPos { filePath.find_last_of('/') };
if (startPos == string::npos)
{
// If no forward slash is found, look for a backslash.
startPos = filePath.find_last_of('\\');
}
else
{
// If a forward slash was found, still check if there was a closer backslash.
auto altStartPos { filePath.find_last_of('\\') };
if (altStartPos != string::npos)
{
startPos = max(startPos, altStartPos);
}
}
return filePath.substr(startPos + 1, filePath.find_last_of('.') - startPos - 1);
}
int main(int argc, char* argv[])
{
vector<string> args { wrapArgs(argc, argv) };
// Last argument is the output file (not a team specification file).
string outFile = args.back();
args.pop_back();
// Populate list of teams from command-line arguments.
vector<Team> teams {};
int teamID { 1 };
for (string filename : args)
{
// Team name is the filename without the .json.
string teamName { getBaseFileName(filename) };
teams.push_back({ teamID, teamName, readFromFile(filename) });
teamID++;
}
// Read root template
string rootTemplate { readFromFile("TeamSkillsTemplate.json") };
// Define team choices for the "What team are you on" question.
replaceAll(rootTemplate, "@TeamChoices@", buildTeamChoices(teams));
// Read template for a question reference in the question list.
string baseQuestionRefTemplate { readFromFile("QuestionRef.json") };
int baseID { 20 }; // allow up to 20 questions that aren't different for each taem.
// Iterate templates in "Questions" directory,.
for (const auto& file : std::filesystem::directory_iterator("./Questions"))
{
// Template name = file name without .json.
string templateName { getBaseFileName(file.path().string()) };
// Read each template and populate it for all teams, then replace the placeholder string in the root template.
Question qTemplate { baseID, templateName, readFromFile(file.path()) };
Question questionRefTemplate { baseID, templateName, baseQuestionRefTemplate };
replaceAll(rootTemplate, "@" + templateName + "@", populateForAllTeams(qTemplate, teams));
replaceAll(rootTemplate, "@" + templateName + "Refs@", populateForAllTeams(questionRefTemplate, teams));
baseID += teams.size();
}
// Write out the final file to the specified output file.
ofstream fileout { outFile };
fileout << rootTemplate;
return 0;
}