set_map - 1Fr3aK2/Cub3d GitHub Wiki
📝 set_map
This function processes the map data and allocates memory for it based on the given input. It checks if the input map structure is valid and processes the map buffer to calculate its length before allocating memory.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
map |
t_map* |
A pointer to the map structure containing the map data. |
i |
int |
The index to start processing from in the map buffer. |
🔁 Returns
| Return value | Description |
|---|---|
bool |
Returns true if the map was successfully processed and memory was allocated, or false in case of an error. |
📖 Description
The set_map function is responsible for processing the map data stored in the map structure's buffer. The function checks for valid input and processes the buffer to determine the length of the map data. It then allocates memory for the map using the alloc_map function.
- The function first checks if the
mappointer is valid and if the indexiis non-negative. If not, it returnsfalse. - It calculates the length of the map buffer by skipping any empty lines.
- It checks if the next valid line exists in the buffer. If not, it returns
false. - The function then calculates the final length of the map and calls
alloc_mapto allocate memory for the map. - If memory allocation fails, the function returns
false. If all operations succeed, it returnstrue.
💡 Example Usage
t_map map = {0}; // Initialize a map structure
int index = 5; // Start processing from index 5 in the map buffer
if (set_map(&map, index)) {
printf("Map set successfully!\n");
} else {
printf("Error setting the map.\n");
}