minishell
Minishell Project for École 42
Loading...
Searching...
No Matches
create_input_array.c File Reference

Functions for creating an array of strings for tokenization. More...

#include "minishell.h"
Include dependency graph for create_input_array.c:

Functions

int len_substr (char *str)
 Calculates the length of a substring('token') until a delimiter or a token is found.
 
int count_elements (char *rl_copy)
 Count the number of elements in the input string.
 
int find_token (char **input_array, char **input, int index)
 Finds predefined tokens in the input string and adds it to the input_array.
 
char ** fill_input_array (char **input_array, char *input)
 Fills the array of strings with substrings('tokens') from the input string.
 
char ** create_input_array (t_shell *shell)
 Create an array of input strings for tokenization.
 

Detailed Description

Functions for creating an array of strings for tokenization.

Function Documentation

◆ count_elements()

int count_elements ( char *  rl_copy)

Count the number of elements in the input string.

Takes an input and counts the substrings('tokens') in it. Makes use of len_substr() to get the length of each substring. Returns the number of substrings found.

Parameters
rl_copyThe string for which to count the number of substrings.
Returns
The number of substrings('tokens') in the input string.

◆ create_input_array()

char ** create_input_array ( t_shell shell)

Create an array of input strings for tokenization.

Creates an array of tokens from shell->rl_copy(removed leading and trailing whitespaces). Calculates the number of elements needed for the array and allocates for it. Calls fill_input_array() to fill the array with substrings ('tokens').

Parameters
shellThe main data structure.
Returns
An array of substrings('tokens').

◆ fill_input_array()

char ** fill_input_array ( char **  input_array,
char *  input 
)

Fills the array of strings with substrings('tokens') from the input string.

Takes an input string, splits it into substrings('tokens') and fills the given array of strings with these substrings('tokens'). The function iterates over the input, calls find_token() to find predefined 'tokens' and adds them to the array. Also handles whitespace separated substrings and adds them to the array.

Parameters
input_arrayPointer to the array of strings to fill.
inputThe input string.
Returns
input_array The array of strings with the tokens and whitespace separated substrings.

◆ find_token()

int find_token ( char **  input_array,
char **  input,
int  index 
)

Finds predefined tokens in the input string and adds it to the input_array.

Finds tokens in the input string and adds them to the input_array. It calls is_token() and is_token2() to check for existing valid tokens. If a token is found, a substring is created and added to the input_array.

Parameters
input_arrayPointer to the array of strings.
inputPointer to the input string.
indexCurrent index in the input_array.
Returns
index The updated index after adding a substring/token to the array of strings.

◆ len_substr()

int len_substr ( char *  str)

Calculates the length of a substring('token') until a delimiter or a token is found.

Takes an input string and finds the length of the substring('token') until a delimiter(whitespaces) or token('<', '>', '|') is encountered. Iterates over the string while checking for delimiter and token. If delimiter is not found, call no_delim_found() to modify the len. Returns the len of the substring until the delimiter or token.

Parameters
strThe input string to find the lenght of substring for.
Returns
len The lenght of the substring until the delimiter or token is found.