mirror of
https://github.com/Savapitech/42sh.git
synced 2026-03-18 21:50:35 +01:00
[MOOVE] moove some function to the utils folder and add utils.h
This commit is contained in:
1
Makefile
1
Makefile
@@ -13,6 +13,7 @@ LIB_NAME := libu.a
|
||||
|
||||
SRC := $(wildcard src/*.c)
|
||||
SRC += $(wildcard src/builtins/*.c)
|
||||
SRC += $(wildcard src/utils/*.c)
|
||||
|
||||
LIB_SRC := $(wildcard ulib/*.c)
|
||||
LIB_SRC += $(wildcard ulib/write/printf/*.c)
|
||||
|
||||
@@ -27,33 +27,7 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static
|
||||
char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy)
|
||||
{
|
||||
int i = 0;
|
||||
int len_str = strlen(str);
|
||||
int size_right = len_str - his_variable->coord_variable - his_variable->size_variable;
|
||||
int size_left = (len_str - size_right) - his_variable->size_variable;
|
||||
char *new_str = malloc(sizeof(char) * (size_right + size_left + strlen(cpy) + 2));//ajout de deux \0
|
||||
|
||||
if (new_str == NULL)
|
||||
return NULL;
|
||||
new_str[0] = 0;
|
||||
for (; i < size_left; i++)
|
||||
new_str[i] = str[i];
|
||||
for (int j = 0; cpy[j] != 0; j++){
|
||||
new_str[i] = cpy[j];
|
||||
i++;
|
||||
}
|
||||
for (int k = 0; k < size_right; k++){
|
||||
new_str[i] = str[k + size_left + his_variable->size_variable];
|
||||
i++;
|
||||
}
|
||||
new_str[i] = '\0';
|
||||
new_str[i + 1] = '\0';
|
||||
return new_str;
|
||||
}
|
||||
#include "utils.h"
|
||||
|
||||
char *his_last_command(char *line,
|
||||
his_variable_t *his_variable, his_command_t *his_command)
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "common.h"
|
||||
#include "env.h"
|
||||
#include "exec.h"
|
||||
@@ -27,27 +28,6 @@ const parsing_history_t tab_fnct[] = {
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
//const size_t TOKENS_LIST_SZ = sizeof TOKENS_LIST / sizeof *TOKENS_LIST;
|
||||
|
||||
int my_atoi(const char *str)
|
||||
{
|
||||
int result = 0;
|
||||
int sign = 1;
|
||||
|
||||
while ((*str > '9' && *str < '0') || *str == '-')
|
||||
str++;
|
||||
if (*str == '-') {
|
||||
sign = -1;
|
||||
str++;
|
||||
} else if (*str == '+') {
|
||||
str++;
|
||||
}
|
||||
for (; *str >= '0' && *str <= '9'; str++) {
|
||||
result = result * 10 + (*str - '0');
|
||||
}
|
||||
return result * sign;
|
||||
}
|
||||
|
||||
static
|
||||
int cmd_history_is_in(char *line)
|
||||
{
|
||||
@@ -58,11 +38,6 @@ int cmd_history_is_in(char *line)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Pour savoir si c est
|
||||
** des builtins a deux
|
||||
** charactères
|
||||
*/
|
||||
static
|
||||
int is_two_char_cmd(char *line, int coord_x)
|
||||
{
|
||||
@@ -80,45 +55,16 @@ int is_two_char_cmd(char *line, int coord_x)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static
|
||||
bool is_a_token(char *str, int index_str)
|
||||
{
|
||||
str+=index_str;
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
if (strncmp(str, TOKENS_LIST[i].str, 2) == 0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static
|
||||
char *strn_to_ndup(int start, int size, char *str)
|
||||
{
|
||||
char *new_str = malloc(sizeof(char) * (size + 1));
|
||||
int count = 0;
|
||||
|
||||
if (new_str == NULL)
|
||||
return NULL;
|
||||
new_str[size] = '\0';
|
||||
for (int i = start; i != start + size; i++){
|
||||
new_str[count] = str[i];
|
||||
count++;
|
||||
}
|
||||
return new_str;
|
||||
}
|
||||
|
||||
static
|
||||
int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str)
|
||||
{
|
||||
int mode = 0;
|
||||
const int cpy_index = index_str;
|
||||
char *last_or_id = NULL;
|
||||
const int cpy_index = index_str;
|
||||
|
||||
if (str[index_str] != CHAR_HIST && is_a_token(str, index_str + 1) == false)
|
||||
return -1;
|
||||
index_str++;
|
||||
for (;str[index_str] != 0; index_str++){
|
||||
for (; str[index_str] != 0; index_str++){
|
||||
if (is_a_token(str, index_str) == true || isblank(str[index_str]))
|
||||
break;
|
||||
if (!isdigit(str[index_str]))
|
||||
@@ -128,8 +74,8 @@ int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str)
|
||||
his_variable->size_variable = index_str - cpy_index;
|
||||
his_variable->str = strn_to_ndup(cpy_index, (index_str - cpy_index), str);
|
||||
if (his_variable->str == NULL)
|
||||
return 3;//La fonction last command dois verifier le NULL
|
||||
his_variable->id = my_atoi(his_variable->str + 1);
|
||||
return 3;
|
||||
his_variable->id = atoi(his_variable->str + 1);
|
||||
return (mode == 1) ? 3 : 4;
|
||||
}
|
||||
|
||||
@@ -153,7 +99,8 @@ int which_his_cmd(his_variable_t *his_variable, char const *line)
|
||||
static
|
||||
char *replace_history(char *line)
|
||||
{
|
||||
his_variable_t his_variable = {.coord_variable = 0, .id = 0, .size_variable = 0, .str = NULL, .type = -1};
|
||||
his_variable_t his_variable = {.coord_variable = 0,
|
||||
.id = 0, .size_variable = 0, .str = NULL, .type = -1};
|
||||
|
||||
which_his_cmd(&his_variable, line);
|
||||
while (his_variable.type != -1){
|
||||
@@ -166,13 +113,12 @@ char *replace_history(char *line)
|
||||
}
|
||||
|
||||
int parse_history(char **pointer_line,
|
||||
size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history)//faut la passer au autre fonction
|
||||
size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history)
|
||||
{
|
||||
char *line = *pointer_line;
|
||||
|
||||
*buffer_sz = 0;
|
||||
if (cmd_history_is_in(line) == 0){
|
||||
//printf("JE REMPLACE...\n......\n....\n..\n.\n");
|
||||
line = replace_history(line);
|
||||
if (line == NULL)
|
||||
return 84;
|
||||
|
||||
17
src/utils.h
Normal file
17
src/utils.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** __
|
||||
** File description:
|
||||
** _
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include "history.h"
|
||||
#include "u_str.h"
|
||||
|
||||
char *strn_to_ndup(int start, int size, char *str);
|
||||
bool is_a_token(char *str, int index_str);
|
||||
char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy);
|
||||
#endif /* UTILS_H */
|
||||
36
src/utils/cat_in_str.c
Normal file
36
src/utils/cat_in_str.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** 42sh
|
||||
** File description:
|
||||
** cat_in_str
|
||||
*/
|
||||
#include "history.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy)
|
||||
{
|
||||
int i = 0;
|
||||
int len_str = strlen(str);
|
||||
int size_right = len_str -
|
||||
his_variable->coord_variable - his_variable->size_variable;
|
||||
int size_left = (len_str - size_right) - his_variable->size_variable;
|
||||
char *new_str = malloc(sizeof(char) *
|
||||
(size_right + size_left + strlen(cpy) + 2));
|
||||
|
||||
if (new_str == NULL)
|
||||
return NULL;
|
||||
for (; i < size_left; i++)
|
||||
new_str[i] = str[i];
|
||||
for (int j = 0; cpy[j] != 0; j++){
|
||||
new_str[i] = cpy[j];
|
||||
i++;
|
||||
}
|
||||
for (int k = 0; k < size_right; k++){
|
||||
new_str[i] = str[k + size_left + his_variable->size_variable];
|
||||
i++;
|
||||
}
|
||||
new_str[i] = '\0';
|
||||
new_str[i + 1] = '\0';
|
||||
return new_str;
|
||||
}
|
||||
20
src/utils/is_a_token.c
Normal file
20
src/utils/is_a_token.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** 42sh
|
||||
** File description:
|
||||
** is_a_token
|
||||
*/
|
||||
#include "string.h"
|
||||
#include "u_str.h"
|
||||
#include "ast.h"
|
||||
|
||||
bool is_a_token(char *str, int index_str)
|
||||
{
|
||||
str += index_str;
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
if (strncmp(str, TOKENS_LIST[i].str, 2) == 0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
22
src/utils/strn_to_ndup.c
Normal file
22
src/utils/strn_to_ndup.c
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** 42sh
|
||||
** File description:
|
||||
** strn_to_ndup
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
char *strn_to_ndup(int start, int size, char *str)
|
||||
{
|
||||
char *new_str = malloc(sizeof(char) * (size + 1));
|
||||
int count = 0;
|
||||
|
||||
if (new_str == NULL)
|
||||
return NULL;
|
||||
new_str[size] = '\0';
|
||||
for (int i = start; i != start + size; i++){
|
||||
new_str[count] = str[i];
|
||||
count++;
|
||||
}
|
||||
return new_str;
|
||||
}
|
||||
Reference in New Issue
Block a user