42 Piscine/BSQ

```
int solve_bsq(t_map_info *map, int *result)
{
    t_bsq_info bsq;

```

Here, a function named `solve\_bsq` is defined, which takes two parameters: a pointer to a structure of type `t\_map\_info` named `map` and a pointer to an integer named `result`. Inside the function, a local variable `bsq` of type `t\_bsq\_info` is declared.

```
    bsq.prev_line = malloc(sizeof(int) * (map->line_len + 1));
    bsq.curr_line = malloc(sizeof(int) * (map->line_len + 1));

```

Memory is allocated dynamically for two integer arrays (`prev\_line` and `curr\_line`) within the `bsq` structure. The size of each array is `(map->line\_len + 1)`.

```
    bsq.row = 0;
    map->line_i = 0;

```

The `row` member of the `bsq` structure is initialized to 0, and the `line\_i` member of the `map` structure is initialized to 0.

```
    while (map->line_i < map->num_lines)
    {

```

A `while` loop begins, iterating through the lines of the map until `line\_i` is no longer less than `num\_lines`.

```
        if (!valid_line(map))
        {
           free(bsq.prev_line);
           free(bsq.curr_line);
            return (0);
        }

```

Within the loop, there is an `if` statement that checks if the current line is valid by calling the `valid\_line` function with the `map` parameter. If the line is not valid, memory allocated for `prev\_line` and `curr\_line` is freed, and the function returns 0, indicating failure.
0

```
       compute_bsq_line(map, &bsq, result);
       set_prev(&bsq, map);
       map->line_i++;
        bsq.row++;
    }

```

If the current line is valid, the `compute\_bsq\_line` function is called with parameters `map`, `&bsq`, and `result`. Then, the `set\_prev` function is called with parameters `&bsq` and `map`. The `line\_i` and `row` members of the `map` and `bsq` structures are incremented. This loop continues until all lines have been processed.

```
   free(bsq.prev_line);
   free(bsq.curr_line);
    return (1);
}

```

After the loop, the memory allocated for `prev\_line` and `curr\_line` is freed, and the function returns 1, indicating success.

```
void compute_bsq_line(t_map_info *map, t_bsq_info *bsq, int *result)
{
    int min;

```

This is the beginning of the function `compute\_bsq\_line`, which takes three parameters: a pointer to a structure of type `t\_map\_info` named `map`, a pointer to a structure of type `t\_bsq\_info` named `bsq`, and a pointer to an integer named `result`. Inside the function, a local variable `min` of type `int` is declared.

```
    bsq->col = 0;
    while (bsq->col < map->line_len)
    {

```

The `col` member of the `bsq` structure is initialized to 0, and a `while` loop begins. It iterates through the columns of the current line until `col` is no longer less than `line\_len`.

```
        if (map->map[map->line_i][bsq->col] != map->obst)
        {

```

An `if` statement checks whether the current cell in the map is not an obstacle (denoted by `obst`). If it's not an obstacle, the following block of code is executed.

```
            if (bsq->col == 0 || bsq->row == 0)
                min = 0;
            else
            {
                min = bsq->prev_line[bsq->col];
                if (min > bsq->prev_line[bsq->col - 1])
                   min = bsq->prev_line[bsq->col - 1];
                if (min > bsq->curr_line[bsq->col - 1])
                   min = bsq->curr_line[bsq->col - 1];
            }

```

This block of code determines the minimum value among three adjacent cells (left, diagonal, and above) in the dynamic programming matrix. If `col` is 0 or `row` is 0, then `min` is set to 0. Otherwise, `min` is set to the minimum value among the three adjacent cells.

```
           bsq->curr_line[bsq->col] = min + 1;
            if (min + 1 > result[0])
               set_result(result, min, bsq);
        }

```

The current cell in the dynamic programming matrix (`curr\_line`) is updated to the value of `min + 1`. If this value is greater than the current result stored in `result[0]`, the `set\_result` function is called to update the result.

```
        else
           bsq->curr_line[bsq->col] = 0;

```

If the current cell in the map is an obstacle, the corresponding cell in the dynamic programming matrix is set to 0.

