From b8a4c9b6082704804221b41fddf66e4be548b7f4 Mon Sep 17 00:00:00 2001 From: Kiyoshika Date: Sun, 22 Jan 2023 20:17:47 -0500 Subject: [PATCH] fix issue with adding multiple routes I slightly redesigned the addRoute function. Instead of trying to return routes to update it, we pass the pointer by address to modify it directly. Otherwise I believe we'd be assigning to the copied pointers on the parameter stack instead of the actual route pointer. --- .gitignore | 1 + include/Routes.h | 2 +- src/Routes.c | 15 +++++++-------- src/main.c | 4 +++- templates/chicken.html | 9 +++++++++ templates/sth.html | 9 +++++++++ 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 templates/chicken.html create mode 100644 templates/sth.html diff --git a/.gitignore b/.gitignore index c68d06b..215e916 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ src/*.o *.o .cache/ +*swp* diff --git a/include/Routes.h b/include/Routes.h index c7bc2a1..41e29f1 100644 --- a/include/Routes.h +++ b/include/Routes.h @@ -10,7 +10,7 @@ struct Route { struct Route * initRoute(char* key, char* value); -struct Route * addRoute(struct Route * root, char* key, char* value); +void addRoute(struct Route ** root, char* key, char* value); struct Route * search(struct Route * root, char * key); diff --git a/src/Routes.c b/src/Routes.c index 367d3ef..ae0bc2a 100644 --- a/src/Routes.c +++ b/src/Routes.c @@ -23,18 +23,17 @@ void inorder(struct Route* root) } } -struct Route * addRoute(struct Route * root, char* key, char* value) { - if (root == NULL) { - return initRoute(key, value); +void addRoute(struct Route ** root, char* key, char* value) { + if (*root == NULL) { + *root = initRoute(key, value); } - - if (strcmp(key, root->key) == 0) { + else if (strcmp(key, (*root)->key) == 0) { printf("============ WARNING ============\n"); printf("A Route For \"%s\" Already Exists\n", key); - }else if (strcmp(key, root->key) > 0) { - root->right = addRoute(root->right, key, value); + }else if (strcmp(key, (*root)->key) > 0) { + addRoute(&(*root)->right, key, value); }else { - root->left = addRoute(root->left, key, value); + addRoute(&(*root)->left, key, value); } } diff --git a/src/main.c b/src/main.c index 867ae86..720cf4c 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,9 @@ int main() { // registering Routes struct Route * route = initRoute("/", "index.html"); - addRoute(route, "/about", "about.html"); + addRoute(&route, "/about", "about.html"); + addRoute(&route, "/sth", "sth.html"); + addRoute(&route, "/chicken", "chicken.html"); printf("\n====================================\n"); diff --git a/templates/chicken.html b/templates/chicken.html new file mode 100644 index 0000000..b983bda --- /dev/null +++ b/templates/chicken.html @@ -0,0 +1,9 @@ + + + + Cerveur + + +

chicken page

+ + diff --git a/templates/sth.html b/templates/sth.html new file mode 100644 index 0000000..0da4453 --- /dev/null +++ b/templates/sth.html @@ -0,0 +1,9 @@ + + + + Cerveur + + +

somethin' page

+ +