lazy week-end stuff B)
started conversion of functions to data structure system still WIP added first iteration of int parser does not check for int overflow
This commit is contained in:
77
srcs/checker.c
Normal file
77
srcs/checker.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/23 14:55:14 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int ft_issign(char c)
|
||||
{
|
||||
return (c == '+' || c == '-');
|
||||
}
|
||||
|
||||
int ft_iswhitespace(char c)
|
||||
{
|
||||
return (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f'
|
||||
|| c == '\r');
|
||||
}
|
||||
|
||||
int ft_hasdigit(char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i] && !ft_isdigit(s[i]))
|
||||
i++;
|
||||
return (ft_isdigit(s[i]));
|
||||
}
|
||||
|
||||
int check_input(char *in, int *ret)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
ret = 0;
|
||||
while (in[i] && ft_iswhitespace(in[i]))
|
||||
i++;
|
||||
if (in[i] && ft_issign(in[i]))
|
||||
i++;
|
||||
while (in[i] && ft_iswhitespace(in[i]))
|
||||
i++;
|
||||
while (in[i] && ft_isdigit(in[i]))
|
||||
i++;
|
||||
while (in[i] && ft_iswhitespace(in[i]))
|
||||
i++;
|
||||
if (in[i] == '\0' && ft_hasdigit(in))
|
||||
*ret = ft_atoi(in);
|
||||
return (in[i] == '\0' && ft_hasdigit(in));
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int nb;
|
||||
t_stack *new;
|
||||
t_psdata data;
|
||||
|
||||
i = ac;
|
||||
while (i > 0)
|
||||
{
|
||||
if (check_input(av[i], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||
ps_stkpsh(&(data.a), new);
|
||||
else
|
||||
{
|
||||
ft_putendl_fd("Error", 2);
|
||||
ps_stkclean(&data);
|
||||
return (2);
|
||||
}
|
||||
i--;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user