Skip to content

Commit

Permalink
init lib
Browse files Browse the repository at this point in the history
  • Loading branch information
jesuismarie committed Jul 20, 2024
1 parent 8308b94 commit d5017f3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
48 changes: 41 additions & 7 deletions sources/constructor/init_book.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#include "library.h"

Book *create_book(char *title, char *author, char *isbn)
Book *create_book(char *title, char *author, char *isbn, bool flag)
{
int fd;
Book *new_book;

new_book = (Book *)calloc(1, sizeof(Book));
if (!new_book)
return (NULL);
new_book->title = ft_split(title, ' ');
if (!new_book->title || !*new_book->title)
return (NULL);
new_book->author = ft_split(author, ' ');
if (!new_book->author || !*new_book->author)
return (NULL);
new_book->title = ft_strdup(title);
new_book->author = ft_strdup(author);
new_book->isbn = ft_strdup(isbn);
new_book->borrow = false;
new_book->reserved = false;
new_book->next = NULL;
if (flag)
{
fd = open("database/books", O_APPEND | O_WRONLY);
ft_putstr_fd(title, fd);
ft_putchar_fd(',', fd);
ft_putstr_fd(author, fd);
ft_putchar_fd(',', fd);
ft_putendl_fd(isbn, fd);
close(fd);
}
return (new_book);
}

Expand All @@ -34,3 +41,30 @@ void add_book(Book **books, Book *new_book)
tmp = tmp->next;
tmp->next = new_book;
}

void remove_book(Book **books, Book *book)
{
Book *tmp;
Book *temp;

if (!(*books))
{
printf("Library don't have any books\n");
return ;
}
tmp = *books;
while (tmp)
{
if (tmp == book)
{
temp = tmp->next;
free(tmp->title);
free(tmp->author);
free(tmp->isbn);
free(tmp);
tmp = temp;
break ;
}
tmp = tmp->next;
}
}
10 changes: 10 additions & 0 deletions sources/constructor/init_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "library.h"

Library *creat_lib(void)
{
Library *lib;
lib = calloc(1, sizeof(Library));
if (!lib)
exit(1);
return (lib);
}
11 changes: 10 additions & 1 deletion sources/constructor/init_user.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "library.h"

User *create_user(char *username, char *password)
User *create_user(char *username, char *password, bool flag)
{
int fd;
User *new_user;

new_user = (User *)calloc(1, sizeof(User));
Expand All @@ -10,6 +11,14 @@ User *create_user(char *username, char *password)
new_user->username = ft_strdup(username);
new_user->password = ft_strdup(password);
new_user->next = NULL;
if (flag)
{
fd = open("database/user", O_APPEND | O_WRONLY);
ft_putstr_fd(username, fd);
ft_putchar_fd(',', fd);
ft_putendl_fd(password, fd);
close(fd);
}
return (new_user);
}

Expand Down

0 comments on commit d5017f3

Please sign in to comment.