changed the split prototypes

This commit is contained in:
Maieul BOYER 2024-07-22 15:41:20 +02:00
parent 4a39152144
commit a00653bd30
No known key found for this signature in database
5 changed files with 158 additions and 122 deletions

View file

@ -6,13 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/07/10 18:04:36 by maiboyer ### ########.fr */
/* Updated: 2024/07/22 15:29:22 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/os/process.h"
#include "me/string/string.h"
#include "me/mem/mem.h"
#include "me/printf/printf.h"
#include "me/os/pipe.h"
#include "me/str/str.h"
#include "me/str/str.h"
@ -52,31 +53,24 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
return (NO_ERROR);
}
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
t_string *s)
{
t_str *splitted_path;
t_usize sp_index;
splitted_path = str_split(path + 5, ':');
if (splitted_path == NULL)
t_vec_str path;
t_usize idx;
(void)(process);
if (str_split(path_raw + 5, ":", &path))
return (string_free(*s), ERROR);
sp_index = 0;
while (splitted_path[sp_index])
idx = 0;
while (idx < path.len)
{
((void)(process), string_clear(s));
string_push(s, splitted_path[sp_index]);
string_push(s, "/");
string_push(s, info->binary_path);
sp_index++;
string_clear(s);
me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path);
if (access(s->buf, X_OK | R_OK) == 0)
break ;
return (vec_str_free(path), NO_ERROR);
}
sp_index = 0;
while (splitted_path[sp_index])
mem_free(splitted_path[sp_index++]);
mem_free(splitted_path);
return (NO_ERROR);
return (vec_str_free(path), ERROR);
}
t_error find_binary(t_spawn_info *info, t_process *process)

View file

@ -6,87 +6,38 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:44:18 by maiboyer ### ########.fr */
/* Updated: 2024/07/22 15:11:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
static t_usize local_count_words(t_const_str str, char chr);
static t_str *local_split_inner(t_const_str str, char chr, t_str *out);
static t_str *local_split_freeall(t_str **to_free);
static t_usize local_count_words(t_const_str str, char chr)
t_error str_split(t_const_str str, t_const_str chr, t_vec_str *out)
{
t_usize i;
t_usize out;
t_vec_str ret;
t_usize idx;
t_string buf;
out = 0;
i = 0;
while (str[i])
if (out == NULL || chr == NULL || str == NULL)
return (ERROR);
idx = 0;
buf = string_new(16);
ret = vec_str_new(16, str_free);
while (str[idx] != '\0')
{
while (str[i] && str[i] == chr)
i++;
if (str[i] == 0)
return (out);
out++;
while (str[i] && str[i] != chr)
i++;
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) != NULL)
idx++;
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) == NULL)
string_push_char(&buf, str[idx]);
if (buf.len != 0)
{
vec_str_push(&ret, buf.buf);
buf = string_new(16);
}
}
return (out);
}
static t_str *local_split_freeall(t_str **to_free)
{
while (*to_free)
mem_free(*(to_free++));
return (NULL);
}
static t_str *local_split_inner(t_const_str str, char chr, t_str *out)
{
t_usize str_i;
t_usize sub_i;
t_usize ptr_i;
str_i = 0;
ptr_i = 0;
while (str[str_i])
{
while (str[str_i] && str[str_i] == chr)
str_i++;
if (str[str_i] == 0)
break ;
sub_i = 0;
while (str[str_i + sub_i] && str[str_i + sub_i] != chr)
sub_i++;
out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1));
if (out[ptr_i] == NULL)
return (local_split_freeall(&out));
str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1);
str_i += sub_i;
}
out[ptr_i] = NULL;
return (out);
}
t_str *str_split(t_const_str str, char chr)
{
t_usize ptr_len;
t_str *out;
if (str == NULL || *str == 0)
{
out = mem_alloc(sizeof(t_str) * 1);
*out = NULL;
return (out);
}
ptr_len = local_count_words(str, chr);
out = mem_alloc_array(sizeof(t_str), (ptr_len + 1));
if (out == NULL)
return (NULL);
return (local_split_inner(str, chr, out));
string_free(buf);
return (*out = ret, NO_ERROR);
}