```
        bsq->col++;
    }
}

```

Finally, the loop iterates to the next column, and the function `compute\_bsq\_line` continues until all columns of the current line are processed.

This is the beginning of the function `set\_result`, which takes three parameters: a pointer to an integer array named `result`, an integer `min`, and a pointer to a structure of type `t\_bsq\_info` named `bsq`.

```
    result[0] = min + 1;

```

The first element of the `result` array is set to `min + 1`. This is typically used to store the size of the largest square found in the map.

```
    result[1] = bsq->row;

```

The second element of the `result` array is set to the `row` member of the `bsq` structure. This is used to store the row index of the bottom-right corner of the largest square found.

```
    result[2] = bsq->col;

```

The third element of the `result` array is set to the `col` member of the `bsq` structure. This is used to store the column index of the bottom-right corner of the largest square found.

This marks the end of the `set\_result` function. The function essentially updates the `result` array with information about the largest square found, including its size and the coordinates of its bottom-right corner.

```
void set_prev(t_bsq_info *bsq, t_map_info *map)
{

```

This is the beginning of the function `set\_prev`, which takes two parameters: a pointer to a structure of type `t\_bsq\_info` named `bsq` and a pointer to a structure of type `t\_map\_info` named `map`.

```
    bsq->col = 0;

```

The `col` member of the `bsq` structure is initialized to 0.

```
    while (bsq->col < map->line_len)
    {

```

A `while` loop begins, iterating through the columns of the current line until `col` is no longer less than `line\_len`.

```
       bsq->prev_line[bsq->col] = bsq->curr_line[bsq->col];

```

For each column, the value in the `curr\_line` array at the current column index is copied to the corresponding position in the `prev\_line` array. This is done to update the previous line in the dynamic programming matrix for the next iteration.

```
        bsq->col++;
    }
}

```

After copying all values, the loop iterates to the next column, and the `set\_prev` function completes. This function essentially updates the `prev\_line` array in the `bsq` structure with the values from the `curr\_line` array, preparing it for the next iteration in the larger context where it is used, likely in a dynamic programming solution.

```
int valid_line(t_map_info *map)
{

```

This is the beginning of the function `valid\_line`, which takes a single parameter: a pointer to a structure of type `t\_map\_info` named `map`.

```
    int i;

```

A local variable `i` of type `int` is declared.

```
    i = 0;

```

The variable `i` is initialized to 0.

```
    while (map->map[map->line_i][i] != '\0')
    {

```

A `while` loop begins, iterating through the characters of the current line until a null character (`'\0'`) is encountered, indicating the end of the string.

```
        if (map->map[map->line_i][i] != map->obst
            && map->map[map->line_i][i] != map->emp)
            return (0);

```

Within the loop, an `if` statement checks if the current character in the map is not equal to the obstacle character (`obst`) and is not equal to the empty character (`emp`). If either condition is true, the function immediately returns 0, indicating that the line is not valid.

```
        i++;
    }

```

After the loop, the variable `i` is incremented to move to the next character in the line.

```
    if (i != map->line_len)
        return (0);

```

An additional check is performed to verify if the length of the line (`i`) is equal to the expected line length (`line\_len`). If not, the function returns 0, indicating that the line is not valid.

```
    return (1);
}

```

If the function has not returned 0 at any point, it reaches this line and returns 1, indicating that the line is valid according to the specified conditions.

```
void display_result(int *result, t_map_info *map)
{

```

This is the beginning of the function `display\_result`, which takes two parameters: a pointer to an integer array named `result` and a pointer to a structure of type `t\_map\_info` named `map`.

```
    int row;
    int col;

```

Two local variables `row` and `col` of type `int` are declared.

```
    row = 0;

```

The variable `row` is initialized to 0.

```
    while (row < map->num_lines)
    {

```

A `while` loop begins, iterating through the rows of the map until `row` is no longer less than the total number of lines (`num\_lines`).

```
        col = 0;

```

Inside the outer loop, the variable `col` is initialized to 0.

```
        while (col < map->line_len)
        {

```

