Branch data MC/DC data Line data Source code
1 : : : /**
2 : : : * @file file_reader.c
3 : : : * @brief Module responsible for handling input file operations related to sensor data.
4 : : : *
5 : : : * This module provides functions to open a file containing sensor input values and read its
6 : : : * contents into a structured format. It skips the header line and parses sensor data values
7 : : : * for later use in simulation or testing environments.
8 : : : */
9 : : :
10 : : : #include "file_reader.h"
11 : : : #include <stdlib.h>
12 : : :
13 : : : /**
14 : : : * @brief Opens a file for reading and skips the first line (header).
15 : : : *
16 : : : * @param filename Name of the file to be opened.
17 : : : * @return FILE* Pointer to the opened file.
18 : : : * @note If the file cannot be opened, the program exits with an error.
19 : : : * \anchor open_file
20 : : : */
21 : : :
22 : : 4 : FILE* open_file(const char* filename) {
23 : : 4 : FILE *file = fopen(filename, "r");
24 [ + + ]: [ T F ]: 4 : if (file == NULL) {
25 : : 1 : perror("Erro ao abrir o arquivo");
26 : : 1 : exit(EXIT_FAILURE);
27 : : : }
28 : : :
29 : : : // Skip header
30 : : : char header[100];
31 : : 3 : fgets(header, sizeof(header), file);
32 : : :
33 : : 3 : return file;
34 : : : }
35 : : :
36 : : :
37 : : : /**
38 : : : * @brief Reads a line from the file and fills the sensor data structure.
39 : : : *
40 : : : * @param file Pointer to the opened file.
41 : : : * @param sensor_data Pointer to the structure where the data will be stored.
42 : : : * @return int Returns 1 if the reading is successful, 0 otherwise.
43 : : : *
44 : : : * @note The expected file format will follow this label formact:
45 : : : Distance(m) Obstacle Speed(m/s) Brake Accelerator AEB_on_off Reverse
46 : : : \anchor read_sensor_data
47 : : : */
48 : : 8 : int read_sensor_data(FILE *file, sensors_input_data *sensor_data) {
49 : : 8 : return fscanf(file, "%lf %d %lf %d %d %d %d %lf",
50 : : : &sensor_data->obstacle_distance,
51 : : : &sensor_data->has_obstacle,
52 : : : &sensor_data->relative_velocity,
53 : : : &sensor_data->brake_pedal,
54 : : : &sensor_data->accelerator_pedal,
55 : : : &sensor_data->aeb_system_enabled,
56 : : : &sensor_data->reverse_enabled,
57 : : : &sensor_data->relative_acceleration
58 : : 8 : ) == 8;
59 : : : }
|