From f6b816e78c76ecff22c8b142f80fc814f14adce3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 8 Nov 2025 22:01:49 +0000 Subject: [PATCH] Refactor: Improve code clarity and remove unused code Co-authored-by: jacegenereux --- main.cpp | 113 +++++++++++++++++-------------------------------------- 1 file changed, 35 insertions(+), 78 deletions(-) diff --git a/main.cpp b/main.cpp index c3fbad7..d051174 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,13 @@ #include #include -#include #include #include -#include #include using json = nlohmann::json; -// used to have a dynamic string +// Used to have a dynamic string for curl response typedef struct Response { char *string; @@ -28,7 +26,6 @@ void formatResponse(char *response); std::string FormatHTMLToString(const std::string &response); TestCaseResponse GetTestCases(const std::string &content); -std::pair GetParamName(const std::string ¶m); void CreateJSON(json *response, const TestCaseResponse &testCases); int main() @@ -47,13 +44,9 @@ int main() std::cerr << "HTTP REQUEST FAILED: curl_easy_init() failed!" << std::endl; return -1; } - else - { - std::cout << "Curl initialized successfully!" << std::endl; - } Response response; - response.string = (char *)malloc(1); + response.string = static_cast(malloc(1)); response.size = 0; // Set options for the HTTP request @@ -87,7 +80,7 @@ int main() curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_chunk); // Address of response string is passed in write_chunk as userData - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast(&response)); // Perform the HTTP request result = curl_easy_perform(curl); @@ -105,32 +98,32 @@ int main() return 0; } -// returns number of bytes in the chunk -// data is set to a ptr that points to block of data recieved in this chunk -// nmemb is the number of bytes in the block of data +// Returns number of bytes in the chunk +// data is set to a ptr that points to block of data received in this chunk +// nmemb is the number of bytes in the block of data // userData points to what we want (points to where the response string is stored) size_t write_chunk(void *data, size_t size, size_t nmemb, void *userData) { // size is always 1 size_t real_size = size * nmemb; - Response *response = (Response *)userData; - // allocate more space for chunk that was recieved - // response->size is size of existing mem and real_size is the size recieved and +1 accounts for null - char *ptr = (char *)realloc(response->string, response->size + real_size + 1); + Response *response = static_cast(userData); + // Allocate more space for chunk that was received + // response->size is size of existing mem and real_size is the size received and +1 accounts for null + char *ptr = static_cast(realloc(response->string, response->size + real_size + 1)); if (ptr == nullptr) { - std::cerr << "Problem reallocating space for chunk recieved" << std::endl; + std::cerr << "Problem reallocating space for chunk received" << std::endl; return 0; } - // set response string to the new (larger) memory address + // Set response string to the new (larger) memory address response->string = ptr; - // append new porition onto existing string + // Append new portion onto existing string memcpy(&(response->string[response->size]), data, real_size); - // update strings size + // Update string's size response->size += real_size; - // append null character + // Append null character response->string[response->size] = '\0'; return real_size; } @@ -195,15 +188,15 @@ void formatResponse(char *response) } } -// check for tag +// Check for HTML tags and convert HTML entities to their character equivalents std::string FormatHTMLToString(const std::string &response) { - int i = 0; - std::string result = ""; + size_t i = 0; + std::string result; while (i < response.length()) { - // check for HTML elements + // Check for HTML elements if (response[i] == '<') { while (response[i] != '>') @@ -214,7 +207,7 @@ std::string FormatHTMLToString(const std::string &response) continue; } - // check for < (<) , > (>); + // Check for < (<) , > (>) if (i < response.length() - 4 && (response.substr(i, 4) == "<" || response.substr(i, 4) == ">")) { std::string expression = response.substr(i, 4); @@ -230,7 +223,7 @@ std::string FormatHTMLToString(const std::string &response) continue; } - // check for & (&) + // Check for & (&) if (i < response.length() - 5 && (response.substr(i, 5) == "&")) { result += "&"; @@ -238,22 +231,22 @@ std::string FormatHTMLToString(const std::string &response) continue; } - // check for 's + // Check for 's if (i < response.length() - 6 && response.substr(i, 6) == "'s") { i += 6; continue; } - // check for   tags + // Check for   tags if (i < response.length() - 6 && response.substr(i, 6) == " ") { i += 6; continue; } - // check for multiple whitespace characters - // want to keep 1 where there are multiple + // Check for multiple whitespace characters + // Keep only 1 where there are multiple if (response[i] == '\n') { result += "\n"; @@ -284,13 +277,13 @@ std::string FormatHTMLToString(const std::string &response) /** * Basic test cases given by leetcode are given in a string of the form. Example case & output. * Should always be at least 2 test cases given. - * @returns array of oxpected outputs for the test cases. + * @returns array of expected outputs for the test cases. */ TestCaseResponse GetTestCases(const std::string &content) { TestCaseResponse tests; - int i = 0; + size_t i = 0; while (i < content.length()) { if (i < content.length() - 7 && content.substr(i, 7) == "Example") @@ -303,7 +296,7 @@ TestCaseResponse GetTestCases(const std::string &content) i += 6; std::string input = ""; std::string paramName = ""; - std::string paramRes = ""; + std::string paramRes; int j = -1; while (i < content.length() - 7 && content.substr(i, 7) != "\nOutput") { @@ -345,7 +338,7 @@ TestCaseResponse GetTestCases(const std::string &content) if (i <= content.length() - 6 && content.substr(i, 6) == "Output") { i += 6; - std::string testCase = ""; + std::string testCase; while (i < content.length() && content[i] != '\n') { if (content[i] != ' ' && content[i] != ':') @@ -360,10 +353,7 @@ TestCaseResponse GetTestCases(const std::string &content) i++; } } - else - { - i++; - } + i++; } return tests; @@ -371,7 +361,7 @@ TestCaseResponse GetTestCases(const std::string &content) void CreateJSON(json *response, const TestCaseResponse &tests) { - // filter out invalid characters from title + // Filter out invalid characters from title std::string title = (*response)["title"]; const std::string invalid_chars = "\\/:*?\"<>|"; for (char c : invalid_chars) @@ -382,7 +372,7 @@ void CreateJSON(json *response, const TestCaseResponse &tests) std::ofstream outputJSON; outputJSON.open(jsonName); - // should have to create the file so always should open + // Should always be able to create the file if (!outputJSON.is_open()) { std::cerr << "Error creating output file for JSON response" << std::endl; @@ -390,14 +380,12 @@ void CreateJSON(json *response, const TestCaseResponse &tests) } outputJSON << "{\n"; - // iterates through json response inserting key and value as pair into output file + // Iterate through json response inserting key and value as pair into output file for (auto it = (*response).begin(); it != (*response).end(); ++it) { outputJSON << "\"" << it.key() << "\"" << ": " << it.value() << ',' << "\n"; } - // handle situation where testCases might not generate - // Insert testcases outputJSON << "\"testCases\"" << ": [" << "\n"; @@ -408,7 +396,7 @@ void CreateJSON(json *response, const TestCaseResponse &tests) // start inserting new object into array inside json file outputJSON << "{\n"; - std::string expectedResult = tests.testCases[i]; // testcase expected outputs + std::string expectedResult = tests.testCases[i]; // Test case expected outputs outputJSON << "\"expectedResult\": " << "\"" << expectedResult << "\",\n"; int numParams = tests.testCaseParams.size() / tests.testCases.size(); @@ -425,7 +413,7 @@ void CreateJSON(json *response, const TestCaseResponse &tests) } } - // if i is at the end then we need to close off the obj + // If i is at the end then we need to close off the object if (i == size - 1) { outputJSON << "}\n"; @@ -441,34 +429,3 @@ void CreateJSON(json *response, const TestCaseResponse &tests) outputJSON << "}"; outputJSON.close(); } - -/** - * params are taken from the json as a string containing 'paramName'='param' - * This function splits the paramName and param seperately to label them in the output JSON easier. - * (the problem function calls explicility used by the users will contain the same paramNames so makes using them easier as well) - */ -std::pair GetParamName(const std::string ¶m) -{ - std::string paramName = ""; - std::string paramResult = ""; - bool nameParsed = false; - for (int i = 0; i < param.length(); i++) - { - - if (param[i] == '=') - { - nameParsed = true; - continue; - } - - if (param[i] != ' ' && !nameParsed) - { - paramName += param[i]; - } - else if (param[i] != ' ' && nameParsed) - { - paramResult += param[i]; - } - } - return {paramName, paramResult}; -} \ No newline at end of file