An inner `while` loop begins, iterating through the columns of the map until `col` is no longer less than the length of each line (`line\_len`).

```
            if (row >= result[1] - result[0] + 1 && row <= result[1]
               && col >= result[2] - result[0] + 1 && col <= result[2])
               write(1, &map->full, 1);

```

Within the inner loop, an `if` statement checks if the current position (row, col) corresponds to the bottom-right corner of the largest square found based on the information in the `result` array. If it does, the character stored in `map->full` is written to the standard output using the `write` function (assuming `map->full` is a single character). This is likely used to mark the cells of the largest square on the map.

```
            else
               write(1, &map->map[row][col], 1);

```

If the current position is not part of the largest square, the character at that position in the original map is written to the standard output.

```
            col++;
        }

```

After processing each column in the inner loop, the `col` variable is incremented.

```
        write(1, "\n", 1);

```

A newline character is written to the standard output, moving to the next line.

```
        row++;
    }
}

```

After processing each row in the outer loop, the `row` variable is incremented, and the function `display\_result` completes. This function is likely used to display the map with the largest square marked on it.

```
char *ft_strdup(char *src)
{

```

This is the beginning of the function `ft\_strdup`, which takes a single parameter: a pointer to a character array (string) named `src`.

```
    char *out;
    int i;

```

Two local variables are declared: `out`, a pointer to a character array, and `i`, an integer.

```
    i = 0;
    while (src[i++])
        ;

```

The variable `i` is initialized to 0. A `while` loop begins, iterating through the characters of the `src` string until the null character (`'\0'`) is encountered. The purpose of this loop is to determine the length of the string.

```
    out = malloc(i);

```

Memory is dynamically allocated for a new string (`out`) with a size of `i`, which is the length of the original string plus one for the null terminator.

```
    i = -1;
    while (src[++i])
        out[i] = src[i];

```

The variable `i` is reset to -1, and a new `while` loop begins. It iterates through the characters of the `src` string, copying each character to the corresponding position in the newly allocated `out` string.

```
    out[i] = '\0';

```

After copying all characters, the null terminator (`'\0'`) is added to the end of the `out` string, ensuring that it is properly terminated.

```
    return (out);

```

The function returns a pointer to the newly allocated string `out`, which is a duplicate of the input string `src`. Note that the memory for the duplicated string needs to be freed later to prevent memory leaks.

This marks the end of the `ft\_strdup` function. The purpose of this function is to create a duplicate of the input string and return a pointer to the new duplicated string.

```
int ft_isspace(char c)
{

```

This is the beginning of the function `ft\_isspace`, which takes a single parameter: a character `c`.

```
        return (1);

```

An `if` statement checks if the input character `c` is equal to any of the specified whitespace characters (`'\t'`, `'\n'`, `'\v'`, `'\f'`, `'\r'`, or `' '`). If `c` matches any of these characters, the function returns 1, indicating that `c` is a whitespace character.

```
    return (0);

```

If the `if` condition is not met, meaning that `c` is not any of the specified whitespace characters, the function returns 0, indicating that `c` is not a whitespace character.

```
}

```

This marks the end of the `ft\_isspace` function. The purpose of this function is to determine whether a given character is a whitespace character (`'\t'`, `'\n'`, `'\v'`, `'\f'`, `'\r'`, or `' '`). It returns 1 if the character is a whitespace character and 0 otherwise.

```
int ft_atoi(char *str)
{

```

This is the beginning of the function `ft\_atoi`, which takes a single parameter: a pointer to a character array (string) named `str`.

```
    int out;
    int sign;

```

Two local variables are declared: `out`, an integer to store the final result, and `sign`, an integer to represent the sign of the number.

```
    out = 0;
    sign = 1;

```

The variables `out` and `sign` are initialized to 0 and 1, respectively.

```
    {
        if (!ft_isspace(*str))
            return (0);
        str++;
    }

```

A `while` loop begins, iterating through the characters of the string until it finds the first non-whitespace character or a valid numeric character ('0' to '9', '-' or '+'). If a character other than a whitespace or a valid numeric character is encountered, the function returns 0. This initial loop is used to skip any leading whitespaces.

