LCOV - code coverage report
Current view: top level - src - extract_relative_path.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 10 10
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            Line data    Source code
       1              : #include "precizer.h"
       2              : 
       3              : /**
       4              :  * @brief Returns the relative path by removing a given prefix from an absolute or relative path.
       5              :  *
       6              :  * This function checks if the given absolute path starts with the specified prefix.
       7              :  * If it does, the function returns the portion of the path after the prefix.
       8              :  * If the prefix is not found at the beginning, the original path is returned unchanged.
       9              :  *
      10              :  * Special cases:
      11              :  * - If the prefix is "/", the function removes the leading slash and returns the rest of the path.
      12              :  * - If the entire absolute path matches the prefix, it returns "." to indicate the current directory.
      13              :  * - If the absolute path does not start with the prefix, it is returned unchanged.
      14              :  *
      15              :  * @param path The absolute or relative path to be processed.
      16              :  * @param prefix The prefix to remove from the path.
      17              :  * @return A pointer to the relative path within the given absolute path.
      18              :  */
      19         9422 : const char *extract_relative_path(
      20              :         const char *path,
      21              :         const char *prefix)
      22              : {
      23         9422 :         size_t prefix_len = strlen(prefix);
      24              : 
      25              :         // Check if the prefix matches the beginning of the absolute path
      26         9422 :         if(strncmp(path,prefix,prefix_len) == 0)
      27              :         {
      28         9420 :                 const char *relative_path = path + prefix_len;
      29              : 
      30              :                 // Skip the leading '/' or '\' if present after the prefix
      31         9420 :                 if(*relative_path == '/' || *relative_path == '\\')
      32              :                 {
      33         9255 :                         relative_path++;
      34              :                 }
      35              : 
      36              :                 // If the resulting path is empty, return "." to represent the current directory
      37         9420 :                 if(*relative_path == '\0')
      38              :                 {
      39          159 :                         return ".";
      40              :                 }
      41              : 
      42         9261 :                 return relative_path;
      43              :         }
      44              : 
      45              :         // If the prefix doesn't match, return the original path
      46            2 :         return path;
      47              : }
        

Generated by: LCOV version 2.0-1