Skip to content

Commit fd15980

Browse files
committed
chore: Build with better template file
1 parent 5281bc2 commit fd15980

2 files changed

Lines changed: 114 additions & 39 deletions

File tree

src/main.cpp

Lines changed: 112 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Project : leetcode-cpp
44
* Author : Wei Tan <tanwei.winterreise@gmail.com>
55
* Date : 2026-02-12 17:15:14
6-
* Last Modified Date: 2026-02-12 18:58:37
6+
* Last Modified Date: 2026-02-23 10:19:48
77
* Last Modified By : Wei Tan <tanwei.winterreise@gmail.com>
88
*/
99

@@ -14,9 +14,11 @@
1414
#include <iomanip>
1515
#include <iostream>
1616
#include <regex>
17+
#include <set>
1718
#include <sstream>
1819
#include <stdexcept>
1920
#include <string>
21+
#include <utility>
2022
#include <vector>
2123

2224
// This project headers
@@ -178,6 +180,81 @@ static std::string parse_extra_use(const std::string& code) {
178180
return extra;
179181
}
180182

183+
static std::string parse_std_headers(const std::string& code) {
184+
std::vector<std::pair<std::string, std::string>> mapping = {
185+
{"vector", "vector"},
186+
{"string", "string"},
187+
{"map", "map"},
188+
{"unordered_map", "unordered_map"},
189+
{"set", "set"},
190+
{"unordered_set", "unordered_set"},
191+
{"tuple", "tuple"},
192+
{"pair", "utility"},
193+
{"queue", "queue"},
194+
{"stack", "stack"},
195+
{"list", "list"},
196+
{"deque", "deque"},
197+
{"array", "array"},
198+
{"bitset", "bitset"},
199+
{"sort", "algorithm"},
200+
{"lower_bound", "algorithm"},
201+
{"upper_bound", "algorithm"},
202+
{"binary_search", "algorithm"},
203+
{"next_permutation", "algorithm"},
204+
{"reverse", "algorithm"},
205+
{"unique", "algorithm"},
206+
{"cout", "iostream"},
207+
{"cin", "iostream"},
208+
{"cerr", "iostream"},
209+
{"stringstream", "sstream"},
210+
{"istringstream", "sstream"},
211+
{"ostringstream", "sstream"},
212+
{"setw", "iomanip"},
213+
{"setfill", "iomanip"},
214+
{"setprecision", "iomanip"},
215+
{"function", "functional"},
216+
{"shared_ptr", "memory"},
217+
{"unique_ptr", "memory"},
218+
{"make_shared", "memory"},
219+
{"accumulate", "numeric"},
220+
{"iota", "numeric"},
221+
{"numeric_limits", "limits"},
222+
{"pow", "cmath"},
223+
{"sqrt", "cmath"},
224+
{"memset", "cstring"},
225+
{"memcpy", "cstring"},
226+
{"int64_t", "cstdint"},
227+
{"uint64_t", "cstdint"},
228+
{"size_t", "cstddef"},
229+
{"regex", "regex"},
230+
{"smatch", "regex"},
231+
{"regex_search", "regex"},
232+
{"enable_if", "type_traits"},
233+
{"is_same", "type_traits"}};
234+
235+
std::set<std::string> headers;
236+
for (auto& pr : mapping) {
237+
try {
238+
std::regex r("\\b" + pr.first + "\\b");
239+
if (std::regex_search(code, r)) {
240+
headers.insert(pr.second);
241+
}
242+
} catch (...) {
243+
// ignore regex errors for safety
244+
}
245+
}
246+
247+
if (headers.empty()) {
248+
headers.insert("iostream");
249+
}
250+
251+
std::ostringstream oss;
252+
for (const auto& header : headers) {
253+
oss << "#include <" << header << ">\n";
254+
}
255+
return oss.str();
256+
}
257+
181258
static std::string build_desc(const std::string& content) {
182259
std::string s = content;
183260
std::vector<std::pair<std::string, std::string>> reps = {
@@ -212,6 +289,13 @@ static std::string build_desc(const std::string& content) {
212289
s.replace(pos, needle.size(), repl);
213290
pos += repl.size();
214291
}
292+
// remove <font ...> and </font> tags that may appear in descriptions
293+
try {
294+
s = std::regex_replace(s, std::regex("<font[^>]*>"), std::string());
295+
s = std::regex_replace(s, std::regex("</font>"), std::string());
296+
} catch (...) {
297+
// ignore regex errors
298+
}
215299
return s;
216300
}
217301

@@ -239,6 +323,7 @@ static void deal_solving(int id) {
239323
throw std::runtime_error("problem file not found");
240324
}
241325
std::string base = found.filename().string();
326+
std::string orig_base = base;
242327
if (base.size() > 4 && base.substr(base.size() - 4) == ".cpp") {
243328
base.resize(base.size() - 4);
244329
}
@@ -251,11 +336,30 @@ static void deal_solving(int id) {
251336
throw std::runtime_error("solution already exists");
252337
}
253338
fs::rename(found, solution_path);
339+
// Update the File header inside the moved file: replace orig filename with
340+
// new one
341+
try {
342+
std::ifstream in(solution_path);
343+
if (in) {
344+
std::ostringstream ss;
345+
ss << in.rdbuf();
346+
std::string content = ss.str();
347+
std::string orig_name = orig_base;
348+
size_t pos = content.find(orig_name);
349+
if (pos != std::string::npos) {
350+
std::string new_name = base + ".cpp";
351+
content.replace(pos, orig_name.length(), new_name);
352+
std::ofstream out(solution_path);
353+
out << content;
354+
}
355+
}
356+
} catch (...) {
357+
// ignore errors updating header
358+
}
254359
}
255360