```
    while (*str && !(*str >= '0' && *str <= '9'))
    {
        if (*str == '-')
            sign *= -1;
        else if (*str != '+')
            return (0);
        str++;
    }

```

Another `while` loop starts, checking for a potential sign ('-' or '+'). If '-' is encountered, the `sign` variable is multiplied by -1 to represent a negative sign. If '+' is encountered, it is simply ignored. If any other character is found, the function returns 0.

```
    while (*str >= '0' && *str <= '9')
    {
        out = (out * 10) + (*(str++) - 48);
    }

```

The final `while` loop processes the consecutive numeric characters ('0' to '9') in the string, converting them to integers and updating the `out` variable accordingly. The loop continues until a non-numeric character is encountered.

```
    return (out * sign);

```

The function returns the final result (`out`) multiplied by the sign (`sign`), ensuring that the number is correctly positive or negative based on the sign encountered earlier.

This marks the end of the `ft\_atoi` function. The purpose of this function is to convert a string representing an integer to its corresponding integer value, handling signs and skipping leading whitespaces. If the input string is not a valid integer representation, the function returns 0.

```
int main(int argc, char *argv[])
{

```

This is the beginning of the `main` function, which is the entry point of the program. It takes two parameters: an integer `argc` representing the number of command-line arguments and an array of strings `argv[]` representing the command-line arguments.

```
    int fd;
    int i;

```

Two local variables are declared: `fd`, an integer to store file descriptor, and `i`, an integer used for iterating through command-line arguments.

```
    if (argc == 1)
        read_file(0);

```

An `if` statement checks if there is only one command-line argument, indicating that the program was executed without specifying a file name. In this case, the `read\_file` function is called with a file descriptor of 0, which typically represents standard input (stdin).

```
    else
    {
        i = 1;
        while (i < argc)
        {

```

If there are more than one command-line arguments, an `else` block begins. The variable `i` is initialized to 1 to skip the program name itself, and a `while` loop starts to iterate through the remaining command-line arguments.

```
            fd = open(argv[i], O_RDONLY);
            if (fd > 0)
               read_file(fd);
            else
               write(1, "map error\n", 10);

```

Inside the loop, the `open` system call is used to open the file specified by the current command-line argument (`argv[i]`). If successful (file descriptor greater than 0), the `read\_file` function is called with the obtained file descriptor. If the file cannot be opened, an error message ("map error\n") is written to standard output using the `write` function.

```
            if (++i < argc)
               write(1, "\n", 1);
        }
    }

```

After processing each file, the loop increments `i` and checks if there are more command-line arguments. If so, a newline character is written to standard output to separate the output for different files.

```
    return (0);
}

```

The `main` function returns 0, indicating successful program execution. This is a standard convention where a return value of 0 typically indicates success, and a non-zero value indicates an error.

```
void read_file(int fd)
{

```

This is the beginning of the `read\_file` function, which takes a single parameter: an integer `fd` representing a file descriptor.

```
    int *result;
    t_map_info map;

```

Two local variables are declared: `result`, a pointer to an integer array, and `map`, a structure of type `t\_map\_info`.

```
    result = malloc(4 * sizeof(int));

```

Memory is dynamically allocated for an integer array of size 4 and the address is stored in the `result` pointer.

```
    result[0] = 0;
    result[1] = 0;
    result[2] = 0;

```

The first three elements of the `result` array are initialized to 0. This array is likely used to store information about the largest square found in the map.

```
    if (read_map(fd, &map))
    {

```

An `if` statement checks the return value of the `read\_map` function, passing the file descriptor `fd` and a pointer to the `map` structure. If the map is successfully read, the code inside the `if` block is executed.

```
        if (fd != 0)
            close(fd);

```

If the file descriptor is not 0 (stdin), the file is closed using the `close` system call. This is done to free up system resources.

```
        if (solve_bsq(&map, result))
           display_result(result, &map);

```

The `solve\_bsq` function is called with pointers to the `map` structure and the `result` array. If the solution is successfully computed, the `display\_result` function is called with the same pointers to display the result.

