Skip to content

Commit

Permalink
updated functions with new and improved skills => CHECK
Browse files Browse the repository at this point in the history
  • Loading branch information
Anaelle LECLABART committed Apr 3, 2019
1 parent 91f95de commit b85f371
Show file tree
Hide file tree
Showing 44 changed files with 401 additions and 318 deletions.
37 changes: 37 additions & 0 deletions '
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fgetc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/23 14:37:34 by anleclab #+# #+# */
/* Updated: 2019/04/03 21:10:10 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Returns the next character of the file associated to the stream, or -1 if
** the stream doesn't exist, the read fails, or the EOF has been reached.
** Increments the iterator of the stream.
*/
int ft_fgetc(t_file *stream)
{
int rval;

if (!stream)
return (-1);
if (!stream->buf[i])
{
stream->i = 0;
if ((rval = read(stream->fd, stream->buf, BUFF_SIZE)) <= 0)
{
stream->buf[0] = 0;
return (-1);
}
stream->buf[rval] = 0;
}
return((int)stream->buf[stream->i++]);
}
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: anaiel <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/11/06 17:19:55 by anleclab #+# #+# #
# Updated: 2019/04/03 20:29:51 by anleclab ### ########.fr #
# Updated: 2019/04/03 22:13:46 by anleclab ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -38,6 +38,7 @@ SRC_NAMES = ft_memset.c \
ft_strncmp.c \
ft_atoi.c \
ft_isalpha.c \
ft_iswhitespace.c \
ft_isdigit.c \
ft_isalnum.c \
ft_isascii.c \
Expand Down Expand Up @@ -74,6 +75,8 @@ SRC_NAMES = ft_memset.c \
ft_lstadd.c \
ft_lstiter.c \
ft_lstmap.c \
ft_lstcpy.c \
ft_lstappend.c \
ft_strsplitlst.c \
ft_putstrarray.c \
ft_putstrlst.c \
Expand All @@ -85,8 +88,6 @@ SRC_NAMES = ft_memset.c \
ft_double_power.c \
ft_array_swap.c \
ft_itoa_base.c \
ft_litoa.c \
ft_llitoa.c \
ft_ldouble_power.c \
ft_tabdel.c \
ft_fopen.c \
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Run the command `make` to create the **libft.a** library file.
- ft_isalnum
- ft_isascii
- ft_isprint
- ft_iswhitespace
- ft_toupper
- ft_tolower

Expand Down Expand Up @@ -62,13 +63,13 @@ Run the command `make` to create the **libft.a** library file.
- ft_lstiter
- ft_lstmap
- ft_strsplitlst
- ft_lstcpy
- ft_lstappend

### Conversions
- ft_atoi
- ft_itoa
- ft_itoa_base
- ft_litoa
- ft_llitoa

### Printing
- ft_putchar
Expand Down Expand Up @@ -99,6 +100,9 @@ Run the command `make` to create the **libft.a** library file.

The project is updated regularly with new functions that could be useful for other projects, or when I learn something new, or when I realize my code looks aweful :wink:

## How to improve
- Update code with new and improved coding skills

## Authors