256361
static void deal_problem(const fetcher::Problem& problem,
257-
const fetcher::CodeDefinition& code,
258-
bool write_mod_file) {
362+
const fetcher::CodeDefinition& code) {
259363
std::ostringstream fn;
260364
fn << "p" << std::setw(4) << std::setfill('0') << problem.question_id
261365
<< "_";
@@ -305,8 +409,11 @@ static void deal_problem(const fetcher::Problem& problem,
305409
std::ostringstream idbuf;
306410
idbuf << std::setw(4) << std::setfill('0') << problem.question_id;
307411
replace_all(tpl, "__PROBLEM_ID__", idbuf.str());
412+
// Replace file name placeholder with actual file name (pXXXX_slug.cpp)
413+
replace_all(tpl, "__FILE_NAME__", file_name + ".cpp");
308414
}
309415
replace_all(tpl, "__EXTRA_USE__", parse_extra_use(default_code));
416+
replace_all(tpl, "__STL_INCLUDES__", parse_std_headers(default_code));
310417
replace_all(tpl, "__PROBLEM_LINK__",
311418
std::string("https://leetcode.com/problems/") +
312419
problem.title_slug +
@@ -323,8 +430,6 @@ static void deal_problem(const fetcher::Problem& problem,
323430
std::ofstream out(file_path);
324431
out << tpl;
325432
out.close();
326-
327-
(void)write_mod_file; // mod file generation intentionally disabled
328433
}
329434

330435
int main() {
@@ -377,7 +482,7 @@ int main() {
377482
continue;
378483
}
379484
try {
380-
deal_problem(*prob, prob->code_definition[0], true);
485+
deal_problem(*prob, prob->code_definition[0]);
381486
} catch (const std::exception& e) {
382487
std::cerr << "Error initializing problem " << fid << ": "
383488
<< e.what() << "\n";
@@ -395,7 +500,7 @@ int main() {
395500
problem->code_definition.empty() ? fetcher::CodeDefinition{}
396501
: problem->code_definition[0];
397502
try {
398-
deal_problem(*problem, cd, true);
503+
deal_problem(*problem, cd);
399504
} catch (const std::exception& e) {
400505
std::cerr << "Error: " << e.what() << "\n";
401506
return 1;

template.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
/**
2-
* File : template.cpp
2+
* File : __FILE_NAME__
33
* Project : leetcode-cpp
44
* Author : Wei Tan <tanwei.winterreise@gmail.com>
55
* Date : __CREATE_DATE__
66
* Last Modified Date: __LAST_MODIFIED_DATE__
77
* Last Modified By : Wei Tan <tanwei.winterreise@gmail.com>
88
*/
99

10-
#include <algorithm>
11-
#include <array>
12-
#include <bitset>
13-
#include <cassert>
14-
#include <cctype>
15-
#include <cerrno>
16-
#include <cmath>
17-
#include <cstddef>
18-
#include <cstdint>
19-
#include <cstring>
20-
#include <deque>
21-
#include <functional>
22-
#include <iomanip>
23-
#include <iostream>
24-
#include <limits>
25-
#include <list>
26-
#include <map>
27-
#include <memory>
28-
#include <numeric>
29-
#include <queue>
30-
#include <set>
31-
#include <sstream>
32-
#include <stack>
33-
#include <string>
34-
#include <tuple>
35-
#include <type_traits>
36-
#include <unordered_map>
37-
#include <unordered_set>
38-
#include <utility>
39-
#include <vector>
40-
4110
/**
4211
* [__PROBLEM_ID__] __PROBLEM_TITLE__
4312
*
@@ -47,6 +16,7 @@
4716
// problem: __PROBLEM_LINK__
4817
// discuss: __DISCUSS_LINK__
4918

19+
__STL_INCLUDES__
5020
using namespace std;
5121
__EXTRA_USE__
5222

0 commit comments

Comments
 (0)