chore: added some comments

trie-skips
Jef Roosens 2022-11-21 14:46:53 +01:00
parent 494946d24a
commit 689a878978
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 23 additions and 14 deletions

View File

@ -5,16 +5,16 @@ extern "C" {
} }
#define ENV(var, env_var) \ #define ENV(var, env_var) \
const char* _##var = getenv(env_var); \ const char *_##var = getenv(env_var); \
if (_##var == NULL) { \ if (_##var == NULL) { \
printf("Missing environment variable %s.\n", env_var); \ printf("Missing environment variable %s.\n", env_var); \
return 1; \ return 1; \
} \ } \
const std::string var = std::string(_##var); const std::string var = std::string(_##var);
#define AUTH() \ #define AUTH() \
std::string provided_api_key = req.get_header_value("X-Api-Key"); \ 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); \ return crow::response(crow::status::UNAUTHORIZED); \
} }
@ -29,7 +29,8 @@ int main() {
std::string file_path = "lander.data"; 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()); int count = ternarytrie_populate(trie, file_path.c_str());
@ -75,8 +76,7 @@ int main() {
}); });
CROW_ROUTE(app, "/<string>") CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Post)( .methods(crow::HTTPMethod::Post)(
[api_key, base_url, trie](const crow::request &req, [api_key, base_url, trie](const crow::request &req, std::string s) {
std::string s) {
AUTH(); AUTH();
std::string key = req.body; std::string key = req.body;

View File

@ -30,6 +30,13 @@ typedef struct ttrie TernaryTrie;
*/ */
TernaryTrie* ternarytrie_init(); 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); 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. * Search whether a string is contained in this trie.
* *
* @param trie * @param trie
* @param string * @param key
* @return true if the string is contained within this trie, false otherwise * @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. * Add a string to this trie.
* *
* @param 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 * @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. * Add a payload by generating a random string as the key.
* *
* @param trie * @param trie
* @param payload payload to add
* @return the generated key * @return the generated key
*/ */
char *ternarytrie_add_random(TernaryTrie *trie, const char *payload); 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. * Removing the string from the trie should not free the string's memory.
* *
* @param trie * @param trie
* @param string * @param key
* @return true if the string was present and has been removed, false if it was not present * @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. * Returns the number of strings in this trie.