Add full env parsing

This commit is contained in:
savalet
2025-02-10 16:43:11 +01:00
parent 7ab3034f65
commit 3dfbd7f444
7 changed files with 162 additions and 9 deletions

94
src/env.c Normal file
View File

@@ -0,0 +1,94 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "env.h"
#include "u_mem.h"
#include "u_str.h"
size_t count_env_entries(char **env)
{
size_t result = 0;
for (; *env != NULL; env++)
result++;
return result;
}
bool ensure_buff_av_capacity(buff_t *buff, size_t requested)
{
char *new_str;
size_t endsize = ENV_BUFF_CAP;
if ((buff->sz + requested) < buff->cap)
return true;
for (; endsize < buff->sz + requested; endsize <<= 1);
if (endsize > buff->cap) {
new_str = u_realloc(buff->str, (sizeof *buff->str) * buff->sz,
(sizeof *buff->str) * endsize);
if (new_str == NULL)
return false;
buff->str = new_str;
buff->cap = endsize;
}
return true;
}
static
bool parse_env_populate(char **env, buff_t *env_values,
env_entry_t *env_entries)
{
size_t env_size = 0;
size_t i = 0;
for (; *env != NULL; env++) {
env_size = u_strlen(*env);
if (!ensure_buff_av_capacity(env_values, env_size))
return false;
env_entries[i].ptr = u_memcpy(env_values->str + env_values->sz, *env,
env_size);
env_entries[i].size = env_size;
env_values->sz += env_size;
i++;
}
return true;
}
void debug_env_entries(env_entry_t *env_entries, size_t env_size)
{
size_t keylen __attribute__((unused));
for (size_t i = 0; i < env_size; i++) {
keylen = u_strcspn(env_entries[i].ptr, '=') + 1;
U_DEBUG("Env entry [%01lu] key [%.*s] value [%.*s]\n", i,
(int)keylen - 1, env_entries[i].ptr,
(int)(env_entries[i].size - keylen),
env_entries[i].ptr + keylen);
}
}
bool parse_env(char **env, buff_t *env_values, env_entry_t *env_entries)
{
if (env_values == NULL || env_entries == NULL)
return false;
u_bzero((char *)env_values, sizeof(buff_t));
env_values->str = malloc(sizeof *env_values->str * ENV_BUFF_CAP);
if (env_values->str == NULL)
return false;
env_values->cap = ENV_BUFF_CAP;
u_bzero(env_values->str, sizeof *env_values->str * env_values->cap);
parse_env_populate(env, env_values, env_entries);
env_values->str[env_values->sz] = '\0';
U_DEBUG("Parsed %zu env entries (%zu)\n",
count_env_entries(env), env_values->sz);
return true;
}

23
src/env.h Normal file
View File

@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#ifndef ENV_H
#define ENV_H
#include "u_str.h"
#define ENV_BUFF_CAP 128
typedef struct {
char *ptr;
size_t size;
} env_entry_t;
// Debug
void debug_env_entries(env_entry_t *env_entries, size_t env_size);
bool parse_env(char **env, buff_t *env_values, env_entry_t *env_entries);
size_t count_env_entries(char **env);
#endif

View File

@@ -6,11 +6,10 @@
*/
#include <stddef.h>
#include <stdint.h>
void *u_memcpy(uint8_t *dest, uint8_t const *src, size_t n)
void *u_memcpy(char *dst, char const *src, size_t sz)
{
for (size_t i = 0; i < n; i++)
dest[i] = src[i];
return dest;
for (size_t i = 0; i < sz; i++)
dst[i] = src[i];
return dst;
}

17
ulib/str/str_isalnum.c Normal file
View File

@@ -0,0 +1,17 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <ctype.h>
#include <stdbool.h>
bool u_str_is_alnum(char *str)
{
for (; *str != '\0'; str++)
if (!isalnum(*str))
return false;
return true;
}

16
ulib/str/strcspn.c Normal file
View File

@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
int u_strcspn(char *str, char c)
{
char *old_str = str;
for (; *str != '\0'; str++)
if (*str == c)
return str - old_str;
return 0;
}

View File

@@ -9,11 +9,10 @@
#define MEM_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define IDX_OF(array, i, mem_s) (array + ((i) * (mem_s)))
void *u_memcpy(uint8_t *, uint8_t const *, size_t);
void *u_realloc(void *, size_t, size_t);
void *u_memcpy(char *dst, char const *src, size_t sz);
void *u_realloc(void *ptr, size_t actual_size, size_t new_size);
void u_swap(int *, int *);
bool u_bzero(char *restrict str, size_t sz);
int swap_uint32(int src);

View File

@@ -7,11 +7,14 @@
#ifndef STRING_H
#define STRING_H
#include <stdbool.h>
#include <stddef.h>
#define WRITE_CONST(fd, str) write(fd, str, sizeof str)
typedef struct {
char *str;
int sz;
size_t sz;
size_t cap;
} buff_t;
int u_getnbr(char const *);
@@ -27,5 +30,7 @@ int u_strcmp(char const *, char const *);
int u_strncmp(char const *, char const *, int);
char *u_numstr(char *, int);
int u_spacelen(char const *str);
int u_strcspn(char *str, char c);
bool u_str_is_alnum(char *str);
#endif /* STRING_H */