```
        else
            write(1, "map error\n", 10);

```

If the solution cannot be computed (e.g., due to a map error), an error message ("map error\n") is written to standard output using the `write` function.

```
       free_map(&map);

```

The `free\_map` function is called to release memory allocated for the map structure.

```
    }
    else
    {
        write(1, "map error\n", 10);
        if (fd != 0)
            close(fd);
    }

```

If the map cannot be read (e.g., due to a file error or an invalid map format), an error message is written to standard output. If the file descriptor is not 0, the file is closed.

```
    free(result);
}

```

Finally, the memory allocated for the `result` array is freed using the `free` function, and the `read\_file` function is completed.

```
int free_map(t_map_info *map)
{

```

This is the beginning of the `free\_map` function, which takes a pointer to a structure of type `t\_map\_info` named `map` and returns an integer.

```
    if (map->map)
    {

```

An `if` statement checks if the `map->map` member is not NULL, meaning that memory has been allocated for the map.

```
        while (--map->num_lines >= 0)
            free(map->map[map->num_lines]);

```

A `while` loop iterates through each row of the map. It decrements the `num\_lines` member of the `map` structure and frees the memory allocated for each row using the `free` function.

```
        free(map->map);

```

After freeing all the rows, the memory allocated for the map itself (the array of rows) is freed using the `free` function.

The closing brace of the `if` statement marks the end of the block that is executed when `map->map` is not NULL.

```
    return (0);

```

The function returns 0, indicating successful execution. The return value here may not carry much significance, as the primary purpose of this function is to free allocated memory.

This marks the end of the `free\_map` function. The purpose of this function is to free the dynamically allocated memory for a map structure. It iterates through each row of the map, frees the memory for each row, and then frees the memory for the array of rows.

```
int read_map(int fd, t_map_info *map)
{

```

This is the beginning of the `read\_map` function, which takes two parameters: an integer `fd` representing a file descriptor, and a pointer to a structure of type `t\_map\_info` named `map`. The function returns an integer.

```
    char *buf;

```

A local variable `buf` is declared. This variable will be used to store temporary strings read from the file.

```
    buf = 0;

```

The `buf` variable is initialized to 0 (NULL).

```
    if (get_len(fd, &(map->leg_len), &buf) < 4)
        return (0);

```

The `get\_len` function is called to read a line from the file (`fd`) and determine its length. The length is stored in `map->leg\_len`, and the line is stored in `buf`. If the length is less than 4, the function returns 0, indicating an error.

```
    if (!set_legend(buf, map->leg_len, map))
        return (0);

```

The `set\_legend` function is called to process the legend line (`buf`) and update the `map` structure accordingly. If the legend cannot be set successfully, the function returns 0.

```
    free(buf);

```

The memory allocated for `buf` is freed.

```
    map->map = malloc((map->num_lines + 1) * sizeof(char *));

```

Memory is allocated for an array of pointers to strings (`map->map`). The number of pointers is `map->num\_lines + 1` to accommodate the legend line.

```
    if (get_len(fd, &(map->line_len), &buf) < 1)
    {
        free(map->map);
        return (0);
    }

```

Another call to `get\_len` is made to read a line from the file and determine its length. The length is stored in `map->line\_len`, and the line is stored in `buf`. If the length is less than 1, indicating an empty line, the function frees the memory allocated for `map->map` and returns 0.

```
    map->map[0] = malloc((map->line_len + 1) * sizeof(char));
    map->map[0] = ft_strdup(buf);
    free(buf);

```

Memory is allocated for the first row of the map (`map->map[0]`) to store the content of the line. The content is duplicated using `ft\_strdup` and stored in `map->map[0]`. The memory allocated for `buf` is then freed.

```
    if (!get_lines(fd, map))
        return (free_map(map));

```

The `get\_lines` function is called to read the remaining lines of the map and store them in `map->map`. If there is an error during this process, the function calls `free\_map` to free any allocated memory and returns 0.

```
    if (map->line_i-- != map->num_lines)
        return (free_map(map));

```

