tgrekov-push_swap
HIVE push_swap July 2024
Loading...
Searching...
No Matches
input.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* input.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/06 04:18:07 by tgrekov #+# #+# */
9/* Updated: 2024/07/17 11:04:33 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdlib.h>
21#include <libft.h>
22#include "utils/utils.h"
23#include "stack/stack.h"
24
35static int init_stacks(int argc, char **argv, char ***arg2, t_stack *stack)
36{
37 *arg2 = 0;
38 if (argc == 1)
39 {
40 *arg2 = ft_split(argv[0], ' ');
41 if (!arg2)
42 return (1);
43 argc = arr_len((void **) *arg2);
44 if (!argc)
45 return (1);
46 }
47 stack[1].n = 0;
48 stack[1].len = 0;
49 stack[0].n = malloc(sizeof(int) * argc);
50 if (!stack[0].n)
51 return (1);
52 stack[1].n = malloc(sizeof(int) * argc);
53 if (!stack[1].n)
54 return (1);
55 return (0);
56}
57
66static int validate_arg(char *arg, int parsed)
67{
68 char *converted;
69 size_t len;
70 int res;
71
72 converted = ft_itoa(parsed);
73 if (!converted)
74 return (1);
75 len = ft_strlen(converted);
76 res = 0;
77 if (ft_strlen(arg) != len)
78 res = 1;
79 if (!res)
80 res = ft_strncmp(arg, converted, len);
81 free(converted);
82 return (res);
83}
84
92static int duplicate_check(t_stack stack, int n)
93{
94 stack.len--;
95 while (stack.len--)
96 {
97 if (stack.n[stack.len] == n)
98 return (1);
99 }
100 return (0);
101}
102
110static int parse_args(char **argv, t_stack *stack)
111{
112 stack[0].len = 0;
113 while (*argv)
114 {
115 stack[0].n[stack[0].len++] = ft_atoi(*argv);
116 if (validate_arg(*argv, stack[0].n[stack[0].len - 1]))
117 return (1);
118 if (duplicate_check(stack[0], stack[0].n[stack[0].len - 1]))
119 return (1);
120 argv++;
121 }
122 return (0);
123}
124
134int input(int argc, char **argv, t_stack *stack)
135{
136 char **arg2;
137 int status;
138
139 if (init_stacks(--argc, ++argv, &arg2, stack))
140 {
141 if (arg2)
142 arr_free((void **) arg2);
143 return (err("Error\n", 1));
144 }
145 if (arg2)
146 {
147 status = parse_args(arg2, stack);
148 arr_free((void **) arg2);
149 }
150 else
151 status = parse_args(argv, stack);
152 if (status)
153 return (err("Error\n", 1));
154 return (0);
155}
void ** arr_free(void **arr)
Free each element in a null-terminated array, and then the array itself.
Definition arr_free.c:29
int arr_len(void **arr)
Count number of elements in a null-terminated array.
Definition arr_len.c:26
int err(char *str, int retval)
Print str to stderr and return retval.
Definition err.c:29
int ft_atoi(const char *str)
Converts ASCII string str to integer representation.
Definition ft_atoi.c:42
char * ft_itoa(int n)
Converts integer n to an allocated ASCII string.
Definition ft_itoa.c:50
char ** ft_split(char const *s, char c)
Split string s by characer c into allocated array of strings, with NULL as the last element.
Definition ft_split.c:72
size_t ft_strlen(const char *str)
Get length of str.
Definition ft_strlen.c:28
int ft_strncmp(const char *s1, const char *s2, size_t n)
Compare up to n characters of string s1 with string s2.
Definition ft_strncmp.c:33
static int init_stacks(int argc, char **argv, char ***arg2, t_stack *stack)
Split input if it was provided as a single argument, validate input length, and allocate stacks.
Definition input.c:35
static int validate_arg(char *arg, int parsed)
Check that parsed is identical to the source arg when converted back into a string.
Definition input.c:66
int input(int argc, char **argv, t_stack *stack)
Parse and validate program input, initialize stacks and fill the first stack.
Definition input.c:134
static int parse_args(char **argv, t_stack *stack)
Parse and validate input arguments and add them to the first stack.
Definition input.c:110
static int duplicate_check(t_stack stack, int n)
Check if n is already present in the stack.
Definition input.c:92