1
0

first commit

This commit is contained in:
KeyZox
2023-08-06 20:12:38 +02:00
parent 1fe062c970
commit eb6b113e51
125 changed files with 2987 additions and 0 deletions

View File

@ -0,0 +1,30 @@
SRCS = ft_main.c ft_errors.c ft_parse_dict.c ft_parse_line.c ft_parser.c ft_utils1.c ft_cut_nbr.c rush02.h
OBJS = ${SRCS:.c=.o}
NAME = rush-02
CC = gcc
CFLAGS = -Wall -Werror -Wextra
RM = rm -f
.c.o:
${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
${NAME}: ${OBJS}
${CC} -o ${NAME} ${OBJS}
all: ${NAME}
clean:
${RM} ${OBJS}
fclean: clean
${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cut_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 19:07:48 by julemart #+# #+# */
/* Updated: 2022/07/31 23:35:38 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char **i_is_1(char **tab, char *nbr)
{
char *temp;
temp = malloc(2);
if (nbr[1] == '1')
{
temp[0] = nbr[1];
temp[1] = nbr[2];
tab = add_tab_str(tab, temp);
}
else if (nbr[1] != '0')
{
temp[0] = nbr[1];
temp[1] = '0';
tab = add_tab_str(tab, temp);
}
return (tab);
}
char **ft_number(char **tab, char *nbr)
{
int i;
char *temp;
i = 0;
temp = malloc(2);
temp[1] = '\0';
while (nbr[i])
{
if (i == 0 && nbr[i] != '0')
{
temp[0] = nbr[i];
tab = add_tab_str(tab, temp);
tab = add_tab_str(tab, "100");
}
else if (i == 1)
{
if (nbr[i] == '1')
i++;
tab = i_is_1(tab, nbr);
}
if (i == 2 && nbr[i] != '0')
{
temp[0] = nbr[i];
tab = add_tab_str(tab, temp);
}
i++;
}
return (tab);
}
char *set_zeros(char *str)
{
int i;
char *test;
i = ft_strlen(str);
if (i % 3 == 1)
{
str = ft_addchar(str, '0');
str = ft_addchar(str, '0');
}
if (i % 3 == 2)
{
str = ft_addchar(str, '0');
}
return (str);
}
char **ft_comp(char *str)
{
int i;
int j;
char *c;
char **tab;
tab = malloc(sizeof(char *));
c = malloc(4);
c[3] = '\0';
j = 0;
i = 0;
while (str[i] != '\0')
{
j = 0;
while (j < 3)
{
c[j] = str[i + j];
j++;
}
tab = ft_number(tab, c);
i += 3;
}
return (tab);
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 17:50:28 by lvincent #+# #+# */
/* Updated: 2022/07/31 15:09:03 by julemart ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
void ft_error(void)
{
write(1, "Error\n", 6);
}
void ft_dict_error(void)
{
write(1, "Dict Error\n", 11);
}

View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 15:11:39 by julemart #+# #+# */
/* Updated: 2022/07/31 23:24:25 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
int main(int argc, char **argv)
{
char **tab_line;
int i;
char *nbr;
struct s_nb *line;
if (argc == 3)
{
tab_line = ft_dict_to_line(argv[1]);
nbr = argv[2];
}
if (argc == 2)
{
tab_line = ft_dict_to_line("numbers.dict.txt");
nbr = argv[1];
}
if (argc <= 3 && argc > 1)
{
if (!ft_parse(nbr))
{
ft_error();
return (0);
}
ft_trim(nbr);
while (tab_line[i])
{
i++;
}
line = malloc(sizeof(struct s_nb) * i);
i = 0;
while (tab_line[i])
{
line[i] = ft_parse_line(tab_line[i]);
i++;
}
while (tab_line[i])
{
free(tab_line[i]);
i++;
}
free(line);
free(tab_line);
}
return (0);
}

View File

@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_dict.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julemart <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 18:30:17 by julemart #+# #+# */
/* Updated: 2022/07/31 15:20:02 by julemart ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char **ft_cut_line2(char *str, char **tab, int count_line, int i)
{
int j;
int k;
while (str[i])
{
j = i;
while (str[i] != '\n' && str[i])
i++;
tab[count_line] = malloc(sizeof(char) * (i - j + 1));
k = 0;
while (str[j] != '\n' && str[j])
{
tab[count_line][k] = str[j];
j++;
k++;
}
if (k > 0)
tab[count_line][k] = '\0';
if (k > 0)
count_line++;
else
free(tab[count_line]);
i++;
}
return (tab);
}
char **ft_cut_line(char *str)
{
int i;
int count_line;
char **tab;
i = 0;
count_line = 1;
while (str[i])
{
if (str[i] == '\n' && str[i - 1] != '\n')
count_line++;
i++;
}
tab = malloc(sizeof(char *) * (count_line));
i = 0;
count_line = 0;
tab = ft_cut_line2(str, tab, count_line, i);
return (tab);
}
char **ft_dict_to_line(char *path)
{
int fd;
int size;
char tmp[1];
char *str;
char **tab_line;
size = 0;
fd = open(path, O_RDONLY);
if (fd == -1)
{
ft_dict_error();
return (NULL);
}
while (read(fd, tmp, 1))
size++;
close(fd);
str = malloc(sizeof(char) * (size + 1));
fd = open(path, O_RDONLY);
read(fd, str, size);
close(fd);
str[size] = '\0';
tab_line = ft_cut_line(str);
free(str);
return (tab_line);
}
int main(void)
{
char **tab_line;
int i;
i = 0;
tab_line = ft_dict_to_line("numbers.dict.txt");
while (tab_line[i])
{
printf("%s\n", tab_line[i]);
i++;
}
i = 0;
while (tab_line[i])
{
free(tab_line[i]);
i++;
}
free(tab_line);
return (0);
}

View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julemart <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 15:38:42 by julemart #+# #+# */
/* Updated: 2022/07/31 15:40:22 by julemart ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char *ft_addchar(char *str, char c)
{
char *return_val;
int i;
i = 0;
while (str[i++])
;
return_val = malloc(i + 2);
i = 0;
while (str[i] != '\0')
{
return_val[i] = str[i];
i++;
}
return_val[i++] = c;
return_val[i] = '\0';
free(str);
return (return_val);
}
struct s_nb ft_parse_line(char *line)
{
struct s_nb result;
int i;
result.nbr = malloc(1);
result.txt = malloc(1);
i = 0;
if (line[0] < '0' || line [0] > '9')
return (result);
while (line[i] >= '0' && line[i] <= '9')
{
result.nbr = ft_addchar(result.nbr, line[i]);
i++;
}
while (line[i] == ' ')
i++;
i++;
while (line[i] == ' ')
i++;
while (line[i] != '\n')
{
result.txt = ft_addchar(result.txt, line[i]);
i++;
}
return (result);
}

View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 17:19:40 by lvincent #+# #+# */
/* Updated: 2022/07/31 16:20:45 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
#define UI_MAX 4294967295
int ft_parse(char *str)
{
int i;
int len;
i = 0;
while (ft_is_whitespace(str[i]) && str[i])
i++;
if (i >= ft_strlen(str))
return (0);
while (str[i])
{
if (ft_is_whitespace(str[i]))
break ;
else if ((str[i] < '0' || str[i] > '9') && !ft_is_whitespace(str[i]))
return (0);
i++;
}
while (str[i])
{
if (!ft_is_whitespace(str[i]))
return (0);
i++;
}
return (1);
}
char *ft_trim(char *str)
{
int i;
int j;
i = 0;
while (str[i] && ft_is_whitespace(str[i]))
i++;
j = i;
while (str[i] && !ft_is_whitespace(str[i]))
i++;
if (i < ft_strlen(str))
str[i] = '\0';
return (str + j);
}

View File

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 17:50:34 by lvincent #+# #+# */
/* Updated: 2022/07/31 23:32:52 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
int ft_is_whitespace(char c)
{
if ((c >= 9 && c <= 13) || c == ' ')
return (1);
else
return (0);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_addchar(char *str, char c)
{
char *return_val;
int i;
i = 0;
while (str[i++])
;
return_val = malloc(i + 1);
i = 0;
return_val[0] = c;
while (str[i] != '\0')
{
return_val[i + 1] = str[i];
i++;
}
return_val[i + 1] = '\0';
free(str);
return (return_val);
}
char *ft_set_string_malloc(char *str)
{
char *r_value;
int i;
r_value = malloc(ft_strlen(str));
i = 0;
while (str[i])
{
r_value[i] = str[i];
i++;
}
r_value[i] = '\0';
return (r_value);
}
char **add_tab_str(char **tab, char *str)
{
char **tmp;
int i;
int size;
i = 0;
size = 0;
while (tab[i])
{
size += ft_strlen(tab[i]);
i++;
}
i = 0;
tmp = malloc(size);
while (tab[i])
{
tmp[i] = malloc(ft_strlen(tab[i]));
tmp[i] = tab[i];
i++;
}
tmp[i] = malloc(ft_strlen(tab[i]));
tmp[i] = str;
puts("yes");
free(tab);
return (tmp);
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rush02.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 23:23:19 by lvincent #+# #+# */
/* Updated: 2022/07/31 23:30:37 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RUSH02_H
# define RUSH02_H
# include <unistd.h>
# include <stdlib.h>
# include <fcntl.h>
struct s_nb
{
char *nbr;
char *txt;
};
char *ft_addchar(char *str, char c);
struct s_nb ft_parse_line(char *line);
void ft_error(void);
void ft_dict_error(void);
int ft_strlen(char *str);
int ft_is_whitespace(char c);
char **ft_cut_line2(char *str, char **tab, int count_line, int i);
char **ft_cut_line(char *str);
char **ft_dict_to_line(char *path);
int ft_parse(char *str);
char *ft_trim(char *str);
char *ft_set_string_malloc(char *str);
char **add_tab_char(char **tab, char c);
char **add_tab_str(char **tab, char *str);
char **i_is_1(char **tab, char *nbr);
char **ft_number(char **tab, char *nbr);
char *set_zeros(char *str);
char **ft_comp(char *str);
#endif