After reading all lines, the `line\_i` member of the `map` structure is decremented. If it does not match the expected number of lines (`num\_lines`), the function calls `free\_map` to free any allocated memory and returns 0.

```
    return (1);

```

If the function has not returned 0 at any point, it returns 1, indicating successful execution.

```
}

```

This marks the end of the `read\_map` function. The function is responsible for reading the map information from a file, processing the legend, allocating memory for the map, and populating it with the contents of the file. If any errors occur during this process, it returns 0.

```
int get_len(int fd, int *len, char **buf)
{

```

This is the beginning of the `get\_len` function, which takes three parameters: an integer `fd` representing a file descriptor, a pointer to an integer `len` representing the length of the read line, and a double pointer to a character `buf` representing the buffer for storing the read line. The function returns an integer.

```
    int buf_size;

```

A local variable `buf\_size` is declared to store the initial size of the buffer.

```
    buf_size = 1024;

```

The `buf\_size` variable is initialized to 1024, representing the initial size of the buffer.

```
    *buf = malloc(buf_size * sizeof(char));

```

Memory is dynamically allocated for the buffer using `malloc`. The size of the buffer is `buf\_size \* sizeof(char)`.

```
    *len = 0;

```

The `len` variable is initialized to 0.

```
    while (read(fd, &(*buf)[*len], 1))
    {

```

A `while` loop begins, reading one character at a time from the file descriptor `fd` and storing it in the buffer at the position `(\*len)`. The loop continues until the `read` function returns 0, indicating the end of the file.

```
        if ((*buf)[(*len)++] == '\n')
            break ;

```

Within the loop, an `if` statement checks if the last character read is a newline (`'\n'`). If so, the loop is terminated with a `break` statement.

```
        if (*len >= buf_size - 1)
        {
            buf_size += 1024;
            (*buf)[*len] = '\0';
            *buf = re_alloc_buf(*buf, buf_size);
        }

```

Another `if` statement checks if the length of the buffer has reached or exceeded `buf\_size - 1`. If true, the buffer size is increased by 1024, the last character is set to the null terminator, and the buffer is reallocated using the `re\_alloc\_buf` function.

```
    }

```

This marks the end of the `while` loop.

```
    if ((*buf)[--*len] != '\n')
        return (0);

```

After the loop, an `if` statement checks if the last character in the buffer is not a newline (`'\n'`). If true, the function returns 0, indicating an error.

```
    (*buf)[*len] = '\0';

```

The last character in the buffer is set to the null terminator.

```
    return (*len);

```

The function returns the length of the read line.

This marks the end of the `get\_len` function. The purpose of this function is to read a line from a file, dynamically allocate memory for the buffer, and handle buffer resizing as needed. If the last character of the line is not a newline, the function returns 0; otherwise, it returns the length of the line.

```
int get_lines(int fd, t_map_info *map)
{

```

This is the beginning of the `get\_lines` function, which takes two parameters: an integer `fd` representing a file descriptor and a pointer to a structure of type `t\_map\_info` named `map`. The function returns an integer.

```
    map->line_i = 1;

```

The `line\_i` member of the `map` structure is initialized to 1. This variable likely represents the current line index being processed.

```
    while (map->line_i < map->num_lines)
        map->map[map->line_i++] = malloc((map->line_len + 1) * sizeof(char));

```

A `while` loop begins, iterating through the lines of the map. For each line (except the first one, as `line\_i` is initially set to 1), memory is dynamically allocated for the line in the `map->map` array. The size of each line is `(map->line\_len + 1) \* sizeof(char)` to accommodate the null terminator.

```
    map->line_i = 1;

```

After allocating memory for each line, `line\_i` is reset to 1 for the next loop.

```
    while (map->line_i < map->num_lines)
    {

```

Another `while` loop begins, iterating through the lines of the map.

```
        if (map->line_len + 1 != read(fd, map->map[map->line_i], map->line_len + 1))
            return (0);

```

Within the loop, an `if` statement checks if the number of characters read from the file (`read` function) is not equal to `map->line\_len + 1`. If true, the function returns 0, indicating an error.

