LCOV - code coverage report
Current view: top level - src - input_parser.c (source / functions) Hit Total Coverage
Test: report.info Lines: 31 39 79.5 %
Date: 2024-01-02 12:43:31 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "input_parser.h"
       2             : 
       3          20 : char *read_input(char *input)
       4             : {
       5          20 :     if (input == NULL)
       6             :     {
       7           0 :         perror("input is not allocated");
       8           0 :         return NULL;
       9             :     }
      10             : 
      11          20 :     if (fgets(input, MAX_INPUT_LENGTH, stdin) == NULL)
      12             :     {
      13           0 :         perror("fgets");
      14           0 :         free(input);
      15           0 :         return NULL;
      16             :     }
      17             : 
      18          20 :     size_t len = strlen(input);
      19          20 :     if (len > 0 && input[len - 1] == '\n')
      20             :     {
      21          20 :         input[len - 1] = '\0';
      22             :     }
      23             : 
      24          20 :     return input;
      25             : }
      26             : 
      27          20 : int is_all_whitespace(const char *input)
      28             : {
      29          20 :     for (int i = 0; i < strlen(input); i++)
      30             :     {
      31          20 :         if (!isspace(input[i]))
      32             :         {
      33          20 :             return 0;
      34             :         }
      35             :     }
      36           0 :     return 1;
      37             : }
      38             : 
      39          20 : void preprocess_input(char *input)
      40             : {
      41          20 :     char *new_input = malloc(strlen(input) * 2 + 1); // Allocate enough space for the new input
      42             : 
      43          20 :     if (new_input == NULL)
      44             :     {
      45           0 :         perror("malloc");
      46           0 :         return;
      47             :     }
      48             : 
      49          20 :     int j = 0;
      50             : 
      51             :     // Iterate over the input and add spaces around the operators
      52         327 :     for (int i = 0; i < strlen(input); i++)
      53             :     {
      54             : 
      55         307 :         char token[2] = {input[i], input[i + 1]};
      56         307 :         OperatorType operator_type = get_operator_type(token);
      57             : 
      58         307 :         if (operator_type != UNKNOWN)
      59             :         {
      60             : 
      61          12 :             const char *operator_string = get_operator_string(operator_type);
      62          12 :             int operator_length = strlen(get_operator_string(operator_type));
      63             : 
      64          12 :             new_input[j++] = ' ';
      65          12 :             strncpy(&new_input[j], operator_string, operator_length);
      66          12 :             j += operator_length;
      67          12 :             new_input[j++] = ' ';
      68          12 :             i += operator_length - 1;
      69          12 :             continue;
      70             :         }
      71             :         else
      72             :         {
      73         295 :             new_input[j++] = input[i];
      74             :         }
      75             :     }
      76          20 :     new_input[j] = '\0';
      77          20 :     strcpy(input, new_input);
      78          20 :     free(new_input);
      79             : }

Generated by: LCOV version 1.14