1- /* *
2- * File : main.cpp
3- * Project : leetcode-cpp
4- * Author : Wei Tan <tanwei.winterreise@gmail.com>
5- * Date : 2026-02-12 15:07:32
6- * Last Modified Date: 2026-02-12 15:46:39
7- * Last Modified By : Wei Tan <tanwei.winterreise@gmail.com>
8- */
1+ #include " fetcher.h"
92
103#include < cstdlib>
114#include < filesystem>
1811#include < string>
1912#include < vector>
2013
21- // This project headers
22- #include " fetcher.h"
23-
2414namespace fs = std::filesystem;
2515using json = nlohmann::json;
2616
@@ -45,7 +35,7 @@ static void load_dotenv(const std::string& path = ".env") {
4535 continue ;
4636 }
4737 if (pos != std::string::npos) {
48- line = line. substr ( 0 , pos);
38+ line. resize ( pos);
4939 }
5040 trim (line);
5141 if (line.empty ()) {
@@ -226,7 +216,7 @@ static void deal_solving(int id) {
226216 }
227217 std::string base = found.filename ().string ();
228218 if (base.size () > 4 && base.substr (base.size () - 4 ) == " .cpp" ) {
229- base = base. substr ( 0 , base.size () - 4 );
219+ base. resize ( base.size () - 4 );
230220 }
231221 if (base.size () > 0 && base[0 ] == ' p' ) {
232222 base[0 ] = ' s' ;
@@ -246,11 +236,7 @@ static void deal_problem(const fetcher::Problem& problem,
246236 fn << " p" << std::setw (4 ) << std::setfill (' 0' ) << problem.question_id
247237 << " _" ;
248238 std::string slug = problem.title_slug ;
249- for (auto & c : slug) {
250- if (c == ' -' ) {
251- c = ' _' ;
252- }
253- }
239+ std::replace (slug.begin (), slug.end (), ' -' , ' _' );
254240 fn << slug;
255241 std::string file_name = fn.str ();
256242 fs::path dir = " ./src/problem" ;
@@ -355,7 +341,12 @@ int main() {
355341 if (prob->code_definition .empty ()) {
356342 continue ;
357343 }
358- deal_problem (*prob, prob->code_definition [0 ], true );
344+ try {
345+ deal_problem (*prob, prob->code_definition [0 ], true );
346+ } catch (const std::exception& e) {
347+ std::cerr << " Error initializing problem " << fid << " : "
348+ << e.what () << " \n " ;
349+ }
359350 }
360351 return 0 ;
361352 }
0 commit comments