* **Anaelle Leclabart** - [anaiel](https://github.com/anaiel)
12 changes: 8 additions & 4 deletions includes/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: anaiel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/06 17:19:32 by anleclab #+# #+# */
/* Updated: 2019/04/03 20:25:37 by anleclab ### ########.fr */
/* Updated: 2019/04/03 22:13:21 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,6 +21,9 @@

# define BUFF_SIZE 4096

# define LLONG_MAX 9223372036854775807
# define INT_MIN -2147483647

typedef struct s_list
{
void *content;
Expand All @@ -37,6 +40,7 @@ typedef struct s_file

int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_iswhitespace(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
Expand Down Expand Up @@ -94,6 +98,8 @@ void ft_lstdel(t_list **alst, void (*del)(void *, size_t));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
t_list *ft_lstcpy(t_list *lst);
t_list *ft_lstappend(t_list **lst, t_list *link);
t_list *ft_strsplitlst(char *s, char c);
void ft_putstrarray(const char **str);
void ft_putstrlst(t_list *lst);
Expand All @@ -105,12 +111,10 @@ unsigned char *ft_wchar_to_bytes(wint_t c);
double ft_double_power(double n, int pow);
void ft_array_swap(int *array, int i1, int i2);
char *ft_itoa_base(int n, int base);
char *ft_litoa(long int nb);
char *ft_llitoa(long long int nb);
long double ft_ldouble_power(long double n, int pow);
void ft_tabdel(char **tab);
t_file *ft_fopen(const char *path);
int ft_fclose(t_file *stream);
int ft_fclose(t_file **stream);
int ft_fgetc(t_file *stream);

#endif
9 changes: 8 additions & 1 deletion srcs/ft_array_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/02 10:35:50 by anleclab #+# #+# */
/* Updated: 2019/01/23 14:08:41 by anleclab ### ########.fr */
/* Updated: 2019/04/03 20:37:43 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Swaps elements at index i1 and i2 if array exists.
*/
void ft_array_swap(int *array, int i1, int i2)
{
int tmp;

if (!array)
return ;
tmp = array[i1];
array[i1] = array[i2];
array[i2] = tmp;
Expand Down
43 changes: 20 additions & 23 deletions srcs/ft_atoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,41 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/09 13:51:13 by anleclab #+# #+# */
/* Updated: 2019/01/23 14:08:43 by anleclab ### ########.fr */
/* Updated: 2019/04/03 20:54:03 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

static int al_isspace(const char c)
{
if (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'
|| c == ' ')
return (1);
else
return (0);
}

/*
** Returns the int form of the number in the string, or 0 if the string is not
** a number.
** Ignores starting spaces and ending characters which are not digits.
*/
int ft_atoi(const char *s)
{
unsigned long long res;
int i;
int sign;

i = 0;
if (!s)
return (0);
res = 0;
sign = 1;
while (s[i] && al_isspace(s[i]))
i++;
if (s[i] == '+' || s[i] == '-')
while (*s && ft_iswhitespace(*s))
s++;
if (*s == '+' || *s == '-')
s++;
else if (*s == '-')
{
if (s[i] == '-')
sign = -1;
i++;
sign = -1;
s++;
}
while (s[i] && ft_isdigit(s[i]))
while (*s && ft_isdigit(*s))
{
res = res * 10 + s[i] - '0';
if (res > 9223372036854775807)
return ((sign > 0 ? -1 : 0));
i++;
res = res * 10 + *s - '0';
if (res > LLONG_MAX)
return ((sign > 0) ? -1 : 0);
s++;
}
return ((int)(res * sign));
}
5 changes: 4 additions & 1 deletion srcs/ft_bzero.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/09 18:22:55 by anleclab #+# #+# */
/* Updated: 2019/01/23 14:08:44 by anleclab ### ########.fr */
/* Updated: 2019/04/03 20:55:50 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Sets n bits to 0.
*/
void ft_bzero(void *s, size_t n)
{
ft_memset(s, 0, n);
Expand Down
7 changes: 6 additions & 1 deletion srcs/ft_double_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/12 14:54:14 by anleclab #+# #+# */
/* Updated: 2019/01/23 14:08:46 by anleclab ### ########.fr */
/* Updated: 2019/04/03 20:57:53 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Returns n to the power of pow where n is a double and pow a signed int.
*/
double ft_double_power(double n, int pow)
{
if (pow == 0)
Expand Down
14 changes: 9 additions & 5 deletions srcs/ft_fclose.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/23 15:14:07 by anleclab #+# #+# */
/* Updated: 2019/01/23 15:22:56 by anleclab ### ########.fr */
/* Updated: 2019/04/03 21:20:23 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int ft_fclose(t_file *stream)
/*
** Closes the file descriptor and frees the buffer contained in the stream.
*/
int ft_fclose(t_file **stream)
{
if (!stream)
if (!stream || !*stream)
return (0);
if (close(stream->fd) == -1)
if (close((*stream)->fd) == -1)
return (-1);
free(stream->buf);
free(*stream);
stream = NULL;
return (0);
}
21 changes: 12 additions & 9 deletions srcs/ft_fgetc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/23 14:37:34 by anleclab #+# #+# */
/* Updated: 2019/01/23 15:42:22 by anleclab ### ########.fr */
/* Updated: 2019/04/03 22:34:00 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Returns the next character of the file associated to the stream, or -1 if
** the stream doesn't exist, the read fails, or the EOF has been reached.
** Increments the iterator of the stream.
*/
int ft_fgetc(t_file *stream)
{
int ret;
unsigned char res;
int rval;

if (!stream)
return (-1);
if (!(stream->buf[0]))
if (!stream->buf[i])
{
if ((ret = read(stream->fd, stream->buf, BUFF_SIZE)) <= 0)
stream->i = 0;
if ((rval = read(stream->fd, stream->buf, BUFF_SIZE)) <= 0)
{
stream->buf[0] = 0;
return (-1);
}
stream->buf[ret] = 0;
stream->i = 0;
stream->buf[rval] = 0;
}
res = stream->buf[stream->i++];
return((int)res);
return((int)stream->buf[stream->i++]);
}
30 changes: 15 additions & 15 deletions srcs/ft_filelinecount.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
/* By: anleclab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/19 09:18:47 by anleclab #+# #+# */
/* Updated: 2019/01/23 14:08:47 by anleclab ### ########.fr */
/* Updated: 2019/04/03 21:16:26 by anleclab ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*
** Returns the number of line of the file.
*/
int ft_filelinecount(int fd)
{
int nblines;
int readchar;
char buf[51];
int rval;
char buf[BUFF_SIZE + 1];
int i;

buf[50] = 0;
readchar = read(fd, buf, 50);
nblines = (readchar > 0 ? 1 : 0);
while (readchar > 0)
if ((rval = read(fd, buf, BUFF_SIZE)) <= 0)
return (0);
buf[rval] = 0;
nblines = 1;
while (rval > 0)
{
i = 0;
while (i < 50)
{
if (buf[i] == '\n')
nblines++;
i++;
}
readchar = read(fd, buf, 50);
i = -1;
while (buf[++i])
nblines += (buf[i] == '\n');
rval = read(fd, buf, BUFF_SIZE);
}
return (nblines);
}
Loading

0 comments on commit b85f371

Please sign in to comment.