From 689a878978c024b14a7e93e72ab5e1b6a99913b9 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 21 Nov 2022 14:46:53 +0100 Subject: [PATCH] chore: added some comments --- src/main.cpp | 14 +++++++------- tries/include/ternarytrie.h | 23 ++++++++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6281e8f..bcc9636 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,16 +5,16 @@ extern "C" { } #define ENV(var, env_var) \ - const char* _##var = getenv(env_var); \ - if (_##var == NULL) { \ + const char *_##var = getenv(env_var); \ + if (_##var == NULL) { \ printf("Missing environment variable %s.\n", env_var); \ return 1; \ - } \ + } \ const std::string var = std::string(_##var); #define AUTH() \ std::string provided_api_key = req.get_header_value("X-Api-Key"); \ - if (api_key.compare(provided_api_key) != 0) { \ + if (api_key.compare(provided_api_key) != 0) { \ return crow::response(crow::status::UNAUTHORIZED); \ } @@ -29,7 +29,8 @@ int main() { std::string file_path = "lander.data"; - std::cout << "Populating trie from file '" << file_path << "'..." << std::endl; + std::cout << "Populating trie from file '" << file_path << "'..." + << std::endl; int count = ternarytrie_populate(trie, file_path.c_str()); @@ -75,8 +76,7 @@ int main() { }); CROW_ROUTE(app, "/") .methods(crow::HTTPMethod::Post)( - [api_key, base_url, trie](const crow::request &req, - std::string s) { + [api_key, base_url, trie](const crow::request &req, std::string s) { AUTH(); std::string key = req.body; diff --git a/tries/include/ternarytrie.h b/tries/include/ternarytrie.h index 8ba94f6..85b6353 100644 --- a/tries/include/ternarytrie.h +++ b/tries/include/ternarytrie.h @@ -30,6 +30,13 @@ typedef struct ttrie TernaryTrie; */ TernaryTrie* ternarytrie_init(); +/** + * Populate trie with entries stored in the given file. + * + * @param trie + * @param file_path path to file containing entries + * @return amount of entries added; -1 if an error occured + */ int ternarytrie_populate(TernaryTrie* trie, const char* file_path); /** @@ -43,24 +50,26 @@ void ternarytrie_free(TernaryTrie* trie); * Search whether a string is contained in this trie. * * @param trie - * @param string - * @return true if the string is contained within this trie, false otherwise + * @param key + * @return pointer to payload string; NULL if not found */ -char * ternarytrie_search(TernaryTrie* trie, const char* string); +char * ternarytrie_search(TernaryTrie* trie, const char* key); /** * Add a string to this trie. * * @param trie - * @param string + * @param key + * @param payload payload to add * @return true if the trie was changed by this operation, false if it was already present */ -bool ternarytrie_add(TernaryTrie* trie, const char* string, const char *payload); +bool ternarytrie_add(TernaryTrie* trie, const char* key, const char *payload); /** * Add a payload by generating a random string as the key. * * @param trie + * @param payload payload to add * @return the generated key */ char *ternarytrie_add_random(TernaryTrie *trie, const char *payload); @@ -72,10 +81,10 @@ char *ternarytrie_add_random(TernaryTrie *trie, const char *payload); * Removing the string from the trie should not free the string's memory. * * @param trie - * @param string + * @param key * @return true if the string was present and has been removed, false if it was not present */ -bool ternarytrie_remove(TernaryTrie* trie, const char* string); +bool ternarytrie_remove(TernaryTrie* trie, const char* key); /** * Returns the number of strings in this trie.