```
        if (map->map[map->line_i][map->line_len] != '\n')
            return (0);

```

Another `if` statement checks if the last character of the line is not a newline (`'\n'`). If true, the function returns 0, indicating an error.

```
        map->map[map->line_i++][map->line_len] = '\0';
    }

```

The last character of each line is set to the null terminator, and `line\_i` is incremented.

```
    return (1);

```

If the function has not returned 0 at any point, it returns 1, indicating successful execution.

This marks the end of the `get\_lines` function. The purpose of this function is to read the remaining lines of the map from a file, allocate memory for each line, and ensure that the lines are terminated with a newline character. If any errors occur during this process, the function returns 0.

```
int set_legend(char *legend, int len, t_map_info *map)
{

```

This is the beginning of the `set\_legend` function, which takes three parameters: a character array `legend`, an integer `len` representing the length of the legend, and a pointer to a structure of type `t\_map\_info` named `map`. The function returns an integer.

```
    int i;

```

A local variable `i` is declared to use as an index in the upcoming loop.

```
    if (len < 4 || len > 13)
        return (0);

```

An `if` statement checks if the length of the legend is less than 4 or greater than 13. If true, the function returns 0, indicating an error.

```
    i = len - 4;
    while (i >= 0)
    {

```

A `while` loop begins, iterating through the characters of the legend in reverse order starting from the fourth-to-last character.

```
        if (legend[i] < '0' || legend[i] > '9')
        {
            return (0);
        }
        i--;
    }

```

Within the loop, an `if` statement checks if the current character is not a digit ('0' to '9'). If true, the function returns 0, indicating an error.

```
    if (legend[len - 1] == legend[len - 2]
        return (0);

```

Another `if` statement checks if the last three characters of the legend are not distinct. If any two characters are equal, the function returns 0, indicating an error.

```
    map->emp = legend[map->leg_len - 3];
    map->obst = legend[map->leg_len - 2];
    map->full = legend[map->leg_len - 1];

```

The three characters representing empty space, obstacle, and full space are extracted from the legend and stored in the corresponding members of the `map` structure.

```
    legend[map->leg_len - 3] = '\0';

```

The last three characters of the legend are replaced with the null terminator to separate them from the numeric part.

```
    map->num_lines = ft_atoi(legend);

```

The `ft\_atoi` function is called to convert the numeric part of the legend to an integer, representing the number of lines in the map. The result is stored in the `num\_lines` member of the `map` structure.

```
    return (1);

```

If the function has not returned 0 at any point, it returns 1, indicating successful execution.

This marks the end of the `set\_legend` function. The purpose of this function is to process the legend of the map, extracting information about the characters representing empty space, obstacle, and full space, as well as the number of lines in the map. If any errors occur during this process, the function returns 0.

```
char *re_alloc_buf(char *buf, int size)
{

```

This is the beginning of the `re\_alloc\_buf` function, which takes two parameters: a character array `buf` and an integer `size` representing the new size of the buffer. The function returns a character pointer.

```
    char *out;
    int i;

```

Two local variables are declared: `out`, a character pointer to store the newly allocated buffer, and `i`, an integer used as an index in the upcoming loop.

```
    out = malloc(size * sizeof(char));

```

Memory is dynamically allocated for a new buffer with the specified size using `malloc`, and the address is stored in the `out` pointer.

```
    i = -1;
    while (buf[++i])
        out[i] = buf[i];

```

A `while` loop begins, iterating through the characters of the original buffer (`buf`). The characters are copied from the original buffer to the new buffer (`out`). The loop terminates when a null terminator (`'\0'`) is encountered in the original buffer.

```
    free(buf);

```

The memory allocated for the original buffer is freed using the `free` function.

```
    return (out);

```

The function returns the address of the newly allocated buffer (`out`).

This marks the end of the `re\_alloc\_buf` function. The purpose of this function is to reallocate memory for a buffer with a new size, copy the contents of the original buffer to the new buffer, free the memory of the original buffer, and return the address of the new buffer. It is commonly used when dynamically resizing buffers during runtime.