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

            Line data    Source code
       1              : #include "rational.h"
       2              : 
       3              : /**
       4              :  * @brief Convert bytes
       5              :  *
       6              :  */
       7              : static inline Byte tobyte(const size_t) __attribute__((always_inline));
       8              : static inline Byte tobyte(const size_t bytes)
       9              : {
      10              :         /// Number of bytes in a kibibyte
      11              :         /// 1024
      12          328 :         const size_t bytes_in_kibibyte = 1024ULL;
      13              : 
      14              :         /// Number of bytes in a mebibyte
      15              :         /// 1024*1024
      16          328 :         const size_t bytes_in_mebibyte = bytes_in_kibibyte * 1024ULL;
      17              : 
      18              :         /// Number of bytes in a gibibyte
      19              :         /// 1024*1024*1024
      20          328 :         const size_t bytes_in_gibibyte = bytes_in_mebibyte * 1024ULL;
      21              : 
      22              :         /// Number of bytes in a tebibyte
      23              :         /// 1024*1024*1024*1024
      24          328 :         const size_t bytes_in_tebibyte = bytes_in_gibibyte * 1024ULL;
      25              : 
      26              :         /// Number of bytes in a pebibyte
      27              :         /// 1024*1024*1024*1024*1024
      28          328 :         const size_t bytes_in_pebibyte = bytes_in_tebibyte * 1024ULL;
      29              : 
      30              :         /// Number of bytes in an exbibyte
      31              :         /// 1024*1024*1024*1024*1024*1024
      32          328 :         const size_t bytes_in_exbibyte = bytes_in_pebibyte * 1024ULL;
      33              : 
      34              :         /*
      35              :          *
      36              :          * Fill out the corresponding values of the structure
      37              :          *
      38              :          */
      39              : 
      40              :         // Initializing of the structure that will be returned
      41              :         // from the function
      42          328 :         Byte byte = {0};
      43              : 
      44          328 :         byte.exbibytes = bytes/bytes_in_exbibyte;
      45              : 
      46          328 :         const size_t exbibytes = byte.exbibytes*bytes_in_exbibyte;
      47          328 :         byte.pebibytes = (bytes - exbibytes)/bytes_in_pebibyte;
      48              : 
      49          328 :         const size_t pebibytes = byte.pebibytes*bytes_in_pebibyte;
      50          328 :         byte.tebibytes = (bytes - exbibytes - pebibytes)/bytes_in_tebibyte;
      51              : 
      52          328 :         const size_t tebibytes = byte.tebibytes*bytes_in_tebibyte;
      53          328 :         byte.gibibytes = (bytes - exbibytes - pebibytes - tebibytes)/bytes_in_gibibyte;
      54              : 
      55          328 :         const size_t gibibytes = byte.gibibytes*bytes_in_gibibyte;
      56          328 :         byte.mebibytes = (bytes - exbibytes - pebibytes - tebibytes - gibibytes)/bytes_in_mebibyte;
      57              : 
      58          328 :         const size_t mebibytes = byte.mebibytes*bytes_in_mebibyte;
      59          328 :         byte.kibibytes = (bytes - exbibytes - pebibytes - tebibytes - gibibytes - mebibytes)/bytes_in_kibibyte;
      60              : 
      61          328 :         const size_t kibibytes = byte.kibibytes*bytes_in_kibibyte;
      62          328 :         byte.bytes = (bytes - exbibytes - pebibytes - tebibytes - gibibytes - mebibytes - kibibytes);
      63              : 
      64          328 :         return(byte);
      65              : }
      66              : 
      67              : /**
      68              :  *
      69              :  * @brief The function for convert bytes to a readable date.
      70              :  * The function generates a string if the structure element
      71              :  * contains data greater than zero.
      72              :  *
      73              :  */
      74         2296 : static void catbyte(
      75              :         char *const       result,
      76              :         const size_t      bytes,
      77              :         const char *const suffix)
      78              : {
      79         2296 :         if(bytes > 0ULL)
      80              :         {
      81              :                 // Temporary array
      82              :                 char tmp[MAX_CHARACTERS];
      83              :                 // Put a number into the temporary string array
      84          559 :                 snprintf(tmp,sizeof(tmp),"%zu",bytes);
      85              :                 // Copy the tmp line to the end of the result line
      86          559 :                 strcat(result,tmp);
      87              :                 // Add suffix
      88          559 :                 strcat(result,suffix);
      89              :                 // Add a space after the suffix
      90          559 :                 strcat(result," ");
      91              :         }
      92         2296 : }
      93              : 
      94              : /**
      95              :  * @details Convert number of bytes to human-readable string:
      96              :  * B   - Byte
      97              :  * KiB - Kibibyte
      98              :  * MiB - Mebibyte
      99              :  * GiB - Gibibyte
     100              :  * TiB - Tebibyte
     101              :  * PiB - Pebibyte
     102              :  * EiB - Exbibyte
     103              :  *
     104              :  */
     105          334 : char *bkbmbgbtbpbeb(const size_t bytes)
     106              : {
     107              :         // Zero out a static memory area with a string array
     108              :         static char result[MAX_CHARACTERS] = {0};
     109          334 :         result[0] = '\0';  /* Initialize buffer as empty string */
     110              : 
     111              :         // If the number passed is 0 Bytes
     112          334 :         if(bytes == 0ULL)
     113              :         {
     114              :                 // Compiling a string 0b
     115            6 :                 strcat(result,"0B");
     116            6 :                 return(result);
     117              :         }
     118              : 
     119              :         Byte byte = tobyte(bytes);
     120              : 
     121          328 :         catbyte(result,byte.exbibytes,"EiB");
     122          328 :         catbyte(result,byte.pebibytes,"PiB");
     123          328 :         catbyte(result,byte.tebibytes,"TiB");
     124          328 :         catbyte(result,byte.gibibytes,"GiB");
     125          328 :         catbyte(result,byte.mebibytes,"MiB");
     126          328 :         catbyte(result,byte.kibibytes,"KiB");
     127          328 :         catbyte(result,byte.bytes,"B");
     128              : 
     129              :         // Remove space at the end of a line
     130          328 :         result[strlen(result) - 1ULL] = '\0';
     131              : 
     132          328 :         return(result);
     133              : }
     134              : #if 0
     135              : /// Test
     136              : /// 4617322122555958282 = ((1024*1024*1024*1024*1024*1024)*4)+((1024*1024*1024*1024*1024)*5)+((1024*1024*1024*1024)*6)+((1024*1024*1024)*7)+((1024*1024)*8)+((1024)*9)+10
     137              : /// Should be 4EiB 5PiB 6TiB 7GiB 8MiB 9KiB 10B
     138              : int main(void)
     139              : {
     140              :         const size_t bytes = 4617322122555958282ULL;
     141              :         printf("%s\n",bkbmbgbtbpbeb(bytes));
     142              :         return 0;
     143              : }
     144              : #endif
        

Generated by: LCOV version 2.0-1