Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with adding multiple routes #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/*.o
*.o
.cache/
*swp*
2 changes: 1 addition & 1 deletion include/Routes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 7 additions & 8 deletions src/Routes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions templates/chicken.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Cerveur</title>
<link rel="stylesheet" href="../static/index.css" />
</head>
<body><h1>chicken page</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions templates/sth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Cerveur</title>
<link rel="stylesheet" href="../static/index.css" />
</head>
<body><h1>somethin' page</h1>
</body>
</html>